/* * 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.RouterConfig; import org.wamblee.xmlrouter.config.RouterConfigService; /** * Router configuration service providing an atomic configuration API for the * XML router. * * @author Erik Brakkee */ public class XMLRouterConfigService implements RouterConfigService { private AtomicLong sequence; private XMLRouterConfiguration config; private Config routerConfigs; public XMLRouterConfigService(XMLRouterConfiguration aConfig) { sequence = new AtomicLong(1L); config = aConfig; routerConfigs = new ConfigImpl(sequence) { public RouterConfig wrap(Id aId, RouterConfig aT) { return aT; } }; } @Override public RouterConfig emptyConfig() { return new SingleRouterConfig(sequence); } @Override public Id apply(RouterConfig aConfig, Id aOldConfig) { config.startConfigurationChange(); try { return applyImpl(aConfig, aOldConfig); } finally { config.endConfigurationChange(); } } private Id applyImpl(RouterConfig aConfig, Id aOldConfig) { if (aOldConfig != null) { routerConfigs.remove(aOldConfig); } Id id = routerConfigs.add(aConfig); update(); return id; } @Override public void clear(Id aConfig) { config.startConfigurationChange(); try { clearImpl(aConfig); } finally { config.endConfigurationChange(); } } private void clearImpl(Id aConfig) { routerConfigs.remove(aConfig); update(); } private void update() { ExtendedRouterConfig newconfig = new CompositeRouterConfig( routerConfigs.map().values()); config.setRouterConfig(newconfig); } }