[jboss-cvs] JBossAS SVN: r97167 - projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Nov 30 10:37:46 EST 2009


Author: alesj
Date: 2009-11-30 10:37:44 -0500 (Mon, 30 Nov 2009)
New Revision: 97167

Added:
   projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/ContextComparator.java
   projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/MDRUtils.java
   projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/PreInstallAction.java
   projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/ServiceRefDictionary.java
Modified:
   projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/AbstractBundleState.java
   projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/DescribeAction.java
   projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/GenericServiceReferenceWrapper.java
   projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiBundleManager.java
   projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiControllerContextActions.java
   projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceAction.java
   projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceState.java
Log:
[JBOSGi-141]; remove direct OSGi service usage.

Modified: projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/AbstractBundleState.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/AbstractBundleState.java	2009-11-30 14:38:21 UTC (rev 97166)
+++ projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/AbstractBundleState.java	2009-11-30 15:37:44 UTC (rev 97167)
@@ -303,7 +303,7 @@
     * 
     * @param context the context
     */
-   void addRegisteredService(org.jboss.dependency.spi.ControllerContext context)
+   void addRegisteredService(ControllerContext context)
    {
       addUsedBy(context, this);
    }
@@ -313,7 +313,7 @@
     * 
     * @param context the context
     */
-   void removeRegisteredService(OSGiServiceState context)
+   void removeRegisteredService(ControllerContext context)
    {
       removeUsedBy(context, this);
    }

Copied: projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/ContextComparator.java (from rev 97142, projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiBundleManager.java)
===================================================================
--- projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/ContextComparator.java	                        (rev 0)
+++ projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/ContextComparator.java	2009-11-30 15:37:44 UTC (rev 97167)
@@ -0,0 +1,57 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors.
+*
+* This 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 (at your option) any later version.
+*
+* This software 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. 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 software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.osgi.framework.bundle;
+
+import java.util.Comparator;
+
+import org.jboss.dependency.spi.ControllerContext;
+
+/**
+ * Compare controller contexts.
+ *
+ * @author <a href="ales.justin at jboss.org">Ales Justin</a>
+ */
+class ContextComparator implements Comparator<ControllerContext>
+{
+   public static final Comparator<ControllerContext> INSTANCE = new ContextComparator();
+   
+   public int compare(ControllerContext c1, ControllerContext c2)
+   {
+      Integer ranking1 = MDRUtils.getRanking(c1);
+      Integer ranking2 = MDRUtils.getRanking(c2);
+      int diff = ranking2 - ranking1;
+      if (diff == 0)
+      {
+         Long id1 = MDRUtils.getId(c1);
+         Long id2 = MDRUtils.getId(c2);
+         if (id1 == null || id2 == null)
+            return 0;
+         if (id1 != null && id2 == null)
+            return -1;
+         if (id2 != null && id1 == null)
+            return 1;
+
+         return (id2 > id1) ? -1 : 1;
+      }
+      return diff;
+   }
+}
\ No newline at end of file

Modified: projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/DescribeAction.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/DescribeAction.java	2009-11-30 14:38:21 UTC (rev 97166)
+++ projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/DescribeAction.java	2009-11-30 15:37:44 UTC (rev 97167)
@@ -21,35 +21,41 @@
 */
 package org.jboss.osgi.framework.bundle;
 
-import org.jboss.dependency.spi.Controller;
-import org.jboss.kernel.Kernel;
-import org.jboss.kernel.spi.dependency.KernelController;
+import java.util.Dictionary;
+
 import org.jboss.kernel.spi.metadata.KernelMetaDataRepository;
+import org.jboss.metadata.spi.loader.MutableMetaDataLoader;
+import org.jboss.metadata.spi.repository.MutableMetaDataRepository;
+import org.jboss.metadata.spi.retrieval.MetaDataRetrieval;
+import org.jboss.metadata.spi.scope.CommonLevels;
+import org.jboss.metadata.spi.scope.Scope;
+import org.jboss.metadata.spi.scope.ScopeKey;
 
 /**
- * Describe osgi service, its metadata.
+ * Describe osgi service.
+ * Put it's properties into MDR.
  *
  * @author <a href="ales.justin at jboss.org">Ales Justin</a>
  */
