From 40aa7960a748c80a2465d5ca78e8f91d43296c8e Mon Sep 17 00:00:00 2001 From: rfeng Date: Fri, 29 Jan 2010 03:34:21 +0000 Subject: Refactor the DomainRegistryFactory to be extensions of DomainRegistryFactoryExtensionPoint git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@904367 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/runtime/BaseDomainRegistryFactory.java | 114 ++++++++++++ .../tuscany/sca/runtime/BaseEndpointRegistry.java | 202 +++++++++++++++++++++ ...DefaultDomainRegistryFactoryExtensionPoint.java | 91 ++++++++++ .../tuscany/sca/runtime/DomainRegistryFactory.java | 3 +- .../DomainRegistryFactoryExtensionPoint.java | 33 ++++ .../tuscany/sca/runtime/EndpointRegistry.java | 28 ++- .../sca/runtime/ExtensibleDomainRegistry.java | 96 ++++++++++ 7 files changed, 561 insertions(+), 6 deletions(-) create mode 100644 sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/BaseDomainRegistryFactory.java create mode 100644 sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/BaseEndpointRegistry.java create mode 100644 sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/DefaultDomainRegistryFactoryExtensionPoint.java create mode 100644 sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/DomainRegistryFactoryExtensionPoint.java create mode 100644 sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/ExtensibleDomainRegistry.java (limited to 'sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany') diff --git a/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/BaseDomainRegistryFactory.java b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/BaseDomainRegistryFactory.java new file mode 100644 index 0000000000..97b3f81017 --- /dev/null +++ b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/BaseDomainRegistryFactory.java @@ -0,0 +1,114 @@ +/* + * 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.runtime; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.LifeCycleListener; + +/** + * The utility responsible for finding the endpoint regstry by the scheme and creating instances for the + * given domain + */ +public abstract class BaseDomainRegistryFactory implements DomainRegistryFactory, LifeCycleListener { + protected ExtensionPointRegistry registry; + protected Map endpointRegistries = new ConcurrentHashMap(); + protected List listeners = new ArrayList(); + + /** + * @param extensionRegistry + */ + public BaseDomainRegistryFactory(ExtensionPointRegistry registry) { + super(); + this.registry = registry; + } + + public void start() { + } + + public synchronized EndpointRegistry getEndpointRegistry(String endpointRegistryURI, String domainURI) { + if (endpointRegistryURI == null) { + endpointRegistryURI = domainURI; + } + + Object key = getKey(endpointRegistryURI, domainURI); + + EndpointRegistry endpointRegistry = endpointRegistries.get(key); + if (endpointRegistry != null) { + return endpointRegistry; + } + + endpointRegistry = createEndpointRegistry(endpointRegistryURI, domainURI); + + if (endpointRegistry instanceof LifeCycleListener) { + ((LifeCycleListener)endpointRegistry).start(); + } + + for (EndpointListener listener : listeners) { + endpointRegistry.addListener(listener); + } + endpointRegistries.put(key, endpointRegistry); + return endpointRegistry; + } + + protected Object getKey(String endpointRegistryURI, String domainURI) { + return endpointRegistryURI + "," + domainURI; + } + + protected abstract EndpointRegistry createEndpointRegistry(String endpointRegistryURI, String domainURI); + + public void stop() { + for (EndpointRegistry endpointRegistry : endpointRegistries.values()) { + if (endpointRegistry instanceof LifeCycleListener) { + ((LifeCycleListener)endpointRegistry).stop(); + } + } + endpointRegistries.clear(); + listeners.clear(); + } + + public synchronized Collection getEndpointRegistries() { + return new ArrayList(endpointRegistries.values()); + } + + public synchronized void addListener(EndpointListener listener) { + listeners.add(listener); + for (EndpointRegistry registry : endpointRegistries.values()) { + registry.addListener(listener); + } + } + + public synchronized List getListeners() { + return listeners; + } + + public synchronized void removeListener(EndpointListener listener) { + listeners.remove(listener); + for (EndpointRegistry registry : endpointRegistries.values()) { + registry.removeListener(listener); + } + } + +} diff --git a/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/BaseEndpointRegistry.java b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/BaseEndpointRegistry.java new file mode 100644 index 0000000000..5646916075 --- /dev/null +++ b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/BaseEndpointRegistry.java @@ -0,0 +1,202 @@ +/* + * 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.runtime; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.logging.Logger; + +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.assembly.EndpointReference; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.LifeCycleListener; + +/** + * A replicated EndpointRegistry based on Apache Tomcat Tribes + */ +public abstract class BaseEndpointRegistry implements EndpointRegistry, LifeCycleListener { + protected final static Logger logger = Logger.getLogger(BaseEndpointRegistry.class.getName()); + + protected String domainRegistryURI; + protected String domainURI; + + protected List endpointreferences = new CopyOnWriteArrayList(); + protected List listeners = new CopyOnWriteArrayList(); + protected ExtensionPointRegistry registry; + protected Map attributes; + + public BaseEndpointRegistry(ExtensionPointRegistry registry, + Map attributes, + String domainRegistryURI, + String domainURI) { + this.registry = registry; + this.domainURI = domainURI; + this.domainRegistryURI = domainRegistryURI; + this.attributes = attributes; + } + + public abstract void addEndpoint(Endpoint endpoint); + + public void addEndpointReference(EndpointReference endpointReference) { + endpointreferences.add(endpointReference); + logger.fine("Add endpoint reference - " + endpointReference); + } + + public void addListener(EndpointListener listener) { + listeners.add(listener); + } + + protected void endpointAdded(Endpoint endpoint) { + ((RuntimeEndpoint)endpoint).bind(registry, this); + for (EndpointListener listener : listeners) { + listener.endpointAdded(endpoint); + } + } + + protected void endpointRemoved(Endpoint endpoint) { + ((RuntimeEndpoint)endpoint).bind(registry, this); + for (EndpointListener listener : listeners) { + listener.endpointRemoved(endpoint); + } + } + + protected void endpointUpdated(Endpoint oldEp, Endpoint newEp) { + ((RuntimeEndpoint)newEp).bind(registry, this); + for (EndpointListener listener : listeners) { + listener.endpointUpdated(oldEp, newEp); + } + } + + public List findEndpoint(EndpointReference endpointReference) { + logger.fine("Find endpoint for reference - " + endpointReference); + + if (endpointReference.getReference() != null) { + Endpoint targetEndpoint = endpointReference.getTargetEndpoint(); + return findEndpoint(targetEndpoint.getURI()); + } + + return new ArrayList(); + } + + public abstract List findEndpoint(String uri); + + public List findEndpointReference(Endpoint endpoint) { + return endpointreferences; + } + + public abstract Endpoint getEndpoint(String uri); + + public List getEndpointReferences() { + return endpointreferences; + } + + public abstract Collection getEndpoints(); + + public List getListeners() { + return listeners; + } + + /** + * Check if a serviceURI matches the given endpoint URI + * @param serviceURI + * @param endpointURI + * @return + */ + protected boolean matches(String serviceURI, String endpointURI) { + String[] parts1 = parseServiceURI(serviceURI); + String[] parts2 = parseStructuralURI(endpointURI); + for (int i = 0; i < parts1.length; i++) { + if (parts1[i] == null || parts1[i].equals(parts2[i])) { + continue; + } else { + return false; + } + } + return true; + } + + /** + * Parse the service URI into an array of names. The service URI is in one of the following formats: + *
    + *
  • componentName + *
  • componentName/serviceName + *
  • componentName/serviceName/bindingName + *
+ * @param serviceURI + * @return + */ + protected String[] parseServiceURI(String serviceURI) { + if (serviceURI.contains("#")) { + return parseStructuralURI(serviceURI); + } + String[] names = new String[3]; + String[] segments = serviceURI.split("/"); + for (int i = 0; i < names.length && i < segments.length; i++) { + names[i] = segments[i]; + } + return names; + } + + /** + * Parse the structural URI into an array of parts (componentURI, serviceName, bindingName) + * @param structuralURI + * @return [0]: componentURI [1]: serviceName [2]: bindingName + */ + protected String[] parseStructuralURI(String structuralURI) { + String[] names = new String[3]; + int index = structuralURI.lastIndexOf('#'); + if (index == -1) { + names[0] = structuralURI; + } else { + names[0] = structuralURI.substring(0, index); + String str = structuralURI.substring(index + 1); + if (str.startsWith("service-binding(") && str.endsWith(")")) { + str = str.substring("service-binding(".length(), str.length() - 1); + String[] parts = str.split("/"); + if (parts.length != 2) { + throw new IllegalArgumentException("Invalid service-binding URI: " + structuralURI); + } + names[1] = parts[0]; + names[2] = parts[1]; + } else if (str.startsWith("service(") && str.endsWith(")")) { + str = str.substring("service(".length(), str.length() - 1); + names[1] = str; + } else { + throw new IllegalArgumentException("Invalid structural URI: " + structuralURI); + } + } + return names; + } + + public abstract void removeEndpoint(Endpoint endpoint); + + public void removeEndpointReference(EndpointReference endpointReference) { + endpointreferences.remove(endpointReference); + logger.fine("Remove endpoint reference - " + endpointReference); + } + + public void removeListener(EndpointListener listener) { + listeners.remove(listener); + } + +} diff --git a/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/DefaultDomainRegistryFactoryExtensionPoint.java b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/DefaultDomainRegistryFactoryExtensionPoint.java new file mode 100644 index 0000000000..600ebb7bce --- /dev/null +++ b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/DefaultDomainRegistryFactoryExtensionPoint.java @@ -0,0 +1,91 @@ +/* + * 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.runtime; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.LifeCycleListener; +import org.apache.tuscany.sca.extensibility.ServiceDeclaration; +import org.apache.tuscany.sca.extensibility.ServiceHelper; +import org.oasisopen.sca.ServiceRuntimeException; + +/** + * + */ +public class DefaultDomainRegistryFactoryExtensionPoint implements DomainRegistryFactoryExtensionPoint, + LifeCycleListener { + private ExtensionPointRegistry registry; + private boolean loaded; + private List factories = new ArrayList(); + + /** + * @param registry + */ + public DefaultDomainRegistryFactoryExtensionPoint(ExtensionPointRegistry registry) { + super(); + this.registry = registry; + } + + public void addDomainRegistryFactory(DomainRegistryFactory factory) { + ServiceHelper.start(factory); + factories.add(factory); + } + + public List getDomainRegistryFactories() { + load(); + return factories; + } + + private synchronized void load() { + if (loaded) { + return; + } + try { + Collection declarations = + registry.getServiceDiscovery().getServiceDeclarations(DomainRegistryFactory.class, true); + for (ServiceDeclaration declaration : declarations) { + DomainRegistryFactory factory = ServiceHelper.newInstance(registry, declaration); + addDomainRegistryFactory(factory); + } + } catch (Exception e) { + throw new ServiceRuntimeException(e); + } finally { + loaded = true; + } + } + + public void removeDomainRegistryFactory(DomainRegistryFactory factory) { + if (factories.remove(factory)) { + ServiceHelper.stop(factory); + } + + } + + public void start() { + } + + public void stop() { + ServiceHelper.stop(factories); + } + +} diff --git a/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/DomainRegistryFactory.java b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/DomainRegistryFactory.java index 7b71406ac6..bd97a525c0 100644 --- a/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/DomainRegistryFactory.java +++ b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/DomainRegistryFactory.java @@ -20,7 +20,6 @@ package org.apache.tuscany.sca.runtime; import java.util.Collection; -import java.util.List; /** * @@ -42,5 +41,5 @@ public interface DomainRegistryFactory { Collection getEndpointRegistries(); void addListener(EndpointListener listener); void removeListener(EndpointListener listener); - List getListeners(); + String[] getSupportedSchemes(); } diff --git a/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/DomainRegistryFactoryExtensionPoint.java b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/DomainRegistryFactoryExtensionPoint.java new file mode 100644 index 0000000000..4b823c0e7d --- /dev/null +++ b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/DomainRegistryFactoryExtensionPoint.java @@ -0,0 +1,33 @@ +/* + * 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.runtime; + +import java.util.List; + +/** + * + */ +public interface DomainRegistryFactoryExtensionPoint { + void addDomainRegistryFactory(DomainRegistryFactory factory); + + void removeDomainRegistryFactory(DomainRegistryFactory factory); + + List getDomainRegistryFactories(); +} diff --git a/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/EndpointRegistry.java b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/EndpointRegistry.java index 43cf9f6416..6c4839018d 100644 --- a/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/EndpointRegistry.java +++ b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/EndpointRegistry.java @@ -19,6 +19,7 @@ package org.apache.tuscany.sca.runtime; +import java.util.Collection; import java.util.List; import org.apache.tuscany.sca.assembly.Endpoint; @@ -28,22 +29,41 @@ import org.apache.tuscany.sca.assembly.EndpointReference; * The EndpointRegistry holds the active service endpoints for the SCA domain */ public interface EndpointRegistry { + /** + * Add an enpoint to the registry. If the endpoint URI is the same as an existing endpoint in the registry, + * the existing one will be updated + * @param endpoint + */ void addEndpoint(Endpoint endpoint); + + /** + * Remove an enpoint from the registry + * @param endpoint + */ void removeEndpoint(Endpoint endpoint); + /** + * Look up an enpoint from the registry + * @param uri The endpoint URI + * @return + */ Endpoint getEndpoint(String uri); - void updateEndpoint(String uri, Endpoint endpoint); + + /** + * Get all endpoints in the registry + * @return + */ + Collection getEndpoints(); + List findEndpoint(String uri); List findEndpoint(EndpointReference endpointReference); - List getEndpoints(); void addEndpointReference(EndpointReference endpointReference); void removeEndpointReference(EndpointReference endpointReference); - List findEndpointReference(Endpoint endpoint); + // List findEndpointReference(Endpoint endpoint); List getEndpointReferences(); void addListener(EndpointListener listener); void removeListener(EndpointListener listener); - List getListeners(); } diff --git a/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/ExtensibleDomainRegistry.java b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/ExtensibleDomainRegistry.java new file mode 100644 index 0000000000..0a0b503206 --- /dev/null +++ b/sca-java-2.x/trunk/modules/core-spi/src/main/java/org/apache/tuscany/sca/runtime/ExtensibleDomainRegistry.java @@ -0,0 +1,96 @@ +/* + * 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.runtime; + +import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.oasisopen.sca.ServiceRuntimeException; + +/** + * + */ +public class ExtensibleDomainRegistry implements DomainRegistryFactory { + private final DomainRegistryFactoryExtensionPoint factories; + + public ExtensibleDomainRegistry(ExtensionPointRegistry registry) { + this.factories = registry.getExtensionPoint(DomainRegistryFactoryExtensionPoint.class); + } + + public ExtensibleDomainRegistry(DomainRegistryFactoryExtensionPoint factories) { + this.factories = factories; + } + + public void addListener(EndpointListener listener) { + for (DomainRegistryFactory factory : factories.getDomainRegistryFactories()) { + factory.addListener(listener); + } + } + + public Collection getEndpointRegistries() { + List registries = new ArrayList(); + for (DomainRegistryFactory factory : factories.getDomainRegistryFactories()) { + registries.addAll(factory.getEndpointRegistries()); + } + return registries; + } + + public EndpointRegistry getEndpointRegistry(String endpointRegistryURI, String domainURI) { + if (endpointRegistryURI == null) { + endpointRegistryURI = domainURI; + } + + URI uri = URI.create(endpointRegistryURI); + String scheme = uri.getScheme(); + if (scheme == null) { + scheme = "vm"; + endpointRegistryURI = "vm:" + endpointRegistryURI; + } else { + scheme = scheme.toLowerCase(); + } + for (DomainRegistryFactory factory : factories.getDomainRegistryFactories()) { + String[] schemes = factory.getSupportedSchemes(); + if (schemes != null && Arrays.asList(schemes).contains(scheme)) { + EndpointRegistry endpointRegistry = factory.getEndpointRegistry(endpointRegistryURI, domainURI); + if (endpointRegistry == null) { + continue; + } else { + return endpointRegistry; + } + } + } + throw new ServiceRuntimeException("No EndpointRegistry can support " + endpointRegistryURI); + } + + public void removeListener(EndpointListener listener) { + for (DomainRegistryFactory factory : factories.getDomainRegistryFactories()) { + factory.removeListener(listener); + } + } + + public String[] getSupportedSchemes() { + return null; + } + +} -- cgit v1.2.3