diff options
9 files changed, 200 insertions, 42 deletions
diff --git a/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/Activator.java b/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/Activator.java index 64c1d740f..e9314e38d 100644 --- a/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/Activator.java +++ b/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/Activator.java @@ -19,6 +19,7 @@ import org.eclipse.osgi.framework.console.CommandProvider; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; +import org.osgi.framework.ServiceRegistration; import org.osgi.service.log.LogService; import org.osgi.util.tracker.ServiceTracker; import org.osgi.util.tracker.ServiceTrackerCustomizer; @@ -41,6 +42,11 @@ public class Activator implements BundleActivator { private FileBasedDiscoveryImpl discovery = null; + private ServiceRegistration commandProviderRegistration; + + private ServiceDescriptionPublisher serviceDescriptionPublisher; + private ServiceRegistration serviceDescriptionPublisherRegistration; + /** * The constructor */ @@ -93,9 +99,15 @@ public class Activator implements BundleActivator { discovery = new FileBasedDiscoveryImpl(bc, logService); discovery.init(); - context.registerService(CommandProvider.class.getName(), - new DiscoveryCommandProvider(discovery), null); + commandProviderRegistration = context.registerService( + CommandProvider.class.getName(), new DiscoveryCommandProvider( + discovery), null); + // Register servicedescription publisher + serviceDescriptionPublisher = new ServiceDescriptionPublisher(discovery); + serviceDescriptionPublisherRegistration = context.registerService( + IServiceEndpointDescriptionPublisher.class.getName(), + serviceDescriptionPublisher, null); } /* @@ -105,6 +117,18 @@ public class Activator implements BundleActivator { * org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext) */ public void stop(BundleContext context) throws Exception { + if (commandProviderRegistration != null) { + commandProviderRegistration.unregister(); + commandProviderRegistration = null; + } + if (serviceDescriptionPublisherRegistration != null) { + serviceDescriptionPublisherRegistration.unregister(); + serviceDescriptionPublisherRegistration = null; + } + if (serviceDescriptionPublisher != null) { + serviceDescriptionPublisher.close(); + serviceDescriptionPublisher = null; + } logServiceTracker.close(); logServiceTracker = null; discovery.destroy(); @@ -131,6 +155,10 @@ public class Activator implements BundleActivator { FileBasedDiscoveryImpl.setLogService(logService); } + LogService getLogService() { + return logService; + } + public BundleContext getBundleContext() { return context; } diff --git a/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/BundleTrackerImpl.java b/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/BundleTrackerImpl.java index 452e55689..0508824d3 100644 --- a/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/BundleTrackerImpl.java +++ b/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/BundleTrackerImpl.java @@ -185,8 +185,8 @@ public class BundleTrackerImpl implements BundleTrackerCustomizer { String property = System.getProperty(substring); path = path.replaceAll("\\$\\{" + substring + "\\}", property); } - String files = token.substring(token.lastIndexOf("/") + 1, token - .length()); + String files = token.substring(token.lastIndexOf("/") + 1, + token.length()); Enumeration enumeration = bundle.findEntries(path, files, false); if (enumeration == null) { // that was maybe an absolute file path so lets check that. diff --git a/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/DSTTracker.java b/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/DSTTracker.java index 5877883d3..14a8e0827 100644 --- a/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/DSTTracker.java +++ b/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/DSTTracker.java @@ -138,10 +138,8 @@ public class DSTTracker implements ServiceTrackerCustomizer { Collection newFilterCriteria = (Collection) serviceReference .getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA); - result - .put(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA, - getAddedEntries(oldInterfaceCriteria, - newInterfaceCriteria)); + result.put(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA, + getAddedEntries(oldInterfaceCriteria, newInterfaceCriteria)); result.put(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA, getAddedEntries(oldFilterCriteria, newFilterCriteria)); } else { @@ -194,14 +192,11 @@ public class DSTTracker implements ServiceTrackerCustomizer { // Retrieve current service properties (required later when modified to // compute the actual modification) Map props = new HashMap(); - props - .put( - DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA, - ref - .getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA)); - - props.put(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA, ref - .getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA)); + props.put(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA, ref + .getProperty(DiscoveredServiceTracker.INTERFACE_MATCH_CRITERIA)); + + props.put(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA, + ref.getProperty(DiscoveredServiceTracker.FILTER_MATCH_CRITERIA)); dsTrackers.put((DiscoveredServiceTracker) context.getService(ref), props); } diff --git a/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/DiscoveryCommandProvider.java b/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/DiscoveryCommandProvider.java index a9c71f51b..418833311 100644 --- a/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/DiscoveryCommandProvider.java +++ b/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/DiscoveryCommandProvider.java @@ -76,15 +76,14 @@ public class DiscoveryCommandProvider implements CommandProvider { InputStream inputStream = url.openStream(); return new ServiceDescriptionParser().load(inputStream); } catch (IOException e) { - ci.print(NLS.bind(Messages - .getString("DiscoveryCommandProvider.IOException"), path)); //$NON-NLS-1$ + ci.print(NLS.bind( + Messages.getString("DiscoveryCommandProvider.IOException"), path)); //$NON-NLS-1$ } catch (ParserConfigurationException e) { - ci - .print(Messages - .getString("DiscoveryCommandProvider.ParserConfigurationException")); //$NON-NLS-1$ + ci.print(Messages + .getString("DiscoveryCommandProvider.ParserConfigurationException")); //$NON-NLS-1$ } catch (SAXException e) { - ci.print(NLS.bind(Messages - .getString("DiscoveryCommandProvider.SAXException"), path)); //$NON-NLS-1$ + ci.print(NLS.bind( + Messages.getString("DiscoveryCommandProvider.SAXException"), path)); //$NON-NLS-1$ } return new ArrayList(); } diff --git a/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/FileBasedDiscoveryImpl.java b/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/FileBasedDiscoveryImpl.java index e60f0e96e..3c0041bd7 100644 --- a/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/FileBasedDiscoveryImpl.java +++ b/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/FileBasedDiscoveryImpl.java @@ -99,8 +99,9 @@ public class FileBasedDiscoveryImpl { DiscoveredServiceTracker.class.getName(), discoTrackerCustomizer); discoTracker.open(); - spTracker = new ServiceTracker(context, ServicePublication.class - .getName(), new ServicePublicationTracker(context, this)); + spTracker = new ServiceTracker(context, + ServicePublication.class.getName(), + new ServicePublicationTracker(context, this)); spTracker.open(); // 36 stands for bundle states RESOLVED OR ACTIVE bt = new BundleTracker(context, 36, new BundleTrackerImpl(this)); @@ -175,8 +176,8 @@ public class FileBasedDiscoveryImpl { protected void unpublishService( final ServiceEndpointDescription serviceDescription) { validateServiceDescription(serviceDescription); - log(LogService.LOG_DEBUG, "unpublish service " - + serviceDescription.toString()); + log(LogService.LOG_DEBUG, + "unpublish service " + serviceDescription.toString()); inMemoryCache.remove(serviceDescription.getEndpointID()); notifyListenersOnRemovedServiceDescription(serviceDescription); } @@ -276,11 +277,9 @@ public class FileBasedDiscoveryImpl { matchingFilters.clear(); if (isTrackerInterestedInSED(svcDescr, matchingCriteria, matchingInterfaces, matchingFilters)) { - tracker - .serviceChanged(new DiscoveredServiceNotificationImpl( - svcDescr, - DiscoveredServiceNotification.AVAILABLE, - matchingInterfaces, matchingFilters)); + tracker.serviceChanged(new DiscoveredServiceNotificationImpl( + svcDescr, DiscoveredServiceNotification.AVAILABLE, + matchingInterfaces, matchingFilters)); } } } @@ -308,8 +307,7 @@ public class FileBasedDiscoveryImpl { svcDescr, DiscoveredServiceNotification.AVAILABLE, matchingInterfaces, matchingFilters)); } catch (Exception e) { - log( - LogService.LOG_ERROR, + log(LogService.LOG_ERROR, "Exceptions where thrown while notifying about a new remote service.", e); } @@ -345,8 +343,7 @@ public class FileBasedDiscoveryImpl { DiscoveredServiceNotification.UNAVAILABLE, matchingInterfaces, matchingFilters)); } catch (Exception e) { - log( - LogService.LOG_ERROR, + log(LogService.LOG_ERROR, "Exceptions where thrown while notifying about removal of a remote service.", e); } @@ -378,8 +375,7 @@ public class FileBasedDiscoveryImpl { svcDescr, DiscoveredServiceNotification.MODIFIED, matchingInterfaces, matchingFilters)); } catch (Exception e) { - log( - LogService.LOG_ERROR, + log(LogService.LOG_ERROR, "Exceptions where thrown while notifying about modification of a remote service.", e); } diff --git a/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/IServiceEndpointDescriptionPublisher.java b/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/IServiceEndpointDescriptionPublisher.java new file mode 100644 index 000000000..43f2368c2 --- /dev/null +++ b/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/IServiceEndpointDescriptionPublisher.java @@ -0,0 +1,29 @@ +/******************************************************************************* + * Copyright (c) 2010 Composent, Inc. and others. All rights reserved. This + * program and the accompanying materials are made available under the terms of + * the Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Composent, Inc. - initial API and implementation + ******************************************************************************/ +package org.eclipse.ecf.osgi.services.discovery.local; + +import java.io.IOException; +import java.io.InputStream; + +public interface IServiceEndpointDescriptionPublisher { + + public void publishServiceDescription(InputStream serviceEndpointDescriptionFile) + throws IOException; + + public void publishServiceDescription( + ServiceEndpointDescriptionImpl serviceDescription); + + public void unpublishServiceDescription(InputStream serviceEndpointDescriptionFile) + throws IOException; + + public void unpublishServiceDescription( + ServiceEndpointDescriptionImpl serviceDescription); + +} diff --git a/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/OneInterfaceSED.java b/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/OneInterfaceSED.java index 7377cc57d..e318d6ca6 100644 --- a/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/OneInterfaceSED.java +++ b/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/OneInterfaceSED.java @@ -192,8 +192,8 @@ public class OneInterfaceSED { sb.append("\tversion=").append(version).append(LINE_SEPARATOR); } if (endpointInterface != null) { - sb.append("\tendpointInterface=").append(endpointInterface).append( - LINE_SEPARATOR); + sb.append("\tendpointInterface=").append(endpointInterface) + .append(LINE_SEPARATOR); } return sb.toString(); } diff --git a/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/ServiceDescriptionParser.java b/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/ServiceDescriptionParser.java index 72e248dd7..977de791d 100644 --- a/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/ServiceDescriptionParser.java +++ b/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/ServiceDescriptionParser.java @@ -144,8 +144,8 @@ public class ServiceDescriptionParser { } else if (type.equals("Byte")) { providedProperties.put(name, Byte.valueOf(value)); } else if (type.equals("Character")) { - providedProperties.put(name, new Character(value - .charAt(0))); + providedProperties.put(name, + new Character(value.charAt(0))); } else if (type.equals("Boolean")) { providedProperties .put(name, Boolean.valueOf(value)); diff --git a/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/ServiceDescriptionPublisher.java b/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/ServiceDescriptionPublisher.java new file mode 100644 index 000000000..284c12848 --- /dev/null +++ b/compendium/bundles/org.eclipse.ecf.osgi.services.discovery.local/src/org/eclipse/ecf/osgi/services/discovery/local/ServiceDescriptionPublisher.java @@ -0,0 +1,111 @@ +/******************************************************************************* + * Copyright (c) 2010 Composent, Inc. and others. All rights reserved. This + * program and the accompanying materials are made available under the terms of + * the Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Composent, Inc. - initial API and implementation + ******************************************************************************/ +package org.eclipse.ecf.osgi.services.discovery.local; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Collection; +import java.util.Iterator; + +import javax.xml.parsers.ParserConfigurationException; + +import org.osgi.service.log.LogService; +import org.xml.sax.SAXException; + +public class ServiceDescriptionPublisher implements + IServiceEndpointDescriptionPublisher { + + private FileBasedDiscoveryImpl discovery; + private ServiceDescriptionParser sdp; + private Object discoveryLock = new Object(); + + public ServiceDescriptionPublisher(FileBasedDiscoveryImpl discovery) { + this.discovery = discovery; + this.sdp = new ServiceDescriptionParser(); + } + + private void logException(String message, Throwable t) { + Activator a = Activator.getDefault(); + if (a != null) { + LogService logService = a.getLogService(); + if (logService != null) + logService.log(LogService.LOG_ERROR, message, t); + } + } + + private Collection getServiceEndpointDescriptions( + InputStream serviceDescriptionStream) throws IOException { + try { + return sdp.load(serviceDescriptionStream); + } catch (IOException e) { + logException("IOException publishing serviceDescriptionStream", e); + throw e; + } catch (ParserConfigurationException e) { + logException( + "Parser exception publishing serviceDescriptionStream", e); + throw new IOException( + "Parser exception publishing serviceDescriptionStream: " + + e.getMessage()); + } catch (SAXException e) { + logException( + "Parser exception publishing serviceDescriptionStream", e); + throw new IOException( + "SAX exception publishing serviceDescriptionStream: " + + e.getMessage()); + } finally { + serviceDescriptionStream.close(); + } + } + + public void publishServiceDescription(InputStream serviceDescriptionStream) + throws IOException { + Collection serviceDescriptions = getServiceEndpointDescriptions(serviceDescriptionStream); + if (serviceDescriptions != null) { + for (Iterator i = serviceDescriptions.iterator(); i.hasNext();) { + publishServiceDescription((ServiceEndpointDescriptionImpl) i + .next()); + } + } + } + + public void unpublishServiceDescription(InputStream serviceDescriptionStream) + throws IOException { + Collection serviceDescriptions = getServiceEndpointDescriptions(serviceDescriptionStream); + if (serviceDescriptions != null) { + for (Iterator i = serviceDescriptions.iterator(); i.hasNext();) { + unpublishServiceDescription((ServiceEndpointDescriptionImpl) i + .next()); + } + } + } + + public void publishServiceDescription( + ServiceEndpointDescriptionImpl serviceEndpointDescription) { + synchronized (discoveryLock) { + if (discovery != null) + discovery.publishService(serviceEndpointDescription); + } + } + + public void unpublishServiceDescription( + ServiceEndpointDescriptionImpl serviceEndpointDescription) { + synchronized (discoveryLock) { + if (discovery != null) + discovery.unpublishService(serviceEndpointDescription); + } + } + + public void close() { + synchronized (discoveryLock) { + this.discovery = null; + } + } + +} |