-class DescribeAction extends SimpleOSGiServiceAction
+class DescribeAction extends OSGiServiceAction
 {
-   protected void handleContext(OSGiServiceState context, boolean install)
+   protected void installAction(OSGiServiceState context) throws Throwable
    {
-      Controller controller = context.getController();
-      if (controller instanceof KernelController)
+      KernelMetaDataRepository repository = getRepository(context);
+      ScopeKey contextScopeKey = context.getScopeInfo().getMutableScope();
+      Scope scope = contextScopeKey.getScope(CommonLevels.INSTANCE);
+      ScopeKey key = new ScopeKey(scope);
+      MutableMetaDataRepository mutable = repository.getMetaDataRepository();
+      MetaDataRetrieval retrieval = mutable.getMetaDataRetrieval(key);
+      if (retrieval instanceof MutableMetaDataLoader)
       {
-         KernelController kc = (KernelController)controller;
-         Kernel kernel = kc.getKernel();
-         KernelMetaDataRepository repository = kernel.getMetaDataRepository();
-         if (install)
-         {
-            repository.addMetaData(context);
-            repository.getMetaData(context); // TODO - remove this once we fix it in MC core
-         }
-         else
-         {
-            repository.removeMetaData(context);
-         }
+         MutableMetaDataLoader mmdl = (MutableMetaDataLoader)retrieval;
+         Dictionary<String, Object> dictionary = new ServiceRefDictionary(context);
+         mmdl.addMetaData(dictionary, Dictionary.class);
       }
    }
+
+   protected void uninstallAction(OSGiServiceState context)
+   {
+   }
 }
\ No newline at end of file

Modified: projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/GenericServiceReferenceWrapper.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/GenericServiceReferenceWrapper.java	2009-11-30 14:38:21 UTC (rev 97166)
+++ projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/GenericServiceReferenceWrapper.java	2009-11-30 15:37:44 UTC (rev 97167)
@@ -21,6 +21,8 @@
 */
 package org.jboss.osgi.framework.bundle;
 
+import java.util.Dictionary;
+import java.util.Enumeration;
 import java.util.HashSet;
 import java.util.Set;
 
@@ -55,14 +57,20 @@
       return context;
    }
 
-   public Object getProperty(String s)
+   public Object getProperty(String key)
    {
-      return null;  // TODO
+      return MDRUtils.getProperty(context, key, Object.class);
    }
 
    public String[] getPropertyKeys()
    {
-      return null; // TODO
+      Dictionary<String, Object> dictionary = MDRUtils.getProperties(context);
+      String[] keys = new String[dictionary.size()];
+      int i = 0;
+      Enumeration<String> e = dictionary.keys();
+      while (e.hasMoreElements())
+         keys[i++] = e.nextElement();
+      return keys;
    }
 
    public Bundle getBundle()
@@ -95,14 +103,14 @@
       return null;
    }
 
-   public boolean isAssignableTo(Bundle bundle, String s)
+   public boolean isAssignableTo(Bundle bundle, String className)
    {
-      return false; // TODO
+      return MDRUtils.isAssignableTo(context, bundleState, bundle, className); 
    }
 
-   public int compareTo(Object o)
+   public int compareTo(Object obj)
    {
-      return 0; // TODO
+      return MDRUtils.compareTo(context, obj);
    }
 
    public int hashCode()

