7a915766ec93d9bca00647dc9c96d13e5131a17d
[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.general.Pair;
22 import org.wamblee.xmlrouter.common.Id;
23 import org.wamblee.xmlrouter.config.RouterConfig;
24
25 /**
26  * Implements the XML Router configuration interface including the required
27  * locking.
28  * 
29  * @author Erik Brakkee
30  */
31 public class XMLRouterConfigurationImpl implements XMLRouterConfiguration {
32
33     private final ReentrantReadWriteLock rwlock = new ReentrantReadWriteLock(
34         true);
35     private final ReentrantReadWriteLock.ReadLock rlock = rwlock.readLock();
36     private final ReentrantReadWriteLock.WriteLock wlock = rwlock.writeLock();
37
38     private ExtendedRouterConfig config;
39     private TransformationPaths transformations;
40
41     public XMLRouterConfigurationImpl(ExtendedRouterConfig aConfig) {
42         config = aConfig;
43         transformations = new TransformationPaths(config.transformationConfig()
44             .values());
45     }
46
47     public XMLRouterConfigurationImpl() {
48         this(new CompositeRouterConfig(new Id<RouterConfig>("routerconfig"),
49             new ArrayList<RouterConfig>()));
50     }
51
52     @Override
53     public Pair<ExtendedRouterConfig, TransformationPaths> getConfig() {
54         rlock.lock();
55         try {
56             return new Pair<ExtendedRouterConfig, TransformationPaths>(config,
57                 transformations);
58         } finally {
59             rlock.unlock();
60         }
61     }
62
63     @Override
64     public void setRouterConfig(ExtendedRouterConfig aConfig) {
65
66         TransformationPaths newTransformations = new TransformationPaths(
67             aConfig.transformationConfig().values());
68
69         wlock.lock();
70         try {
71             config = aConfig;
72             transformations = newTransformations;
73         } finally {
74             wlock.unlock();
75         }
76     }
77 }