--- /dev/null
+/*
+ * 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 org.wamblee.xmlrouter.common.Id;
+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 RouterConfigServiceImpl implements RouterConfigService {
+
+ @Override
+ public RouterConfig emptyConfig() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public Id<RouterConfig> apply(RouterConfig aConfig,
+ Id<RouterConfig> aOldConfig) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public void clear(Id<RouterConfig> aConfig) {
+ // TODO Auto-generated method stub
+
+ }
+
+}
private Clock clock;
private AtomicLong nextEventId;
- private ExtendedRouterConfig routerConfig;
- private TransformationPaths transformations;
+ private XMLRouterConfiguration config;
private Map<Id<Destination>, Destination> destinations;
- public XMLRouter(Clock aClock, ExtendedRouterConfig aRouterConfig,
+ public XMLRouter(Clock aClock, XMLRouterConfiguration aConfig,
EventListener aListener) {
sequenceNumbers = new AtomicLong(1);
listener = aListener;
clock = aClock;
nextEventId = new AtomicLong(clock.currentTimeMillis());
- routerConfig = aRouterConfig;
- transformations = new TransformationPaths();
+ config = aConfig;
destinations = new LinkedHashMap<Id<Destination>, Destination>();
}
@Override
public void publish(String aSource, DOMSource aEvent) {
+ config.startPublishEvent();
+ try {
+ publishImpl(aSource, aEvent);
+ } finally {
+ config.endPublishEvent();
+ }
+ }
+
+ private void publishImpl(String aSource, DOMSource aEvent) {
long time = clock.currentTimeMillis();
- if (routerConfig.isDirty()) {
- transformations.replaceTransformations(routerConfig
- .transformationConfig().map());
- routerConfig.resetDirty();
+ // TODO dirty flag will become unnecessary in the future.
+ if (config.routerConfig().isDirty()) {
+ config.transformations().replaceTransformations(
+ config.routerConfig().transformationConfig().map());
+ config.routerConfig().resetDirty();
}
Id<DOMSource> id = new Id<DOMSource>(nextEventId.getAndIncrement());
boolean delivered = false;
Set<String> possibleTargetTypes = new HashSet<String>();
- possibleTargetTypes.addAll(transformations
+ possibleTargetTypes.addAll(config.transformations()
.getPossibleTargetTypes(aInputType));
// ask each destination what target types, if any they want to have.
if (!requested.isEmpty()) {
// Deliver to the destination.
for (String targetType : requested) {
- TransformationPath path = transformations.getPath(
+ TransformationPath path = config.transformations().getPath(
aInputType, targetType);
List<Transformation> ts = path.getTransformations();
int i = 0;
private boolean isAllowedByFilters(String aType, DOMSource aEvent) {
boolean allowed = true;
- for (Filter filter : routerConfig.filterConfig().map().values()) {
+ for (Filter filter : config.routerConfig().filterConfig().map()
+ .values()) {
if (!filter.isAllowed(aType, aEvent)) {
allowed = false;
}
private List<String> determineDocumentTypes(DOMSource aEvent) {
List<String> res = new ArrayList<String>();
- for (DocumentType type : routerConfig.documentTypeConfig().map()
- .values()) {
+ for (DocumentType type : config.routerConfig().documentTypeConfig()
+ .map().values()) {
if (type.isInstance(aEvent)) {
res.add(type.getName());
}
--- /dev/null
+/*
+ * 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;
+
+/**
+ * The XML Router configuration contains both configuration data as well as
+ * derived data used by the XML router. It is important that all this data is
+ * consistent. Therefore, this class provides a read-write locking mechanism for
+ * accessing the data.
+ *
+ * @author Erik Brakkee
+ *
+ */
+public interface XMLRouterConfiguration {
+
+ /**
+ * To be called before the configuration is updated. Corresponds to write
+ * lock acquire.
+ */
+ void startConfigurationChange();
+
+ /**
+ * To be called after the configuration is updated. Corresponds to write
+ * lock acquire.
+ */
+ void endConfigurationChange();
+
+ /**
+ * To be called before the configuration is used. Corresponds to read lock
+ * acquire.
+ */
+ void startPublishEvent();
+
+ /**
+ * To be called after the configuration is used. Corresponds to read lock
+ * release.
+ */
+ void endPublishEvent();
+
+ /**
+ * @return Configuration data.
+ */
+ ExtendedRouterConfig routerConfig();
+
+ /**
+ * @return Transformation paths (derived data).
+ */
+ TransformationPaths transformations();
+}
--- /dev/null
+/*
+ * 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.locks.ReentrantReadWriteLock;
+
+/**
+ * Implements the XML Router configuration interface including the required
+ * locking.
+ *
+ * @author Erik Brakkee
+ */
+public class XMLRouterConfigurationImpl implements XMLRouterConfiguration {
+
+ private final ReentrantReadWriteLock rwlock = new ReentrantReadWriteLock(
+ true);
+ private final ReentrantReadWriteLock.ReadLock rlock = rwlock.readLock();
+ private final ReentrantReadWriteLock.WriteLock wlock = rwlock.writeLock();
+
+ private ExtendedRouterConfig config;
+ private TransformationPaths transformations;
+
+ public XMLRouterConfigurationImpl(ExtendedRouterConfig aConfig,
+ TransformationPaths aTransformations) {
+ config = aConfig;
+ transformations = aTransformations;
+ }
+
+ @Override
+ public void startConfigurationChange() {
+ wlock.lock();
+ }
+
+ @Override
+ public void endConfigurationChange() {
+ wlock.unlock();
+ }
+
+ @Override
+ public void startPublishEvent() {
+ rlock.lock();
+ }
+
+ @Override
+ public void endPublishEvent() {
+ rlock.unlock();
+ }
+
+ @Override
+ public ExtendedRouterConfig routerConfig() {
+ return config;
+ }
+
+ @Override
+ public TransformationPaths transformations() {
+ return transformations;
+ }
+}
}
private ExtendedRouterConfig routerConfig;
+ private XMLRouterConfiguration config;
private XMLRouter router;
private DOMSource source1;
private DOMSource source2;
@Before
public void setUp() {
routerConfig = new SingleRouterConfig(new AtomicLong(1L));
+ config = new XMLRouterConfigurationImpl(routerConfig,
+ new TransformationPaths());
EventListener logListener = new LoggingEventListener(Level.INFO);
listener = spy(logListener);
- router = new XMLRouter(new SystemClock(), routerConfig, listener);
+ router = new XMLRouter(new SystemClock(), config, listener);
source1 = mock(DOMSource.class);
source2 = mock(DOMSource.class);
source3 = mock(DOMSource.class);