Java源码示例:org.osgi.service.component.ComponentConstants

示例1
/**
 *
 */
PropertyDictionary(Component comp,
                   Map<String,Object> cm,
                   Dictionary<String,Object> instance,
                   boolean service) {
  props = new Hashtable<String,Object>();
  final ComponentDescription cd = comp.compDesc;
  addDict(cd.getProperties(), service);
  if (cm != null) {
    addDict(cm, service);
  }
  if (instance != null) {
    addDict(instance, service);
  }
  props.put(ComponentConstants.COMPONENT_ID, comp.id);
  props.put(ComponentConstants.COMPONENT_NAME, cd.getName());
}
 
示例2
/**
 * Disable component. Dispose of all ComponentConfigurations and
 * stop listening for constraint changes.
 */
void disable(int reason, Deferred<Void> d) {
  Activator.logInfo(bc, "Disable " + toString());
  synchronized (lock) {
     final boolean dispose =  reason == ComponentConstants.DEACTIVATION_REASON_DISPOSED ||
       reason == ComponentConstants.DEACTIVATION_REASON_BUNDLE_STOPPED;
    if (d != null || isEnabled()) {
      state = dispose ? STATE_DISPOSING : STATE_DISABLING;
      disposeComponentConfigs(reason);
      untrackConstraints();
      refs = null;
      id = Long.valueOf(-1);
      state = dispose ? STATE_DISPOSED : STATE_DISABLED;
    } else if (dispose && state == STATE_DISABLED) {
      state = STATE_DISPOSED;
    }
  }
  if (d != null) {
    d.resolve(null);
  }
}
 
示例3
/**
 * Process bundle components when it starts.
 */
public void bundleChanged(BundleEvent event) {
  postponeCheckin();
  try {
    final Bundle bundle = event.getBundle();

    switch (event.getType()) {
    case BundleEvent.LAZY_ACTIVATION:
      lazy.add(bundle);
      processBundle(bundle);
      break;
    case BundleEvent.STARTED:
      if (!lazy.remove(bundle)) {
        processBundle(bundle);
      }
      break;
    case BundleEvent.STOPPING:
      lazy.remove(bundle);
      removeBundle(bundle, ComponentConstants.DEACTIVATION_REASON_BUNDLE_STOPPED);
      break;
    }
  } finally {
    postponeCheckout();
  }
}
 
示例4
protected void modified(Map<String, Object> properties) {
    for (Entry<String, Object> entry : properties.entrySet()) {
        if (Constants.SERVICE_PID.equals(entry.getKey()) || ComponentConstants.COMPONENT_ID.equals(entry.getKey())
                || ComponentConstants.COMPONENT_NAME.equals(entry.getKey())) {
            continue;
        }
        String poolName = entry.getKey();
        Object config = entry.getValue();
        if (config == null) {
            configs.remove(poolName);
        }
        if (config instanceof String) {
            try {
                Integer poolSize = Integer.valueOf((String) config);
                configs.put(poolName, poolSize);
                ThreadPoolExecutor pool = (ThreadPoolExecutor) pools.get(poolName);
                if (pool instanceof ScheduledThreadPoolExecutor) {
                    pool.setCorePoolSize(poolSize);
                    LOGGER.debug("Updated scheduled thread pool '{}' to size {}", poolName, poolSize);
                } else if (pool instanceof QueueingThreadPoolExecutor) {
                    pool.setMaximumPoolSize(poolSize);
                    LOGGER.debug("Updated queuing thread pool '{}' to size {}", poolName, poolSize);
                }
            } catch (NumberFormatException e) {
                LOGGER.warn("Ignoring invalid configuration for pool '{}': {} - value must be an integer", poolName,
                        config);
                continue;
            }
        }
    }
}
 
示例5
/**
 * Factory component satisfied, register component factory service.
 *
 */
@Override
ComponentConfiguration [] satisfied(ComponentConfiguration old) {
  Activator.logInfo(bc, "Satisfied: " + toString());
  state = STATE_SATISFIED;
  componentFactory = new ComponentFactoryImpl(this);
  final Hashtable<String, String> p = new Hashtable<String, String>();
  p.put(ComponentConstants.COMPONENT_NAME, compDesc.getName());
  p.put(ComponentConstants.COMPONENT_FACTORY, compDesc.getFactory());
  factoryService = bc.registerService(ComponentFactory.class.getName(), componentFactory, p);
  return null;
}
 
