ca5e41748e08f8e22851c7ab0b21760f4e904a0f
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / XMLRouterConfigService.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.xmlrouter.impl;
17
18 import java.util.concurrent.atomic.AtomicLong;
19
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;
24
25 /**
26  * Router configuration service providing an atomic configuration API for the
27  * XML router.
28  * 
29  * @author Erik Brakkee
30  */
31 public class XMLRouterConfigService implements RouterConfigService {
32
33     private AtomicLong sequence;
34     private XMLRouterConfiguration config;
35     private Config<RouterConfig> routerConfigs;
36
37     public XMLRouterConfigService(XMLRouterConfiguration aConfig) {
38         sequence = new AtomicLong(1L);
39         config = aConfig;
40         routerConfigs = new ConfigImpl<RouterConfig>(sequence) {
41             public RouterConfig wrap(Id<RouterConfig> aId, RouterConfig aT) {
42                 return aT;
43             }
44         };
45     }
46
47     @Override
48     public RouterConfig emptyConfig() {
49         return new SingleRouterConfig(sequence);
50     }
51
52     @Override
53     public Id<RouterConfig> apply(RouterConfig aConfig,
54         Id<RouterConfig> aOldConfig) {
55         config.startConfigurationChange();
56         try {
57             return applyImpl(aConfig, aOldConfig);
58         } finally {
59             config.endConfigurationChange();
60         }
61     }
62
63     private Id<RouterConfig> applyImpl(RouterConfig aConfig,
64         Id<RouterConfig> aOldConfig) {
65         if (aOldConfig != null) {
66             routerConfigs.remove(aOldConfig);
67         }
68         Id<RouterConfig> id = routerConfigs.add(aConfig);
69         update();
70         return id;
71     }
72
73     @Override
74     public void clear(Id<RouterConfig> aConfig) {
75         config.startConfigurationChange();
76         try {
77             clearImpl(aConfig);
78         } finally {
79             config.endConfigurationChange();
80         }
81     }
82
83     private void clearImpl(Id<RouterConfig> aConfig) {
84         routerConfigs.remove(aConfig);
85         update();
86     }
87
88     private void update() {
89         ExtendedRouterConfig newconfig = new CompositeRouterConfig(
90             routerConfigs.map().values());
91         config.setRouterConfig(newconfig);
92     }
93
94 }