[jboss-osgi-commits] JBoss-OSGI SVN: r99158 - in projects/jboss-osgi: projects/bundles/husky/trunk/src/main/java/org/jboss/osgi/testing/internal and 4 other directories.

jboss-osgi-commits at lists.jboss.org jboss-osgi-commits at lists.jboss.org
Fri Jan 8 08:25:29 EST 2010


Author: thomas.diesler at jboss.com
Date: 2010-01-08 08:25:27 -0500 (Fri, 08 Jan 2010)
New Revision: 99158

Added:
   projects/jboss-osgi/trunk/testsuite/functional/src/test/resources/fragments/resource.txt
Modified:
   projects/jboss-osgi/projects/bundles/husky/trunk/src/main/java/org/jboss/osgi/testing/OSGiBundle.java
   projects/jboss-osgi/projects/bundles/husky/trunk/src/main/java/org/jboss/osgi/testing/internal/EmbeddedBundle.java
   projects/jboss-osgi/projects/bundles/husky/trunk/src/main/java/org/jboss/osgi/testing/internal/RemoteBundle.java
   projects/jboss-osgi/projects/runtime/framework/trunk/src/main/java/org/jboss/osgi/framework/bundle/OSGiFragmentState.java
   projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/management/ManagedBundle.java
   projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/management/ManagedBundleMBean.java
   projects/jboss-osgi/trunk/testsuite/functional/src/test/java/org/jboss/test/osgi/fragments/FragmentTestCase.java
   projects/jboss-osgi/trunk/testsuite/functional/src/test/resources/fragments/simple-frgmA.bnd
   projects/jboss-osgi/trunk/testsuite/functional/src/test/resources/fragments/simple-hostA.bnd
Log:
Test loadClass, findEntry, getResource on fragment bundle

Modified: projects/jboss-osgi/projects/bundles/husky/trunk/src/main/java/org/jboss/osgi/testing/OSGiBundle.java
===================================================================
--- projects/jboss-osgi/projects/bundles/husky/trunk/src/main/java/org/jboss/osgi/testing/OSGiBundle.java	2010-01-08 12:33:37 UTC (rev 99157)
+++ projects/jboss-osgi/projects/bundles/husky/trunk/src/main/java/org/jboss/osgi/testing/OSGiBundle.java	2010-01-08 13:25:27 UTC (rev 99158)
@@ -21,6 +21,7 @@
  */
 package org.jboss.osgi.testing;
 
+import java.net.URL;
 import java.util.Dictionary;
 
 import org.osgi.framework.Bundle;
@@ -76,6 +77,30 @@
    public abstract String getProperty(String key);
 
    /**
+    * Loads the specified class using this bundle's class loader. 
+    * 
+    * @param name The name of the class to load
+    * @return The OSGiBundle that is wired to this bundle class loader and contains the class.
+    * @throws ClassNotFoundException If no such class can be found or if this bundle is a fragment bundle
+    */
+   public abstract OSGiBundle loadClass(String name) throws ClassNotFoundException;
+   
+   /**
+    * Returns a URL to the entry at the specified path in this bundle.
+    * 
+    * @param name The path name of the entry
+    * @return A URL to the entry, or null if no entry could be found
+    */
+   public abstract URL getEntry(String path);
+   
+   /**
+    * Find the specified resource from this bundle's class loader. 
+    * @param name The name of the resource.
+    * @return A URL to the named resource, or null if the resource could not be found
+    */
+   public abstract URL getResource(String name);
+   
+   /**
     * Starts this bundle.
     */
    public abstract void start() throws BundleException;

Modified: projects/jboss-osgi/projects/bundles/husky/trunk/src/main/java/org/jboss/osgi/testing/internal/EmbeddedBundle.java
===================================================================
--- projects/jboss-osgi/projects/bundles/husky/trunk/src/main/java/org/jboss/osgi/testing/internal/EmbeddedBundle.java	2010-01-08 12:33:37 UTC (rev 99157)
+++ projects/jboss-osgi/projects/bundles/husky/trunk/src/main/java/org/jboss/osgi/testing/internal/EmbeddedBundle.java	2010-01-08 13:25:27 UTC (rev 99158)
@@ -26,11 +26,13 @@
 
 import org.jboss.osgi.deployment.deployer.DeployerService;
 import org.jboss.osgi.spi.util.ExportedPackageHelper;