示例6
/**
 *
 */
@Override
void cmConfigDeleted(String ccid) {
  Activator.logDebug("cmConfigDeleted for ccid = " + ccid);
  resetCMRev(ccid);
  for (final ComponentConfiguration [] componentConfigurations : compConfigs.values()) {
    for (final ComponentConfiguration cc : componentConfigurations) {
      cc.cmConfigUpdated(ccid, null);
    }
  }
  if (cmConfig.isRequired()) {
    unresolvedConstraint(ComponentConstants.DEACTIVATION_REASON_CONFIGURATION_DELETED);
  }
}
 
示例7
/**
 * The tracked reference has become unavailable.
 */
boolean refUnavailable(Reference r) {
  Activator.logDebug("Reference unavailable, unresolved=" + unresolvedConstraints);
  if (!r.isRefOptional()) {
    synchronized (lock) {
      if (unresolvedConstraints++ == 0) {
        // TODO do we need to move this outside synchronized
        unsatisfied(ComponentConstants.DEACTIVATION_REASON_REFERENCE);
        return true;
      }
    }
  }
  return false;
}
 
示例8
/**
 * Stop SCR.
 *
 */
void stop() {
  bc.removeBundleListener(this);
  cmHandler.stop();
  final Bundle [] b = bundleComponents.keySet().toArray(new Bundle[bundleComponents.size()]);
  for (final Bundle element : b) {
    removeBundle(element, ComponentConstants.DEACTIVATION_REASON_DISPOSED);
  }
}
 
示例9
/**
 * Disabled named component or all components owned by
 * specified bundle.
 *
 * @param name Component to disable or null if we want all
 * @param b Bundle owning component
 */
void disableComponent(String name, Bundle b) {
  final Component [] ca = bundleComponents.get(b);
  if (ca != null) {
    for (final Component element : ca) {
      if (name == null || name.equals(element.compDesc.getName())) {
        element.disable(ComponentConstants.DEACTIVATION_REASON_DISABLED, null);
      }
    }
  }
}
 
示例10
/**
 * CM data for this component has changed. Check if we need to change state
 * for the component configuration. Also call modified method when its is specified.
 *
 */
void cmConfigUpdated(String ccid, Map<String, Object> dict)
{
  Activator.logDebug("CC.cmConfigUpdated, " + toString() + ", ccid=" + ccid
                     + ", activeCount=" + activeCount);
  int disposeReason = -1;
  List<ComponentContextImpl> ccis = null;
  synchronized (this) {
    if (state == STATE_ACTIVE) { // TODO check this for FactoryComponents
      ccId = ccid;
      if (dict == null && component.compDesc.getScrNSminor() < 3) {
        disposeReason = ComponentConstants.DEACTIVATION_REASON_CONFIGURATION_DELETED;
      } else if (component.modifiedMethod == null
          || component.modifiedMethod.isMissing(true)) {
        // Dispose when we have no modify method
        disposeReason = dict == null ? ComponentConstants.DEACTIVATION_REASON_CONFIGURATION_DELETED
                        : ComponentConstants.DEACTIVATION_REASON_CONFIGURATION_MODIFIED;
      } else {
        cmDict = dict;
        ccProps = null;
        ccis = getActiveContexts();
      }
    } else {
      cmDict = dict;
      ccProps = null;
      return;
    }
  }
  if (ccis != null) {
    for (final ComponentContextImpl cci : ccis) {
      component.modifiedMethod.invoke(cci);
    }
    modifyService();
  } else {
    dispose(disposeReason, true);
  }
}
 
示例11
/**
 * Get service, but activate before so that we will
 * get any ComponentExceptions.
 */
private Object getServiceCheckActivate(ServiceReference<?> sr, ComponentContextImpl cci) {
  Object s = getBound(sr, cci);
  if (s == null) {
    final Object o = sr.getProperty(ComponentConstants.COMPONENT_NAME);
    if (o != null && o instanceof String) {
      final Component [] cs = ref.comp.scr.getComponent((String)o);
      if (cs != null) {
        ref.comp.scr.postponeCheckin();
        try {
          for (final Component element : cs) {
            final ComponentConfiguration cc = element.getComponentConfiguration(sr);
            if (cc != null) {
              if (cc.getState() != ComponentConfiguration.STATE_DEACTIVATING ||
                  !cc.isUnregistering()) {
                cc.activate(ref.comp.bc.getBundle(), false);
              }
              break;
            }
          }
        } finally {
          ref.comp.scr.postponeCheckout();
        }
      }
    }
    if (ref.isScopeBundle()) {
      s = ref.comp.bc.getService(sr);
    }
  }
  bound(sr, s, cci);
  return s;
}
 