Copied: projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/MDRUtils.java (from rev 97142, projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiBundleManager.java)
===================================================================
--- projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/MDRUtils.java	                        (rev 0)
+++ projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/MDRUtils.java	2009-11-30 15:37:44 UTC (rev 97167)
@@ -0,0 +1,349 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors.
+*
+* This 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 (at your option) any later version.
+*
+* This software 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. 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 software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.osgi.framework.bundle;
+
+import java.util.Arrays;
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.jboss.dependency.spi.ControllerContext;
+import org.jboss.logging.Logger;
+import org.jboss.metadata.spi.MetaData;
+import org.jboss.metadata.spi.scope.CommonLevels;
+import org.jboss.metadata.spi.scope.ScopeLevel;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
+
+/**
+ * MetaDataRepository utils.
+ *
+ * @author <a href="ales.justin at jboss.org">Ales Justin</a>
+ */
+public class MDRUtils
+{
+   /** The log */
+   private static final Logger log = Logger.getLogger(MDRUtils.class);
+
+   /** The empty dictionary */
+   private static final Dictionary<String, Object> EMPTY = new Hashtable<String, Object>();
+
+   /**
+    * Get metadata.
+    *
+    * @param context the context
+    * @param level the scope level
+    * @return metadata
+    */
+   private static MetaData getMetaData(ControllerContext context, ScopeLevel level)
+   {
+      if (context == null)
+         throw new IllegalArgumentException("Null context");
+
+      MetaData metaData = context.getScopeInfo().getMetaData();
+      if (level != null && metaData != null)
+         metaData = metaData.getScopeMetaData(level);
+      return metaData;
+   }
+
+   /**
+    * Get context's properties.
+    *
+    * @param context the context
+    * @return the properties
+    */
+   @SuppressWarnings({"unchecked"})
+   static Dictionary<String, Object> getProperties(ControllerContext context)
+   {
+      MetaData metaData = getMetaData(context, CommonLevels.INSTANCE);
+      if (metaData != null)
+      {
+         Dictionary<String, Object> properties = metaData.getMetaData(Dictionary.class);
+         if (properties != null)
+            return properties;
+      }
+      return EMPTY;
+   }
+
+   /**
+    * Get property.
+    *
+    * @param context the context
+    * @param key the property key
+    * @param expectedType the expected type
+    * @return found property or null
+    */
+   static <T> T getProperty(ControllerContext context, String key, Class<T> expectedType)
+   {
+      return getProperty(context, key, expectedType, null);
+   }
+
+   /**
+    * Get property.
+    *
+    * @param context the context
+    * @param key the property key
+    * @param expectedType the expected type
+    * @param defaultValue the default value
+    * @return found property or null
+    */
+   static <T> T getProperty(ControllerContext context, String key, Class<T> expectedType, T defaultValue)
+   {
+      if (key == null)
+         throw new IllegalArgumentException("Null key");
+      if (expectedType == null)
+         throw new IllegalArgumentException("Null expected type");
+
+      Dictionary<String, Object> properties = getProperties(context);
+      Object result = properties.get(key);
+      if (result != null && expectedType.isInstance(result) == false)
+      {
+         if (defaultValue == null)
+            throw new IllegalArgumentException("Illegal result type: " + result + ", expected: " + expectedType);
+         else
+            result = defaultValue;
+      }
+
+      if (result == null)
+         result = defaultValue;
+
+      return expectedType.cast(result);
+   }
+
+   /**
+    * Is assignable to bundle.
+    *
+    * @param context the context
+    * @param bundleState the bundle state
+    * @param otherBundle the other bundle
+    * @param className the class name
+    * @return true if assignable, false otherwise
+    */
+   private static boolean isAssignableTo(ControllerContext context, AbstractBundleState bundleState, AbstractBundleState otherBundle, String className)
+   {
+      if (bundleState == otherBundle)
+         return true;
+
+      if (OSGiBundleManager.isUnregistered(context))
+         return false;
+
+      return isAssignableTo(bundleState, otherBundle, className);
+   }
+
+   /**
+    * Is assignable.
+    *
+    * @param bundleState the bundle state
+    * @param other the other bundle
+    * @param className the class name
+    * @return true if assignable, false otherwise
+    */
+   private static boolean isAssignableTo(AbstractBundleState bundleState, AbstractBundleState other, String className)
+   {
+      Object source = bundleState.getSource(className);
+      if (source == null)
+         throw new IllegalStateException("Cannot load '" + className + "' from: " + bundleState);
+
+      Object otherSource = other.getSource(className);
+      if (otherSource == null)
+      {
+         log.debug("Cannot load '" + className + "' from: " + other);
+         return false;
+      }
+
+      boolean equals = source.equals(otherSource);
+      if (equals == false)
+      {
+         ClassLoader otherLoader = otherSource.getClass().getClassLoader();
+         ClassLoader sourceLoader = source.getClass().getClassLoader();
+         StringBuffer buffer = new StringBuffer("Cannot assign '" + className + "' comming from different exporters");
+         buffer.append("\n  service: ").append(sourceLoader);
+         buffer.append("\n  request: ").append(otherLoader);
+         log.warn(buffer.toString());
+      }
+      return equals;
+   }
+
+   /**
+    * Match class.
+    *
+    * @param context the context
+    * @param clazz the class to match
+    * @return true if the class matches any of the classes
+    */
+   public static boolean matchClass(ControllerContext context, String clazz)
+   {
+      String[] classes = getProperty(context, Constants.OBJECTCLASS, String[].class);
+      return classes != null && Arrays.asList(classes).contains(clazz);
+   }
+
+   /**
+    * Get service ranking.
+    *
+    * @param context the context
+    * @return the ranking or null
+    */
+   public static Integer getRanking(ControllerContext context)
+   {
+      return getProperty(context, Constants.SERVICE_RANKING, Integer.class, 0);
+   }
+
+   /**
+    * Get service id.
+    *
+    * @param context the context
+    * @return the id or null
+    */
+   public static Long getId(ControllerContext context)
+   {
+      return getProperty(context, Constants.SERVICE_ID, Long.class);
+   }
+
+   /**
+    * Is context assignable to bundle.
+    *
+    * @param context the context
+    * @param bundleState the bundle state
+    * @return true if assignable, false otherwise
+    */
+   public static boolean isAssignableTo(ControllerContext context, AbstractBundleState bundleState)
+   {
+      if (context == null)
+         throw new IllegalArgumentException("Null context");
+      if (bundleState == null)
+         throw new IllegalArgumentException("Null bundle state");
+
+      OSGiBundleManager manager = bundleState.getBundleManager();
+      // context's bundle
+      AbstractBundleState other = manager.getBundleForContext(context);
+      if (bundleState == other)
+         return true;
+      if (OSGiBundleManager.isUnregistered(context))
+         return false;
+
+      String[] classes = getProperty(context, Constants.OBJECTCLASS, String[].class);
+      if (classes == null)
+         return false;
+
+      for (String className : classes)
+      {
+         if (isAssignableTo(bundleState, other, className) == false)
+            return false;
+      }
+      return true;
+   }
+
+   /**
+    * Is assignable to bundle.
+    *
+    * @param context the context
+    * @param bundleState the bundle state
+    * @param className the class name
+    * @return true if assignable, false otherwise
+    */
+   public static boolean isAssignableTo(ControllerContext context, AbstractBundleState bundleState, String className)
+   {
+      if (context == null)
+         throw new IllegalArgumentException("Null context");
+      if (bundleState == null)
+         throw new IllegalArgumentException("Null bundle state");
+      if (className == null)
+         throw new IllegalArgumentException("Null class name");
+
+      OSGiBundleManager manager = bundleState.getBundleManager();
+      // context's bundle
+      AbstractBundleState other = manager.getBundleForContext(context);
+      return isAssignableTo(context, bundleState, other, className);
+   }
+
+   /**
+    * Is assignable to bundle.
+    *
+    * @param context the context
+    * @param bundleState the bundle state
+    * @param bundle the other bundle
+    * @param className the class name
+    * @return true if assignable, false otherwise
+    */
+   public static boolean isAssignableTo(ControllerContext context, AbstractBundleState bundleState, Bundle bundle, String className)
+   {
+      if (context == null)
+         throw new IllegalArgumentException("Null context");
+      if (bundleState == null)
+         throw new IllegalArgumentException("Null bundle state");
+      if (bundle == null)
+         throw new IllegalArgumentException("Null bundle");
+      if (className == null)
+         throw new IllegalArgumentException("Null class name");
+
+      AbstractBundleState otherBundle;
+      if (bundle instanceof AbstractBundleState)
+         otherBundle = (AbstractBundleState)bundle;
+      else if (bundle instanceof OSGiBundleWrapper)
+         otherBundle = ((OSGiBundleWrapper)bundle).getBundleState();
+      else
+         throw new IllegalArgumentException("Illegal bundle type: " + bundle);
+
+      return isAssignableTo(context, bundleState, otherBundle, className);
+   }
+
+   /**
+    * Compare to.
+    *
+    * @param context the context
+    * @param reference the other reference
+    * @return compare value
+    */
+   public static int compareTo(ControllerContext context, Object reference)
+   {
+      if (context == null)
+         throw new IllegalArgumentException("Null context");
+      if (reference == null)
+         throw new IllegalArgumentException("Null reference");
+
+      ControllerContext other;
+      if (reference instanceof ControllerContext)
+         other = (ControllerContext)reference;
+      else if (reference instanceof ControllerContextHandle)
+         other = ((ControllerContextHandle)reference).getContext();
+      else
+         throw new IllegalArgumentException(reference + " is not a service reference");
+
+      Long thisServiceId = getId(context);
+      Long otherServiceId = getId(other);
+      if (thisServiceId == null && otherServiceId == null)
+         return 0;
+      if (otherServiceId == null)
+         return -1; // TODO?
+      if (thisServiceId == null)
+         return 1; // TODO?
+      if (thisServiceId - otherServiceId == 0)
+         return 0;
+
+      int thisRanking = getRanking(context);
+      int otherRanking = getRanking(other);
+      int ranking = thisRanking - otherRanking;
+      if (ranking != 0)
+         return ranking;
+
+      return (thisServiceId > otherServiceId) ? -1 : 1;
+   }
+}
\ No newline at end of file

