From 7e3cc953eb4a9cc95acd48d3c3548112f882b9b0 Mon Sep 17 00:00:00 2001 From: rfeng Date: Fri, 26 Jun 2009 00:03:12 +0000 Subject: Some refactoring on the discovery git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@788553 13f79535-47bb-0310-9956-ffa450edef68 --- .../dosgi/discovery/AbstractDiscoveryService.java | 8 ++- .../DiscoveredServiceNotificationImpl.java | 67 +++++++++++++++++++++ .../dosgi/discovery/DomainDiscoveryService.java | 22 +------ .../sca/dosgi/discovery/EndpointDescription.java | 69 ++++++++++++++++++++++ .../sca/dosgi/discovery/EndpointNotification.java | 67 --------------------- .../sca/dosgi/discovery/EndpointPublication.java | 7 ++- 6 files changed, 147 insertions(+), 93 deletions(-) create mode 100644 java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/DiscoveredServiceNotificationImpl.java create mode 100644 java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/EndpointDescription.java delete mode 100644 java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/EndpointNotification.java (limited to 'java/sca/modules/implementation-osgi-runtime/src') diff --git a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/AbstractDiscoveryService.java b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/AbstractDiscoveryService.java index 22c0e5bc7d..7f0b128bd0 100644 --- a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/AbstractDiscoveryService.java +++ b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/AbstractDiscoveryService.java @@ -42,6 +42,7 @@ import org.osgi.framework.Filter; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceRegistration; +import org.osgi.service.discovery.DiscoveredServiceNotification; import org.osgi.service.discovery.DiscoveredServiceTracker; import org.osgi.service.discovery.Discovery; import org.osgi.service.discovery.ServiceEndpointDescription; @@ -191,9 +192,10 @@ public abstract class AbstractDiscoveryService implements Discovery { + (isFilter ? " matches " : " contained by ") + sd.getProvidedInterfaces()); - EndpointNotification notification = - isFilter ? (filterMatches(toMatch, sd) ? new EndpointNotification(sd, true, toMatch, type) : null) : (sd - .getProvidedInterfaces().contains(toMatch) ? new EndpointNotification(sd, false, toMatch, type) : null); + DiscoveredServiceNotification notification = + isFilter ? (filterMatches(toMatch, sd) ? new DiscoveredServiceNotificationImpl(sd, true, toMatch, type) + : null) : (sd.getProvidedInterfaces().contains(toMatch) + ? new DiscoveredServiceNotificationImpl(sd, false, toMatch, type) : null); if (notification != null) { tracker.serviceChanged(notification); diff --git a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/DiscoveredServiceNotificationImpl.java b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/DiscoveredServiceNotificationImpl.java new file mode 100644 index 0000000000..a110fa9528 --- /dev/null +++ b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/DiscoveredServiceNotificationImpl.java @@ -0,0 +1,67 @@ +/* + * 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.dosgi.discovery; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; + +import org.osgi.service.discovery.DiscoveredServiceNotification; +import org.osgi.service.discovery.ServiceEndpointDescription; + +public class DiscoveredServiceNotificationImpl implements DiscoveredServiceNotification { + + private ServiceEndpointDescription discription; + private Collection interfaces; + private Collection filters; + private int type; + + public DiscoveredServiceNotificationImpl(ServiceEndpointDescription sd, boolean isFilter, String match, int type) { + this.discription = sd; + if (isFilter) { + filters = new ArrayList(); + filters.add(match); + interfaces = Collections.emptySet(); + } else { + interfaces = new HashSet(); + interfaces.add(match); + filters = Collections.emptyList(); + } + + this.type = type; + } + + public ServiceEndpointDescription getServiceEndpointDescription() { + return discription; + } + + public int getType() { + return type; + } + + public Collection getInterfaces() { + return interfaces; + } + + public Collection getFilters() { + return filters; + } +} diff --git a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/DomainDiscoveryService.java b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/DomainDiscoveryService.java index acd112395e..97eddfa9e5 100644 --- a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/DomainDiscoveryService.java +++ b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/DomainDiscoveryService.java @@ -22,8 +22,6 @@ package org.apache.tuscany.sca.dosgi.discovery; import static org.osgi.service.discovery.DiscoveredServiceNotification.AVAILABLE; import static org.osgi.service.discovery.DiscoveredServiceNotification.UNAVAILABLE; -import java.util.Collections; -import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -41,7 +39,6 @@ import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceRegistration; import org.osgi.service.discovery.ServiceEndpointDescription; -import org.osgi.service.discovery.ServicePublication; /** * Discovery service based on the distributed SCA domain @@ -90,7 +87,7 @@ public class DomainDiscoveryService extends AbstractDiscoveryService implements } } else { // Remote endpoints - ServiceEndpointDescription description = getServiceEndpointDescription(endpoint); + ServiceEndpointDescription description = new EndpointDescription(endpoint); discoveredServiceChanged(description, AVAILABLE); } } @@ -104,7 +101,7 @@ public class DomainDiscoveryService extends AbstractDiscoveryService implements } } else { // Remote endpoints - ServiceEndpointDescription description = getServiceEndpointDescription(endpoint); + ServiceEndpointDescription description = new EndpointDescription(endpoint); discoveredServiceChanged(description, UNAVAILABLE); } } @@ -120,19 +117,4 @@ public class DomainDiscoveryService extends AbstractDiscoveryService implements super.stop(); } - public Map getServiceProperties(Endpoint endpoint) { - Map serviceProps = new HashMap(); - serviceProps.put(ServicePublication.ENDPOINT_LOCATION, endpoint.getURI()); - // TODO: Populate the properties from the Endpoint object - return serviceProps; - } - - private ServiceEndpointDescription getServiceEndpointDescription(Endpoint endpoint) { - Interface interface1 = endpoint.getService().getInterfaceContract().getInterface(); - JavaInterface javaInterface = (JavaInterface)interface1; - ServiceEndpointDescription description = - new ServiceEndpointDescriptionImpl(Collections.singleton(javaInterface.getName()), - getServiceProperties(endpoint)); - return description; - } } diff --git a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/EndpointDescription.java b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/EndpointDescription.java new file mode 100644 index 0000000000..3f47183a56 --- /dev/null +++ b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/EndpointDescription.java @@ -0,0 +1,69 @@ +/* + * 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.dosgi.discovery; + +import static org.osgi.service.discovery.ServicePublication.ENDPOINT_ID; +import static org.osgi.service.discovery.ServicePublication.ENDPOINT_LOCATION; + +import java.net.URI; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.apache.tuscany.sca.assembly.ComponentService; +import org.apache.tuscany.sca.assembly.Endpoint; +import org.apache.tuscany.sca.interfacedef.Interface; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.java.JavaInterface; + +/** + * + */ +public class EndpointDescription extends ServiceEndpointDescriptionImpl { + public EndpointDescription(Endpoint endpoint) { + super(Collections.singleton(getInterfaceName(endpoint)), getServiceProperties(endpoint)); + } + + static String getInterfaceName(Endpoint endpoint) { + ComponentService service = endpoint.getService(); + if (service == null) { + return null; + } + InterfaceContract contract = service.getInterfaceContract(); + if (contract == null) { + return null; + } + Interface intf = contract.getInterface(); + if (intf instanceof JavaInterface) { + JavaInterface javaInterface = (JavaInterface)intf; + return javaInterface.getName(); + } + + return null; + } + + static Map getServiceProperties(Endpoint endpoint) { + Map serviceProps = new HashMap(); + serviceProps.put(ENDPOINT_ID, endpoint.getURI()); + serviceProps.put(ENDPOINT_LOCATION, URI.create(endpoint.getBinding().getURI())); + // TODO: Populate the properties from the Endpoint object + return serviceProps; + } +} diff --git a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/EndpointNotification.java b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/EndpointNotification.java deleted file mode 100644 index 659a683089..0000000000 --- a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/EndpointNotification.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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.dosgi.discovery; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; - -import org.osgi.service.discovery.DiscoveredServiceNotification; -import org.osgi.service.discovery.ServiceEndpointDescription; - -class EndpointNotification implements DiscoveredServiceNotification { - - private ServiceEndpointDescription discription; - private Collection interfaces; - private Collection filters; - private int type; - - EndpointNotification(ServiceEndpointDescription sd, boolean isFilter, String match, int type) { - this.discription = sd; - if (isFilter) { - filters = new ArrayList(); - filters.add(match); - interfaces = Collections.emptySet(); - } else { - interfaces = new HashSet(); - interfaces.add(match); - filters = Collections.emptyList(); - } - - this.type = type; - } - - public ServiceEndpointDescription getServiceEndpointDescription() { - return discription; - } - - public int getType() { - return type; - } - - public Collection getInterfaces() { - return interfaces; - } - - public Collection getFilters() { - return filters; - } -} diff --git a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/EndpointPublication.java b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/EndpointPublication.java index 82080a2dc1..184d3a12bf 100644 --- a/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/EndpointPublication.java +++ b/java/sca/modules/implementation-osgi-runtime/src/main/java/org/apache/tuscany/sca/dosgi/discovery/EndpointPublication.java @@ -19,8 +19,8 @@ package org.apache.tuscany.sca.dosgi.discovery; +import java.util.Collections; import java.util.Dictionary; -import java.util.HashMap; import java.util.Hashtable; import java.util.Map; @@ -52,10 +52,11 @@ public class EndpointPublication implements ServicePublication { public Dictionary getProperties() { Dictionary props = new Hashtable(); - Map serviceProps = new HashMap(); - serviceProps.put(ENDPOINT_LOCATION, endpoint.getURI()); + Map serviceProps = EndpointDescription.getServiceProperties(endpoint); props.put(SERVICE_PROPERTIES, serviceProps); // TODO: Populate the properties from the Endpoint object + String name = EndpointDescription.getInterfaceName(endpoint); + props.put(ENDPOINT_INTERFACE_NAME, Collections.singleton(name)); return props; } -- cgit v1.2.3