You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by ge...@apache.org on 2009/02/12 15:38:55 UTC
svn commit: r743766 - in /servicemix/smx4/nmr/trunk:
jbi/osgi/src/main/java/org/apache/servicemix/jbi/osgi/
nmr/api/src/main/java/org/apache/servicemix/nmr/api/
nmr/api/src/main/java/org/apache/servicemix/nmr/api/service/
nmr/api/src/test/java/org/apac...
Author: gertv
Date: Thu Feb 12 14:38:54 2009
New Revision: 743766
URL: http://svn.apache.org/viewvc?rev=743766&view=rev
Log:
SMX4NMR-20: Implement SA connections -- adding wiring to the NMR
Added:
servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/Wire.java
servicemix/smx4/nmr/trunk/nmr/api/src/test/java/org/apache/servicemix/nmr/api/service/
servicemix/smx4/nmr/trunk/nmr/api/src/test/java/org/apache/servicemix/nmr/api/service/ServiceHelperTest.java
Modified:
servicemix/smx4/nmr/trunk/jbi/osgi/src/main/java/org/apache/servicemix/jbi/osgi/RegistryWrapper.java
servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/EndpointRegistry.java
servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/service/ServiceHelper.java
servicemix/smx4/nmr/trunk/nmr/core/src/main/java/org/apache/servicemix/nmr/core/EndpointRegistryImpl.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
Modified: servicemix/smx4/nmr/trunk/jbi/osgi/src/main/java/org/apache/servicemix/jbi/osgi/RegistryWrapper.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/jbi/osgi/src/main/java/org/apache/servicemix/jbi/osgi/RegistryWrapper.java?rev=743766&r1=743765&r2=743766&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/jbi/osgi/src/main/java/org/apache/servicemix/jbi/osgi/RegistryWrapper.java (original)
+++ servicemix/smx4/nmr/trunk/jbi/osgi/src/main/java/org/apache/servicemix/jbi/osgi/RegistryWrapper.java Thu Feb 12 14:38:54 2009
@@ -26,6 +26,7 @@
import org.apache.servicemix.nmr.api.EndpointRegistry;
import org.apache.servicemix.nmr.api.Endpoint;
import org.apache.servicemix.nmr.api.Reference;
+import org.apache.servicemix.nmr.api.Wire;
import org.apache.servicemix.nmr.core.util.MapToDictionary;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
@@ -86,4 +87,12 @@
public Set<Endpoint> getServices() {
return registry.getServices();
}
+
+ public void register(Wire wire) {
+ registry.register(wire);
+ }
+
+ public void unregister(Wire wire) {
+ registry.unregister(wire);
+ }
}
Modified: servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/EndpointRegistry.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/EndpointRegistry.java?rev=743766&r1=743765&r2=743766&view=diff
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/EndpointRegistry.java (original)
+++ servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/EndpointRegistry.java Thu Feb 12 14:38:54 2009
@@ -89,5 +89,20 @@
*/
Reference lookup(String filter);
+ /**
+ * Register a {@link Wire}. This will allow you to access an existing endpoint through the wire's from address as well.
+ *
+ * @param wire the wire to be registered
+ */
+ void register(Wire wire);
+
+ /**
+ * Unregister an existing {@link Wire} from the registry. After calling this method, the registry will no longer take the
+ * wire into account when resolving endpoints.
+ *
+ * @param wire the wire to be unregistered
+ */
+ void unregister(Wire wire);
+
}
Added: servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/Wire.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/Wire.java?rev=743766&view=auto
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/Wire.java (added)
+++ servicemix/smx4/nmr/trunk/nmr/api/src/main/java/org/apache/servicemix/nmr/api/Wire.java Thu Feb 12 14:38:54 2009
@@ -0,0 +1,47 @@
+/*
+ * 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.nmr.api;
+
+import java.util.Map;
+
+import org.apache.servicemix.nmr.api.service.ServiceHelper;
+
+/**
+ * Represents a wire to an endpoint. It provides a means of linking another set of endpoint properties to an existing endpoint.
+ *
+ * A wire can be created using {@link ServiceHelper#createWire(Map, Map)} and needs to be registered in the
+ * {@link EndpointRegistry} to take effect.
+ *
+ * A wire allows you to link one
+ */
+public interface Wire {
+
+ /**
+ * Get the new address made available by the wire.
+ *
+ * @return the new address
+ */
+ public Map<String, ?> getFrom();
+
+ /**
+ * Get the target endpoint that is accessed when sending something to the from address
+ *
+ * @return the target endpoint
+ */
+ public Map<String, ?> getTo();
+
+}
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=743766&r1=743765&r2=743766&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 12 14:38:54 2009
@@ -18,6 +18,9 @@
import java.util.Map;
import java.util.HashMap;
+import java.util.Map.Entry;
+
+import org.apache.servicemix.nmr.api.Wire;
/**
*
@@ -35,4 +38,51 @@
return props;
}
+ /**
+ * Creates a {@link Wire} instance
+ *
+ * @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
+ */
+ 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 + "]";
+ }
+ };
+ }
+
+ /**
+ * 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 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 boolean equals(Map<String, ?> first, Map<String, ?> second) {
+ if (first == null || second == null) {
+ return false;
+ }
+ if (first.size() != second.size()) {
+ return false;
+ }
+ for (Entry<String, ?> entry : first.entrySet()) {
+ if (!second.containsKey(entry.getKey())) {
+ return false;
+ }
+ if (!second.get(entry.getKey()).equals(entry.getValue())) {
+ return false;
+ }
+ }
+ return true;
+ }
}
Added: 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=743766&view=auto
==============================================================================
--- servicemix/smx4/nmr/trunk/nmr/api/src/test/java/org/apache/servicemix/nmr/api/service/ServiceHelperTest.java (added)
+++ servicemix/smx4/nmr/trunk/nmr/api/src/test/java/org/apache/servicemix/nmr/api/service/ServiceHelperTest.java Thu Feb 12 14:38:54 2009
@@ -0,0 +1,87 @@
+/*
+ * 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.nmr.api.service;
+
+import java.util.Map;
+
+import org.apache.servicemix.nmr.api.Endpoint;
+import org.apache.servicemix.nmr.api.Wire;
+import org.junit.Test;
+
+import junit.framework.TestCase;
+
+/**
+ * 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",
+ Endpoint.ENDPOINT_NAME, "endpoint");
+ Map<String, Object> to = ServiceHelper.createMap(Endpoint.SERVICE_NAME, "test:wired-service",
+ Endpoint.ENDPOINT_NAME, "endpoint");
+ Wire wire = ServiceHelper.createWire(from, to);
+ assertEquals(from, wire.getFrom());
+ assertEquals(to, wire.getTo());
+ }
+
+ @Test
+ public void testEqualsHandleNull() throws Exception {
+ Map<String, Object> map = ServiceHelper.createMap(Endpoint.SERVICE_NAME, "test:service",
+ Endpoint.ENDPOINT_NAME, "endpoint");
+ assertFalse("Should always return false when either one is null", ServiceHelper.equals(null, map));
+ assertFalse("Should always return false when either one is null", ServiceHelper.equals(map, null));
+ }
+
+ @Test
+ 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",
+ Endpoint.ENDPOINT_NAME, "endpoint",
+ Endpoint.INTERFACE_NAME, "test:interface");
+ assertFalse("Maps with different sizes shouldn't match", ServiceHelper.equals(first, second));
+ }
+
+ @Test
+ public void testEqualsSameKeys() 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",
+ Endpoint.INTERFACE_NAME, "test:interface");
+ assertFalse("Maps with different keys shouldn't match", ServiceHelper.equals(first, second));
+ }
+
+ @Test
+ public void testEqualsSameValues() 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",
+ Endpoint.ENDPOINT_NAME, "endpoint");
+ assertFalse("Maps with different values for the same key shouldn't match", ServiceHelper.equals(first, second));
+ }
+
+ @Test
+ public void testEquals() 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:service",
+ Endpoint.ENDPOINT_NAME, "endpoint");
+ assertTrue(ServiceHelper.equals(first, second));
+ }
+}
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=743766&r1=743765&r2=743766&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 12 14:38:54 2009
@@ -23,7 +23,6 @@
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
@@ -34,8 +33,10 @@
import org.apache.servicemix.nmr.api.NMR;
import org.apache.servicemix.nmr.api.Reference;
import org.apache.servicemix.nmr.api.ServiceMixException;
+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;
@@ -54,6 +55,7 @@
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() {
}
@@ -187,7 +189,26 @@
*/
@SuppressWarnings("unchecked")
public List<Endpoint> query(Map<String, ?> properties) {
- return (List<Endpoint>) (List) internalQuery(properties);
+ return (List<Endpoint>) (List) internalQuery(handleWiring(properties));
+ }
+
+ /**
+ * 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 (Map<String, ?> key : wires.keySet()) {
+ if (ServiceHelper.equals(properties, key)) {
+ return wires.get(key).getTo();
+ }
+ }
+ //no wires registered, just returning the Map itself
+ return properties;
}
/**
@@ -197,7 +218,8 @@
* <p/>
* This could return actual endpoints, or a dynamic proxy to a number of endpoints
*/
- public Reference lookup(final Map<String, ?> properties) {
+ public Reference lookup(Map<String, ?> props) {
+ final Map<String, ?> properties = handleWiring(props);
DynamicReferenceImpl ref = new DynamicReferenceImpl(this, new Filter<InternalEndpoint>() {
public boolean match(InternalEndpoint endpoint) {
Map<String, ?> epProps = registry.getProperties(endpoint);
@@ -284,4 +306,11 @@
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/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=743766&r1=743765&r2=743766&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 12 14:38:54 2009
@@ -17,6 +17,8 @@
package org.apache.servicemix.nmr.core;
import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -116,6 +118,57 @@
registry.unregister(endpoint, null);
assertTrue(unregLatch.await(1, TimeUnit.SECONDS));
}
+
+ @Test
+ public void testHandleWiring() throws Exception {
+ final Map<String, Object> from = ServiceHelper.createMap(Endpoint.SERVICE_NAME, "test:wired-service",
+ Endpoint.ENDPOINT_NAME, "endpoint");
+ final Map<String, Object> to = ServiceHelper.createMap(Endpoint.SERVICE_NAME, "test:service",
+ Endpoint.ENDPOINT_NAME, "endpoint");
+ createWiredEndpoint(from, to);
+ assertEquals(to, ((EndpointRegistryImpl) registry).handleWiring(from));
+ assertEquals(to, ((EndpointRegistryImpl) registry).handleWiring(to));
+ }
+
+ public void testEndpointWiringOnQuery() throws Exception {
+ final Map<String, Object> from = ServiceHelper.createMap(Endpoint.SERVICE_NAME, "test:wired-service",
+ Endpoint.ENDPOINT_NAME, "endpoint");
+ final Endpoint endpoint = createWiredEndpoint(from);
+
+ // make sure that the query for the wire's from returns the target endpoint
+ List<Endpoint> result = registry.query(from);
+ assertEquals(1, result.size());
+ assertEquals(endpoint, ((InternalEndpoint) result.get(0)).getEndpoint());
+ }
+
+ @Test
+ public void testEndpointWiringOnLookup() throws Exception {
+ final Map<String, Object> from = ServiceHelper.createMap(Endpoint.SERVICE_NAME, "test:wired-service",
+ Endpoint.ENDPOINT_NAME, "endpoint");
+ final Endpoint endpoint = createWiredEndpoint(from);
+
+ // make sure that the query for the wire's from returns the target endpoint
+ Reference ref = registry.lookup(from);
+ assertNotNull(ref);
+ assertTrue(ref instanceof DynamicReferenceImpl);
+ DynamicReferenceImpl reference = (DynamicReferenceImpl) ref;
+ Iterable<InternalEndpoint> endpoints = reference.choose();
+ assertNotNull(endpoints);
+ assertTrue(endpoints.iterator().hasNext());
+ assertEquals(endpoint, endpoints.iterator().next().getEndpoint());
+ }
+
+ private Endpoint createWiredEndpoint(Map<String, Object> from) {
+ return createWiredEndpoint(from, ServiceHelper.createMap(Endpoint.SERVICE_NAME, "test:service",
+ Endpoint.ENDPOINT_NAME, "endpoint"));
+ }
+
+ private Endpoint createWiredEndpoint(Map<String, Object> from, Map<String, Object> to) {
+ final Endpoint endpoint = new DummyEndpoint();
+ registry.register(endpoint, to);
+ registry.register(ServiceHelper.createWire(from, to));
+ return endpoint;
+ }
protected static class DummyEndpoint implements Endpoint {
public void setChannel(Channel channel) {
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=743766&r1=743765&r2=743766&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 12 14:38:54 2009
@@ -16,6 +16,8 @@
*/
package org.apache.servicemix.nmr.core;
+import java.util.Map;
+
import org.apache.servicemix.nmr.api.*;
import org.apache.servicemix.nmr.api.service.ServiceHelper;
import org.apache.servicemix.nmr.core.ServiceMix;
@@ -50,7 +52,25 @@
assertNotNull(endpoint.getExchange());
assertEquals(Status.Done, e.getStatus());
}
-
+
+ @Test
+ public void testSendExchangeToWiredEndpointUsingClient() throws Exception {
+ MyEndpoint endpoint = new MyEndpoint();
+ Map<String, Object> target = ServiceHelper.createMap(Endpoint.NAME, "id");
+ Map<String, Object> wire = ServiceHelper.createMap(Endpoint.NAME, "wire");
+ //register the endpoint and a wire to the endpoint
+ nmr.getEndpointRegistry().register(endpoint, target);
+ nmr.getEndpointRegistry().register(ServiceHelper.createWire(wire, target));
+
+ Channel client = nmr.createChannel();
+ Exchange e = client.createExchange(Pattern.InOnly);
+ e.setTarget(nmr.getEndpointRegistry().lookup(wire));
+ e.getIn().setBody("Hello");
+ boolean res = client.sendSync(e);
+ assertTrue(res);
+ assertNotNull(endpoint.getExchange());
+ assertEquals(Status.Done, e.getStatus());
+ }
public static class MyEndpoint implements Endpoint {