Modified: projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiBundleManager.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiBundleManager.java	2009-11-30 14:38:21 UTC (rev 97166)
+++ projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiBundleManager.java	2009-11-30 15:37:44 UTC (rev 97167)
@@ -32,7 +32,6 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Comparator;
 import java.util.Dictionary;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
@@ -40,6 +39,7 @@
 import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
+import java.util.SortedSet;
 import java.util.TreeSet;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CopyOnWriteArrayList;
@@ -97,8 +97,8 @@
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceEvent;
 import org.osgi.framework.ServiceReference;
-import org.osgi.framework.Version;
 import org.osgi.framework.ServiceRegistration;
+import org.osgi.framework.Version;
 import org.osgi.service.packageadmin.PackageAdmin;
 
 /**
@@ -1155,6 +1155,43 @@
    }
 
    /**
+    * Load class from a bundle.
+    * If it cannot be loaded, return null.
+    *
+    * @param bundle the bundle to load from
+    * @param clazz the class
+    * @return class or null
+    */
+   private Class<?> loadClass(Bundle bundle, String clazz)
+   {
+      try
+      {
+         return bundle.loadClass(clazz);
+      }
+      catch (ClassNotFoundException e)
+      {
+         return null;
+      }
+   }
+
+   /**
+    * Do we have a permission to use context.
+    *
+    * @param context the context
+    * @return true if allowed to use context, false otherwise
+    */
+   private boolean hasPermission(ControllerContext context)
+   {
+      // TODO - make thisa generic, w/o casting
+      if (context instanceof OSGiServiceState)
+      {
+         OSGiServiceState serviceState = (OSGiServiceState)context;
+         return serviceState.hasPermission();
+      }
+      return true;
+   }
+
+   /**
     * Get services
     * 
     * @param bundle the referencing bundle
@@ -1163,7 +1200,7 @@
     * @param checkAssignable whether to check isAssignable
     * @return the services
     */
