Java源码示例:org.osgi.service.cm.ManagedService

示例1
public void start(BundleContext bc)
{
  TelnetServer.bc = bc;

  log = new LogRef(bc, true);

  telnetSessions = new Hashtable<TelnetSession, Thread>();

  final Dictionary<String, String> conf = new Hashtable<String, String>();
  try {
    telnetConfig = new TelnetConfig(bc);
    conf.put(Constants.SERVICE_PID, getClass().getName());

    configServReg =
      bc.registerService(ManagedService.class, this, conf);
  } catch (final ConfigurationException cexp) {
    log.error("Consoletelnet configuration error " + cexp.toString());
  }

  telnetServerThread = new Thread(this, "ConsoleTelnetServer");
  telnetServerThread.setDaemon(true);
  telnetServerThread.start();
}
 
示例2
public void start(BundleContext bc) throws Exception {
  this.bc = bc;

  log(LogService.LOG_INFO, "Starting");

  // Get config
  Dictionary<String,String> p = new Hashtable<String,String>();
  p.put(Constants.SERVICE_PID, getClass().getName());
  bc.registerService(ManagedService.class, this, p);

  inStream  = new SystemIn(bc);
  outStream = System.out;
  errStream = System.err;

  cmdProcTracker = new ServiceTracker(bc, CommandProcessor.class, this);
  cmdProcTracker.open();

  logTracker = new ServiceTracker(bc, LogService.class, null);
  logTracker.open();

}
 
示例3
public void doUpdate(PluginManager pm)
    throws ConfigurationException
{
  if (sr == null) {
    return;
  }
  Object targetService = getTargetService();
  if (targetService == null) {
    return;
  }
  processedConfiguration = pm.callPluginsAndCreateACopy(sr, configuration);
  if (factoryPid == null) {
    update((ManagedService) targetService);
  } else {
    update((ManagedServiceFactory) targetService);
  }
}
 
示例4
private void serviceChanged(ServiceReference<?> sr,
                            int eventType,
                            String objectClass)
{
  if (ManagedServiceFactory.class.getName().equals(objectClass)) {
    @SuppressWarnings("unchecked")
    final ServiceReference<ManagedServiceFactory> srF =
      (ServiceReference<ManagedServiceFactory>) sr;
    managedServiceFactoryChanged(srF, eventType);
  } else if (ManagedService.class.getName().equals(objectClass)) {
    @SuppressWarnings("unchecked")
    final ServiceReference<ManagedService> srM =
      (ServiceReference<ManagedService>) sr;
    managedServiceChanged(srM, eventType);
  } else if (ConfigurationPlugin.class.getName().equals(objectClass)) {
    @SuppressWarnings("unchecked")
    final ServiceReference<ConfigurationPlugin> srC =
      (ServiceReference<ConfigurationPlugin>) sr;
    pluginManager.configurationPluginChanged(srC, eventType);
  }
}
 
示例5
/**
 * Called when this bundle is started by OSGi Framework.
 * Determines underlying platform and loads the relevant service
 * which implements <code>GPIO</code> interface.
 */
public void start(BundleContext context) throws Exception {

    /* Linux user space GPIO implementation, "sysfs" based */
    if (System.getProperty("os.name").toLowerCase().startsWith("linux")) {

        GPIOLinux gpioLinux = new GPIOLinux();

        Dictionary<String, String> properties = new Hashtable<String, String>();
        properties.put("service.pid", "org.openhab.gpio");

        context.registerService(GPIO.class, gpioLinux, null);
        context.registerService(ManagedService.class, gpioLinux, properties);
    } else {
        /*
         * Throwing exception is not implemented because it's causing Equinox to constantly trying to start the
         * bundle
         */
        logger.error("No supported operating system was found, GPIO service won't be available");
    }
}
 
示例6
/**
 * Called by the framework when this bundle is started.
 *
 * @param bc
 *          Bundle context.
 */
@Override
public void start(BundleContext bc)
{
  this.bc = bc;

  // Get config
  final Dictionary<String,Object> p = new Hashtable<String,Object>();
  p.put(Constants.SERVICE_PID, getClass().getName());
  bc.registerService(ManagedService.class.getName(), this, p);
}
 
示例7
private void extractMetaTypeInformation(ServiceReference<?> sr)
{
  Object s = null;
  try {
    s = bc.getService(sr);
    if (!(s instanceof MetaTypeProvider)) {
      return;
    }
    final MetaTypeProvider mtp = (MetaTypeProvider) s;

    final List<String> pidProps = new ArrayList<String>();
    final List<String> fpidProps = new ArrayList<String>();
    if(s instanceof ManagedService){
      pidProps.add(Constants.SERVICE_PID);
      pidProps.add(MetaTypeProvider.METATYPE_PID);
      fpidProps.add(MetaTypeProvider.METATYPE_FACTORY_PID);
    } else if (s instanceof ManagedServiceFactory) {
      pidProps.add(MetaTypeProvider.METATYPE_PID);
      fpidProps.add(Constants.SERVICE_PID);
      fpidProps.add(MetaTypeProvider.METATYPE_FACTORY_PID);
    } else {
      // MetaTypeProvide service
      pidProps.add(MetaTypeProvider.METATYPE_PID);
      fpidProps.add(MetaTypeProvider.METATYPE_FACTORY_PID);
    }
    addSrPropertiesToListAndMapIds(sr, pidProps, pids, mtp);
    addSrPropertiesToListAndMapIds(sr, fpidProps, factoryPids, mtp);

    final String[] ls = mtp.getLocales();
    if (ls != null && ls.length > 0) {
      locales.addAll(Arrays.asList(ls));
    }
  } finally {
    if (s != null) {
      bc.ungetService(sr);
    }
  }
}
 
