Restructured the project creating a router directory for the router bundle implementa...
[xmlrouter] / router / impl / src / main / java / org / wamblee / xmlrouter / impl / XMLRouterConfigService.java
diff --git a/router/impl/src/main/java/org/wamblee/xmlrouter/impl/XMLRouterConfigService.java b/router/impl/src/main/java/org/wamblee/xmlrouter/impl/XMLRouterConfigService.java
new file mode 100644 (file)
index 0000000..a20da62
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2005-2011 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.xmlrouter.impl;
+
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.wamblee.xmlrouter.common.Id;
+import org.wamblee.xmlrouter.config.DocumentType;
+import org.wamblee.xmlrouter.config.Filter;
+import org.wamblee.xmlrouter.config.Transformation;
+
+/**
+ * Router configuration service providing an atomic configuration API for the
+ * XML router.
+ * 
+ * @author Erik Brakkee
+ */
+public class XMLRouterConfigService implements RouterConfigService {
+
+    private String application;
+    private AtomicLong sequence;
+    private XMLRouterConfiguration config;
+    private Config<RouterConfig> routerConfigs;
+
+    public XMLRouterConfigService(String aApplication,
+        XMLRouterConfiguration aConfig) {
+        application = aApplication;
+        sequence = new AtomicLong(1L);
+        config = aConfig;
+        routerConfigs = new ConfigImpl<RouterConfig>(RouterConfig.class,
+            aApplication) {
+            public RouterConfig wrap(final RouterConfig aT) {
+                return new RouterConfig() {
+                    @Override
+                    public Id<RouterConfig> getId() {
+                        return aT.getId();
+                    }
+
+                    @Override
+                    public Config<DocumentType> documentTypeConfig() {
+                        return aT.documentTypeConfig();
+                    }
+
+                    @Override
+                    public Config<Filter> filterConfig() {
+                        return aT.filterConfig();
+                    }
+
+                    @Override
+                    public Config<Transformation> transformationConfig() {
+                        return aT.transformationConfig();
+                    }
+                };
+            }
+        };
+    }
+
+    @Override
+    public RouterConfig emptyConfig(String aId) {
+        return new SingleRouterConfig(application + "." + aId);
+    }
+
+    @Override
+    public void apply(RouterConfig aConfig, Id<RouterConfig> aOldConfig) {
+        if (aOldConfig != null) {
+            if (!routerConfigs.remove(new Id<RouterConfig>(application + "." +
+                aOldConfig.getId()))) {
+                throw new ConfigException("Unknown routerconfig '" +
+                    aOldConfig + "'");
+            }
+        }
+        routerConfigs.add(aConfig);
+        update();
+    }
+
+    @Override
+    public void clear(Id<RouterConfig> aConfig) {
+        routerConfigs.remove(aConfig);
+        update();
+    }
+
+    private void update() {
+        ExtendedRouterConfig newconfig = new CompositeRouterConfig(
+            new Id<RouterConfig>("routerconfig"), routerConfigs.values());
+        config.setRouterConfig(newconfig);
+    }
+}