equality based on the ids of the contents of SingleRouterConfig.
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / XMLRouterConfigurationImpl.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.ArrayList;
19 import java.util.concurrent.locks.ReentrantReadWriteLock;
20
21 import org.wamblee.xmlrouter.common.Id;
22 import org.wamblee.xmlrouter.config.RouterConfig;
23
24 /**
25  * Implements the XML Router configuration interface including the required
26  * locking.
27  * 
28  * @author Erik Brakkee
29  */
30 public class XMLRouterConfigurationImpl implements XMLRouterConfiguration {
31
32     private final ReentrantReadWriteLock rwlock = new ReentrantReadWriteLock(
33         true);
34     private final ReentrantReadWriteLock.ReadLock rlock = rwlock.readLock();
35     private final ReentrantReadWriteLock.WriteLock wlock = rwlock.writeLock();
36
37     private ExtendedRouterConfig config;
38     private TransformationPaths transformations;
39
40     public XMLRouterConfigurationImpl(ExtendedRouterConfig aConfig) {
41         config = aConfig;
42         transformations = new TransformationPaths(config.transformationConfig()
43             .values());
44     }
45
46     public XMLRouterConfigurationImpl() {
47         this(new CompositeRouterConfig(new Id<RouterConfig>("routerconfig"),
48             new ArrayList<RouterConfig>()));
49     }
50
51     @Override
52     public void startPublishEvent() {
53         rlock.lock();
54     }
55
56     @Override
57     public void endPublishEvent() {
58         rlock.unlock();
59     }
60
61     @Override
62     public ExtendedRouterConfig getRouterConfig() {
63         return config;
64     }
65
66     @Override
67     public void setRouterConfig(ExtendedRouterConfig aConfig) {
68
69         TransformationPaths newTransformations = new TransformationPaths(
70             aConfig.transformationConfig().values());
71
72         wlock.lock();
73         try {
74             config = aConfig;
75             transformations = newTransformations;
76         } finally {
77             wlock.unlock();
78         }
79     }
80
81     @Override
82     public TransformationPaths getTransformations() {
83         return transformations;
84     }
85 }