-   Collection<OSGiServiceState> getServices(AbstractBundleState bundle, String clazz, Filter filter, boolean checkAssignable)
+   Collection<ControllerContext> getServices(AbstractBundleState bundle, String clazz, Filter filter, boolean checkAssignable)
    {
       Set<ControllerContext> contexts;
       KernelController controller = kernel.getController();
@@ -1193,23 +1230,20 @@
       if (filter == null)
          filter = NoFilter.INSTANCE;
 
-      // review: optimise this, e.g. index by class
       // Use a sorted set to order services according to spec
-      Set<OSGiServiceState> result = new TreeSet<OSGiServiceState>(ServiceComparator.INSTANCE);
+      SortedSet<ControllerContext> result = new TreeSet<ControllerContext>(ContextComparator.INSTANCE);
       for (ControllerContext context : contexts)
       {
-         // TODO - fix filtering
-         if (context instanceof OSGiServiceState)
+         if (isUnregistered(context) == false)
          {
-            OSGiServiceState service = OSGiServiceState.class.cast(context);
-            // Check the state, filter and permission
-            if (service.isUnregistered() == false && filter.match(service) && service.hasPermission())
+            ServiceReference ref = getServiceReferenceForContext(context);
+            if (filter.match(ref) && hasPermission(context))
             {
-               if (clazz == null || isSystemBundle == false || service.matchClass(clazz))
+               if (clazz == null || isSystemBundle == false || MDRUtils.matchClass(context, clazz))
                {
                   // Check the assignability
-                  if (checkAssignable == false || service.isAssignable(bundle))
-                     result.add(service);
+                  if (checkAssignable == false || MDRUtils.isAssignableTo(context, bundle))
+                     result.add(context);
                }
             }
          }
@@ -1218,26 +1252,6 @@
    }
 
    /**
-    * Load class from a bundle.
-    * If it cannot be loaded, return null.
-    *
-    * @param bundle the bundle to load from
-    * @param clazz the class
-    * @return class or null
-    */
-   private Class<?> loadClass(Bundle bundle, String clazz)
-   {
-      try
-      {
-         return bundle.loadClass(clazz);
-      }
-      catch (ClassNotFoundException e)
-      {
-         return null;
-      }
-   }
-
-   /**
     * Get service reference
     * 
     * @param bundle the referencing bundle
@@ -1246,30 +1260,12 @@
     */
    ServiceReference getServiceReference(AbstractBundleState bundle, String clazz)
    {
-      Collection<OSGiServiceState> services = getServices(bundle, clazz, null, true);
-      if (services == null || services.isEmpty())
+      Collection<ControllerContext> contexts = getServices(bundle, clazz, null, true);
+      if (contexts == null || contexts.isEmpty())
          return null;
 
-      // If multiple such services exist, the service with the highest ranking (as specified in its SERVICE_RANKING property) is returned.
-      // If there is a tie in ranking, the service with the lowest service ID (as specified in its SERVICE_ID property); 
-      // that is, the service that was registered first is returned.
-
-      long bestId = 0;
-      int bestRanking = 0;
-      ServiceReference bestMatch = null;
-      for (OSGiServiceState service : services)
-      {
-         long id = service.getServiceId();
-         int ranking = service.getServiceRanking();
-         if (bestMatch == null || ranking > bestRanking || (ranking == bestRanking && id < bestId))
-         {
-            bestMatch = service.getReferenceInternal();
-            bestRanking = ranking;
-            bestId = id;
-         }
-      }
-
-      return bestMatch;
+      ControllerContext context = contexts.iterator().next();
+      return getServiceReferenceForContext(context);
    }
 
    /**
@@ -1283,15 +1279,15 @@
     */
    ServiceReference[] getServiceReferences(AbstractBundleState bundle, String clazz, Filter filter, boolean checkAssignable)
    {
-      Collection<OSGiServiceState> services = getServices(bundle, clazz, filter, checkAssignable);
-      if (services == null || services.isEmpty())
+      Collection<ControllerContext> contexts = getServices(bundle, clazz, filter, checkAssignable);
+      if (contexts == null || contexts.isEmpty())
          return null;
 
-      ServiceReference[] result = new ServiceReference[services.size()];
+      ServiceReference[] result = new ServiceReference[contexts.size()];
 
       int i = 0;
-      for (OSGiServiceState service : services)
-         result[i++] = service.getReferenceInternal();
+      for (ControllerContext context : contexts)
+         result[i++] = getServiceReferenceForContext(context);
 
       return result;
    }
@@ -1389,14 +1385,25 @@
    {
       ControllerContextHandle handle = (ControllerContextHandle)reference;
       ControllerContext context = handle.getContext();
-      Controller controller = context.getController();
-      if (controller == null || controller.getStates().isBeforeState(context.getState(), ControllerState.INSTALLED))
-         return null; // we're probably not installed anymore  
+      if (isUnregistered(context))  // we're probably not installed anymore
+         return null;
 
       return bundleState.addContextInUse(context);
    }
 
    /**
+    * Is the context undergisted.
+    *
+    * @param context the context
+    * @return true if the context is unregisted, false otherwise
+    */
+   static boolean isUnregistered(ControllerContext context)
+   {
+      Controller controller = context.getController();
+      return controller == null || controller.getStates().isBeforeState(context.getState(), ControllerState.INSTALLED);
+   }
+
+   /**
     * Unget a service
     * 
     * @param bundleState the bundle state
@@ -1620,17 +1627,4 @@
       else
          plugin.fireFrameworkEvent(systemBundle, FrameworkEvent.WARNING, new BundleException("Error " + context, t));
    }
-
-   /**
-    * ServiceComparator, reverses the order of compareTo
-    */
-   static class ServiceComparator implements Comparator<OSGiServiceState>
-   {
-      public static ServiceComparator INSTANCE = new ServiceComparator();
-
-      public int compare(OSGiServiceState o1, OSGiServiceState o2)
-      {
-         return -o1.compareTo(o2);
-      }
-   }
 }

