summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2009-06-08 21:44:14 +0000
committerrfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68>2009-06-08 21:44:14 +0000
commit6ebe0961b2ec3b32b261a0965515fb91a443a008 (patch)
treea780fd2089db5788453dd7e542fc2dd03457a860
parentfe46ff50ea80c96adc552d3dda232834a20f159f (diff)
Start to add an Apache Tomcat Tribes based replicated EndpointRegistry
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@782800 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
-rw-r--r--java/sca/modules/endpoint-tribes/META-INF/MANIFEST.MF17
-rw-r--r--java/sca/modules/endpoint-tribes/pom.xml46
-rw-r--r--java/sca/modules/endpoint-tribes/src/main/java/org/apache/tuscany/sca/endpoint/tribes/ReplicatedEndpointRegistry.java211
-rw-r--r--java/sca/modules/endpoint-tribes/src/main/resources/META-INF/services/org.apache.tuscany.sca.runtime.EndpointRegistry17
4 files changed, 291 insertions, 0 deletions
diff --git a/java/sca/modules/endpoint-tribes/META-INF/MANIFEST.MF b/java/sca/modules/endpoint-tribes/META-INF/MANIFEST.MF
new file mode 100644
index 0000000000..8b2bc031b8
--- /dev/null
+++ b/java/sca/modules/endpoint-tribes/META-INF/MANIFEST.MF
@@ -0,0 +1,17 @@
+Manifest-Version: 1.0
+Private-Package: org.apache.tuscany.sca.xsd.impl;version="2.0.0"
+Tool: Bnd-0.0.255
+Bundle-Name: Apache Tuscany SCA Tomcat Tribes Based EndPoint Registry
+Created-By: 1.6.0_07 (Sun Microsystems Inc.)
+Bundle-Vendor: The Apache Software Foundation
+Bundle-Version: 2.0.0
+Bnd-LastModified: 1225397174343
+Bundle-ManifestVersion: 2
+Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt
+Bundle-Description: Apache Tuscany SCA XSD Model
+Bundle-SymbolicName: org.apache.tuscany.sca.endpoint.tribes
+Bundle-DocURL: http://www.apache.org/
+Bundle-RequiredExecutionEnvironment: J2SE-1.5,JavaSE-1.6
+Import-Package: org.apache.tuscany.sca.assembly;version="2.0.0",
+ org.apache.tuscany.sca.policy;version="2.0.0",
+ org.apache.tuscany.sca.runtime;version="2.0.0"
diff --git a/java/sca/modules/endpoint-tribes/pom.xml b/java/sca/modules/endpoint-tribes/pom.xml
new file mode 100644
index 0000000000..c52893ddf5
--- /dev/null
+++ b/java/sca/modules/endpoint-tribes/pom.xml
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * 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.
+-->
+<project>
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-modules</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ <relativePath>../pom.xml</relativePath>
+ </parent>
+ <artifactId>tuscany-endpoint-tribes</artifactId>
+ <name>Apache Tuscany SCA Tomcat Tribes Based EndPoint Registry</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.tomcat</groupId>
+ <artifactId>tribes</artifactId>
+ <version>6.0.18</version>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-core-spi</artifactId>
+ <version>2.0-SNAPSHOT</version>
+ <scope>compile</scope>
+ </dependency>
+ </dependencies>
+
+</project>
diff --git a/java/sca/modules/endpoint-tribes/src/main/java/org/apache/tuscany/sca/endpoint/tribes/ReplicatedEndpointRegistry.java b/java/sca/modules/endpoint-tribes/src/main/java/org/apache/tuscany/sca/endpoint/tribes/ReplicatedEndpointRegistry.java
new file mode 100644
index 0000000000..81ddfe8b70
--- /dev/null
+++ b/java/sca/modules/endpoint-tribes/src/main/java/org/apache/tuscany/sca/endpoint/tribes/ReplicatedEndpointRegistry.java
@@ -0,0 +1,211 @@
+/*
+ * 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.tuscany.sca.endpoint.tribes;
+
+import java.net.InetAddress;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.logging.Logger;
+
+import org.apache.catalina.tribes.Channel;
+import org.apache.catalina.tribes.ChannelException;
+import org.apache.catalina.tribes.group.GroupChannel;
+import org.apache.catalina.tribes.membership.McastService;
+import org.apache.catalina.tribes.tipis.AbstractReplicatedMap;
+import org.apache.catalina.tribes.tipis.ReplicatedMap;
+import org.apache.tuscany.sca.assembly.Endpoint2;
+import org.apache.tuscany.sca.assembly.EndpointReference2;
+import org.apache.tuscany.sca.runtime.EndpointListener;
+import org.apache.tuscany.sca.runtime.EndpointRegistry;
+
+public class ReplicatedEndpointRegistry implements EndpointRegistry {
+ private final static Logger logger = Logger.getLogger(ReplicatedEndpointRegistry.class.getName());
+ private static final String MULTICAST_ADDRESS = "228.0.0.100";
+ private static final int MULTICAST_PORT = 50000;
+ private String domainURI;
+
+ private List<EndpointReference2> endpointreferences = new CopyOnWriteArrayList<EndpointReference2>();
+ private List<EndpointListener> listeners = new CopyOnWriteArrayList<EndpointListener>();
+
+ private ReplicatedMap map;
+
+ private static final Channel createChannel(String address, int port, String bindAddress) {
+
+ //create a channel
+ GroupChannel channel = new GroupChannel();
+ McastService mcastService = (McastService)channel.getMembershipService();
+ mcastService.setPort(port);
+ mcastService.setAddress(address);
+
+ // REVIEW: In my case, there are multiple IP addresses
+ // One for the WIFI and the other one for VPN. For some reason the VPN one doesn't support
+ // Multicast
+ if (bindAddress != null) {
+ mcastService.setBind(bindAddress);
+ }
+
+ try {
+ channel.start(Channel.DEFAULT);
+ } catch (ChannelException e) {
+ throw new IllegalStateException(e);
+ }
+ return channel;
+ }
+
+ public ReplicatedEndpointRegistry(String domainURI) {
+ this.domainURI = domainURI;
+ map =
+ new ReplicatedMap(null, createChannel(MULTICAST_ADDRESS, MULTICAST_PORT, null), 50, domainURI,
+ new ClassLoader[] {ReplicatedEndpointRegistry.class.getClassLoader()});
+ }
+
+ public void addEndpoint(Endpoint2 endpoint) {
+ map.put(getURI(endpoint), endpoint);
+ for (EndpointListener listener : listeners) {
+ listener.endpointAdded(endpoint);
+ }
+ logger.info("EndpointRegistry: Add endpoint - " + endpoint);
+ }
+
+ public void addEndpointReference(EndpointReference2 endpointReference) {
+ endpointreferences.add(endpointReference);
+ logger.info("EndpointRegistry: Add endpoint reference - " + endpointReference);
+ }
+
+ public void addListener(EndpointListener listener) {
+ listeners.add(listener);
+ }
+
+ public List<Endpoint2> findEndpoint(EndpointReference2 endpointReference) {
+ List<Endpoint2> foundEndpoints = new ArrayList<Endpoint2>();
+
+ logger.info("EndpointRegistry: Find endpoint for reference - " + endpointReference);
+
+ if (endpointReference.getReference() != null) {
+ Endpoint2 targetEndpoint = endpointReference.getTargetEndpoint();
+ for (Object v : map.values()) {
+ Endpoint2 endpoint = (Endpoint2)v;
+ // TODO: implement more complete matching
+ if (endpoint.getComponentName().equals(targetEndpoint.getComponentName())) {
+ if ((targetEndpoint.getServiceName() != null) && (endpoint.getServiceName().equals(targetEndpoint
+ .getServiceName()))) {
+ foundEndpoints.add(endpoint);
+ logger.info("EndpointRegistry: Found endpoint with matching service - " + endpoint);
+ } else if (targetEndpoint.getServiceName() == null) {
+ foundEndpoints.add(endpoint);
+ logger.info("EndpointRegistry: Found endpoint with matching component - " + endpoint);
+ }
+ // else the service name doesn't match
+ }
+ }
+ }
+ return foundEndpoints;
+ }
+
+ public List<EndpointReference2> findEndpointReference(Endpoint2 endpoint) {
+ return endpointreferences;
+ }
+
+ public Endpoint2 getEndpoint(String uri) {
+ return (Endpoint2)map.get(uri);
+ }
+
+ public List<EndpointReference2> getEndpointRefereneces() {
+ return endpointreferences;
+ }
+
+ public List<Endpoint2> getEndpoints() {
+ return new ArrayList<Endpoint2>(map.values());
+ }
+
+ public List<EndpointListener> getListeners() {
+ return listeners;
+ }
+
+ private String getURI(Endpoint2 ep) {
+ String bindingName = ep.getBinding().getName();
+ if (bindingName == null) {
+ bindingName = ep.getService().getName();
+ }
+ String epURI = ep.getComponent().getURI() + "#" + ep.getService().getName() + "/" + bindingName;
+ return epURI;
+ }
+
+ public void removeEndpoint(Endpoint2 endpoint) {
+ map.remove(getURI(endpoint));
+ for (EndpointListener listener : listeners) {
+ listener.endpointRemoved(endpoint);
+ }
+ logger.info("EndpointRegistry: Remove endpoint - " + endpoint);
+ }
+
+ public void removeEndpointReference(EndpointReference2 endpointReference) {
+ endpointreferences.remove(endpointReference);
+ logger.info("EndpointRegistry: Remove endpoint reference - " + endpointReference);
+ }
+
+ public void removeListener(EndpointListener listener) {
+ listeners.remove(listener);
+ }
+
+ public void updateEndpoint(String uri, Endpoint2 endpoint) {
+ Endpoint2 oldEndpoint = getEndpoint(uri);
+ if (oldEndpoint == null) {
+ throw new IllegalArgumentException("Endpoint is not found: " + uri);
+ }
+ map.put(getURI(endpoint), endpoint);
+ for (EndpointListener listener : listeners) {
+ listener.endpointUpdated(oldEndpoint, endpoint);
+ }
+ }
+
+ public static void main(String[] args) throws Exception {
+ //create a channel
+ GroupChannel channel = new GroupChannel();
+ McastService mcastService = (McastService)channel.getMembershipService();
+ mcastService.setPort(MULTICAST_PORT);
+ mcastService.setAddress(MULTICAST_ADDRESS);
+
+ InetAddress localhost = InetAddress.getLocalHost();
+
+ // REVIEW: In my case, there are multiple IP addresses
+ // One for the WIFI and the other one for VPN. For some reason the VPN one doesn't support
+ // Multicast
+ mcastService.setBind("192.168.1.100");
+ channel.start(Channel.DEFAULT);
+ ReplicatedMap map = new ReplicatedMap(null, channel, 50, "01", null);
+ map.put(UUID.randomUUID().toString(), localhost.getHostAddress());
+ for (int i = 0; i < 5; i++) {
+ Thread.sleep(2000);
+ System.out.println(localhost + ": " + map.keySet());
+ }
+ for(Object e: map.entrySetFull()) {
+ Map.Entry en = (Map.Entry) e;
+ AbstractReplicatedMap.MapEntry entry = (AbstractReplicatedMap.MapEntry) en.getValue();
+ entry.isPrimary();
+ }
+ map.breakdown();
+ channel.stop(Channel.DEFAULT);
+ }
+
+}
diff --git a/java/sca/modules/endpoint-tribes/src/main/resources/META-INF/services/org.apache.tuscany.sca.runtime.EndpointRegistry b/java/sca/modules/endpoint-tribes/src/main/resources/META-INF/services/org.apache.tuscany.sca.runtime.EndpointRegistry
new file mode 100644
index 0000000000..1d0e5ffe6d
--- /dev/null
+++ b/java/sca/modules/endpoint-tribes/src/main/resources/META-INF/services/org.apache.tuscany.sca.runtime.EndpointRegistry
@@ -0,0 +1,17 @@
+# 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.
+org.apache.tuscany.sca.endpoint.tribes.ReplicatedEndpointRegistry \ No newline at end of file