/* * 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.UUID; 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(new Id("config")) { public RouterConfig wrap(RouterConfig aT) { return aT; } }; } @Override public RouterConfig emptyConfig() { // TODO check and document API impacts. String id = UUID.randomUUID().toString(); return new SingleRouterConfig(new Id(id)); } @Override public void apply(RouterConfig aConfig, Id aOldConfig) { if (aOldConfig != null) { routerConfigs.remove(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); } }