Author: mes
Date: 2012-04-13 14:24:17 -0700 (Fri, 13 Apr 2012)
New Revision: 28831

Added:
   
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/utils/
   
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/utils/CyServiceListener.java
   
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/utils/RegisterUtil.java
   
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/utils/ServiceUtil.java
Removed:
   
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/CyServiceListener.java
   
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/RegisterUtil.java
   
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/ServiceUtil.java
Modified:
   
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/AbstractCyActivator.java
   
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/CyServiceRegistrarImpl.java
Log:
Moved static classes to separate package to avoid cyclic dependency.

Modified: 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/AbstractCyActivator.java
===================================================================
--- 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/AbstractCyActivator.java
       2012-04-13 21:12:38 UTC (rev 28830)
+++ 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/AbstractCyActivator.java
       2012-04-13 21:24:17 UTC (rev 28831)
@@ -12,9 +12,9 @@
 import org.osgi.framework.ServiceReference; 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.cytoscape.service.util.internal.CyServiceListener;
-import org.cytoscape.service.util.internal.RegisterUtil;
-import org.cytoscape.service.util.internal.ServiceUtil;
+import org.cytoscape.service.util.internal.utils.CyServiceListener;
+import org.cytoscape.service.util.internal.utils.RegisterUtil;
+import org.cytoscape.service.util.internal.utils.ServiceUtil;
 
 /**
  * A simple BundleActivator with convenience methods for registering

Deleted: 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/CyServiceListener.java
===================================================================
--- 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/CyServiceListener.java
        2012-04-13 21:12:38 UTC (rev 28830)
+++ 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/CyServiceListener.java
        2012-04-13 21:24:17 UTC (rev 28831)
@@ -1,148 +0,0 @@
-
-/*
- Copyright (c) 2006, 2007, The Cytoscape Consortium (www.cytoscape.org)
-
- The Cytoscape Consortium is:
- - Institute for Systems Biology
- - University of California San Diego
- - Memorial Sloan-Kettering Cancer Center
- - Institut Pasteur
- - Agilent Technologies
-
- This library is free software; you can redistribute it and/or modify it
- under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2.1 of the License, or
- any later version.
-
- This library is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
- MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
- documentation provided hereunder is on an "as is" basis, and the
- Institute for Systems Biology and the Whitehead Institute
- have no obligations to provide maintenance, support,
- updates, enhancements or modifications.  In no event shall the
- Institute for Systems Biology and the Whitehead Institute
- be liable to any party for direct, indirect, special,
- incidental or consequential damages, including lost profits, arising
- out of the use of this software and its documentation, even if the
- Institute for Systems Biology and the Whitehead Institute
- have been advised of the possibility of such damage.  See
- the GNU Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with this library; if not, write to the Free Software Foundation,
- Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
-*/
-package org.cytoscape.service.util.internal;
-
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
-import org.osgi.framework.Constants;
-import org.osgi.framework.Filter;
-import org.osgi.util.tracker.ServiceTracker;
-import org.osgi.framework.InvalidSyntaxException;
-
-import java.lang.reflect.Method;
-import java.lang.NoSuchMethodException;
-import java.util.Properties;
-import java.util.Dictionary;
-import java.util.Map;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-
-
-public class CyServiceListener extends ServiceTracker {
-
-
-       private final BundleContext bc;
-       private final Object target;
-       private final Method registerMethod;
-       private final Method unregisterMethod;
-       private final Class<?> serviceClass;
-       private final Class<?> methodClass;
-       private static final Logger logger = 
LoggerFactory.getLogger(CyServiceListener.class);
-       
-       public CyServiceListener(BundleContext bc, Object target, String 
registerMethodName, String unregisterMethodName, Class<?> serviceClass, 
Class<?> methodClass, String additionalFilter) throws NoSuchMethodException {
-               super(bc, genFilter(bc,serviceClass, additionalFilter), null);
-               this.bc = bc;
-               this.target = target;
-               this.serviceClass = serviceClass;
-               this.methodClass = methodClass;
-               this.registerMethod = getMethod(registerMethodName);
-               this.unregisterMethod = getMethod(unregisterMethodName);
-       }
-
-       private static Filter genFilter(BundleContext context, Class<?> 
serviceClass, String additionalFilter) {
-               // create class filter
-               String filter = "(" + Constants.OBJECTCLASS + "=" + 
serviceClass.getName() + ")";
-
-               // append any additional filter
-               if ( additionalFilter != null && !additionalFilter.isEmpty() )
-                       filter = "(&" + filter + additionalFilter + ")";
-
-               // create OSGi filter
-               try {
-                       return context.createFilter(filter);
-               } catch (InvalidSyntaxException e) {
-                       IllegalArgumentException iae = new 
IllegalArgumentException( "Invalid filter syntax: " + filter, e);
-                       iae.initCause(e);
-                       throw iae;
-               }
-       }
-
-       /**
-        * Tries the different possible method declarations.
-        */
-       private Method getMethod(String name) throws NoSuchMethodException {
-               Method m; 
-               try {
-                       m = target.getClass().getMethod(name, methodClass, 
Dictionary.class);
-               } catch (NoSuchMethodException e) {
-                       // Ignore exception and try different signature.
-                       // If we throw an exception here, we WANT it to 
-                       // propagate, because that signals an error.
-                       m = target.getClass().getMethod(name, methodClass, 
Map.class);
-               }
-               return m;
-       }
-
-       /**
-        * Invokes the register method on the listener target class with the 
specified service.
-        */
-       @Override
-       public Object addingService(ServiceReference ref) {
-               try {
-                       Object service = super.addingService(ref);
-                       registerMethod.invoke(target, 
serviceClass.cast(service), getProperties(ref));
-                       return service;
-               } catch (Exception nse) {
-                       logger.warn("Failed to register service: ",nse);
-               }
-               return null;
-       }
-
-       /**
-        * Invokes the UNregister method on the listener target class with the 
specified service.
-        */
-       @Override
-       public void removedService(ServiceReference ref, Object service) {
-               super.removedService(ref, service);
-               try {
-                       unregisterMethod.invoke(target, 
serviceClass.cast(service), getProperties(ref));
-               } catch (Exception nse) {
-                       logger.warn("Failed to unregister service: ",nse);
-               }
-       }
-
-       /**
-        * Converts the service properties contained in a ServiceReference to a 
Properties object.
-        */
-       private Properties getProperties(ServiceReference ref) {
-               Properties props = new Properties();
-               for ( String key : ref.getPropertyKeys() ) 
-                       props.setProperty(key,ref.getProperty(key).toString());
-               return props;
-       }
-}