Modified: projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiControllerContextActions.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiControllerContextActions.java	2009-11-30 14:38:21 UTC (rev 97166)
+++ projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiControllerContextActions.java	2009-11-30 15:37:44 UTC (rev 97167)
@@ -51,6 +51,7 @@
    private static Map<ControllerState, ControllerContextAction> getActions()
    {
       Map<ControllerState, ControllerContextAction> map = new HashMap<ControllerState, ControllerContextAction>(2);
+      map.put(ControllerState.PRE_INSTALL, new PreInstallAction());
       map.put(ControllerState.DESCRIBED, new DescribeAction());
       map.put(ControllerState.INSTANTIATED, new ContextRegistryAction());
       return map;

Modified: projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceAction.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceAction.java	2009-11-30 14:38:21 UTC (rev 97166)
+++ projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceAction.java	2009-11-30 15:37:44 UTC (rev 97167)
@@ -22,6 +22,9 @@
 package org.jboss.osgi.framework.bundle;
 
 import org.jboss.dependency.plugins.action.SimpleControllerContextAction;
+import org.jboss.dependency.spi.ControllerContext;
+import org.jboss.kernel.Kernel;
+import org.jboss.kernel.spi.metadata.KernelMetaDataRepository;
 import org.jboss.logging.Logger;
 
 /**
@@ -33,13 +36,38 @@
 {
    protected Logger log = Logger.getLogger(getClass());
    
-   protected OSGiServiceState contextCast(org.jboss.dependency.spi.ControllerContext context)
+   protected OSGiServiceState contextCast(ControllerContext context)
    {
       return OSGiServiceState.class.cast(context);
    }
 
-   protected boolean validateContext(org.jboss.dependency.spi.ControllerContext context)
+   protected boolean validateContext(ControllerContext context)
    {
       return (context instanceof OSGiServiceState);
    }
+
+   /**
+    * Get kernel.
+    *
+    * @param context the context
+    * @return MC kernel
+    */
+   protected Kernel getKernel(OSGiServiceState context)
+   {
+      AbstractBundleState bundleState = context.getBundleState();
+      OSGiBundleManager manager = bundleState.getBundleManager();
+      return manager.getKernel();
+   }
+
+   /**
+    * Get metadata repository.
+    *
+    * @param context the context
+    * @return kernel metadata repository
+    */
+   protected KernelMetaDataRepository getRepository(OSGiServiceState context)
+   {
+      Kernel kernel = getKernel(context);
+      return kernel.getMetaDataRepository();
+   }
 }
