Author: gertv
Date: Thu Feb 19 21:01:53 2009
New Revision: 745993
URL: http://svn.apache.org/viewvc?rev=745993&view=rev
Log:
SMX4NMR-20: Implement SA connections
Added:
servicemix/smx4/nmr/trunk/jbi/deployer/src/test/java/org/apache/servicemix/jbi/deployer/artifacts/
servicemix/smx4/nmr/trunk/jbi/deployer/src/test/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceAssemblyImplTest.java
servicemix/smx4/nmr/trunk/jbi/deployer/src/test/java/org/apache/servicemix/jbi/deployer/descriptor/ConnectionTest.java
Modified:
servicemix/smx4/nmr/trunk/examples/ (props changed)
servicemix/smx4/nmr/trunk/examples/nmr/ (props changed)
servicemix/smx4/nmr/trunk/examples/nmr/client/ (props changed)
servicemix/smx4/nmr/trunk/examples/nmr/endpoint/ (props changed)
servicemix/smx4/nmr/trunk/jbi/deployer/pom.xml
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/ServiceAssembly.java
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceAssemblyImpl.java
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/descriptor/Connection.java
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/descriptor/Consumer.java
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/descriptor/Provider.java
servicemix/smx4/nmr/trunk/jbi/deployer/src/test/java/org/apache/servicemix/jbi/deployer/descriptor/DescriptorFactoryTest.java
servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/WireRegistry.java
servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/service/ServiceHelper.java
servicemix/smx4/nmr/trunk/nmr/api/src/test/java/org/apache/servicemix/nmr/api/service/ServiceHelperTest.java
servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/EndpointRegistryImpl.java
servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/WireRegistryImpl.java
servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/EndpointRegistryImplTest.java
servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/IntegrationTest.java
servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/WireRegistryImplTest.java
servicemix/smx4/nmr/trunk/pom.xml
Propchange: servicemix/smx4/nmr/trunk/examples/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Thu Feb 19 21:01:53 2009
@@ -0,0 +1 @@
+target
Propchange: servicemix/smx4/nmr/trunk/examples/nmr/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Thu Feb 19 21:01:53 2009
@@ -0,0 +1 @@
+target
Propchange: servicemix/smx4/nmr/trunk/examples/nmr/client/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Thu Feb 19 21:01:53 2009
@@ -0,0 +1,5 @@
+eclipse-classes
+target
+.classpath
+.project
+.settings
Propchange: servicemix/smx4/nmr/trunk/examples/nmr/endpoint/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Thu Feb 19 21:01:53 2009
@@ -0,0 +1,5 @@
+eclipse-classes
+target
+.classpath
+.project
+.settings
Modified: servicemix/smx4/nmr/trunk/jbi/deployer/pom.xml
URL:
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/pom.xml?rev=745993&r1=745992&r2=745993&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/jbi/deployer/pom.xml (original)
+++ servicemix/smx4/nmr/trunk/jbi/deployer/pom.xml Thu Feb 19 21:01:53 2009
@@ -71,6 +71,11 @@
<artifactId>log4j</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.jmock</groupId>
+ <artifactId>jmock</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
@@ -94,4 +99,4 @@
</plugins>
</build>
-</project>
\ No newline at end of file
+</project>
Modified:
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/ServiceAssembly.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/ServiceAssembly.java?rev=745993&r1=745992&r2=745993&view=diff
==============================================================================
---
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/ServiceAssembly.java
(original)
+++
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/ServiceAssembly.java
Thu Feb 19 21:01:53 2009
@@ -19,6 +19,9 @@
import javax.jbi.JBIException;
import javax.jbi.management.LifeCycleMBean;
+import org.apache.servicemix.jbi.deployer.descriptor.Provider;
+import org.apache.servicemix.nmr.api.Wire;
+
/**
* This interface represents a JBI Service Assembly and will be registered
* in the OSGi registry
@@ -59,4 +62,5 @@
* @throws JBIException
*/
void forceShutDown() throws JBIException;
+
}
Modified:
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceAssemblyImpl.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceAssemblyImpl.java?rev=745993&r1=745992&r2=745993&view=diff
==============================================================================
---
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceAssemblyImpl.java
(original)
+++
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceAssemblyImpl.java
Thu Feb 19 21:01:53 2009
@@ -19,14 +19,22 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
+import java.util.Map;
import javax.jbi.JBIException;
+import javax.xml.namespace.QName;
import org.apache.servicemix.jbi.deployer.ServiceAssembly;
import org.apache.servicemix.jbi.deployer.ServiceUnit;
+import org.apache.servicemix.jbi.deployer.descriptor.Connection;
import org.apache.servicemix.jbi.deployer.descriptor.DescriptorFactory;
+import org.apache.servicemix.jbi.deployer.descriptor.Provider;
import org.apache.servicemix.jbi.deployer.descriptor.ServiceAssemblyDesc;
import org.apache.servicemix.jbi.deployer.impl.AssemblyReferencesListener;
+import org.apache.servicemix.nmr.api.Endpoint;
+import org.apache.servicemix.nmr.api.Wire;
+import org.apache.servicemix.nmr.api.service.ServiceHelper;
+import org.apache.servicemix.nmr.core.util.MapToDictionary;
import org.osgi.framework.Bundle;
import org.osgi.service.prefs.Preferences;
@@ -105,6 +113,7 @@
if (state == State.Shutdown) {
transition(State.Initialized);
}
+ startConnections();
transition(State.Started);
if (persist) {
saveState();
@@ -130,6 +139,7 @@
if (state == State.Started) {
transition(State.Stopped);
}
+ stopConnections();
if (persist) {
saveState();
}
@@ -214,5 +224,31 @@
break;
}
}
-
+
+ private void startConnections() {
+ if (serviceAssemblyDesc.getConnections() != null &&
serviceAssemblyDesc.getConnections().getConnections() != null) {
+ for (Connection connection :
serviceAssemblyDesc.getConnections().getConnections()) {
+ Wire wire = connection.getWire();
+ registerWire(wire, wire.getFrom());
+ }
+ }
+ }
+
+ private void stopConnections() {
+ if (serviceAssemblyDesc.getConnections() != null &&
serviceAssemblyDesc.getConnections().getConnections() != null) {
+ for (Connection connection :
serviceAssemblyDesc.getConnections().getConnections()) {
+ Wire wire = connection.getWire();
+ unregisterWire(wire, wire.getFrom());
+ }
+ }
+ }
+
+ protected void registerWire(Wire wire, Map<String, ?> from) {
+ bundle.getBundleContext().registerService(Wire.class.getName(),
+ wire, new
MapToDictionary(from));
+ }
+
+ protected void unregisterWire(Wire wire, Map<String, ?> from) {
+
+ }
}
Modified:
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/descriptor/Connection.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/descriptor/Connection.java?rev=745993&r1=745992&r2=745993&view=diff
==============================================================================
---
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/descriptor/Connection.java
(original)
+++
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/descriptor/Connection.java
Thu Feb 19 21:01:53 2009
@@ -16,12 +16,16 @@
*/
package org.apache.servicemix.jbi.deployer.descriptor;
+import org.apache.servicemix.nmr.api.Wire;
+import org.apache.servicemix.nmr.api.service.ServiceHelper;
+
/**
* The <code><jbi:connection></code> element.
*/
public class Connection {
private Consumer consumer;
private Provider provider;
+ private Wire wire;
public Consumer getConsumer() {
return consumer;
@@ -38,4 +42,11 @@
public void setProvider(Provider provider) {
this.provider = provider;
}
+
+ public Wire getWire() {
+ if (wire == null) {
+ wire = ServiceHelper.createWire(consumer.toMap(),
provider.toMap());
+ }
+ return wire;
+ }
}
Modified:
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/descriptor/Consumer.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/descriptor/Consumer.java?rev=745993&r1=745992&r2=745993&view=diff
==============================================================================
---
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/descriptor/Consumer.java
(original)
+++
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/descriptor/Consumer.java
Thu Feb 19 21:01:53 2009
@@ -16,8 +16,13 @@
*/
package org.apache.servicemix.jbi.deployer.descriptor;
+import java.util.Map;
+
import javax.xml.namespace.QName;
+import org.apache.servicemix.nmr.api.Endpoint;
+import org.apache.servicemix.nmr.api.service.ServiceHelper;
+
/**
* The <code><jbi:consumer></code> element.
*/
@@ -49,4 +54,13 @@
public void setEndpointName(String endpointName) {
this.endpointName = endpointName;
}
+
+ public Map<String, ?> toMap() {
+ if (getInterfaceName() == null) {
+ return ServiceHelper.createMap(Endpoint.SERVICE_NAME,
getServiceName().toString(),
+ Endpoint.ENDPOINT_NAME,
getEndpointName());
+ } else {
+ return ServiceHelper.createMap(Endpoint.INTERFACE_NAME,
getInterfaceName().toString());
+ }
+ }
}
Modified:
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/descriptor/Provider.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/descriptor/Provider.java?rev=745993&r1=745992&r2=745993&view=diff
==============================================================================
---
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/descriptor/Provider.java
(original)
+++
servicemix/smx4/nmr/trunk/jbi/deployer/src/main/java/org/apache/servicemix/jbi/deployer/descriptor/Provider.java
Thu Feb 19 21:01:53 2009
@@ -16,8 +16,13 @@
*/
package org.apache.servicemix.jbi.deployer.descriptor;
+import java.util.Map;
+
import javax.xml.namespace.QName;
+import org.apache.servicemix.nmr.api.Endpoint;
+import org.apache.servicemix.nmr.api.service.ServiceHelper;
+
/**
* The <code><jbi:provider></code> element.
*/
@@ -40,4 +45,9 @@
public void setEndpointName(String endpointName) {
this.endpointName = endpointName;
}
+
+ public Map<String, Object> toMap() {
+ return ServiceHelper.createMap(Endpoint.SERVICE_NAME,
getServiceName().toString(),
+ Endpoint.ENDPOINT_NAME,
getEndpointName());
+ }
}
Added:
servicemix/smx4/nmr/trunk/jbi/deployer/src/test/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceAssemblyImplTest.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/src/test/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceAssemblyImplTest.java?rev=745993&view=auto
==============================================================================
---
servicemix/smx4/nmr/trunk/jbi/deployer/src/test/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceAssemblyImplTest.java
(added)
+++
servicemix/smx4/nmr/trunk/jbi/deployer/src/test/java/org/apache/servicemix/jbi/deployer/artifacts/ServiceAssemblyImplTest.java
Thu Feb 19 21:01:53 2009
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.servicemix.jbi.deployer.artifacts;
+
+import static org.junit.Assert.*;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.servicemix.jbi.deployer.ServiceAssembly;
+import
org.apache.servicemix.jbi.deployer.artifacts.AbstractLifecycleJbiArtifact.State;
+import org.apache.servicemix.jbi.deployer.descriptor.DescriptorFactory;
+import org.apache.servicemix.jbi.deployer.descriptor.ServiceAssemblyDesc;
+import org.apache.servicemix.jbi.deployer.impl.AssemblyReferencesListener;
+import org.apache.servicemix.nmr.api.Wire;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.service.prefs.Preferences;
+
+/**
+ * Test cases for {...@link ServiceAssemblyImpl}
+ */
+public class ServiceAssemblyImplTest {
+
+ private Mockery mockery;
+
+ @Before
+ public void setup() {
+ mockery = new Mockery();
+ }
+
+ @Test
+ public void testWiringOnServiceAssemblyConnections() throws Exception {
+ ServiceAssemblyDesc descriptor =
DescriptorFactory.buildDescriptor(DescriptorFactory.class.getResource("serviceAssembly.xml")).getServiceAssembly();
+ final Preferences prefs = mockery.mock(Preferences.class);
+ mockery.checking(new Expectations() {{
+ one(prefs).get("state", State.Initialized.name());
+ will(returnValue(State.Initialized.name()));
+ one(prefs).put("state", State.Started.name());
+ one(prefs).flush();
+ one(prefs).put("state", State.Stopped.name());
+ one(prefs).flush();
+ }});
+ final List<Wire> wires = new LinkedList<Wire>();
+ ServiceAssembly sa = new ServiceAssemblyImpl(null, descriptor, new
ArrayList<ServiceUnitImpl>(), prefs, new AssemblyReferencesListener(), false) {
+ @Override
+ protected void registerWire(Wire wire, Map<String, ?> from) {
+ wires.add(wire);
+ }
+ @Override
+ protected void unregisterWire(Wire wire, Map<String, ?> from) {
+ wires.remove(wire);
+ }
+ };
+ sa.start();
+ assertEquals(2, wires.size());
+
+ sa.stop();
+ assertEquals(0, wires.size());
+ }
+}
Added:
servicemix/smx4/nmr/trunk/jbi/deployer/src/test/java/org/apache/servicemix/jbi/deployer/descriptor/ConnectionTest.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/src/test/java/org/apache/servicemix/jbi/deployer/descriptor/ConnectionTest.java?rev=745993&view=auto
==============================================================================
---
servicemix/smx4/nmr/trunk/jbi/deployer/src/test/java/org/apache/servicemix/jbi/deployer/descriptor/ConnectionTest.java
(added)
+++
servicemix/smx4/nmr/trunk/jbi/deployer/src/test/java/org/apache/servicemix/jbi/deployer/descriptor/ConnectionTest.java
Thu Feb 19 21:01:53 2009
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.servicemix.jbi.deployer.descriptor;
+
+import static org.junit.Assert.*;
+
+import java.util.Map;
+
+import javax.xml.namespace.QName;
+
+import org.apache.servicemix.nmr.api.Endpoint;
+import org.apache.servicemix.nmr.api.Wire;
+import org.apache.servicemix.nmr.api.service.ServiceHelper;
+import org.junit.Test;
+
+/**
+ * Test case for {...@link Connection}
+ */
+public class ConnectionTest {
+
+ private static final String CONSUMER_ENDPOINT = "consumer-endpoint";
+ private static final QName CONSUMER_INTERFACE = new
QName("urn:test:consumer", "consumer-inferface");
+ private static final QName CONSUMER_SERVICE = new
QName("urn:test:consumer", "consumer-service");
+ private static final String PROVIDER_ENDPOINT = "provider-endpoint";
+ private static final QName PROVIDER_SERVICE = new
QName("urn:test:provider", "provider-service");
+
+ @Test
+ public void wireOnInterface() throws Exception {
+ Connection connection = new Connection();
+ Consumer consumer = new Consumer();
+ consumer.setInterfaceName(CONSUMER_INTERFACE);
+ connection.setConsumer(consumer);
+ connection.setProvider(createProvider());
+
+ Wire wire = connection.getWire();
+ assertNotNull(wire);
+ assertSame(wire, connection.getWire());
+ Map<String, ?> from = ServiceHelper.createMap(Endpoint.INTERFACE_NAME,
CONSUMER_INTERFACE.toString());
+ assertTrue(ServiceHelper.equals(from, wire.getFrom()));
+ assertWireToEnd(wire);
+ }
+
+ @Test
+ public void wireOnServiceAndEndpoint() throws Exception {
+ Connection connection = new Connection();
+ Consumer consumer = new Consumer();
+ consumer.setServiceName(CONSUMER_SERVICE);
+ consumer.setEndpointName(CONSUMER_ENDPOINT);
+ connection.setConsumer(consumer);
+ connection.setProvider(createProvider());
+
+ Wire wire = connection.getWire();
+ assertNotNull(wire);
+ assertSame(wire, connection.getWire());
+ Map<String, ?> from = ServiceHelper.createMap(Endpoint.SERVICE_NAME,
CONSUMER_SERVICE.toString(),
+ Endpoint.ENDPOINT_NAME,
CONSUMER_ENDPOINT);
+ assertTrue(ServiceHelper.equals(from, wire.getFrom()));
+ assertWireToEnd(wire);
+ }
+
+ private void assertWireToEnd(Wire wire) {
+ Map<String, ?> to = ServiceHelper.createMap(Endpoint.SERVICE_NAME,
PROVIDER_SERVICE.toString(),
+ Endpoint.ENDPOINT_NAME,
PROVIDER_ENDPOINT);
+ assertTrue(ServiceHelper.equals(to, wire.getTo()));
+
+ }
+
+ private Provider createProvider() {
+ Provider provider = new Provider();
+ provider.setServiceName(PROVIDER_SERVICE);
+ provider.setEndpointName(PROVIDER_ENDPOINT);
+ return provider;
+ }
+
+}
Modified:
servicemix/smx4/nmr/trunk/jbi/deployer/src/test/java/org/apache/servicemix/jbi/deployer/descriptor/DescriptorFactoryTest.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/deployer/src/test/java/org/apache/servicemix/jbi/deployer/descriptor/DescriptorFactoryTest.java?rev=745993&r1=745992&r2=745993&view=diff
==============================================================================
---
servicemix/smx4/nmr/trunk/jbi/deployer/src/test/java/org/apache/servicemix/jbi/deployer/descriptor/DescriptorFactoryTest.java
(original)
+++
servicemix/smx4/nmr/trunk/jbi/deployer/src/test/java/org/apache/servicemix/jbi/deployer/descriptor/DescriptorFactoryTest.java
Thu Feb 19 21:01:53 2009
@@ -140,5 +140,8 @@
buffer.append("]");
return buffer.toString();
}
-
+
+ public Descriptor createTestDescriptor(String xmlfile) {
+ return
DescriptorFactory.buildDescriptor(getClass().getResource(xmlfile));
+ }
}
Modified:
servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/WireRegistry.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/WireRegistry.java?rev=745993&r1=745992&r2=745993&view=diff
==============================================================================
---
servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/WireRegistry.java
(original)
+++
servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/WireRegistry.java
Thu Feb 19 21:01:53 2009
@@ -16,11 +16,36 @@
*/
package org.apache.servicemix.nmr.api;
+import java.util.Map;
+
import org.apache.servicemix.nmr.api.service.ServiceRegistry;
/**
* This registry is used to register/unregister {...@link Wire}s.
*/
public interface WireRegistry extends ServiceRegistry<Wire> {
-
+
+ /**
+ * Get the wire that matches the given properties
+ *
+ * @param properties the wire's properties
+ * @return the wire or <code>null</code> if there's no wire to match this
property set
+ */
+ public Wire getWire(Map<String, ?> properties);
+
+ /**
+ * Convenience method for registering a wire without having to specify the
wire's properties. The wire will be registered using
+ * the from properties map.
+ *
+ * @param wire the wire to be registered
+ */
+ public void register(Wire wire);
+
+ /**
+ * Convenience method for unregistering a wire without having to specify
the wire's properties. The wire will be unregistered
+ * using the from properties map.
+ *
+ * @param wire the wire to be unregistered
+ */
+ public void unregister(Wire wire);
}
Modified:
servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/service/ServiceHelper.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/service/ServiceHelper.java?rev=745993&r1=745992&r2=745993&view=diff
==============================================================================
---
servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/service/ServiceHelper.java
(original)
+++
servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/service/ServiceHelper.java
Thu Feb 19 21:01:53 2009
@@ -33,46 +33,46 @@
public static Map<String, Object> createMap(String... data) {
Map<String, Object> props = new HashMap<String, Object>();
for (int i = 0; i < data.length / 2; i++) {
- props.put(data[i*2], data[i*2+1]);
+ props.put(data[i * 2], data[i * 2 + 1]);
}
return props;
}
/**
- * Creates a {...@link Wire} instance
+ * Check if two endpoint propery maps are equal. This will check both maps
for equal sizes, keys and values. If either map is
+ * <code>null</code>, it will return <code>false</code>.
*
- * @param from the 'from' end of the wire
- * @param to the 'to' end for the wire (i.e. the target endpoint)
- * @return the wire instance
+ * @param first the first endpoint property map
+ * @param second the second endpoint property map
+ * @return <code>true</code> if the endpoint maps are equal,
<code>false</code> if it
*/
- public static Wire createWire(final Map<String, Object> from, final
Map<String, Object> to) {
- return new Wire() {
- public Map<String, ?> getFrom() {
- return from;
- }
- public Map<String, ?> getTo() {
- return to;
- }
- @Override
- public String toString() {
- return "Wire[" + from + " -> " + to + "]";
- }
- };
+ public static boolean equals(Map<String, ?> first, Map<String, ?> second) {
+ return doMatch(first, second, true);
}
-
+
/**
- * Check if two endpoint propery maps are equal. This will check both
maps for equal sizes, keys and values.
- * If either map is <code>null</code>, it will return <code>false</code>.
+ * Check if two endpoint propery maps match. If will return
<code>true</code> if, for every key in the first map, there's a matching key
+ * and value in the second map. If either map is <code>null</code>, it
will return <code>false</code>.
+ *
+ * Compared to the {...@link ServiceHelper#equals(Map, Map)} method, this
will allow for the second Map to contain more information than
+ * the first Map and still be considered a match
*
* @param first the first endpoint property map
* @param second the second endpoint property map
- * @return <code>true</code> if the endpoint maps are equal,
<code>false</code> if it
+ * @return <code>true</code> if the endpoint maps are equal,
<code>false</code> if it
*/
- public static boolean equals(Map<String, ?> first, Map<String, ?> second) {
+ public static boolean matches(Map<String, ?> first, Map<String, ?> second)
{
+ return doMatch(first, second, false);
+ }
+
+ /*
+ * Shared implementation for equals and matches
+ */
+ private static boolean doMatch(Map<String, ?> first, Map<String, ?>
second, boolean equal) {
if (first == null || second == null) {
return false;
}
- if (first.size() != second.size()) {
+ if (equal && first.size() != second.size()) {
return false;
}
for (Entry<String, ?> entry : first.entrySet()) {
@@ -85,4 +85,21 @@
}
return true;
}
+
+ /**
+ * Creates a {...@link Wire} instance
+ *
+ * @param to the target for the wire
+ * @return the wire object
+ */
+ public static Wire createWire(final Map<String, ?> from, final Map<String,
Object> to) {
+ return new Wire() {
+ public Map<String, ?> getFrom() {
+ return from;
+ }
+ public Map<String, ?> getTo() {
+ return to;
+ }
+ };
+ }
}
Modified:
servicemix/smx4/nmr/trunk/nmr/api/src/test/java/org/apache/servicemix/nmr/api/service/ServiceHelperTest.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/api/src/test/java/org/apache/servicemix/nmr/api/service/ServiceHelperTest.java?rev=745993&r1=745992&r2=745993&view=diff
==============================================================================
---
servicemix/smx4/nmr/trunk/nmr/api/src/test/java/org/apache/servicemix/nmr/api/service/ServiceHelperTest.java
(original)
+++
servicemix/smx4/nmr/trunk/nmr/api/src/test/java/org/apache/servicemix/nmr/api/service/ServiceHelperTest.java
Thu Feb 19 21:01:53 2009
@@ -28,7 +28,7 @@
* Test cases for {...@link ServiceHelper}
*/
public class ServiceHelperTest extends TestCase {
-
+
@Test
public void testCreateWire() throws Exception {
Map<String, Object> from =
ServiceHelper.createMap(Endpoint.SERVICE_NAME, "test:service",
@@ -52,10 +52,11 @@
public void testEqualsSameSize() throws Exception {
Map<String, Object> first =
ServiceHelper.createMap(Endpoint.SERVICE_NAME, "test:service",
Endpoint.ENDPOINT_NAME, "endpoint");
- Map<String, Object> second =
ServiceHelper.createMap(Endpoint.SERVICE_NAME, "test:wired-service",
+ Map<String, Object> second =
ServiceHelper.createMap(Endpoint.SERVICE_NAME, "test:service",
Endpoint.ENDPOINT_NAME, "endpoint",
Endpoint.INTERFACE_NAME, "test:interface");
- assertFalse("Maps with different sizes shouldn't match",
ServiceHelper.equals(first, second));
+ assertFalse("Maps with different sizes shouldn't be equal",
ServiceHelper.equals(first, second));
+ assertTrue("Maps with different sizes but the same values should
match", ServiceHelper.matches(first, second));
}
@Test
@@ -64,7 +65,8 @@
Endpoint.ENDPOINT_NAME, "endpoint");
Map<String, Object> second =
ServiceHelper.createMap(Endpoint.SERVICE_NAME, "test:wired-service",
Endpoint.INTERFACE_NAME, "test:interface");
- assertFalse("Maps with different keys shouldn't match",
ServiceHelper.equals(first, second));
+ assertFalse("Maps with different values shouldn't be equal",
ServiceHelper.equals(first, second));
+ assertFalse("Maps with different values shouldn't match",
ServiceHelper.matches(first, second));
}
@Test
Modified:
servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/EndpointRegistryImpl.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/EndpointRegistryImpl.java?rev=745993&r1=745992&r2=745993&view=diff
==============================================================================
---
servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/EndpointRegistryImpl.java
(original)
+++
servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/EndpointRegistryImpl.java
Thu Feb 19 21:01:53 2009
@@ -36,14 +36,13 @@
import org.apache.servicemix.nmr.api.Wire;
import org.apache.servicemix.nmr.api.event.EndpointListener;
import org.apache.servicemix.nmr.api.internal.InternalEndpoint;
-import org.apache.servicemix.nmr.api.service.ServiceHelper;
import org.apache.servicemix.nmr.api.service.ServiceRegistry;
import org.apache.servicemix.nmr.core.util.Filter;
import org.apache.servicemix.nmr.core.util.MapToDictionary;
/**
* Implementation of {...@link EndpointRegistry} interface that defines
- * methods to register, undergister and query endpoints.
+ * methods to register, unregister and query endpoints.
*
* @version $Revision: $
* @since 4.0
@@ -55,7 +54,6 @@
private Map<InternalEndpoint, Endpoint> wrappers = new
ConcurrentHashMap<InternalEndpoint, Endpoint>();
private Map<DynamicReferenceImpl, Boolean> references = new
WeakHashMap<DynamicReferenceImpl, Boolean>();
private ServiceRegistry<InternalEndpoint> registry;
- private Map<Map<String, ?>, Wire> wires = new
ConcurrentHashMap<Map<String,?>, Wire>();
public EndpointRegistryImpl() {
}
@@ -193,22 +191,20 @@
}
/**
- * Helper method that checks if this map represents the from end of a
registered {...@link Wire}
- * and returns the {...@link Wire}s to properties map if it does. If no
matching {...@link Wire} was found,
- * it will just return the original map.
+ * Helper method that checks if this map represents the from end of a
registered {...@link Wire} and returns the {...@link Wire}s to
+ * properties map if it does. If no matching {...@link Wire} was found, it
will just return the original map.
*
* @param properties the original properties
* @return the target endpoint if there is a registered {...@link Wire} or
the original properties
*/
protected Map<String, ?> handleWiring(Map<String, ?> properties) {
- //check for wires on this Map
- for (Wire wire : nmr.getWireRegistry().getServices()) {
- if (ServiceHelper.equals(properties, wire.getFrom())) {
- return wire.getTo();
- }
+ // check for wires on this Map
+ Wire wire = nmr.getWireRegistry().getWire(properties);
+ if (wire == null) {
+ return properties;
+ } else {
+ return wire.getTo();
}
- //no wires registered, just returning the Map itself
- return properties;
}
/**
@@ -305,12 +301,4 @@
}
return endpoints;
}
-
- public void register(Wire wire) {
- wires.put(wire.getFrom(), wire);
- }
-
- public void unregister(Wire wire) {
- wires.remove(wire);
- }
}
Modified:
servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/WireRegistryImpl.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/WireRegistryImpl.java?rev=745993&r1=745992&r2=745993&view=diff
==============================================================================
---
servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/WireRegistryImpl.java
(original)
+++
servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/WireRegistryImpl.java
Thu Feb 19 21:01:53 2009
@@ -16,12 +16,32 @@
*/
package org.apache.servicemix.nmr.core;
+import java.util.Map;
+
import org.apache.servicemix.nmr.api.Wire;
import org.apache.servicemix.nmr.api.WireRegistry;
+import org.apache.servicemix.nmr.api.service.ServiceHelper;
/**
* Default implementation for a {...@link WireRegistry}
*/
public class WireRegistryImpl extends ServiceRegistryImpl<Wire> implements
WireRegistry {
+ public Wire getWire(Map<String, ?> properties) {
+ for (Wire wire : getServices()) {
+ //TODO: we are using matches here instead of equals, so we should
find a way to deal with multiple wires
+ if (ServiceHelper.matches(properties, getProperties(wire))) {
+ return wire;
+ }
+ }
+ return null;
+ }
+
+ public void register(Wire wire) {
+ register(wire, wire.getFrom());
+ }
+
+ public void unregister(Wire wire) {
+ unregister(wire, wire.getFrom());
+ }
}
Modified:
servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/EndpointRegistryImplTest.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/EndpointRegistryImplTest.java?rev=745993&r1=745992&r2=745993&view=diff
==============================================================================
---
servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/EndpointRegistryImplTest.java
(original)
+++
servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/EndpointRegistryImplTest.java
Thu Feb 19 21:01:53 2009
@@ -166,7 +166,7 @@
private Endpoint createWiredEndpoint(Map<String, Object> from, Map<String,
Object> to) {
final Endpoint endpoint = new DummyEndpoint();
registry.register(endpoint, to);
- nmr.getWireRegistry().register(ServiceHelper.createWire(from, to),
null);
+ nmr.getWireRegistry().register(ServiceHelper.createWire(from, to));
return endpoint;
}
Modified:
servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/IntegrationTest.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/IntegrationTest.java?rev=745993&r1=745992&r2=745993&view=diff
==============================================================================
---
servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/IntegrationTest.java
(original)
+++
servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/IntegrationTest.java
Thu Feb 19 21:01:53 2009
@@ -60,7 +60,7 @@
Map<String, Object> wire = ServiceHelper.createMap(Endpoint.NAME,
"wire");
//register the endpoint and a wire to the endpoint
nmr.getEndpointRegistry().register(endpoint, target);
- nmr.getWireRegistry().register(ServiceHelper.createWire(wire, target),
null);
+ nmr.getWireRegistry().register(ServiceHelper.createWire(wire, target));
Channel client = nmr.createChannel();
Exchange e = client.createExchange(Pattern.InOnly);
Modified:
servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/WireRegistryImplTest.java
URL:
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/WireRegistryImplTest.java?rev=745993&r1=745992&r2=745993&view=diff
==============================================================================
---
servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/WireRegistryImplTest.java
(original)
+++
servicemix/smx4/nmr/trunk/nmr/core/src/test/java/org/apache/servicemix/nmr/core/WireRegistryImplTest.java
Thu Feb 19 21:01:53 2009
@@ -23,6 +23,7 @@
import org.apache.servicemix.nmr.api.Wire;
import org.apache.servicemix.nmr.api.WireRegistry;
+import org.apache.servicemix.nmr.api.service.ServiceHelper;
/**
* Test cases for {...@link WireRegistryImpl}
@@ -40,11 +41,31 @@
assertEquals(1, registry.getServices().size());
assertEquals(properties, registry.getProperties(wire));
+ assertSame(wire, registry.getWire(properties));
+
registry.unregister(wire, properties);
assertEquals(0, registry.getServices().size());
assertEquals(null, registry.getProperties(wire));
}
+ public void testBasicRegistryBehaviorWithConvenienceMethods() {
+ Map<String, Object> from = new HashMap<String, Object>();
+ from.put("wire-length", 12);
+ from.put("wire-type", "coton");
+ Wire wire = ServiceHelper.createWire(from,
ServiceHelper.createMap("wire-usage", "knitting"));
+
+ WireRegistry registry = new WireRegistryImpl();
+ registry.register(wire);
+ assertEquals(1, registry.getServices().size());
+ assertEquals(from, registry.getProperties(wire));
+
+ assertSame(wire, registry.getWire(from));
+
+ registry.unregister(wire);
+ assertEquals(0, registry.getServices().size());
+ assertEquals(null, registry.getProperties(wire));
+ }
+
private static final class MockWire implements Wire {
public Map<String, ?> getFrom() {
Modified: servicemix/smx4/nmr/trunk/pom.xml
URL:
http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/pom.xml?rev=745993&r1=745992&r2=745993&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/pom.xml (original)
+++ servicemix/smx4/nmr/trunk/pom.xml Thu Feb 19 21:01:53 2009
@@ -82,6 +82,7 @@
<geronimo.stax.version>1.0.1</geronimo.stax.version>
<jencks.version>2.1_1</jencks.version>
<jline.version>0.9.94</jline.version>
+ <jmock.version>2.5.1</jmock.version>
<junit.version>3.8.2_1</junit.version>
<log4j.version>1.2.14</log4j.version>
<pax.logging.version>1.1.1</pax.logging.version>
@@ -614,6 +615,11 @@
<version>${servicemix.kernel.version}</version>
<type>tar.gz</type>
</dependency>
+ <dependency>
+ <groupId>org.jmock</groupId>
+ <artifactId>jmock</artifactId>
+ <version>${jmock.version}</version>
+ </dependency>
</dependencies>
</dependencyManagement>