a20da62d53f60ca6a6b36306b0177b7a41db29c1
[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.DocumentType;
22 import org.wamblee.xmlrouter.config.Filter;
23 import org.wamblee.xmlrouter.config.Transformation;
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 String application;
34     private AtomicLong sequence;
35     private XMLRouterConfiguration config;
36     private Config<RouterConfig> routerConfigs;
37
38     public XMLRouterConfigService(String aApplication,
39         XMLRouterConfiguration aConfig) {
40         application = aApplication;
41         sequence = new AtomicLong(1L);
42         config = aConfig;
43         routerConfigs = new ConfigImpl<RouterConfig>(RouterConfig.class,
44             aApplication) {
45             public RouterConfig wrap(final RouterConfig aT) {
46                 return new RouterConfig() {
47                     @Override
48                     public Id<RouterConfig> getId() {
49                         return aT.getId();
50                     }
51
52                     @Override
53                     public Config<DocumentType> documentTypeConfig() {
54                         return aT.documentTypeConfig();
55                     }
56
57                     @Override
58                     public Config<Filter> filterConfig() {
59                         return aT.filterConfig();
60                     }
61
62                     @Override
63                     public Config<Transformation> transformationConfig() {
64                         return aT.transformationConfig();
65                     }
66                 };
67             }
68         };
69     }
70
71     @Override
72     public RouterConfig emptyConfig(String aId) {
73         return new SingleRouterConfig(application + "." + aId);
74     }
75
76     @Override
77     public void apply(RouterConfig aConfig, Id<RouterConfig> aOldConfig) {
78         if (aOldConfig != null) {
79             if (!routerConfigs.remove(new Id<RouterConfig>(application + "." +
80                 aOldConfig.getId()))) {
81                 throw new ConfigException("Unknown routerconfig '" +
82                     aOldConfig + "'");
83             }
84         }
85         routerConfigs.add(aConfig);
86         update();
87     }
88
89     @Override
90     public void clear(Id<RouterConfig> aConfig) {
91         routerConfigs.remove(aConfig);
92         update();
93     }
94
95     private void update() {
96         ExtendedRouterConfig newconfig = new CompositeRouterConfig(
97             new Id<RouterConfig>("routerconfig"), routerConfigs.values());
98         config.setRouterConfig(newconfig);
99     }
100 }