+import org.jboss.osgi.testing.OSGiBundle;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.Version;
+import org.osgi.service.packageadmin.PackageAdmin;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -102,6 +104,29 @@
    }
 
    @Override
+   public URL getEntry(String path)
+   {
+      return bundle.getEntry(path);
+   }
+
+   @Override
+   public URL getResource(String name)
+   {
+      return bundle.getResource(name);
+   }
+
+   @Override
+   public OSGiBundle loadClass(String name) throws ClassNotFoundException
+   {
+      Class<?> clazz = bundle.loadClass(name);
+      BundleContext context = bundle.getBundleContext();
+      ServiceReference sref = context.getServiceReference(PackageAdmin.class.getName());
+      PackageAdmin packageAdmin = (PackageAdmin)context.getService(sref);
+      Bundle providerBundle = packageAdmin.getBundle(clazz);
+      return getRuntime().getBundle(providerBundle.getBundleId());
+   }
+
+   @Override
    public void start() throws BundleException
    {
       bundle.start();

Modified: projects/jboss-osgi/projects/bundles/husky/trunk/src/main/java/org/jboss/osgi/testing/internal/RemoteBundle.java
===================================================================
--- projects/jboss-osgi/projects/bundles/husky/trunk/src/main/java/org/jboss/osgi/testing/internal/RemoteBundle.java	2010-01-08 12:33:37 UTC (rev 99157)
+++ projects/jboss-osgi/projects/bundles/husky/trunk/src/main/java/org/jboss/osgi/testing/internal/RemoteBundle.java	2010-01-08 13:25:27 UTC (rev 99158)
@@ -26,6 +26,9 @@
 import java.net.URL;
 import java.util.Dictionary;
 
+import javax.management.ObjectName;
+
+import org.jboss.osgi.spi.management.ManagedBundle;
 import org.jboss.osgi.spi.management.ManagedBundleMBean;
 import org.jboss.osgi.spi.util.BundleInfo;
 import org.jboss.osgi.spi.util.UnmodifiableDictionary;
@@ -124,6 +127,28 @@
    }
 
    @Override