\ No newline at end of file

Modified: projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceState.java
===================================================================
--- projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceState.java	2009-11-30 14:38:21 UTC (rev 97166)
+++ projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceState.java	2009-11-30 15:37:44 UTC (rev 97167)
@@ -35,6 +35,7 @@
 
 import org.jboss.beans.info.spi.BeanInfo;
 import org.jboss.dependency.plugins.AbstractControllerContext;
+import org.jboss.dependency.spi.ControllerContext;
 import org.jboss.dependency.spi.ScopeInfo;
 import org.jboss.dependency.spi.dispatch.InvokeDispatchContext;
 import org.jboss.dependency.spi.tracker.ContextTracker;
@@ -448,102 +449,9 @@
 
    public boolean isAssignableTo(Bundle bundle, String className)
    {
-      if (bundle == null)
-         throw new IllegalArgumentException("Null bundle");
-      if (className == null)
-         throw new IllegalArgumentException("Null class name");
-
-      if (bundle instanceof OSGiBundleWrapper == false)
-         throw new IllegalArgumentException("Unknown bundle: " + bundle);
-
-      OSGiBundleWrapper wrapper = (OSGiBundleWrapper)bundle;
-      AbstractBundleState bundleState = wrapper.getBundleState();
-      return isAssignableTo(bundleState, className);
+      return MDRUtils.isAssignableTo(this, bundleState, bundle, className);
    }
 