示例12
private String getServiceId(ServiceReference<?> serviceReference) {
    Object pid = serviceReference.getProperty(Constants.SERVICE_PID);
    if (pid != null) {
        return (String) pid;
    } else {
        return (String) serviceReference.getProperty(ComponentConstants.COMPONENT_NAME);
    }
}
 
示例13
/**
 *
 */
void dispose() {
  cc.deactivate(this, ComponentConstants.DEACTIVATION_REASON_DISPOSED, true, true);
}
 
示例14
/**
 *
 */
private Operation prepare(Object instance,
                          ComponentContextImpl cci,
                          int reason,
                          ServiceReference<?> s,
                          ReferenceListener rl) {
  method.setAccessible(true);
  final Object [] args = new Object[params.length];
  for (int i = 0; i < params.length; i++) {
    if (params[i] == ComponentContext.class) {
      args[i] = cci;
    } else if (params[i] == BundleContext.class) {
      args[i] = comp.bc;
    } else if (params[i] == ComponentServiceObjects.class) {
      args[i] = cci.getComponentServiceObjects(s, rl);
    } else if (params[i].isAnnotation()) {
      args[i] = Proxy.newProxyInstance(params[i].getClassLoader(),
                                       new Class[] { params[i] },
                                       new ComponentPropertyProxy(cci));
  
    } else if (params[i] == Map.class) {
      if (ref != null) {
        args[i] = new PropertyDictionary(s);
      } else {
        args[i] =  cci.getProperties();
      }
    } else if (params[i] == int.class || params[i] == Integer.class) {
      args[i] = new Integer(reason > Component.KF_DEACTIVATION_REASON_BASE ?
                            ComponentConstants.DEACTIVATION_REASON_UNSPECIFIED :
                            reason);
    } else if (params[i] == ServiceReference.class) {
      args[i] = s;
    } else {
      try {
        // TODO think about exceptions and circular activations
        args[i] = rl.getService(s, cci);
        if (args[i] == null) {
          Activator.logDebug("Got null service argument for " + method +
                             " in " + instance.getClass() +  " for component " +
                             comp.compDesc.getName() + " registered for " +
                             comp.compDesc.bundle);
          throw new ComponentException("Got null service, " + Activator.srInfo(s));
        }
      } catch (final Exception e) {
        Activator.logDebug("Got " + e + " when getting service argument for " + method +
                           " in " + instance.getClass() +  " for component " +
                           comp.compDesc.getName() + " registered for " +
                           comp.compDesc.bundle);
        if (e instanceof ComponentException) {
          throw (ComponentException)e;
        } else {
          throw new ComponentException("Failed to get service, " + Activator.srInfo(s), e);
        }
      }
    }
  }
  return new Operation(instance, args);
}
 
示例15
@Override
public String getProp(String p) {
  System.out.println("VImpl: getProp from: " + props.get(ComponentConstants.COMPONENT_ID));
  return (String) props.get(p);
}
 
示例16
/**
 * Check if component still is satisfied and the deactivation reason is
 * one of REFERENCE, CONFIGURATION_MODIFIED, CONFIGURATION_DELETED or
 * COMPONENT_DEACTIVATED.
 *
 * @param reason Deactivation reason code
 * @return True if we should keep component configuration service.
 */
boolean keepService(int reason) {
  return isSatisfied() &&
      (reason == ComponentConstants.DEACTIVATION_REASON_REFERENCE ||
       reason == ComponentConstants.DEACTIVATION_REASON_CONFIGURATION_MODIFIED ||
       reason == ComponentConstants.DEACTIVATION_REASON_CONFIGURATION_DELETED ||
       reason == KF_DEACTIVATION_REASON_COMPONENT_DEACTIVATED);
}
 
示例17
/**
 * We do not want to handle or try to normalize OSGi provided configuration parameters
 *
 * @param name The configuration parameter name
 */
private static boolean isOSGiConfigParameter(String name) {
    return Constants.OBJECTCLASS.equals(name) || ComponentConstants.COMPONENT_NAME.equals(name)
            || ComponentConstants.COMPONENT_ID.equals(name);
}