Turned the API packages into bundles.
[xmlrouter] / router / 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
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 Pair<ExtendedRouterConfig, TransformationPaths> getConfig() {
53         rlock.lock();
54         try {
55             return new Pair<ExtendedRouterConfig, TransformationPaths>(config,
56                 transformations);
57         } finally {
58             rlock.unlock();
59         }
60     }
61
62     @Override
63     public void setRouterConfig(ExtendedRouterConfig aConfig) {
64
65         TransformationPaths newTransformations = new TransformationPaths(
66             aConfig.transformationConfig().values());
67
68         wlock.lock();
69         try {
70             config = aConfig;
71             transformations = newTransformations;
72         } finally {
73             wlock.unlock();
74         }
75     }
76 }