Java源码示例:org.osgi.framework.ServiceFactory
示例1
/**
* DOCUMENT ME!
*
* @param context DOCUMENT ME!
*
* @throws BundleException DOCUMENT ME!
*/
public void start(BundleContext context) throws BundleException {
ensureSecurityManager();
this.bc = context;
try {
O2J = new Osgi2Jini();
O2J.open();
} catch (Exception o2j) {
Debug.printDebugInfo(10, "OSGi to Jini bridge not started", o2j);
}
try {
J2O = new JiniServiceFactory();
jiniServiceFactory = bc.registerService("org.osgi.service.jini.JiniDriver",
(ServiceFactory) J2O, null);
} catch (Exception j2o) {
Debug.printDebugInfo(10, "Jini to OSGi bridge not started", j2o);
}
}
示例2
boolean ungetService(Bundle bundle) {
synchronized (this.useCounters) {
if (this.service == null) {
return false;
}
Integer num = (Integer) this.useCounters.get(bundle);
if (num == null) {
return false;
} else if (num.intValue() == 1) {
this.useCounters.remove(bundle);
if (this.isServiceFactory) {
((ServiceFactory) this.service).ungetService(bundle, this.registration, this.cachedServices.get(bundle));
this.cachedServices.remove(bundle);
}
return false;
} else {
this.useCounters.put(bundle, Integer.valueOf(num.intValue() - 1));
return true;
}
}
}
示例3
S factorService(final Bundle theBundle, boolean count){
@SuppressWarnings("unchecked")
final ServiceFactory<S> factory = (ServiceFactory<S>) service;
final S factoredService;
try {
if(count)
incrementCounter(theBundle);
marker = true;
factoredService = factory.getService(theBundle,
registration);
marker = false;
checkService(factoredService,
(String[]) properties.get(Constants.OBJECTCLASS));
// catch failed check and exceptions thrown in factory
} catch (final IllegalArgumentException iae) {
if(count)
decrementCounter(theBundle);
framework.notifyFrameworkListeners(FrameworkEvent.ERROR,
bundle, new ServiceException(
"Invalid service object",
ServiceException.FACTORY_ERROR));
return null;
} catch (final Throwable t) {
if(count)
decrementCounter(theBundle);
framework.notifyFrameworkListeners(FrameworkEvent.ERROR,
bundle, new ServiceException(
"Exception while factoring the service",
ServiceException.FACTORY_EXCEPTION, t));
return null;
}
return factoredService;
}
示例4
public void ungetService(S s) {
if(isPrototype){
if(services.remove(s)) {
try {
final ServiceFactory<S> factory = (ServiceFactory<S>) service;
factory.ungetService(b, (ServiceRegistration<S>) registration, s);
} catch (final Throwable t) {
framework.notifyFrameworkListeners(
FrameworkEvent.ERROR, b, t);
}
if(services.size() == 0){
if(useCounters.get(b) == 0){
useCounters.remove(b);
}
}
} else {
throw new IllegalArgumentException("Service object was not provided "
+ "by this ServiceObjects instance");
}
} else {
// in case of bundle scope, only unget if this is from the right bundle
if(isServiceFactory) {
if(s != cachedServices.get(b)){
throw new IllegalArgumentException("Service object was not provided "
+ "by this ServiceObjects instance");
}
}
ServiceReferenceImpl.this.ungetService(b);
}
}
示例5
@Override
<S> S callGetService(final ServiceRegistrationImpl<S> sr, final Bundle b)
{
return AccessController.doPrivileged(new PrivilegedAction<S>() {
public S run()
{
@SuppressWarnings("unchecked")
final ServiceFactory<S> srf = (ServiceFactory<S>) sr.service;
return srf.getService(b, sr);
}
});
}
示例6
@Override
<S> void callUngetService(final ServiceRegistrationImpl<S> sr,
final Bundle b,
final S instance)
{
@SuppressWarnings("unchecked")
final ServiceFactory<S> srf = (ServiceFactory<S>) sr.service;
AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run()
{
srf.ungetService(b, sr, instance);
return null;
}
});
}
示例7
<S> void callUngetService(final ServiceRegistrationImpl<S> sr,
final Bundle b,
final S instance)
{
@SuppressWarnings("unchecked")
final ServiceFactory<S> srf = (ServiceFactory<S>)sr.service;
srf.ungetService(b, sr, instance);
}
示例8
/**
* Registers the specified service factory object with the specified
* properties under the name of the specified class with the Framework.
*
* @see org.osgi.framework.BundleContext#registerService
*/
public <S> ServiceRegistration<S> registerService(Class<S> clazz,
ServiceFactory<S> factory,
Dictionary<String, ?> properties)
{
@SuppressWarnings("unchecked")
final ServiceRegistration<S> res = (ServiceRegistration<S>)
registerService(clazz == null ? null
: clazz.getName(),factory,properties);
return res;
}
示例9
ServiceReferenceImpl(Bundle bundle, Object obj, Dictionary<String, ?> dictionary, String[] strArr) {
this.useCounters = new HashMap(0);
this.cachedServices = null;
if (obj instanceof ServiceFactory) {
this.isServiceFactory = true;
} else {
this.isServiceFactory = false;
checkService(obj, strArr);
}
this.bundle = bundle;
this.service = obj;
this.properties = dictionary == null ? new Hashtable() : new Hashtable(dictionary.size());
if (dictionary != null) {
Enumeration keys = dictionary.keys();
while (keys.hasMoreElements()) {
String str = (String) keys.nextElement();
this.properties.put(str, dictionary.get(str));
}
}
this.properties.put(Constants.OBJECTCLASS, strArr);
Dictionary dictionary2 = this.properties;
String str2 = Constants.SERVICE_ID;
long j = nextServiceID + 1;
nextServiceID = j;
dictionary2.put(str2, Long.valueOf(j));
Integer num = dictionary == null ? null : (Integer) dictionary.get(Constants.SERVICE_RANKING);
this.properties.put(Constants.SERVICE_RANKING, Integer.valueOf(num == null ? 0 : num.intValue()));
this.registration = new ServiceRegistrationImpl();
}
示例10
@Override
public <S> ServiceRegistration<S> registerService(Class<S> type, ServiceFactory<S> sf, Dictionary<String, ?> dctnr) {
return delegate.registerService(type, sf, dctnr);
}
示例11
@Override
public <S> ServiceRegistration<S> registerService(Class<S> aClass, ServiceFactory<S> serviceFactory,
Dictionary<String, ?> dictionary) {
throw new UnsupportedOperationException();
}
示例12
protected ServiceRegistration<Application> registerApplication(
ServiceFactory<Application> serviceFactory, Object... keyValues) {
Dictionary<String, Object> properties = new Hashtable<>();
properties.put(JAX_RS_APPLICATION_BASE, "/test-application");
for (int i = 0; i < keyValues.length; i = i + 2) {
properties.put(keyValues[i].toString(), keyValues[i + 1]);
}
ServiceRegistration<Application> serviceRegistration =
bundleContext.registerService(
Application.class, serviceFactory, properties);
_registrations.add(serviceRegistration);
return serviceRegistration;
}
示例13
protected ServiceRegistration<Application> registerApplication(
ServiceFactory<Application> serviceFactory, Object... keyValues) {
Dictionary<String, Object> properties = new Hashtable<>();
properties.put(JAX_RS_APPLICATION_BASE, "/test-application");
for (int i = 0; i < keyValues.length; i = i + 2) {
properties.put(keyValues[i].toString(), keyValues[i + 1]);
}
ServiceRegistration<Application> serviceRegistration =
bundleContext.registerService(
Application.class, serviceFactory, properties);
_registrations.add(serviceRegistration);
return serviceRegistration;
}
示例14
protected ServiceRegistration<Application> registerApplication(
ServiceFactory<Application> serviceFactory, Object... keyValues) {
Dictionary<String, Object> properties = new Hashtable<>();
properties.put(JAX_RS_APPLICATION_BASE, "/test-application");
for (int i = 0; i < keyValues.length; i = i + 2) {
properties.put(keyValues[i].toString(), keyValues[i + 1]);
}
ServiceRegistration<Application> serviceRegistration =
bundleContext.registerService(
Application.class, serviceFactory, properties);
_registrations.add(serviceRegistration);
return serviceRegistration;
}
示例15
protected ServiceRegistration<Application> registerApplication(
ServiceFactory<Application> serviceFactory, Object... keyValues) {
Dictionary<String, Object> properties = new Hashtable<>();
properties.put(JAX_RS_APPLICATION_BASE, "/test-application");
for (int i = 0; i < keyValues.length; i = i + 2) {
properties.put(keyValues[i].toString(), keyValues[i + 1]);
}
ServiceRegistration<Application> serviceRegistration =
bundleContext.registerService(
Application.class, serviceFactory, properties);
_registrations.add(serviceRegistration);
return serviceRegistration;
}
示例16
protected ServiceRegistration<Application> registerApplication(
ServiceFactory<Application> serviceFactory, Object... keyValues) {
Dictionary<String, Object> properties = new Hashtable<>();
properties.put(JAX_RS_APPLICATION_BASE, "/test-application");
for (int i = 0; i < keyValues.length; i = i + 2) {
properties.put(keyValues[i].toString(), keyValues[i + 1]);
}
ServiceRegistration<Application> serviceRegistration =
bundleContext.registerService(
Application.class, serviceFactory, properties);
_registrations.add(serviceRegistration);
return serviceRegistration;
}
示例17
/**
* create a new service reference implementation instance.
*
* @param bundle
* the bundle.
* @param service
* the service object.
* @param props
* the service properties.
* @param clazzes
* the interface classes that the service is registered under.
* @throws ClassNotFoundException
*/
ServiceReferenceImpl(final Concierge framework, final Bundle bundle,
final S service, final Dictionary<String, ?> props,
final String[] clazzes) {
String scope = "singleton";
if (service instanceof PrototypeServiceFactory) {
isServiceFactory = true;
isPrototype = true;
scope = "prototype";
} else if(service instanceof ServiceFactory) {
isServiceFactory = true;
isPrototype = false;
scope = "bundle";
} else {
isServiceFactory = false;
isPrototype = false;
checkService(service, clazzes);
}
this.framework = framework;
this.bundle = bundle;
this.service = service;
this.properties = new HashMap<String, Object>(props == null ? 5
: props.size() + 5);
if (props != null) {
for (final Enumeration<String> keys = props.keys(); keys
.hasMoreElements();) {
final String key = keys.nextElement();
properties.put(key, props.get(key));
}
}
properties.put(Constants.OBJECTCLASS, clazzes);
properties.put(Constants.SERVICE_BUNDLEID, bundle.getBundleId());
properties.put(Constants.SERVICE_ID, new Long(++nextServiceID));
final Integer ranking = props == null ? null : (Integer) props
.get(Constants.SERVICE_RANKING);
properties.put(Constants.SERVICE_RANKING,
ranking == null ? new Integer(0) : ranking);
properties.put(Constants.SERVICE_SCOPE, scope);
this.registration = new ServiceRegistrationImpl();
}
示例18
/**
* unget the service.
*
* @param theBundle
* the using bundle.
* @return <tt>false</tt> if the context bundle's use count for the service
* is zero or if the service has been unregistered; <tt>true</tt>
* otherwise.
*/
@SuppressWarnings("unchecked")
boolean ungetService(final Bundle theBundle) {
synchronized (useCounters) {
if (service == null) {
return false;
}
Integer counter = useCounters.get(theBundle);
if (counter == null) {
return false;
}
if (counter.intValue() == 1) {
if (isServiceFactory) {
try {
((ServiceFactory<S>) service).ungetService(theBundle,
registration, cachedServices.get(theBundle));
// catch exceptions thrown in factory
} catch (final Throwable t) {
framework.notifyFrameworkListeners(
FrameworkEvent.ERROR, bundle, t);
}
cachedServices.remove(theBundle);
}
if(isPrototype){
// check if there is a serviceobjects that could also have used service instances
if(serviceObjects != null){
ServiceObjectsImpl so = serviceObjects.get(theBundle);
if(so != null){
if(so.services.size() != 0){
useCounters.put(theBundle, new Integer(0));
return true;
}
}
}
}
useCounters.remove(theBundle);
return true;
} else if(counter.intValue()== 0){
// prototype services in use don't count here...
return false;
} else {
counter = new Integer(counter.intValue() - 1);
useCounters.put(theBundle, counter);
return true;
}
}
}
示例19
@SuppressWarnings("unchecked")
public <S> ServiceRegistration<S> registerService(final Class<S> clazz,
final ServiceFactory<S> factory, final Dictionary<String, ?> properties) {
return (ServiceRegistration<S>) registerService(clazz.getName(),
factory, properties);
}
示例20
public boolean isAssignableTo(Bundle bundle, String className) {
final BundleImpl sBundle = registration.bundle;
if (sBundle == null) {
throw new IllegalStateException("Service is unregistered");
}
final FrameworkContext fwCtx = sBundle.fwCtx;
if (((BundleImpl)bundle).fwCtx != fwCtx) {
throw new IllegalArgumentException("Bundle is not from same framework as service");
}
// Check if bootdelegated
if (fwCtx.isBootDelegated(className)) {
return true;
}
final int pos = className.lastIndexOf('.');
if (pos != -1) {
final String name = className.substring(0, pos);
final Pkg p = fwCtx.resolver.getPkg(name);
// Is package exported by a bundle
if (p != null) {
final BundlePackages rbp = sBundle.current().bpkgs;
final BundlePackages pkgExporter = rbp.getProviderBundlePackages(name);
List<BundleGeneration> pkgProvider;
if (pkgExporter == null) {
// Package not imported by provide, is it required
pkgProvider = rbp.getRequiredBundleGenerations(name);
} else {
pkgProvider = new ArrayList<BundleGeneration>(1);
pkgProvider.add(pkgExporter.bg);
}
final BundlePackages bb = ((BundleImpl)bundle).current().bpkgs;
final BundlePackages bbp = bb.getProviderBundlePackages(name);
List<BundleGeneration> pkgConsumer;
if (bbp == null) {
// Package not imported by bundle, is it required
pkgConsumer = bb.getRequiredBundleGenerations(name);
} else {
pkgConsumer = new ArrayList<BundleGeneration>(1);
pkgConsumer.add(bbp.bg);
}
if (pkgConsumer == null) {
// NYI! Check dynamic import?
if (bb.isExported(name)) {
// If bundle only exports package, then return true if
// bundle is provider.
return pkgProvider!=null ? pkgProvider.contains(bb.bg) : true;
} else {
// If bundle doesn't import or export package, then return true and
// assume that the bundle only uses reflection to access service.
return true;
}
} else if (pkgProvider == null) {
// Package not imported by registrar. E.g. proxy registration.
final Object sService = registration.service;
if (sService == null) {
throw new IllegalStateException("Service is unregistered");
}
if (sService instanceof ServiceFactory) {
// Factory, allow.
return true;
} else {
// Use the classloader of bundle to load the class, then check
// if the service's class is assignable.
final ClassLoader bCL = bb.getClassLoader();
if (bCL!=null) {
try {
final Class<?> bCls = bCL.loadClass(className);
// NYI, Handle Service Factories.
return bCls.isAssignableFrom(sService.getClass());
} catch (final Exception e) {
// If we can not load, assume that we are just a proxy.
return true;
}
}
}
// Fallback: Always OK when singleton provider of the package
} else { // Package imported by both parties
// Return true if we have same provider as service.
for (final Object element : pkgProvider) {
if (pkgConsumer.contains(element)) {
return true;
}
}
}
} else {
// Not a package under package control. System package?
if (name.startsWith("java.") || sBundle == bundle) {
return true;
} else {
// NYI! We have a private service, check if bundle can use it.
// Now, allow it to handle reflection of service.
return true;
}
}
}
return false;
}
示例21
/**
* Register a service in the framework wide register.
*
* @param bundle The bundle registering the service.
* @param classes The class names under which the service can be located.
* @param service The service object.
* @param properties The properties for this service.
* @return A {@link ServiceRegistration} object.
* @exception java.lang.IllegalArgumentException If one of the following is true:
* <ul>
* <li>The service object is null.</li>
* <li>The defining class of the service parameter is not owned by the bundle.</li>
* <li>The service parameter is not a ServiceFactory and is not an
* instance of all the named classes in the classes parameter.</li>
* </ul>
*/
@SuppressWarnings("deprecation")
ServiceRegistration<?> register(BundleImpl bundle,
String[] classes,
Object service,
Dictionary<String, ?> properties)
{
if (service == null) {
throw new IllegalArgumentException("Can't register null as a service");
}
String scope;
if (service instanceof ServiceFactory) {
scope = service instanceof PrototypeServiceFactory ? Constants.SCOPE_PROTOTYPE : Constants.SCOPE_BUNDLE;
} else {
scope = Constants.SCOPE_SINGLETON;
}
// Check if service implements claimed classes and that they exist.
for (final String cls : classes) {
if (cls == null) {
throw new IllegalArgumentException("Can't register as null class");
}
secure.checkRegisterServicePerm(cls);
if (bundle.id != 0) {
if (cls.equals(org.osgi.service.packageadmin.PackageAdmin.class.getName())) {
throw new IllegalArgumentException
("Registeration of a PackageAdmin service is not allowed");
}
if (cls.equals(PermissionAdmin.class.getName())) {
throw new IllegalArgumentException
("Registeration of a PermissionAdmin service is not allowed");
}
if (cls.equals(ConditionalPermissionAdmin.class.getName())) {
throw new IllegalArgumentException
("Registeration of a ConditionalPermissionAdmin service is not allowed");
}
}
if (scope == Constants.SCOPE_SINGLETON) {
if (!checkServiceClass(service, cls)) {
throw new IllegalArgumentException
("Service object is not an instance of " + cls);
}
}
}
@SuppressWarnings("rawtypes")
final ServiceRegistrationImpl<?> res =
new ServiceRegistrationImpl(bundle, service, scope,
new PropertiesDictionary(properties, classes, null, new Long(bundle.id), scope));
synchronized (this) {
services.put(res, classes);
for (final String clazz : classes) {
List<ServiceRegistrationImpl<?>> s
= classServices.get(clazz);
if (s == null) {
s = new ArrayList<ServiceRegistrationImpl<?>>(1);
classServices.put(clazz, s);
}
final int ip = Math.abs(Util.binarySearch(s, sComp, res) + 1);
s.add(ip, res);
}
}
final ServiceReference<?> r = res.getReference();
bundle.fwCtx.perm
.callServiceChanged(bundle.fwCtx,
bundle.fwCtx.listeners.getMatchingServiceListeners(r),
new ServiceEvent(ServiceEvent.REGISTERED, r),
null);
return res;
}
示例22
<S> S callGetService(final ServiceRegistrationImpl<S> sr, final Bundle b) {
@SuppressWarnings("unchecked")
final ServiceFactory<S> srf = (ServiceFactory<S>)sr.service;
return srf.getService(b, sr);
}