From 6ebe0961b2ec3b32b261a0965515fb91a443a008 Mon Sep 17 00:00:00 2001 From: rfeng Date: Mon, 8 Jun 2009 21:44:14 +0000 Subject: 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 --- .../modules/endpoint-tribes/META-INF/MANIFEST.MF | 17 ++ java/sca/modules/endpoint-tribes/pom.xml | 46 +++++ .../tribes/ReplicatedEndpointRegistry.java | 211 +++++++++++++++++++++ ...org.apache.tuscany.sca.runtime.EndpointRegistry | 17 ++ 4 files changed, 291 insertions(+) create mode 100644 java/sca/modules/endpoint-tribes/META-INF/MANIFEST.MF create mode 100644 java/sca/modules/endpoint-tribes/pom.xml create mode 100644 java/sca/modules/endpoint-tribes/src/main/java/org/apache/tuscany/sca/endpoint/tribes/ReplicatedEndpointRegistry.java create mode 100644 java/sca/modules/endpoint-tribes/src/main/resources/META-INF/services/org.apache.tuscany.sca.runtime.EndpointRegistry (limited to 'java/sca/modules/endpoint-tribes') 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 @@ + + + + 4.0.0 + + org.apache.tuscany.sca + tuscany-modules + 2.0-SNAPSHOT + ../pom.xml + + tuscany-endpoint-tribes + Apache Tuscany SCA Tomcat Tribes Based EndPoint Registry + + + + org.apache.tomcat + tribes + 6.0.18 + compile + + + org.apache.tuscany.sca + tuscany-core-spi + 2.0-SNAPSHOT + compile + + + + 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 endpointreferences = new CopyOnWriteArrayList(); + private List listeners = new CopyOnWriteArrayList(); + + 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 findEndpoint(EndpointReference2 endpointReference) { + List foundEndpoints = new ArrayList(); + + 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 findEndpointReference(Endpoint2 endpoint) { + return endpointreferences; + } + + public Endpoint2 getEndpoint(String uri) { + return (Endpoint2)map.get(uri); + } + + public List getEndpointRefereneces() { + return endpointreferences; + } + + public List getEndpoints() { + return new ArrayList(map.values()); + } + + public List 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 -- cgit v1.2.3