[jboss-cvs] JBossAS SVN: r92996 - in projects/jboss-osgi/projects/runtime/microcontainer/branches/tdiesler/src: test/java/org/jboss/test/osgi/service/test and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Sun Aug 30 03:55:58 EDT 2009
Author: thomas.diesler at jboss.com
Date: 2009-08-30 03:55:58 -0400 (Sun, 30 Aug 2009)
New Revision: 92996
Modified:
projects/jboss-osgi/projects/runtime/microcontainer/branches/tdiesler/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleManager.java
projects/jboss-osgi/projects/runtime/microcontainer/branches/tdiesler/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiServiceState.java
projects/jboss-osgi/projects/runtime/microcontainer/branches/tdiesler/src/test/java/org/jboss/test/osgi/service/test/GetUnGetServiceUnitTestCase.java
projects/jboss-osgi/projects/runtime/microcontainer/branches/tdiesler/src/test/java/org/jboss/test/osgi/service/test/RegisterServiceUnitTestCase.java
projects/jboss-osgi/projects/runtime/microcontainer/branches/tdiesler/src/test/java/org/jboss/test/osgi/service/test/ServiceRegistrationUnitTestCase.java
Log:
[JBOSGI-144]
Modified: projects/jboss-osgi/projects/runtime/microcontainer/branches/tdiesler/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleManager.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/branches/tdiesler/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleManager.java 2009-08-30 07:54:31 UTC (rev 92995)
+++ projects/jboss-osgi/projects/runtime/microcontainer/branches/tdiesler/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiBundleManager.java 2009-08-30 07:55:58 UTC (rev 92996)
@@ -691,6 +691,11 @@
Collection<OSGiServiceState> services = getServices(bundleState, clazz, null, checkAssignable);
if (services == null || services.isEmpty())
return null;
+
+ // [TODO] service ranking and service id
+ // 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.
OSGiServiceState service = services.iterator().next();
return service.getReferenceInternal();
@@ -773,7 +778,7 @@
/**
* Get a service
*
- * @param bundleState the bundle state
+ * @param bundleState the bundle that requests the service
* @param reference the service reference
* @return the service
*/
Modified: projects/jboss-osgi/projects/runtime/microcontainer/branches/tdiesler/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiServiceState.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/branches/tdiesler/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiServiceState.java 2009-08-30 07:54:31 UTC (rev 92995)
+++ projects/jboss-osgi/projects/runtime/microcontainer/branches/tdiesler/src/main/java/org/jboss/osgi/plugins/facade/bundle/OSGiServiceState.java 2009-08-30 07:55:58 UTC (rev 92996)
@@ -27,7 +27,9 @@
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashSet;
+import java.util.Map;
import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import org.jboss.logging.Logger;
@@ -54,37 +56,37 @@
{
/** The log */
private static final Logger log = Logger.getLogger(OSGiServiceState.class);
-
+
/** Used to generate a unique id */
private static final AtomicLong serviceIDGenerator = new AtomicLong();
- /** The bundle state */
+ /** The bundle that registered the service */
private AbstractBundleState bundleState;
/** The service reference */
private OSGiServiceReferenceWrapper serviceReference;
-
+
/** The service registration */
private OSGiServiceRegistrationWrapper serviceRegistration;
/** The service id */
private long serviceId = serviceIDGenerator.incrementAndGet();
-
+
/** The service interfaces */
private String[] clazzes;
-
- /** The service */
- private Object service;
-
- /** The service factory */
- private ServiceFactory serviceFactory;
-
+
+ /** The service or service factory */
+ private Object serviceOrFactory;
+
+ /** The service factory provided service cache */
+ private Map<AbstractBundleState, Object> serviceCache;
+
/** The properties */
private CaseInsensitiveDictionary properties;
/** The using bundles */
- private Set<AbstractBundleState> usingBundles = new ConcurrentSet<AbstractBundleState>();
-
+ private Set<AbstractBundleState> usingBundles = new ConcurrentSet<AbstractBundleState>();
+
/**
* Create a new OSGiServiceState.
*
@@ -101,6 +103,7 @@
throw new IllegalArgumentException("Null bundle state");
if (clazzes == null || clazzes.length == 0)
throw new IllegalArgumentException("Null or empty clazzes");
+
for (String clazz : clazzes)
{
if (clazz == null)
@@ -111,20 +114,16 @@
this.bundleState = bundleState;
this.clazzes = clazzes;
- if (service instanceof ServiceFactory)
- {
- this.serviceFactory = (ServiceFactory) service;
- }
- else
- {
- this.service = service;
+ this.serviceOrFactory = service;
+ if (service instanceof ServiceFactory == false)
checkObjClass(service);
- }
+
if (properties != null)
this.properties = new CaseInsensitiveDictionary(properties);
+
serviceRegistration = new OSGiServiceRegistrationWrapper(this);
}
-
+
/**
* Get the serviceId.
*
@@ -169,25 +168,32 @@
// [TODO] fix race condition with unregistration
if (isUnregistered())
return null;
-
+
checkPermission("get", false);
- if (service != null)
- return service;
-
- if (serviceFactory != null)
+ Object service = serviceOrFactory;
+ if (serviceOrFactory instanceof ServiceFactory)
{
- try
+ if (serviceCache == null)
+ serviceCache = new ConcurrentHashMap<AbstractBundleState, Object>();
+
+ service = serviceCache.get(bundleState);
+ if (service == null)
{
- service = checkObjClass(serviceFactory.getService(bundleState.getBundle(), getRegistration()));
+ ServiceFactory serviceFactory = (ServiceFactory)serviceOrFactory;
+ try
+ {
+ service = checkObjClass(serviceFactory.getService(bundleState.getBundle(), getRegistration()));
+ serviceCache.put(bundleState, service);
+ }
+ catch (Throwable t)
+ {
+ log.error("Error from getService for " + this, t);
+ FrameworkEventsPlugin plugin = bundleState.getBundleManager().getPlugin(FrameworkEventsPlugin.class);
+ plugin.fireFrameworkEvent(bundleState, FrameworkEvent.ERROR, new BundleException("Error using service factory:" + serviceFactory, t));
+ return null;
+ }
}
- catch (Throwable t)
- {
- log.error("Error from getService for " + this, t);
- FrameworkEventsPlugin plugin = bundleState.getBundleManager().getPlugin(FrameworkEventsPlugin.class);
- plugin.fireFrameworkEvent(bundleState, FrameworkEvent.ERROR, new BundleException("Error using service factory:" + serviceFactory, t));
- return null;
- }
}
return service;
}
@@ -201,13 +207,13 @@
{
return serviceRegistration;
}
-
+
public ServiceReference getReference()
{
checkUnregistered();
return getReferenceInternal();
}
-
+
public ServiceReference getReferenceInternal()
{
if (serviceReference == null)
@@ -256,19 +262,19 @@
}
result.add(Constants.SERVICE_ID);
result.add(Constants.OBJECTCLASS);
- return result.toArray(new String[result.size()]);
+ return result.toArray(new String[result.size()]);
}
@SuppressWarnings("unchecked")
public void setProperties(Dictionary properties)
{
checkUnregistered();
-
+
if (properties == null)
this.properties = null;
else
this.properties = new CaseInsensitiveDictionary(properties);
-
+
FrameworkEventsPlugin plugin = bundleState.getBundleManager().getPlugin(FrameworkEventsPlugin.class);
plugin.fireServiceEvent(bundleState, ServiceEvent.MODIFIED, this);
}
@@ -292,7 +298,7 @@
{
usingBundles.remove(bundleState);
}
-
+
public Bundle[] getUsingBundles()
{
if (usingBundles.isEmpty())
@@ -312,8 +318,8 @@
throw new IllegalArgumentException("Null class name");
if (bundle instanceof OSGiBundleWrapper == false)
throw new IllegalArgumentException("Unknown bundle: " + bundle);
-
- OSGiBundleWrapper wrapper = (OSGiBundleWrapper) bundle;
+
+ OSGiBundleWrapper wrapper = (OSGiBundleWrapper)bundle;
AbstractBundleState other = wrapper.getBundleState();
return isAssignableTo(other, className);
}
@@ -339,11 +345,11 @@
Class<?> otherSource = (Class<?>)other.getSource(className);
if (otherSource == null)
return false;
-
- Class<?> source = (Class<?>)bundleState.getSource(className);
+
+ Class<?> source = (Class<?>)bundleState.getSource(className);
if (source == null)
return false;
-
+
boolean equals = otherSource.equals(source);
if (equals == false && otherSource.getName().equals(source.getName()))
{
@@ -381,7 +387,7 @@
/**
* Match the class
- *
+ *
* @param other the other bundle state
* @param className the class name
* @return true when the class name matches
@@ -402,10 +408,10 @@
public void unregister()
{
checkUnregistered();
-
+
try
{
- getBundleState().unregisterService(this);
+ bundleState.unregisterService(this);
}
finally
{
@@ -420,12 +426,12 @@
{
if (reference == null)
throw new IllegalArgumentException("Null reference");
-
+
OSGiServiceState other;
if (reference instanceof OSGiServiceState)
- other = (OSGiServiceState) reference;
+ other = (OSGiServiceState)reference;
else if (reference instanceof OSGiServiceReferenceWrapper)
- other = ((OSGiServiceReferenceWrapper) reference).getServiceState();
+ other = ((OSGiServiceReferenceWrapper)reference).getServiceState();
else
throw new IllegalArgumentException(reference + " is not a service reference");
@@ -433,13 +439,13 @@
long otherServiceId = other.getServiceId();
if (thisServiceId == otherServiceId)
return 0;
-
+
int thisRanking = this.getServiceRanking();
int otherRanking = other.getServiceRanking();
int ranking = thisRanking - otherRanking;
if (ranking != 0)
return ranking;
-
+
if (thisServiceId > otherServiceId)
return -1;
else
@@ -451,15 +457,15 @@
{
if (obj == null)
return false;
-
+
OSGiServiceState other;
if (obj instanceof OSGiServiceState)
- other = (OSGiServiceState) obj;
+ other = (OSGiServiceState)obj;
else if (obj instanceof OSGiServiceReferenceWrapper)
- other = ((OSGiServiceReferenceWrapper) obj).getServiceState();
+ other = ((OSGiServiceReferenceWrapper)obj).getServiceState();
else
return false;
- return this == other;
+ return this == other;
}
@Override
@@ -487,10 +493,7 @@
builder.append("id=").append(getServiceId());
builder.append(" bundle=").append(getBundleState().getCanonicalName());
builder.append(" classes=").append(Arrays.asList(getClasses()));
- if (service != null)
- builder.append(" service=").append(service);
- if (serviceFactory != null)
- builder.append(" factory=").append(serviceFactory);
+ builder.append(serviceOrFactory instanceof ServiceFactory ? " factory=" : " service=").append(serviceOrFactory);
if (properties != null)
builder.append(" properties=").append(properties);
if (usingBundles.isEmpty() == false)
@@ -516,28 +519,30 @@
if (usingBundles.isEmpty() == false)
{
for (AbstractBundleState using : usingBundles)
- using.ungetService(this);
+ {
+ if (using.ungetService(this) == false)
+ {
+ if (serviceOrFactory instanceof ServiceFactory)
+ {
+ ServiceFactory serviceFactory = (ServiceFactory)serviceOrFactory;
+ try
+ {
+ Object service = serviceCache.remove(using);
+ serviceFactory.ungetService(using.getBundle(), getRegistration(), service);
+ }
+ catch (Throwable t)
+ {
+ log.warn("Error from ungetService for " + this, t);
+ FrameworkEventsPlugin plugin = bundleState.getBundleManager().getPlugin(FrameworkEventsPlugin.class);
+ plugin.fireFrameworkEvent(bundleState, FrameworkEvent.WARNING, new BundleException("Error using service factory:" + serviceFactory, t));
+ }
+ }
+ }
+ }
}
getBundleState().removeRegisteredService(this);
-
- if (serviceFactory != null && service != null)
- {
- try
- {
- serviceFactory.ungetService(getBundle(), getRegistration(), service);
- }
- catch (Throwable t)
- {
- log.warn("Error from ungetService for " + this, t);
- FrameworkEventsPlugin plugin = bundleState.getBundleManager().getPlugin(FrameworkEventsPlugin.class);
- plugin.fireFrameworkEvent(bundleState, FrameworkEvent.WARNING, new BundleException("Error using service factory:" + serviceFactory, t));
- }
- finally
- {
- service = null;
- }
- }
+ serviceOrFactory = null;
}
/**
@@ -550,7 +555,7 @@
{
if (object == null)
throw new IllegalArgumentException("Null object");
-
+
for (String className : getClasses())
{
try
@@ -558,16 +563,16 @@
Class<?> clazz = getBundleState().loadClass(className);
// [TODO] show classloader information all interfaces for debugging purposes
if (clazz.isInstance(object) == false)
- throw new IllegalArgumentException(object.getClass().getName() + " does not implement " + className);
+ throw new IllegalArgumentException(object.getClass().getName() + " does not implement " + className);
}
catch (ClassNotFoundException e)
{
- throw new IllegalArgumentException(object.getClass().getName() + " cannot load class: " + className, e);
+ throw new IllegalArgumentException(object.getClass().getName() + " cannot load class: " + className, e);
}
}
return object;
}
-
+
/**
* Check whether the caller has permission
*
@@ -579,7 +584,7 @@
SecurityManager sm = System.getSecurityManager();
if (sm == null)
return;
-
+
String[] clazzes = getClasses();
SecurityException se = null;
for (String clazz : clazzes)
@@ -601,10 +606,10 @@
if (se != null)
throw se;
}
-
+
/**
* Check whether the caller has permission
- *
+ *
* @param accessControlContext access control context
* @param action the action to check
* @param all whether all permissions are required
@@ -613,7 +618,7 @@
{
if (System.getSecurityManager() == null)
return;
-
+
String[] clazzes = getClasses();
SecurityException se = null;
for (String clazz : clazzes)
@@ -672,10 +677,10 @@
}
return false;
}
-
+
/**
* Check if the service is unregistered
- *
+ *
* @throws IllegalStateException if unregistered
*/
private void checkUnregistered()
Modified: projects/jboss-osgi/projects/runtime/microcontainer/branches/tdiesler/src/test/java/org/jboss/test/osgi/service/test/GetUnGetServiceUnitTestCase.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/branches/tdiesler/src/test/java/org/jboss/test/osgi/service/test/GetUnGetServiceUnitTestCase.java 2009-08-30 07:54:31 UTC (rev 92995)
+++ projects/jboss-osgi/projects/runtime/microcontainer/branches/tdiesler/src/test/java/org/jboss/test/osgi/service/test/GetUnGetServiceUnitTestCase.java 2009-08-30 07:55:58 UTC (rev 92996)
@@ -152,103 +152,6 @@
}
}
- public void testGetServiceFactory() throws Exception
- {
- String OBJCLASS = BundleContext.class.getName();
-
- Bundle bundle = addBundle("/bundles/simple/", "simple-bundle1");
- try
- {
- bundle.start();
- BundleContext bundleContext = bundle.getBundleContext();
- assertNotNull(bundleContext);
-
- ServiceRegistration registration = bundleContext.registerService(OBJCLASS, new SimpleServiceFactory(bundleContext), null);
- ServiceReference reference = registration.getReference();
-
- Object actual = bundleContext.getService(reference);
- assertEquals(bundleContext, actual);
-
- actual = bundleContext.getService(reference);
- assertEquals(bundleContext, actual);
-
- registration.unregister();
- actual = bundleContext.getService(reference);
- assertNull("" + actual, actual);
- }
- finally
- {
- uninstall(bundle);
- }
- }
-
- public void testGetServiceFactoryAfterStop() throws Exception
- {
- String OBJCLASS = BundleContext.class.getName();
-
- Bundle bundle = addBundle("/bundles/simple/", "simple-bundle1");
- try
- {
- bundle.start();
- BundleContext bundleContext = bundle.getBundleContext();
- assertNotNull(bundleContext);
-
- ServiceRegistration registration = bundleContext.registerService(OBJCLASS, new SimpleServiceFactory(bundleContext), null);
- ServiceReference reference = registration.getReference();
-
- Object actual = bundleContext.getService(reference);
- assertEquals(bundleContext, actual);
-
- bundle.stop();
- try
- {
- bundleContext.getService(reference);
- fail("Should not be here!");
- }
- catch (Throwable t)
- {
- checkThrowable(IllegalStateException.class, t);
- }
- }
- finally
- {
- uninstall(bundle);
- }
- }
-
- public void testGetWrongInterfacesForServiceFactory() throws Exception
- {
- String[] OBJCLASSES = {String.class.getName(), BundleContext.class.getName()};
-
- Bundle bundle = addBundle("/bundles/simple/", "simple-bundle1");
- try
- {
- bundle.start();
- BundleContext bundleContext = bundle.getBundleContext();
- assertNotNull(bundleContext);
-
- bundleContext.addFrameworkListener(this);
-
- ServiceRegistration registration = bundleContext.registerService(String.class.getName(), new SimpleServiceFactory(bundleContext), null);
- ServiceReference reference = registration.getReference();
- Object actual = bundleContext.getService(reference);
- assertNull("" + actual, actual);
-
- assertFrameworkEvent(FrameworkEvent.ERROR, bundle, IllegalArgumentException.class);
-
- registration = bundleContext.registerService(OBJCLASSES, new SimpleServiceFactory(bundleContext), null);
- reference = registration.getReference();
- actual = bundleContext.getService(reference);
- assertNull("" + actual, actual);
-
- assertFrameworkEvent(FrameworkEvent.ERROR, bundle, IllegalArgumentException.class);
- }
- finally
- {
- uninstall(bundle);
- }
- }
-
public void testErrorInGetService() throws Exception
{
Bundle bundle = addBundle("/bundles/simple/", "simple-bundle1");
Modified: projects/jboss-osgi/projects/runtime/microcontainer/branches/tdiesler/src/test/java/org/jboss/test/osgi/service/test/RegisterServiceUnitTestCase.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/branches/tdiesler/src/test/java/org/jboss/test/osgi/service/test/RegisterServiceUnitTestCase.java 2009-08-30 07:54:31 UTC (rev 92995)
+++ projects/jboss-osgi/projects/runtime/microcontainer/branches/tdiesler/src/test/java/org/jboss/test/osgi/service/test/RegisterServiceUnitTestCase.java 2009-08-30 07:55:58 UTC (rev 92996)
@@ -26,9 +26,7 @@
import junit.framework.Test;
-import org.jboss.osgi.plugins.facade.bundle.OSGiBundleWrapper;
import org.jboss.test.osgi.OSGiTestCase;
-import org.jboss.test.osgi.service.support.SimpleServiceFactory;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
@@ -246,67 +244,4 @@
uninstall(bundle);
}
}
-
- public void testRegisterServiceFactory() throws Exception
- {
- Bundle bundleA = addBundle("/bundles/simple/", "simple-bundle1");
- try
- {
- bundleA.start();
- BundleContext contextA = bundleA.getBundleContext();
- assertNotNull(contextA);
-
- SimpleServiceFactory serviceFactory = new SimpleServiceFactory(contextA);
- ServiceRegistration sregA = contextA.registerService(OBJCLASS, serviceFactory, null);
-
- ServiceReference srefA = sregA.getReference();
- Object actual = contextA.getService(srefA);
- assertEquals(contextA, actual);
- assertInstanceOf(serviceFactory.getBundle, OSGiBundleWrapper.class);
- assertEquals(bundleA.getSymbolicName(), serviceFactory.getBundle.getSymbolicName());
- assertEquals(1, serviceFactory.getCount);
-
- srefA = contextA.getServiceReference(OBJCLASS);
- actual = contextA.getService(srefA);
- assertEquals(contextA, actual);
- assertInstanceOf(serviceFactory.getBundle, OSGiBundleWrapper.class);
- assertEquals(bundleA.getSymbolicName(), serviceFactory.getBundle.getSymbolicName());
- assertEquals(1, serviceFactory.getCount);
-
- sregA = contextA.registerService(OBJCLASSES, serviceFactory, null);
- srefA = sregA.getReference();
- actual = contextA.getService(srefA);
- assertEquals(contextA, actual);
- assertInstanceOf(serviceFactory.getBundle, OSGiBundleWrapper.class);
- assertEquals(bundleA.getSymbolicName(), serviceFactory.getBundle.getSymbolicName());
-
- System.out.println("[JBOSGI-144] Framework does not handle ServiceFactory provided services properly");
- //assertEquals(1, serviceFactory.getCount);
-
- Bundle bundleB = addBundle("/bundles/simple/", "simple-bundle2");
- try
- {
- bundleB.start();
- BundleContext contextB = bundleB.getBundleContext();
- assertNotNull(contextB);
-
- ServiceReference srefB = contextB.getServiceReference(OBJCLASS);
- actual = contextB.getService(srefB);
- assertEquals(contextA, actual);
- assertInstanceOf(serviceFactory.getBundle, OSGiBundleWrapper.class);
-
- System.out.println("[JBOSGI-144] Framework does not handle ServiceFactory provided services properly");
- //assertEquals(bundleB.getSymbolicName(), serviceFactory.getBundle.getSymbolicName());
- //assertEquals(2, serviceFactory.getCount);
- }
- finally
- {
- uninstall(bundleB);
- }
- }
- finally
- {
- uninstall(bundleA);
- }
- }
}
Modified: projects/jboss-osgi/projects/runtime/microcontainer/branches/tdiesler/src/test/java/org/jboss/test/osgi/service/test/ServiceRegistrationUnitTestCase.java
===================================================================
--- projects/jboss-osgi/projects/runtime/microcontainer/branches/tdiesler/src/test/java/org/jboss/test/osgi/service/test/ServiceRegistrationUnitTestCase.java 2009-08-30 07:54:31 UTC (rev 92995)
+++ projects/jboss-osgi/projects/runtime/microcontainer/branches/tdiesler/src/test/java/org/jboss/test/osgi/service/test/ServiceRegistrationUnitTestCase.java 2009-08-30 07:55:58 UTC (rev 92996)
@@ -306,7 +306,7 @@
inUse = bundle2.getServicesInUse();
assertNull(inUse);
- assertEquals(bundle, factory.ungetBundle);
+ assertEquals(bundle2, factory.ungetBundle);
assertEquals(registration, factory.ungetRegisation);
assertEquals(bundleContext, factory.ungetService);
}
More information about the jboss-cvs-commits
mailing list