2 * Copyright 2005-2011 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.xmlrouter.impl;
18 import java.util.concurrent.atomic.AtomicLong;
20 import org.wamblee.xmlrouter.common.Id;
21 import org.wamblee.xmlrouter.config.Config;
22 import org.wamblee.xmlrouter.config.ConfigException;
23 import org.wamblee.xmlrouter.config.DocumentType;
24 import org.wamblee.xmlrouter.config.Filter;
25 import org.wamblee.xmlrouter.config.RouterConfig;
26 import org.wamblee.xmlrouter.config.RouterConfigService;
27 import org.wamblee.xmlrouter.config.Transformation;
29 // TODO pass unique id of config service in the constructor to allow multiple instances with
30 // unique ids (ServiceFactory)
33 * Router configuration service providing an atomic configuration API for the
36 * @author Erik Brakkee
38 public class XMLRouterConfigService implements RouterConfigService {
40 private String application;
41 private AtomicLong sequence;
42 private XMLRouterConfiguration config;
43 private Config<RouterConfig> routerConfigs;
45 public XMLRouterConfigService(String aApplication,
46 XMLRouterConfiguration aConfig) {
47 application = aApplication;
48 sequence = new AtomicLong(1L);
50 routerConfigs = new ConfigImpl<RouterConfig>(new Id<Config>(
52 public RouterConfig wrap(final String aPrefix, final RouterConfig aT) {
53 return new RouterConfig() {
55 public Id<RouterConfig> getId() {
56 return new Id<RouterConfig>(aT.getId().toString());
60 public Config<DocumentType> documentTypeConfig() {
61 return aT.documentTypeConfig();
65 public Config<Filter> filterConfig() {
66 return aT.filterConfig();
70 public Config<Transformation> transformationConfig() {
71 return aT.transformationConfig();
79 public RouterConfig emptyConfig(String aId) {
80 return new SingleRouterConfig(new Id<RouterConfig>(application + "." +
85 public void apply(RouterConfig aConfig, Id<RouterConfig> aOldConfig) {
86 if (aOldConfig != null) {
87 if (!routerConfigs.remove(new Id<RouterConfig>(application + "." +
88 aOldConfig.getId()))) {
89 throw new ConfigException("Unknown routerconfig '" +
93 routerConfigs.add(aConfig);
98 public void clear(Id<RouterConfig> aConfig) {
99 routerConfigs.remove(aConfig);
103 private void update() {
104 ExtendedRouterConfig newconfig = new CompositeRouterConfig(
105 new Id<RouterConfig>("routerconfig"), routerConfigs.values());
106 config.setRouterConfig(newconfig);