Java Code Examples for org.osgi.framework.Constants#SCOPE_BUNDLE

The following examples show how to use org.osgi.framework.Constants#SCOPE_BUNDLE . 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: ServiceRegistrationImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Construct a ServiceRegistration for a registered service.
 *
 * @param b Bundle providing service.
 * @param s Service object.
 * @param props Properties describing service.
 */
ServiceRegistrationImpl(BundleImpl b, Object s, String ss, PropertiesDictionary props) {
  fwCtx = b.fwCtx;
  bundle = b;
  service = s;
  properties = props;
  reference = new ServiceReferenceImpl<S>(this);
  available = true;
  scope = ss;
  if (scope == Constants.SCOPE_BUNDLE) {
    serviceInstances = new HashMap<Bundle, S>();
  } else if (scope == Constants.SCOPE_PROTOTYPE) {
    serviceInstances = new HashMap<Bundle, S>();
    prototypeServiceInstances = new HashMap<Bundle, List<S>>();
  }
}
 
Example 2
Source File: ComponentDescription.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void parseService(XmlPullParser p)
    throws IOException, XmlPullParserException
{
  if (services != null) {
    throw new IllegalXMLException(
                                  "More than one service-tag in component: \""
                                      + componentName + "\"", p);
  }
  ArrayList<String> sl = new ArrayList<String>();
  if (!immediateSet) {
    immediate = false;
  }
  /* If there is an attribute in the service tag */
  for (int i = 0; i < p.getAttributeCount(); i++) {
    if (scrNSminor < 3 && p.getAttributeName(i).equals("servicefactory")) {
      isServiceFactory = parseBoolean(p, i);
      if (isServiceFactory) {
        if (factory != null) {
          throw new IllegalXMLException("Attribute servicefactory in service-tag "
                                        + "cannot be set to \"true\" when component "
                                        + "is a factory component", p);
        }
        if (immediate) {
          throw new IllegalXMLException("Attribute servicefactory in service-tag "
                                        + "cannot be set to \"true\" when component "
                                        + "is an immediate component", p);
        }
        scope = Constants.SCOPE_BUNDLE;
      }
    } else if (scrNSminor > 2 && p.getAttributeName(i).equals("scope")) {
      scope = p.getAttributeValue(i);
      if (!Constants.SCOPE_SINGLETON.equals(scope)) {
        isServiceFactory = true;
        if (!Constants.SCOPE_BUNDLE.equals(scope) && !Constants.SCOPE_PROTOTYPE.equals(scope)) {
          throw new IllegalXMLException("Attribute scope in service-tag must be set to "
                                        + "\"bundle\", \"prototype\" or \"singleton\"", p);
        }
        if (factory != null) {
          throw new IllegalXMLException("Attribute scope in service-tag must be "
                                        + "set to \"singleton\" when component "
                                        + "is a factory component", p);
        }
        if (immediate) {
          throw new IllegalXMLException(
                                        "Attribute scope in service-tag must be "
                                        + "set to \"singleton\" when component "
                                        + "is an immediate component", p);
        }
      }
    } else {
      unrecognizedAttr(p, i);
    }
  }
  int event = p.next();
  while (event != XmlPullParser.END_TAG) {
    if (event != XmlPullParser.START_TAG) {
      event = p.next();
      continue;
    }
    if ("provide".equals(p.getName())) {
      String interfaceName = null;
      for (int i = 0; i < p.getAttributeCount(); i++) {
        if (p.getAttributeName(i).equals("interface")) {
          interfaceName = p.getAttributeValue(i);
        } else {
          throw new IllegalXMLException("Unrecognized attribute \""
                                        + p.getAttributeName(i)
                                        + "\" in provide-tag", p);
        }
      }
      if (interfaceName == null) {
        missingAttr(p, "interface");
      }
      sl.add(interfaceName);
    }
    skip(p);
    event = p.getEventType();
  }
  p.next();
  /* check if required attributes has been set */
  if (sl.isEmpty()) {
    throw new IllegalXMLException("Service-tag did not contain a proper provide-tag",
                                  p);
  }
  services = sl.toArray(new String[sl.size()]);
}
 
Example 3
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;
}