fe6d50773e7f4d9c5399f5b7abfe9b5d2e38a19e
[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(config.transformationConfig()
42             .map());
43     }
44
45     public XMLRouterConfigurationImpl() {
46         this(new CompositeRouterConfig(new ArrayList<RouterConfig>()));
47     }
48
49     @Override
50     public void startPublishEvent() {
51         rlock.lock();
52     }
53
54     @Override
55     public void endPublishEvent() {
56         rlock.unlock();
57     }
58
59     @Override
60     public ExtendedRouterConfig getRouterConfig() {
61         return config;
62     }
63
64     @Override
65     public void setRouterConfig(ExtendedRouterConfig aConfig) {
66
67         TransformationPaths newTransformations = new TransformationPaths(
68             aConfig.transformationConfig().map());
69
70         wlock.lock();
71         try {
72             config = aConfig;
73             transformations = newTransformations;
74         } finally {
75             wlock.unlock();
76         }
77     }
78
79     @Override
80     public TransformationPaths getTransformations() {
81         return transformations;
82     }
83 }