org.osgi.framework.PrototypeServiceFactory Java Examples

The following examples show how to use org.osgi.framework.PrototypeServiceFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ServiceReferenceImpl.java    From concierge with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 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();
}
 
Example #2
Source File: Services.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * 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;
}