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.RouterConfig;
23 import org.wamblee.xmlrouter.config.RouterConfigService;
26 * Router configuration service providing an atomic configuration API for the
29 * @author Erik Brakkee
31 public class XMLRouterConfigService implements RouterConfigService {
33 private AtomicLong sequence;
34 private XMLRouterConfiguration config;
35 private Config<RouterConfig> routerConfigs;
37 public XMLRouterConfigService(XMLRouterConfiguration aConfig) {
38 sequence = new AtomicLong(1L);
40 routerConfigs = new ConfigImpl<RouterConfig>(sequence) {
41 public RouterConfig wrap(Id<RouterConfig> aId, RouterConfig aT) {
48 public RouterConfig emptyConfig() {
49 return new SingleRouterConfig(sequence);
53 public Id<RouterConfig> apply(RouterConfig aConfig,
54 Id<RouterConfig> aOldConfig) {
55 config.startConfigurationChange();
57 return applyImpl(aConfig, aOldConfig);
59 config.endConfigurationChange();
63 private Id<RouterConfig> applyImpl(RouterConfig aConfig,
64 Id<RouterConfig> aOldConfig) {
65 if (aOldConfig != null) {
66 routerConfigs.remove(aOldConfig);
68 Id<RouterConfig> id = routerConfigs.add(aConfig);
74 public void clear(Id<RouterConfig> aConfig) {
75 config.startConfigurationChange();
79 config.endConfigurationChange();
83 private void clearImpl(Id<RouterConfig> aConfig) {
84 routerConfigs.remove(aConfig);
88 private void update() {
89 ExtendedRouterConfig newconfig = new CompositeRouterConfig(
90 routerConfigs.map().values());
91 config.setRouterConfig(newconfig);