示例8
private void update(ManagedService targetService)
    throws ConfigurationException
{
  if (targetService == null) {
    return;
  }
  targetService.updated(processedConfiguration);
}
 
示例9
public ConfigurationAdminFactory(File storeDir)
{
  storeDir.mkdirs();
  try {
    this.store = new ConfigurationStore(storeDir);
  } catch (final Exception e) {
    Activator.log.error("Error while initializing configurations store", e);
  }

  pluginManager = new PluginManager();

  listenerEventQueue = new ListenerEventQueue(Activator.bc);

  configurationDispatcher = new ConfigurationDispatcher(pluginManager);

  lookForExisitingBundleLocations();

  final String filter =
    "(|(objectClass=" + ManagedServiceFactory.class.getName() + ")"
        + "(objectClass=" + ManagedService.class.getName() + ")"
        + "(objectClass=" + ConfigurationPlugin.class.getName() + "))";
  try {
    Activator.bc.addServiceListener(this, filter);
    Activator.bc.addBundleListener(this);
  } catch (final InvalidSyntaxException ignored) {
  }

  lookForAlreadyRegisteredServices();
}
 
示例10
private void updateManagedServices(Collection<ServiceReference<ManagedService>> srs,
                                   String servicePid,
                                   String boundLocation)
{
  final Collection<ServiceReference<ManagedService>> filtered =
    filterOnMatchingLocations(srs, boundLocation);
  for (final ServiceReference<ManagedService> sr : filtered) {
    configurationDispatcher.dispatchUpdateFor(sr, servicePid, null, null);
  }
}
 
示例11
/**
 * Update managed services for a deleted targeted PID. This will switch to
 * another, less specific, targeted configuration if one exists.
 *
 * @param targetedPid
 *          the PID of the deleted configuration.
 * @param oldBundleLocation
 *          the location that the deleted configuration was bound to.
 * @param pid
 *          the PID of the deleted configuration without target specification.
 * @param srs
 *          collection of matching (on {@code pid}) managed services.
 * @throws IOException
 */
private void updateManagedServicesForDeletedTargetedPID(final String targetedPid,
                                                        final String oldBundleLocation,
                                                        final String pid,
                                                        final Collection<ServiceReference<ManagedService>> srs)
    throws IOException
{
  {
    final Collection<ServiceReference<ManagedService>> filtered =
      filterOnMatchingLocations(srs, oldBundleLocation);
    for (final ServiceReference<ManagedService> sr : filtered) {
      if (targetedPidMatches(targetedPid, sr.getBundle())) {
        // The target specification in the PID of the deleted configuration
        // matches the bundle owning the current MS, find the currently best
        // matching configuration if any.
        ConfigurationDictionary srCd = load(pid, sr.getBundle());
        if (srCd != null) {
          // Found matching configuration, is this a change of configuration?
          if (targetedPid.length() > srCd.getPid().length()) {
            // The deleted PID was a better match, must update MS with srCd.
            srCd = bindLocationIfNecessary(sr, srCd);
            final String newLocation = srCd.getLocation();
            if (!locationsMatch(sr.getBundle(), newLocation)) {
              // Multi-location region did not match, skip.
              srCd = null;
            }
          } else {
            srCd = null;
          }
        }
        configurationDispatcher.dispatchUpdateFor(sr, pid, null, srCd);
      }
    }
  }
}
 
示例12
private void updateManagedService(ServiceReference<ManagedService> sr)
    throws IOException
{
  // Newly registered managed service; all PIDs are added.
  final ChangedPids cps = new ChangedPids();
  cps.added.addAll(Arrays.asList(getPids(sr)));
  updateManagedService(sr, cps);
}
 
示例13
void register()
{
  if (reg != null) {
    return;
  }

  final Dictionary<String, Object> props = new Hashtable<String, Object>();
  props.put("service.pid", PID);

  reg = Activator.bc.registerService(ManagedService.class, this, props);

}
 
示例14
void register()
{
  if (reg != null) {
    return;
  }

  final Dictionary<String, Object> props = new Hashtable<String, Object>();
  props.put("service.pid", PID);

  reg = Activator.bc.registerService(ManagedService.class, this, props);

}
 
