/* * 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.Config; import org.wamblee.xmlrouter.config.ConfigException; import org.wamblee.xmlrouter.config.DocumentType; import org.wamblee.xmlrouter.config.Filter; import org.wamblee.xmlrouter.config.RouterConfig; import org.wamblee.xmlrouter.config.RouterConfigService; 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 routerConfigs; public XMLRouterConfigService(String aApplication, XMLRouterConfiguration aConfig) { application = aApplication; sequence = new AtomicLong(1L); config = aConfig; routerConfigs = new ConfigImpl(new Id( aApplication)) { public RouterConfig wrap(final String aPrefix, final RouterConfig aT) { return new RouterConfig() { @Override public Id getId() { return new Id(aT.getId().toString()); } @Override public Config documentTypeConfig() { return aT.documentTypeConfig(); } @Override public Config filterConfig() { return aT.filterConfig(); } @Override public Config transformationConfig() { return aT.transformationConfig(); } }; } }; } @Override public RouterConfig emptyConfig(String aId) { return new SingleRouterConfig(new Id(application + "." + aId)); } @Override public void apply(RouterConfig aConfig, Id aOldConfig) { if (aOldConfig != null) { if (!routerConfigs.remove(new Id(application + "." + aOldConfig.getId()))) { throw new ConfigException("Unknown routerconfig '" + aOldConfig + "'"); } } routerConfigs.add(aConfig); update(); } @Override public void clear(Id aConfig) { routerConfigs.remove(aConfig); update(); } private void update() { ExtendedRouterConfig newconfig = new CompositeRouterConfig( new Id("routerconfig"), routerConfigs.values()); config.setRouterConfig(newconfig); } }