-   /**
-    * Check the isAssignableTo
-    * 
-    * @param other the bundle state
-    * @param className the class name
-    * @return true when assignable
-    */
-   boolean isAssignableTo(AbstractBundleState other, String className)
-   {
-      if (className == null)
-         throw new IllegalArgumentException("Null class name");
-
-      if (other == bundleState)
-         return true;
-
-      if (isUnregistered())
-         return false;
-
-      Class<?> source = (Class<?>)bundleState.getSource(className);
-      if (source == null)
-         throw new IllegalStateException("Cannot load '" + className + "' from: " + bundleState);
-
-      Class<?> otherSource = (Class<?>)other.getSource(className);
-      if (otherSource == null)
-      {
-         log.debug("Cannot load '" + className + "' from: " + other);
-         return false;
-      }
-
-      boolean equals = source.equals(otherSource);
-      if (equals == false)
-      {
-         ClassLoader otherLoader = otherSource.getClassLoader();
-         ClassLoader sourceLoader = source.getClassLoader();
-         StringBuffer buffer = new StringBuffer("Cannot assign '" + className + "' comming from different exporters");
-         buffer.append("\n  service: ").append(sourceLoader);
-         buffer.append("\n  request: ").append(otherLoader);
-         log.warn(buffer.toString());
-      }
-      return equals;
-   }
-
-   /**
-    * Check the isAssignable
-    * 
-    * @param bundle the bundle state
-    * @return true when assignable
-    */
-   boolean isAssignable(AbstractBundleState bundle)
-   {
-      if (bundle == bundleState)
-         return true;
-
-      if (isUnregistered())
-         return false;
-
-      for (String clazz : getClasses())
-      {
-         if (isAssignableTo(bundle, clazz) == false)
-            return false;
-      }
-      return true;
-   }
-
-   /**
-    * Match the class
-    * 
-    * @param className the class name
-    * @return true when the class name matches
-    */
-   boolean matchClass(String className)
-   {
-      if (clazzes == null || clazzes.length == 0)
-         return false;
-
-      for (String clazz : clazzes)
-      {
-         if (className.equals(clazz))
-            return true;
-      }
-      return false;
-   }
-
    public void unregister()
    {
       checkUnregistered();
@@ -566,21 +474,24 @@
       if (reference == null)
          throw new IllegalArgumentException("Null reference");
 
-      OSGiServiceState other;
-      if (reference instanceof OSGiServiceState)
-         other = (OSGiServiceState)reference;
-      else if (reference instanceof OSGiServiceReferenceWrapper)
-         other = ((OSGiServiceReferenceWrapper)reference).getServiceState();
-      else // TODO - handle generic service ref wrapper via MDR?
+      ControllerContext other;
+      if (reference instanceof ControllerContext)
+         other = (ControllerContext)reference;
+      else if (reference instanceof ControllerContextHandle)
+         other = ((ControllerContextHandle)reference).getContext();
+      else
          throw new IllegalArgumentException(reference + " is not a service reference");
 
+      Long otherServiceId = MDRUtils.getId(other);
+      if (otherServiceId == null)
+         return -1; // TODO?
+
       long thisServiceId = getServiceId();
-      long otherServiceId = other.getServiceId();
       if (thisServiceId == otherServiceId)
          return 0;
 
+      Integer otherRanking = MDRUtils.getRanking(other);
       int thisRanking = getServiceRanking();
-      int otherRanking = other.getServiceRanking();
       int ranking = thisRanking - otherRanking;
       if (ranking != 0)
          return ranking;

Copied: projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/PreInstallAction.java (from rev 97111, projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/DescribeAction.java)
===================================================================
--- projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/PreInstallAction.java	                        (rev 0)
+++ projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/PreInstallAction.java	2009-11-30 15:37:44 UTC (rev 97167)
@@ -0,0 +1,46 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors.
+*
+* This 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 (at your option) any later version.
+*
+* This software 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. 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 software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.osgi.framework.bundle;
+
+import org.jboss.kernel.spi.metadata.KernelMetaDataRepository;
+
+/**
+ * PreInstall osgi service, its metadata.
+ *
+ * @author <a href="ales.justin at jboss.org">Ales Justin</a>
+ */
+class PreInstallAction extends SimpleOSGiServiceAction
+{
+   protected void handleContext(OSGiServiceState context, boolean install)
+   {
+      KernelMetaDataRepository repository = getRepository(context);
+      if (install)
+      {
+         repository.addMetaData(context);
+         repository.getMetaData(context); // TODO - remove this once we fix it in MC core
+      }
+      else
+      {
+         repository.removeMetaData(context);
+      }
+   }
+}
\ No newline at end of file

Copied: projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/ServiceRefDictionary.java (from rev 97142, projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/OSGiServiceState.java)
===================================================================
--- projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/ServiceRefDictionary.java	                        (rev 0)
+++ projects/jboss-osgi/trunk/reactor/framework/src/main/java/org/jboss/osgi/framework/bundle/ServiceRefDictionary.java	2009-11-30 15:37:44 UTC (rev 97167)
@@ -0,0 +1,111 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors.
+*
+* This 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 (at your option) any later version.
+*
+* This software 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. 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 software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.osgi.framework.bundle;
+
+import java.util.Dictionary;
+import java.util.Enumeration;
+
+import org.osgi.framework.ServiceReference;
+
+/**
+ * Service reference based dictionary.
+ *
+ * @author <a href="ales.justin at jboss.org">Ales Justin</a>
+ */
+class ServiceRefDictionary extends Dictionary<String, Object>
+{
+   private ServiceReference ref;
+
+   ServiceRefDictionary(ServiceReference ref)
+   {
+      if (ref == null)
+         throw new IllegalArgumentException("Null ref");
+      this.ref = ref;
+   }
+
+   public int size()
+   {
+      String[] keys = ref.getPropertyKeys();
+      return (keys != null) ? keys.length : 0;
+   }
+
+   public boolean isEmpty()
+   {
+      return size() == 0;
+   }
+
+   public Enumeration<String> keys()
+   {
+      return new Enumeration<String>()
+      {
+         String[] keys = ref.getPropertyKeys();
+         int index = 0;
+
+         public boolean hasMoreElements()
+         {
+            return index < size();
+         }
+
+         public String nextElement()
+         {
+            return keys[index++];
+         }
+      };
+   }
+
+   public Enumeration<Object> elements()
+   {
+      return new Enumeration<Object>()
+      {
+         String[] keys = ref.getPropertyKeys();
+         int index = 0;
+
+         public boolean hasMoreElements()
+         {
+            return index < size();
+         }
+
+         public Object nextElement()
+         {
+            return get(keys[index++]);
+         }
+      };
+   }
+
+   public Object get(Object key)
+   {
+      if (key == null)
+         return null;
+
+      return ref.getProperty(key.toString());
+   }
+
+   public Object put(String key, Object value)
+   {
+      return null;
+   }
+
+   public Object remove(Object key)
+   {
+      return null;
+   }
+}
\ No newline at end of file




More information about the jboss-cvs-commits mailing list