示例15
private void registerWithConfigAdmin ()
{
    logger.info ( "Register with config admin wrapper" );

    this.adapter = new StorageManagerAdapter ( this.context );

    final Dictionary<String, Object> properties = new Hashtable<> ();
    properties.put ( Constants.SERVICE_PID, "drone.storage.manager" );
    this.adapterHandle = this.context.registerService ( ManagedService.class, this.adapter, properties );
}
 
示例16
@SuppressWarnings("unchecked")
@Override
public void start(BundleContext context) throws Exception {
	cassandra = new OsgiEmbeddedCassandra(context);

	@SuppressWarnings("rawtypes")
	Dictionary props = new Hashtable();
	props.put("service.pid", "de.nierbeck.cassandra.embedded");
	cassandraService = context.registerService(new String[] {
					ManagedService.class.getName(),
					CassandraService.class.getName() },
			cassandra, props);

}
 
示例17
@Override
public void start(BundleContext context) throws Exception {
    conduitConfigurer = new ConduitConfigurer(context);
    conduitConfigurer.open();
    Dictionary<String, Object> properties = new Hashtable<>();
    properties.put(Constants.SERVICE_PID, "org.apache.cxf.transport.http.async");
    context.registerService(ManagedService.class.getName(), conduitConfigurer, properties);
}
 
示例18
@Override
public HttpService addingService(ServiceReference<HttpService> reference) {
    HttpService httpService = context.getService(reference);
    Servlet servlet = new CXFNonSpringServlet(destinationRegistry, false);
    servletExporter = new ServletExporter(servlet, httpService);
    servletPublisherReg = context.registerService(ManagedService.class,
                                                  servletExporter,
                                                  CollectionUtils.singletonDictionary(Constants.SERVICE_PID,
                                                                                      CXF_CONFIG_PID));
    return httpService;
}
 
示例19
synchronized void start() {
  dir = initDir((String) configCollection.get(DIR));
  final String[] clazzes = new String[]{ManagedService.class.getName(),
      LogConfig.class.getName()};
  bc.registerService(clazzes, this, configCollection);
}
 
示例20
private void lookForAlreadyRegisteredServices()
{
  lookForAlreadyRegisteredServices(ConfigurationPlugin.class);
  lookForAlreadyRegisteredServices(ManagedServiceFactory.class);
  lookForAlreadyRegisteredServices(ManagedService.class);
}
 
示例21
private void updateManagedServicesMatching(final String targetedPid,
                                           final String oldBundleLocation)
    throws IOException
{
  boolean isTargetedPID = false;
  String pid = targetedPid;
  Collection<ServiceReference<ManagedService>> srs =
    getServiceReferencesWithPid(ManagedService.class, pid);

  if (srs.isEmpty()) {
    // No MS with the given PID, try to handle it as a targetedPID:
    final int barPos = targetedPid.indexOf('|');
    isTargetedPID = barPos > 0; // At least one char in the PID.
    if (isTargetedPID) {
      pid = targetedPid.substring(0, barPos);
      srs = getServiceReferencesWithPid(ManagedService.class, pid);
    }
  }

  if (srs.isEmpty()) {
    // No managed services registered for this PID.
    return;
  }

  // Note: We can not filter the set of MSs based on the target specification
  // in the PID since we must select the most specific matching targeted
  // configuration for each MS. Thus it may be that we need to update other
  // MSs with this PID than those that does match on the current target
  // specification.

  final boolean isDeleted = null == load(targetedPid, null);
  if (isDeleted) {
    if (!isTargetedPID) {
      // A non-targeted configuration has been deleted, no other configuration
      // will be available for any matching managed service!
      updateManagedServices(srs, pid, oldBundleLocation);
    } else {
      updateManagedServicesForDeletedTargetedPID(targetedPid,
                                                 oldBundleLocation, pid, srs);
    }
  } else {
    // New or updated configuration
    final ServiceReference<ManagedService> bestSr = srs.iterator().next();
    final ConfigurationDictionary cd = load(pid, bestSr.getBundle());
    final ConfigurationDictionary bound = bindLocationIfNecessary(bestSr, cd);
    final String newBundleLocation = bound.getLocation();

    final Collection<ServiceReference<ManagedService>> filtered =
      filterOnMatchingLocations(srs, newBundleLocation);
    for (final ServiceReference<ManagedService> sr : filtered) {
      ConfigurationDictionary srCd = bound;
      if (newBundleLocation.charAt(0) == '?') {
        // There may be another configuration for this SR when multi-locations
        // are in use!
        srCd = load(pid, sr.getBundle());
        if (!targetedPid.equals(srCd.getPid())) {
          // This MS uses another targeted configuration than the changed one,
          // thus no update call.
          continue;
        }
      }
      configurationDispatcher.dispatchUpdateFor(sr, pid, null, srCd);
    }
  }
}
 
示例22
@Override
public void start(final BundleContext bundleContext) {
    Dictionary<String, String> properties = new Hashtable<>();
    properties.put(Constants.SERVICE_PID, CONFIG_PID);
    bundleContext.registerService(ManagedService.class.getName(), new ConfigUpdater(bundleContext), properties);
}