Modified: 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/CyServiceRegistrarImpl.java
===================================================================
--- 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/CyServiceRegistrarImpl.java
   2012-04-13 21:12:38 UTC (rev 28830)
+++ 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/CyServiceRegistrarImpl.java
   2012-04-13 21:24:17 UTC (rev 28831)
@@ -15,6 +15,8 @@
 
 import org.cytoscape.service.util.AbstractCyActivator;
 import org.cytoscape.service.util.CyServiceRegistrar;
+import org.cytoscape.service.util.internal.utils.RegisterUtil;
+import org.cytoscape.service.util.internal.utils.ServiceUtil;
 
 public class CyServiceRegistrarImpl  implements CyServiceRegistrar{
        

Deleted: 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/RegisterUtil.java
===================================================================
--- 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/RegisterUtil.java
     2012-04-13 21:12:38 UTC (rev 28830)
+++ 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/RegisterUtil.java
     2012-04-13 21:24:17 UTC (rev 28831)
@@ -1,68 +0,0 @@
-
-/*
- Copyright (c) 2011, The Cytoscape Consortium (www.cytoscape.org)
-
- The Cytoscape Consortium is:
- - Institute for Systems Biology
- - University of California San Diego
- - Memorial Sloan-Kettering Cancer Center
- - Institut Pasteur
- - Agilent Technologies
-
- This library is free software; you can redistribute it and/or modify it
- under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2.1 of the License, or
- any later version.
-
- This library is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
- MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
- documentation provided hereunder is on an "as is" basis, and the
- Institute for Systems Biology and the Whitehead Institute
- have no obligations to provide maintenance, support,
- updates, enhancements or modifications.  In no event shall the
- Institute for Systems Biology and the Whitehead Institute
- be liable to any party for direct, indirect, special,
- incidental or consequential damages, including lost profits, arising
- out of the use of this software and its documentation, even if the
- Institute for Systems Biology and the Whitehead Institute
- have been advised of the possibility of such damage.  See
- the GNU Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public License
- along with this library; if not, write to the Free Software Foundation,
- Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
-*/
-package org.cytoscape.service.util.internal;
-
-
-import java.util.List;
-import java.util.ArrayList;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Various utilities for registering services. 
- */
-public class RegisterUtil {
-
-       private static final Logger logger = 
LoggerFactory.getLogger(RegisterUtil.class);
-
-       public static List<Class<?>> getAllInterfaces(Class<?> clazz) {
-               List<Class<?>> interfaces = new ArrayList<Class<?>>();
-               addAllInterfaces(interfaces, clazz);
-               return interfaces;
-       }
-       
-       static void addAllInterfaces(List<Class<?>> list, Class<?> clazz) {
-               for ( Class<?> c : clazz.getInterfaces() ) {
-                       list.add(c);
-                       addAllInterfaces(list, c);
-               }
-
-               // recurse into superclass
-               Class<?> superClass = clazz.getSuperclass();
-               if ( superClass != null && superClass != Object.class )
-                       addAllInterfaces(list, superClass);
-       }
-}

Deleted: 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/ServiceUtil.java
===================================================================
--- 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/ServiceUtil.java
      2012-04-13 21:12:38 UTC (rev 28830)
+++ 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/ServiceUtil.java
      2012-04-13 21:24:17 UTC (rev 28831)
@@ -1,82 +0,0 @@
-package org.cytoscape.service.util.internal;
-
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
-import org.osgi.framework.ServiceRegistration;
-
-public final class ServiceUtil {
-       
-       private ServiceUtil(){
-               
-       }
-
-       static public <S> S getService(BundleContext bc, Class<S> serviceClass, 
List<ServiceReference> gottenServices) {
-               try {
-                       ServiceReference ref = 
bc.getServiceReference(serviceClass.getName());
-                       if ( ref == null ) 
-                               throw new 
NullPointerException("ServiceReference is null for: " + serviceClass.getName());
-                       if (gottenServices != null)
-                               gottenServices.add(ref);        
-                       return serviceClass.cast( bc.getService(ref) );
-
-               } catch (Exception e) {
-                       throw new RuntimeException("Couldn't find service: " + 
serviceClass.getName(),e);
-               }
-       }
-
-       static public <S> S getService(BundleContext bc, Class<S> serviceClass, 
String filter, List<ServiceReference> gottenServices) {
-               try { 
-                       ServiceReference[] refs = 
bc.getServiceReferences(serviceClass.getName(),filter);
-                       if ( refs == null ) 
-                               throw new 
NullPointerException("ServiceReference is null for: " + serviceClass.getName() 
+ " with filter: " + filter);
-                       if (gottenServices != null)
-                               gottenServices.add(refs[0]);    
-                       return serviceClass.cast( bc.getService(refs[0]) );
-               } catch (Exception e) {
-                       throw new RuntimeException("Couldn't find service: " + 
serviceClass.getName() + " with filter: " + filter, e);
-               }
-       }
-       
-       static public void registerServiceListener(final BundleContext bc, 
final Object listener, final String registerMethodName, 
-                       final String unregisterMethodName, final Class<?> 
serviceClass, final Class<?> methodClass, final String additionalFilter , 
-                       List<CyServiceListener> serviceListeners) {
-               try {
-                       CyServiceListener serviceListener = new 
CyServiceListener(bc, listener, registerMethodName, unregisterMethodName, 
serviceClass, methodClass, additionalFilter);
-                       serviceListener.open();
-                       if (serviceListeners != null)
-                               serviceListeners.add( serviceListener );
-               } catch (Exception e) {
-                       throw new RuntimeException("Could not listen to 
services for object: " + listener + " with methods: " + registerMethodName + ", 
" + unregisterMethodName + ", service type: " + serviceClass + ", and 
additional filter: " + additionalFilter, e); 
-               }
-       }
-       
-       static public void registerService(final BundleContext bc, final Object 
service, final Class<?> serviceClass, final Properties props, 
-                       Map<Class,Map<Object,ServiceRegistration>> 
serviceRegistrations) {
-               
-               if ( service == null )
-                       throw new NullPointerException( "service object is 
null" );
-               if ( serviceClass == null )
-                       throw new NullPointerException( "class is null" );
-               if ( props == null )
-                       throw new NullPointerException( "props are null" );
-               if ( bc == null )
-                       throw new IllegalStateException( "BundleContext is 
null" );
-
-               //logger.debug("attempting to register service: " + 
service.toString() + " of type " + serviceClass.getName());
-               ServiceRegistration s = bc.registerService( 
serviceClass.getName(), service, props );
-
-               Map<Object, ServiceRegistration> registrations = 
serviceRegistrations.get(serviceClass);
-               if ( registrations == null ) {
-                       registrations = new 
HashMap<Object,ServiceRegistration>();
-                       serviceRegistrations.put(serviceClass, registrations );
-               }
-
-               registrations.put(service,s);
-       }
-
-}

Copied: 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/utils/CyServiceListener.java
 (from rev 28828, 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/CyServiceListener.java)
===================================================================
--- 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/utils/CyServiceListener.java
                          (rev 0)
+++ 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/utils/CyServiceListener.java
  2012-04-13 21:24:17 UTC (rev 28831)
@@ -0,0 +1,148 @@
+
+/*
+ Copyright (c) 2006, 2007, The Cytoscape Consortium (www.cytoscape.org)
+
+ The Cytoscape Consortium is:
+ - Institute for Systems Biology
+ - University of California San Diego
+ - Memorial Sloan-Kettering Cancer Center
+ - Institut Pasteur
+ - Agilent Technologies
+
+ This library is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License, or
+ any later version.
+
+ This library is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
+ MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
+ documentation provided hereunder is on an "as is" basis, and the
+ Institute for Systems Biology and the Whitehead Institute
+ have no obligations to provide maintenance, support,
+ updates, enhancements or modifications.  In no event shall the
+ Institute for Systems Biology and the Whitehead Institute
+ be liable to any party for direct, indirect, special,
+ incidental or consequential damages, including lost profits, arising
+ out of the use of this software and its documentation, even if the
+ Institute for Systems Biology and the Whitehead Institute
+ have been advised of the possibility of such damage.  See
+ the GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this library; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+*/
+package org.cytoscape.service.util.internal.utils;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.Constants;
+import org.osgi.framework.Filter;
+import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.framework.InvalidSyntaxException;
+
+import java.lang.reflect.Method;
+import java.lang.NoSuchMethodException;
+import java.util.Properties;
+import java.util.Dictionary;
+import java.util.Map;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+
+public class CyServiceListener extends ServiceTracker {
+
+
+       private final BundleContext bc;
+       private final Object target;
+       private final Method registerMethod;
+       private final Method unregisterMethod;
+       private final Class<?> serviceClass;
+       private final Class<?> methodClass;
+       private static final Logger logger = 
LoggerFactory.getLogger(CyServiceListener.class);
+       
+       public CyServiceListener(BundleContext bc, Object target, String 
registerMethodName, String unregisterMethodName, Class<?> serviceClass, 
Class<?> methodClass, String additionalFilter) throws NoSuchMethodException {
+               super(bc, genFilter(bc,serviceClass, additionalFilter), null);
+               this.bc = bc;
+               this.target = target;
+               this.serviceClass = serviceClass;
+               this.methodClass = methodClass;
+               this.registerMethod = getMethod(registerMethodName);
+               this.unregisterMethod = getMethod(unregisterMethodName);
+       }
+
+       private static Filter genFilter(BundleContext context, Class<?> 
serviceClass, String additionalFilter) {
+               // create class filter
+               String filter = "(" + Constants.OBJECTCLASS + "=" + 
serviceClass.getName() + ")";
+
+               // append any additional filter
+               if ( additionalFilter != null && !additionalFilter.isEmpty() )
+                       filter = "(&" + filter + additionalFilter + ")";
+
+               // create OSGi filter
+               try {
+                       return context.createFilter(filter);
+               } catch (InvalidSyntaxException e) {
+                       IllegalArgumentException iae = new 
IllegalArgumentException( "Invalid filter syntax: " + filter, e);
+                       iae.initCause(e);
+                       throw iae;
+               }
+       }
+
+       /**
+        * Tries the different possible method declarations.
+        */
+       private Method getMethod(String name) throws NoSuchMethodException {
+               Method m; 
+               try {
+                       m = target.getClass().getMethod(name, methodClass, 
Dictionary.class);
+               } catch (NoSuchMethodException e) {
+                       // Ignore exception and try different signature.
+                       // If we throw an exception here, we WANT it to 
+                       // propagate, because that signals an error.
+                       m = target.getClass().getMethod(name, methodClass, 
Map.class);
+               }
+               return m;
+       }
+
+       /**
+        * Invokes the register method on the listener target class with the 
specified service.
+        */
+       @Override
+       public Object addingService(ServiceReference ref) {
+               try {
+                       Object service = super.addingService(ref);
+                       registerMethod.invoke(target, 
serviceClass.cast(service), getProperties(ref));
+                       return service;
+               } catch (Exception nse) {
+                       logger.warn("Failed to register service: ",nse);
+               }
+               return null;
+       }
+
+       /**
+        * Invokes the UNregister method on the listener target class with the 
specified service.
+        */
+       @Override
+       public void removedService(ServiceReference ref, Object service) {
+               super.removedService(ref, service);
+               try {
+                       unregisterMethod.invoke(target, 
serviceClass.cast(service), getProperties(ref));
+               } catch (Exception nse) {
+                       logger.warn("Failed to unregister service: ",nse);
+               }
+       }
+
+       /**
+        * Converts the service properties contained in a ServiceReference to a 
Properties object.
+        */
+       private Properties getProperties(ServiceReference ref) {
+               Properties props = new Properties();
+               for ( String key : ref.getPropertyKeys() ) 
+                       props.setProperty(key,ref.getProperty(key).toString());
+               return props;
+       }
+}

Copied: 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/utils/RegisterUtil.java
 (from rev 28828, 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/RegisterUtil.java)
===================================================================
--- 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/utils/RegisterUtil.java
                               (rev 0)
+++ 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/utils/RegisterUtil.java
       2012-04-13 21:24:17 UTC (rev 28831)
@@ -0,0 +1,68 @@
+
+/*
+ Copyright (c) 2011, The Cytoscape Consortium (www.cytoscape.org)
+
+ The Cytoscape Consortium is:
+ - Institute for Systems Biology
+ - University of California San Diego
+ - Memorial Sloan-Kettering Cancer Center
+ - Institut Pasteur
+ - Agilent Technologies
+
+ This library is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License, or
+ any later version.
+
+ This library is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
+ MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
+ documentation provided hereunder is on an "as is" basis, and the
+ Institute for Systems Biology and the Whitehead Institute
+ have no obligations to provide maintenance, support,
+ updates, enhancements or modifications.  In no event shall the
+ Institute for Systems Biology and the Whitehead Institute
+ be liable to any party for direct, indirect, special,
+ incidental or consequential damages, including lost profits, arising
+ out of the use of this software and its documentation, even if the
+ Institute for Systems Biology and the Whitehead Institute
+ have been advised of the possibility of such damage.  See
+ the GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this library; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+*/
+package org.cytoscape.service.util.internal.utils;
+
+
+import java.util.List;
+import java.util.ArrayList;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Various utilities for registering services. 
+ */
+public class RegisterUtil {
+
+       private static final Logger logger = 
LoggerFactory.getLogger(RegisterUtil.class);
+
+       public static List<Class<?>> getAllInterfaces(Class<?> clazz) {
+               List<Class<?>> interfaces = new ArrayList<Class<?>>();
+               addAllInterfaces(interfaces, clazz);
+               return interfaces;
+       }
+       
+       private static void addAllInterfaces(List<Class<?>> list, Class<?> 
clazz) {
+               for ( Class<?> c : clazz.getInterfaces() ) {
+                       list.add(c);
+                       addAllInterfaces(list, c);
+               }
+
+               // recurse into superclass
+               Class<?> superClass = clazz.getSuperclass();
+               if ( superClass != null && superClass != Object.class )
+                       addAllInterfaces(list, superClass);
+       }
+}

Copied: 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/utils/ServiceUtil.java
 (from rev 28828, 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/ServiceUtil.java)
===================================================================
--- 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/utils/ServiceUtil.java
                                (rev 0)
+++ 
core3/api/trunk/service-api/src/main/java/org/cytoscape/service/util/internal/utils/ServiceUtil.java
        2012-04-13 21:24:17 UTC (rev 28831)
@@ -0,0 +1,82 @@
+package org.cytoscape.service.util.internal.utils;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+
+public final class ServiceUtil {
+       
+       private ServiceUtil(){
+               
+       }
+
+       public static <S> S getService(BundleContext bc, Class<S> serviceClass, 
List<ServiceReference> gottenServices) {
+               try {
+                       ServiceReference ref = 
bc.getServiceReference(serviceClass.getName());
+                       if ( ref == null ) 
+                               throw new 
NullPointerException("ServiceReference is null for: " + serviceClass.getName());
+                       if (gottenServices != null)
+                               gottenServices.add(ref);        
+                       return serviceClass.cast( bc.getService(ref) );
+
+               } catch (Exception e) {
+                       throw new RuntimeException("Couldn't find service: " + 
serviceClass.getName(),e);
+               }
+       }
+
+       public static <S> S getService(BundleContext bc, Class<S> serviceClass, 
String filter, List<ServiceReference> gottenServices) {
+               try { 
+                       ServiceReference[] refs = 
bc.getServiceReferences(serviceClass.getName(),filter);
+                       if ( refs == null ) 
+                               throw new 
NullPointerException("ServiceReference is null for: " + serviceClass.getName() 
+ " with filter: " + filter);
+                       if (gottenServices != null)
+                               gottenServices.add(refs[0]);    
+                       return serviceClass.cast( bc.getService(refs[0]) );
+               } catch (Exception e) {
+                       throw new RuntimeException("Couldn't find service: " + 
serviceClass.getName() + " with filter: " + filter, e);
+               }
+       }
+       
+       public static void registerServiceListener(final BundleContext bc, 
final Object listener, final String registerMethodName, 
+                       final String unregisterMethodName, final Class<?> 
serviceClass, final Class<?> methodClass, final String additionalFilter , 
+                       List<CyServiceListener> serviceListeners) {
+               try {
+                       CyServiceListener serviceListener = new 
CyServiceListener(bc, listener, registerMethodName, unregisterMethodName, 
serviceClass, methodClass, additionalFilter);
+                       serviceListener.open();
+                       if (serviceListeners != null)
+                               serviceListeners.add( serviceListener );
+               } catch (Exception e) {
+                       throw new RuntimeException("Could not listen to 
services for object: " + listener + " with methods: " + registerMethodName + ", 
" + unregisterMethodName + ", service type: " + serviceClass + ", and 
additional filter: " + additionalFilter, e); 
+               }
+       }
+       
+       public static void registerService(final BundleContext bc, final Object 
service, final Class<?> serviceClass, final Properties props, 
+                       Map<Class,Map<Object,ServiceRegistration>> 
serviceRegistrations) {
+               
+               if ( service == null )
+                       throw new NullPointerException( "service object is 
null" );
+               if ( serviceClass == null )
+                       throw new NullPointerException( "class is null" );
+               if ( props == null )
+                       throw new NullPointerException( "props are null" );
+               if ( bc == null )
+                       throw new IllegalStateException( "BundleContext is 
null" );
+
+               //logger.debug("attempting to register service: " + 
service.toString() + " of type " + serviceClass.getName());
+               ServiceRegistration s = bc.registerService( 
serviceClass.getName(), service, props );
+
+               Map<Object, ServiceRegistration> registrations = 
serviceRegistrations.get(serviceClass);
+               if ( registrations == null ) {
+                       registrations = new 
HashMap<Object,ServiceRegistration>();
+                       serviceRegistrations.put(serviceClass, registrations );
+               }
+
+               registrations.put(service,s);
+       }
+
+}

-- 
You received this message because you are subscribed to the Google Groups 
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/cytoscape-cvs?hl=en.

Reply via email to