99db0fd2b65a71b77e0e3eaade316cdbd4bcbf23
[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.config.RouterConfig;
22
23 /**
24  * Implements the XML Router configuration interface including the required
25  * locking.
26  * 
27  * @author Erik Brakkee
28  */
29 public class XMLRouterConfigurationImpl implements XMLRouterConfiguration {
30
31     private final ReentrantReadWriteLock rwlock = new ReentrantReadWriteLock(
32         true);
33     private final ReentrantReadWriteLock.ReadLock rlock = rwlock.readLock();
34     private final ReentrantReadWriteLock.WriteLock wlock = rwlock.writeLock();
35
36     private ExtendedRouterConfig config;
37     private TransformationPaths transformations;
38
39     public XMLRouterConfigurationImpl(ExtendedRouterConfig aConfig) {
40         config = aConfig;
41         transformations = new TransformationPaths();
42     }
43
44     public XMLRouterConfigurationImpl() {
45         this(new CompositeRouterConfig(new ArrayList<RouterConfig>()));
46     }
47
48     @Override
49     public void startConfigurationChange() {
50         wlock.lock();
51     }
52
53     @Override
54     public void endConfigurationChange() {
55         wlock.unlock();
56     }
57
58     @Override
59     public void startPublishEvent() {
60         rlock.lock();
61     }
62
63     @Override
64     public void endPublishEvent() {
65         rlock.unlock();
66     }
67
68     @Override
69     public ExtendedRouterConfig getRouterConfig() {
70         return config;
71     }
72
73     @Override
74     public void setRouterConfig(ExtendedRouterConfig aConfig) {
75         config = aConfig;
76         transformations.replaceTransformations(config.transformationConfig()
77             .map());
78     }
79
80     @Override
81     public TransformationPaths getTransformations() {
82         return transformations;
83     }
84 }