more TODOs in the code.
[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 // TODO pass unique id of config service in the constructor to allow multiple instances with
26 // unique ids (ServiceFactory) 
27
28 /**
29  * Router configuration service providing an atomic configuration API for the
30  * XML router.
31  * 
32  * @author Erik Brakkee
33  */
34 public class XMLRouterConfigService implements RouterConfigService {
35
36     private AtomicLong sequence;
37     private XMLRouterConfiguration config;
38     private Config<RouterConfig> routerConfigs;
39
40     public XMLRouterConfigService(XMLRouterConfiguration aConfig) {
41         sequence = new AtomicLong(1L);
42         config = aConfig;
43         routerConfigs = new ConfigImpl<RouterConfig>(new Id<Config>("config")) {
44             public RouterConfig wrap(String aPrefix, RouterConfig aT) {
45                 return aT;
46             }
47         };
48     }
49
50     @Override
51     public RouterConfig emptyConfig(String aId) {
52         // TODO check AP{ impacts.
53         return new SingleRouterConfig(new Id<RouterConfig>(aId));
54     }
55
56     @Override
57     public void apply(RouterConfig aConfig, Id<RouterConfig> aOldConfig) {
58         if (aOldConfig != null) {
59             routerConfigs.remove(aOldConfig);
60         }
61         routerConfigs.add(aConfig);
62         update();
63     }
64
65     @Override
66     public void clear(Id<RouterConfig> aConfig) {
67         routerConfigs.remove(aConfig);
68         update();
69     }
70
71     private void update() {
72         ExtendedRouterConfig newconfig = new CompositeRouterConfig(
73             new Id<RouterConfig>("routerconfig"), routerConfigs.values());
74         config.setRouterConfig(newconfig);
75     }
76
77 }