+   public URL getEntry(String path)
+   {
+      assertNotUninstalled();
+      return bundle.getEntry(path);
+   }
+
+   @Override
+   public URL getResource(String name)
+   {
+      assertNotUninstalled();
+      return bundle.getResource(name);
+   }
+
+   @Override
+   public OSGiBundle loadClass(String name) throws ClassNotFoundException
+   {
+      assertNotUninstalled();
+      ObjectName providerBundle = bundle.loadClass(name);
+      String bundleId = providerBundle.getKeyProperty(ManagedBundle.PROPERTY_ID);
+      return getRuntime().getBundle(new Long(bundleId));
+   }
+   @Override
    public void start() throws BundleException
    {
       assertNotUninstalled();

Modified: projects/jboss-osgi/projects/runtime/framework/trunk/src/main/java/org/jboss/osgi/framework/bundle/OSGiFragmentState.java
===================================================================
--- projects/jboss-osgi/projects/runtime/framework/trunk/src/main/java/org/jboss/osgi/framework/bundle/OSGiFragmentState.java	2010-01-08 12:33:37 UTC (rev 99157)
+++ projects/jboss-osgi/projects/runtime/framework/trunk/src/main/java/org/jboss/osgi/framework/bundle/OSGiFragmentState.java	2010-01-08 13:25:27 UTC (rev 99158)
@@ -73,19 +73,21 @@
 
    public URL getResource(String name)
    {
-      throw new NotImplementedException();
+      // Null if the resource could not be found or if this bundle is a fragment bundle
+      return null;
    }
 
    @SuppressWarnings("rawtypes")
    public Enumeration getResources(String name) throws IOException
    {
-      throw new NotImplementedException();
+      // Null if the resource could not be found or if this bundle is a fragment bundle
+      return null;
    }
 
    @SuppressWarnings("rawtypes")
    public Class loadClass(String name) throws ClassNotFoundException
    {
-      throw new NotImplementedException();
+      throw new ClassNotFoundException("Cannot load class from a fragment: " + this);
    }
 
    public void start(int options) throws BundleException

Modified: projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/management/ManagedBundle.java
===================================================================
--- projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/management/ManagedBundle.java	2010-01-08 12:33:37 UTC (rev 99157)
+++ projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/management/ManagedBundle.java	2010-01-08 13:25:27 UTC (rev 99158)
@@ -25,6 +25,7 @@
 
 import static org.jboss.osgi.spi.OSGiConstants.DOMAIN_NAME;
 
+import java.net.URL;
 import java.util.Dictionary;
 import java.util.Enumeration;
 import java.util.Hashtable;
@@ -32,8 +33,11 @@
 import javax.management.ObjectName;
 
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleException;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.Version;
+import org.osgi.service.packageadmin.PackageAdmin;
 
 /**
  * The managed view of an OSGi Bundle
@@ -115,6 +119,26 @@
       return retHeaders;
    }
 
+   public URL getEntry(String path)
+   {
+      return bundle.getEntry(path);
+   }
+
+   public URL getResource(String name)
+   {
+      return bundle.getResource(name);
+   }
+
+   public ObjectName loadClass(String name) throws ClassNotFoundException
+   {
+      Class<?> clazz = bundle.loadClass(name);
+      BundleContext context = bundle.getBundleContext();
+      ServiceReference sref = context.getServiceReference(PackageAdmin.class.getName());
+      PackageAdmin packageAdmin = (PackageAdmin)context.getService(sref);
+      Bundle providingBundle = packageAdmin.getBundle(clazz);
+      return getObjectName(providingBundle);
+   }
+   
    public void start() throws BundleException
    {
       bundle.start();

Modified: projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/management/ManagedBundleMBean.java
===================================================================
--- projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/management/ManagedBundleMBean.java	2010-01-08 12:33:37 UTC (rev 99157)
+++ projects/jboss-osgi/projects/spi/trunk/src/main/java/org/jboss/osgi/spi/management/ManagedBundleMBean.java	2010-01-08 13:25:27 UTC (rev 99158)
@@ -23,6 +23,7 @@
 
 //$Id$
 
+import java.net.URL;
 import java.util.Dictionary;
 
 import javax.management.ObjectName;
@@ -34,7 +35,7 @@
  * 
  * Bundles are registered under the name
  * 
- * jboss.osgi:bundle=[SymbolicName],id=[BundleId]
+ * jboss.osgi:name=[SymbolicName],id=[BundleId],version=[BundleVersion]
  * 
  * @author thomas.diesler at jboss.com
  * @since 04-Mar-2009
@@ -79,6 +80,30 @@
    String getProperty(String key);
 
    /**
+    * Loads the specified class using this bundle's class loader. 
+    * 
+    * @param name The name of the class to load
+    * @return The object name of the bundle that is wired to this bundle class loader and contains the class.
+    * @throws ClassNotFoundException If no such class can be found or if this bundle is a fragment bundle
+    */
+   ObjectName loadClass(String name) throws ClassNotFoundException;
+   
+   /**
+    * Returns a URL to the entry at the specified path in this bundle.
+    * 
+    * @param name The path name of the entry
+    * @return A URL to the entry, or null if no entry could be found
+    */
+   URL getEntry(String path);
+   
+   /**
+    * Find the specified resource from this bundle's class loader. 
+    * @param name The name of the resource.
+    * @return A URL to the named resource, or null if the resource could not be found
+    */
+   URL getResource(String name);
+   
+   /**
     * Starts this bundle with no options
     */
    void start() throws BundleException;

Modified: projects/jboss-osgi/trunk/testsuite/functional/src/test/java/org/jboss/test/osgi/fragments/FragmentTestCase.java
===================================================================
--- projects/jboss-osgi/trunk/testsuite/functional/src/test/java/org/jboss/test/osgi/fragments/FragmentTestCase.java	2010-01-08 12:33:37 UTC (rev 99157)
+++ projects/jboss-osgi/trunk/testsuite/functional/src/test/java/org/jboss/test/osgi/fragments/FragmentTestCase.java	2010-01-08 13:25:27 UTC (rev 99158)
@@ -23,11 +23,17 @@
 
 //$Id$
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.fail;
 
+import java.net.URL;
+
 import org.jboss.osgi.testing.OSGiBundle;
 import org.jboss.osgi.testing.OSGiRuntime;
 import org.jboss.osgi.testing.OSGiTest;
+import org.jboss.test.osgi.fragments.frgmA.FragmentService;
 import org.junit.Test;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleException;
@@ -49,6 +55,9 @@
          OSGiBundle host = runtime.installBundle("fragments-simple-hostA.jar");
          assertBundleState(Bundle.INSTALLED, host.getState());
          
+         URL resourceURL = host.getResource("resources/resource.txt");
+         assertNull("Resource URL null", resourceURL);
+
          host.start();
          assertBundleState(Bundle.ACTIVE, host.getState());
          
@@ -70,6 +79,12 @@
          OSGiBundle fragment = runtime.installBundle("fragments-simple-frgmA.jar");
          assertBundleState(Bundle.INSTALLED, fragment.getState());
          
+         URL resourceURL = fragment.getResource("resources/resource.txt");
+         assertNull("Resource URL null", resourceURL);
+
+         URL entryURL = fragment.getEntry("resources/resource.txt");
+         assertNotNull("Entry URL not null", entryURL);
+
          try
          {
             fragment.start();
@@ -105,6 +120,15 @@
          assertBundleState(Bundle.ACTIVE, host.getState());
          assertBundleState(Bundle.RESOLVED, fragment.getState());
          
+         OSGiBundle classProvider = host.loadClass(FragmentService.class.getName());
+         assertEquals("Class provided by fragment", host, classProvider);
+         
+         URL resourceURL = host.getResource("resources/resource.txt");
+         assertNotNull("Resource URL not null", resourceURL);
+
+         URL entryURL = host.getEntry("resources/resource.txt");
+         assertNull("Entry URL null", entryURL);
+
          host.uninstall();
          assertBundleState(Bundle.UNINSTALLED, host.getState());
          assertBundleState(Bundle.RESOLVED, fragment.getState());

Added: projects/jboss-osgi/trunk/testsuite/functional/src/test/resources/fragments/resource.txt
===================================================================
--- projects/jboss-osgi/trunk/testsuite/functional/src/test/resources/fragments/resource.txt	                        (rev 0)
+++ projects/jboss-osgi/trunk/testsuite/functional/src/test/resources/fragments/resource.txt	2010-01-08 13:25:27 UTC (rev 99158)
@@ -0,0 +1 @@
+An attached fragment resource.
\ No newline at end of file


Property changes on: projects/jboss-osgi/trunk/testsuite/functional/src/test/resources/fragments/resource.txt
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF

Modified: projects/jboss-osgi/trunk/testsuite/functional/src/test/resources/fragments/simple-frgmA.bnd
===================================================================
--- projects/jboss-osgi/trunk/testsuite/functional/src/test/resources/fragments/simple-frgmA.bnd	2010-01-08 12:33:37 UTC (rev 99157)
+++ projects/jboss-osgi/trunk/testsuite/functional/src/test/resources/fragments/simple-frgmA.bnd	2010-01-08 13:25:27 UTC (rev 99158)
@@ -1,6 +1,9 @@
-# bnd build -classpath target/test-classes -output target/test-libs/jbosgi108-bundleA.jar src/test/resources/jbosgi108/jbosgi108-bundleA.bnd
+# bnd build -classpath target/test-classes -output target/test-libs/fragments-simple-frgmA.jar src/test/resources/fragments/simple-frgmA.bnd
 
 Bundle-SymbolicName: simple-frgmA
 Fragment-Host: simple-hostA
 Export-Package: org.jboss.test.osgi.fragments.frgmA
+Include-Resource: resources/resource.txt=resource.txt
 
+-removeheaders: Include-Resource
+

Modified: projects/jboss-osgi/trunk/testsuite/functional/src/test/resources/fragments/simple-hostA.bnd
===================================================================
--- projects/jboss-osgi/trunk/testsuite/functional/src/test/resources/fragments/simple-hostA.bnd	2010-01-08 12:33:37 UTC (rev 99157)
+++ projects/jboss-osgi/trunk/testsuite/functional/src/test/resources/fragments/simple-hostA.bnd	2010-01-08 13:25:27 UTC (rev 99158)
@@ -1,4 +1,4 @@
-# bnd build -classpath target/test-classes -output target/test-libs/jbosgi108-bundleA.jar src/test/resources/jbosgi108/jbosgi108-bundleA.bnd
+# bnd build -classpath target/test-classes -output target/test-libs/fragments-simple-hostA.jar src/test/resources/fragments/simple-hostA.bnd
 
 Bundle-SymbolicName: simple-hostA
 Bundle-Activator: org.jboss.test.osgi.fragments.hostA.FragmentHostActivator



More information about the jboss-osgi-commits mailing list