054956df0a0e6d8255119298fb751e9fd9ed23d7
[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.UUID;
19 import java.util.concurrent.atomic.AtomicLong;
20
21 import org.wamblee.xmlrouter.common.Id;
22 import org.wamblee.xmlrouter.config.Config;
23 import org.wamblee.xmlrouter.config.RouterConfig;
24 import org.wamblee.xmlrouter.config.RouterConfigService;
25
26 /**
27  * Router configuration service providing an atomic configuration API for the
28  * XML router.
29  * 
30  * @author Erik Brakkee
31  */
32 public class XMLRouterConfigService implements RouterConfigService {
33
34     private AtomicLong sequence;
35     private XMLRouterConfiguration config;
36     private Config<RouterConfig> routerConfigs;
37
38     public XMLRouterConfigService(XMLRouterConfiguration aConfig) {
39         sequence = new AtomicLong(1L);
40         config = aConfig;
41         routerConfigs = new ConfigImpl<RouterConfig>(new Id<Config>("config")) {
42             public RouterConfig wrap(RouterConfig aT) {
43                 return aT;
44             }
45         };
46     }
47
48     @Override
49     public RouterConfig emptyConfig() {
50         // TODO check and document API impacts.
51         String id = UUID.randomUUID().toString();
52         return new SingleRouterConfig(new Id<RouterConfig>(id));
53     }
54
55     @Override
56     public void apply(RouterConfig aConfig, Id<RouterConfig> aOldConfig) {
57         if (aOldConfig != null) {
58             routerConfigs.remove(aOldConfig);
59         }
60         routerConfigs.add(aConfig);
61         update();
62     }
63
64     @Override
65     public void clear(Id<RouterConfig> aConfig) {
66         routerConfigs.remove(aConfig);
67         update();
68     }
69
70     private void update() {
71         ExtendedRouterConfig newconfig = new CompositeRouterConfig(
72             new Id<RouterConfig>("routerconfig"), routerConfigs.values());
73         config.setRouterConfig(newconfig);
74     }
75
76 }