Eclipse Equinox DS(Declarative Service)

  • Equinox DS's METE-INF/MANIFEST.MF
  •  1 Manifest-Version: 1.0
     2 Lazy-ManifestFilter: (Service-Component=*)
     3 Bundle-Localization: plugin
     4 Bundle-RequiredExecutionEnvironment: OSGi/Minimum-1.2,CDC-1.1/Foundation-1.1,J2SE-1.4
     6 Bundle-SymbolicName: org.eclipse.equinox.ds;singleton:=true
     7 Eclipse-SourceReferences: scm:git:git://git.eclipse.org/gitroot/equino
     8  x/rt.equinox.bundles.git;path="bundles/org.eclipse.equinox.ds";tag=v2
     9  0120522-1841
    10 Bundle-Activator: org.eclipse.equinox.internal.ds.Activator
    11 Export-Package: org.eclipse.equinox.internal.ds;x-internal:=true,org.e
    12  clipse.equinox.internal.ds.impl;x-internal:=true,org.eclipse.equinox.
    13  internal.ds.model;x-internal:=true,org.eclipse.equinox.internal.ds.st
    14  orage.file;x-internal:=true,org.eclipse.equinox.internal.util.io;x-in
    15  ternal:=true,org.eclipse.equinox.internal.util.xml;x-internal:=true,o
    16  rg.eclipse.equinox.internal.util.xml.impl;x-internal:=true,org.eclips
    17  e.equinox.internal.util.string;x-internal:=true,org.apache.felix.scr;
    18  version="1.6"
    19 Bundle-Version: 1.4.0.v20120522-1841
    20 Bundle-Description: This bundle provides support for OSGi Declarative Services
    22 Bundle-Vendor: %bundleVendor
    23 Bundle-Name: %bundleName
    24 Import-Package: org.eclipse.equinox.internal.util.event;version="1.0",
    25  org.eclipse.equinox.internal.util.hash;version="1.0",org.eclipse.equi
    26  nox.internal.util.pool;version="1.0",org.eclipse.equinox.internal.uti
    27  l.ref;version="1.0",org.eclipse.equinox.internal.util.threadpool;vers
    28  ion="1.0",org.eclipse.equinox.internal.util.timer;version="1.0",org.e
    29  clipse.osgi.framework.console;version="1.0.0";resolution:=optional,or
    30  g.eclipse.osgi.framework.log;version="1.0.0",org.eclipse.osgi.service
    31  .debug;version="1.0",org.eclipse.osgi.service.environment;version="1.
    32  2.0",org.eclipse.osgi.util,org.osgi.framework;version="1.3",org.osgi.
    33  service.cm;version="1.2",org.osgi.service.component;version="[1.1,1.3
    34  )",org.osgi.service.log;version="1.3.0",org.osgi.util.tracker;version
    35  ="1.3",org.apache.felix.scr; version="[1.6,1.7)"
    36 Bundle-ManifestVersion: 2
  • org.eclipse.equinox.internal.ds.Activator
  •   1 /*******************************************************************************
      2  * Copyright (c) 1997, 2010 by ProSyst Software GmbH
      3  * http://www.prosyst.com
      4  * All rights reserved. This program and the accompanying materials
      5  * are made available under the terms of the Eclipse Public License v1.0
      6  * which accompanies this distribution, and is available at
      7  * http://www.eclipse.org/legal/epl-v10.html
      8  *
      9  * Contributors:
     10  *    ProSyst Software GmbH - initial API and implementation
     11  *******************************************************************************/
     12 package org.eclipse.equinox.internal.ds;
     13 
     14 import java.io.IOException;
     15 import java.util.Dictionary;
     16 import java.util.Hashtable;
     17 import org.apache.felix.scr.ScrService;
     18 import org.eclipse.equinox.internal.util.ref.Log;
     19 import org.eclipse.osgi.framework.log.FrameworkLog;
     20 import org.eclipse.osgi.framework.log.FrameworkLogEntry;
     21 import org.eclipse.osgi.service.debug.DebugOptions;
     22 import org.eclipse.osgi.service.environment.EnvironmentInfo;
     23 import org.osgi.framework.*;
     24 import org.osgi.service.cm.*;
     25 import org.osgi.service.component.ComponentConstants;
     26 import org.osgi.service.log.LogService;
     27 import org.osgi.util.tracker.ServiceTracker;
     28 
     29 /**
     30  * This is the main starting class for the Service Component Runtime.
     31  * The SCR is not fully initialized until it detects at least one bundle providing DS components. 
     32  * Thus it has considerably small startup time and does improve a little the runtime performance 
     33  * since it does not listen for service events.
     34  * 
     35  * @author Valentin Valchev
     36  * @author Stoyan Boshev
     37  * @author Pavlin Dobrev
     38  */
     39 
     40 public class Activator implements BundleActivator, SynchronousBundleListener, ServiceListener {
     41 
     42     public static BundleContext bc = null;
     43     public static ConfigurationAdmin configAdmin = null;
     44     public static boolean security = false;
     45 
     46     private ServiceRegistration configListenerReg;
     47     private SCRManager scrManager = null;
     48     public ScrServiceImpl scrService = null;
     49     private ServiceRegistration scrServiceReg;
     50     private ServiceRegistration scrCommandProviderReg;
     51     private static FrameworkLog fwLog;
     52     private boolean inited = false;
     53 
     54     public static Log log;
     55     public static boolean DEBUG;
     56     public static boolean PERF;
     57     public static boolean DBSTORE;
     58     public static boolean INSTANTIATE_ALL;
     59     public static boolean startup;
     60 
     61     static long time[] = null;
     62 
     63     public static void timeLog(String message) {
     64         time[1] = time[0];
     65         log.debug(message + String.valueOf((time[0] = System.currentTimeMillis()) - time[1]), null);
     66     }
     67 
     68     private void initSCR() {
     69         synchronized (this) {
     70             if (inited)
     71                 return;
     72             inited = true;
     73         }
     74 
     75         boolean lazyIniting = false;
     76         if (startup && time == null) {
     77             long tmp = System.currentTimeMillis();
     78             time = new long[] {tmp, 0, tmp};
     79             lazyIniting = true;
     80             if (startup)
     81                 timeLog("[BEGIN - lazy SCR init]"); //$NON-NLS-1$
     82         }
     83 
     84         WorkThread.IDLE_TIMEOUT = getInteger("equinox.ds.idle_timeout", 1000); //$NON-NLS-1$
     85         WorkThread.BLOCK_TIMEOUT = getInteger("equinox.ds.block_timeout", 30000); //$NON-NLS-1$
     86 
     87         try {
     88             bc.addServiceListener(this, "(objectClass=" + ConfigurationAdmin.class.getName() + ')'); //$NON-NLS-1$
     89         } catch (InvalidSyntaxException e) {
     90             //should never happen
     91         }
     92         //get config admin service if available
     93         ServiceReference caRef = bc.getServiceReference(ConfigurationAdmin.class.getName());
     94         if (caRef != null) {
     95             configAdmin = (ConfigurationAdmin) bc.getService(caRef);
     96         }
     97         if (startup)
     98             timeLog("ConfigurationAdmin service getting took "); //$NON-NLS-1$
     99 
    100         scrManager = new SCRManager();
    101         if (startup)
    102             timeLog("SCRManager instantiation took "); //$NON-NLS-1$
    103 
    104         // add the configuration listener - we to receive CM events to restart
    105         // components
    106         configListenerReg = bc.registerService(ConfigurationListener.class.getName(), scrManager, null);
    107         if (startup)
    108             timeLog("ConfigurationListener service registered for "); //$NON-NLS-1$
    109         bc.addServiceListener(scrManager);
    110 
    111         scrManager.startIt();
    112         if (Activator.startup)
    113             Activator.timeLog("startIt() method took "); //$NON-NLS-1$
    114 
    115         installCommandProvider();
    116 
    117         if (startup && lazyIniting) {
    118             log.debug("[END - lazy SCR init] Activator.initSCR() method executed for " + String.valueOf(time[0] - time[2]), null); //$NON-NLS-1$
    119             time = null;
    120         }
    121     }
    122 
    123     /*
    124      * (non-Javadoc)
    125      * 
    126      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
    127      */
    128     public void start(BundleContext bundleContext) throws Exception {
    129         Activator.bc = bundleContext;
    130         startup = getBoolean("equinox.measurements.bundles", false); //$NON-NLS-1$
    131         if (startup) {
    132             long tmp = System.currentTimeMillis();
    133             time = new long[] {tmp, 0, tmp};
    134         }
    135         // initialize the logging routines
    136         log = new Log(bundleContext, false);
    137         ServiceTracker debugTracker = new ServiceTracker(bundleContext, DebugOptions.class.getName(), null);
    138         debugTracker.open();
    139         DebugOptions debugOptions = (DebugOptions) debugTracker.getService();
    140         DEBUG = getBooleanDebugOption(debugOptions, "org.eclipse.equinox.ds/debug", false) || getBoolean("equinox.ds.debug", false); //$NON-NLS-1$ //$NON-NLS-2$
    141         PERF = getBooleanDebugOption(debugOptions, "org.eclipse.equinox.ds/performance", false) || getBoolean("equinox.ds.perf", false); //$NON-NLS-1$ //$NON-NLS-2$
    142         INSTANTIATE_ALL = getBooleanDebugOption(debugOptions, "org.eclipse.equinox.ds/instantiate_all", false) || getBoolean("equinox.ds.instantiate_all", false); //$NON-NLS-1$ //$NON-NLS-2$
    143 
    144         DBSTORE = getBooleanDebugOption(debugOptions, "org.eclipse.equinox.ds/cache_descriptions", true) || getBoolean("equinox.ds.dbstore", true); //$NON-NLS-1$ //$NON-NLS-2$
    145         boolean print = getBooleanDebugOption(debugOptions, "org.eclipse.equinox.ds/print_on_console", false) || getBoolean("equinox.ds.print", false); //$NON-NLS-1$ //$NON-NLS-2$
    146         log.setDebug(DEBUG);
    147         log.setPrintOnConsole(print);
    148         //DebugOptions no longer needed
    149         debugTracker.close();
    150         ServiceReference fwRef = bc.getServiceReference(FrameworkLog.class.getName());
    151         if (fwRef != null) {
    152             fwLog = (FrameworkLog) bc.getService(fwRef);
    153         }
    154 
    155         if (startup)
    156             timeLog("[BEGIN - start method] Creating Log instance and initializing log system took "); //$NON-NLS-1$
    157 
    158         security = Log.security();
    159         boolean hasHeaders = false;
    160         Bundle[] allBundles = bundleContext.getBundles();
    161         for (int i = 0; i < allBundles.length; i++) {
    162             Dictionary allHeaders = allBundles[i].getHeaders(""); //$NON-NLS-1$
    163             if (allHeaders.get(ComponentConstants.SERVICE_COMPONENT) != null) {
    164                 hasHeaders = true;
    165                 break;
    166             }
    167         }
    168 
    169         if (hasHeaders) {
    170             initSCR();
    171         } else {
    172             // there are no bundles holding components - SCR will not be
    173             // initialized yet
    174             bundleContext.addBundleListener(this);
    175         }
    176         ServiceReference envInfoRef = bc.getServiceReference(EnvironmentInfo.class.getName());
    177         EnvironmentInfo envInfo = null;
    178         if (envInfoRef != null) {
    179             envInfo = (EnvironmentInfo) bc.getService(envInfoRef);
    180         }
    181         if (envInfo != null) {
    182             envInfo.setProperty("equinox.use.ds", "true"); //$NON-NLS-1$//$NON-NLS-2$
    183             bc.ungetService(envInfoRef);
    184         } else {
    185             System.setProperty("equinox.use.ds", "true"); //$NON-NLS-1$ //$NON-NLS-2$
    186         }
    187 
    188         scrService = new ScrServiceImpl();
    189         scrServiceReg = bc.registerService(ScrService.class.getName(), scrService, null);
    190 
    191         if (startup) {
    192             log.debug("[END - start method] Activator.start() method executed for " + String.valueOf(time[0] - time[2]), null); //$NON-NLS-1$
    193             time = null;
    194         }
    195     }
    196 
    197     /*
    198      * (non-Javadoc)
    199      * 
    200      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
    201      */
    202     public void stop(BundleContext bundleContext) throws Exception {
    203         if (scrManager != null) {
    204             scrManager.stopIt();
    205             bundleContext.removeServiceListener(scrManager);
    206         }
    207         // dispose the CM Listener
    208         if (configListenerReg != null) {
    209             configListenerReg.unregister();
    210         }
    211         if (scrService != null) {
    212             scrService.dispose();
    213             scrServiceReg.unregister();
    214         }
    215 
    216         if (scrCommandProviderReg != null)
    217             scrCommandProviderReg.unregister();
    218 
    219         if (scrManager != null) {
    220             bundleContext.removeBundleListener(scrManager);
    221         } else {
    222             bundleContext.removeBundleListener(this);
    223         }
    224         ServiceReference envInfoRef = bc.getServiceReference(EnvironmentInfo.class.getName());
    225         EnvironmentInfo envInfo = null;
    226         if (envInfoRef != null) {
    227             envInfo = (EnvironmentInfo) bc.getService(envInfoRef);
    228         }
    229         if (envInfo != null) {
    230             envInfo.setProperty("equinox.use.ds", "false"); //$NON-NLS-1$//$NON-NLS-2$
    231             bc.ungetService(envInfoRef);
    232         } else {
    233             System.setProperty("equinox.use.ds", "false"); //$NON-NLS-1$ //$NON-NLS-2$
    234         }
    235 
    236         log.close();
    237         log = null;
    238     }
    239 
    240     public static Filter createFilter(String filter) throws InvalidSyntaxException {
    241         return bc.createFilter(filter);
    242     }
    243 
    244     public void bundleChanged(BundleEvent event) {
    245         if (event.getType() == BundleEvent.STARTED || event.getType() == BundleEvent.LAZY_ACTIVATION) {
    246             Dictionary allHeaders = event.getBundle().getHeaders(""); //$NON-NLS-1$ 
    247             if ((allHeaders.get(ComponentConstants.SERVICE_COMPONENT)) != null) {
    248                 // The bundle is holding components - activate scr
    249                 bc.removeBundleListener(this);
    250                 initSCR();
    251             }
    252         }
    253     }
    254 
    255     public static Configuration getConfiguration(String pid) throws IOException {
    256         if (configAdmin != null) {
    257             return configAdmin.getConfiguration(pid);
    258         }
    259         return null;
    260     }
    261 
    262     public static Configuration[] listConfigurations(String filter) throws IOException, InvalidSyntaxException {
    263         if (configAdmin != null) {
    264             return configAdmin.listConfigurations(filter);
    265         }
    266         return null;
    267     }
    268 
    269     public static boolean getBoolean(String property, boolean defaultValue) {
    270         String prop = (bc != null) ? bc.getProperty(property) : System.getProperty(property);
    271         if (prop != null) {
    272             return prop.equalsIgnoreCase("true"); //$NON-NLS-1$
    273         }
    274         return defaultValue;
    275     }
    276 
    277     public static boolean getBoolean(String property) {
    278         return getBoolean(property, false);
    279     }
    280 
    281     public static int getInteger(String property, int defaultValue) {
    282         String prop = (bc != null) ? bc.getProperty(property) : System.getProperty(property);
    283         if (prop != null) {
    284             try {
    285                 return Integer.decode(prop).intValue();
    286             } catch (NumberFormatException e) {
    287                 //do nothing
    288             }
    289         }
    290         return defaultValue;
    291     }
    292 
    293     public boolean getBooleanDebugOption(DebugOptions optionsService, String option, boolean defaultValue) {
    294         if (optionsService != null) {
    295             String value = optionsService.getOption(option);
    296             if (value != null)
    297                 return value.equalsIgnoreCase("true"); //$NON-NLS-1$
    298         }
    299         return defaultValue;
    300     }
    301 
    302     private void installCommandProvider() {
    303         try {
    304             SCRCommandProvider scrCommandProvider = new SCRCommandProvider(scrManager);
    305             Hashtable reg_props = new Hashtable(1, 1);
    306             reg_props.put(org.osgi.framework.Constants.SERVICE_RANKING, new Integer(Integer.MAX_VALUE));
    307             scrCommandProviderReg = bc.registerService(org.eclipse.osgi.framework.console.CommandProvider.class.getName(), scrCommandProvider, reg_props);
    308         } catch (NoClassDefFoundError e) {
    309             //the org.eclipse.osgi.framework.console package is optional 
    310             if (Activator.DEBUG) {
    311                 log.debug("Cannot register SCR CommandProvider!", e); //$NON-NLS-1$
    312             }
    313         }
    314     }
    315 
    316     public static void log(BundleContext bundleContext, int level, String message, Throwable t) {
    317         LogService logService = null;
    318         ServiceReference logRef = null;
    319         if (bundleContext != null) {
    320             try {
    321                 logRef = bundleContext.getServiceReference(LogService.class.getName());
    322                 if (logRef != null) {
    323                     logService = (LogService) bundleContext.getService(logRef);
    324                 }
    325             } catch (Exception e) {
    326                 if (Activator.DEBUG) {
    327                     log.debug("Cannot get LogService for bundle " + bundleContext.getBundle().getSymbolicName(), e); //$NON-NLS-1$
    328                 }
    329             }
    330         }
    331         if (logService != null) {
    332             logService.log(level, message, t);
    333             bundleContext.ungetService(logRef);
    334             if (log.getPrintOnConsole()) {
    335                 String prefix = ""; //$NON-NLS-1$
    336                 switch (level) {
    337                     case LogService.LOG_ERROR :
    338                         prefix = "ERROR "; //$NON-NLS-1$
    339                         break;
    340                     case LogService.LOG_WARNING :
    341                         prefix = "WARNING "; //$NON-NLS-1$
    342                         break;
    343                     case LogService.LOG_INFO :
    344                         prefix = "INFO "; //$NON-NLS-1$
    345                         break;
    346                 }
    347                 dumpOnConsole(prefix, bundleContext, message, t, level == LogService.LOG_ERROR);
    348             }
    349         } else {
    350             logRef = bc.getServiceReference(LogService.class.getName());
    351             if (logRef == null) {
    352                 //log service is not available
    353                 if (!log.getPrintOnConsole() && !log.autoPrintOnConsole && fwLog == null) {
    354                     //The log will not print the message on the console and the FrameworkLog service is not available
    355                     //Will print errors on the console as last resort
    356                     if (level == LogService.LOG_ERROR) {
    357                         dumpOnConsole("ERROR ", bundleContext, message, t, true); //$NON-NLS-1$
    358                     }
    359                 }
    360             }
    361 
    362             //using the SCR log
    363             switch (level) {
    364                 case LogService.LOG_ERROR :
    365                     log.error(message, t);
    366                     break;
    367                 case LogService.LOG_WARNING :
    368                     log.warning(message, t);
    369                     break;
    370                 case LogService.LOG_INFO :
    371                     log.info(message);
    372                     break;
    373                 default :
    374                     log.debug(message, t);
    375                     break;
    376             }
    377         }
    378         if (fwLog != null) {
    379             logToFWLog(bundleContext != null ? bundleContext.getBundle().getSymbolicName() : bc.getBundle().getSymbolicName(), level, message, t);
    380         }
    381     }
    382 
    383     private static void dumpOnConsole(String prefix, BundleContext bundleContext, String msg, Throwable t, boolean printInErr) {
    384         String message = prefix + bundleContext.getBundle().getBundleId() + " " + msg; //$NON-NLS-1$
    385         if (printInErr) {
    386             System.err.println(message);
    387         } else {
    388             System.out.println(message);
    389         }
    390         if (t != null) {
    391             t.printStackTrace();
    392         }
    393     }
    394 
    395     private static void logToFWLog(String bsn, int level, String message, Throwable t) {
    396         int severity = FrameworkLogEntry.INFO;
    397         switch (level) {
    398             case LogService.LOG_ERROR :
    399                 severity = FrameworkLogEntry.ERROR;
    400                 break;
    401             case LogService.LOG_WARNING :
    402                 severity = FrameworkLogEntry.WARNING;
    403                 break;
    404             case LogService.LOG_INFO :
    405                 severity = FrameworkLogEntry.INFO;
    406                 break;
    407             case LogService.LOG_DEBUG :
    408                 severity = FrameworkLogEntry.INFO;
    409                 break;
    410         }
    411         fwLog.log(new FrameworkLogEntry(bsn, severity, 0, message, 0, t, null));
    412     }
    413 
    414     public void serviceChanged(ServiceEvent event) {
    415         switch (event.getType()) {
    416             case ServiceEvent.REGISTERED :
    417                 Object caService = bc.getService(event.getServiceReference());
    418                 configAdmin = (ConfigurationAdmin) caService;
    419                 if (caService != null) {
    420                     // Config Admin registered
    421                     if (scrManager != null) {
    422                         scrManager.configAdminRegistered((ConfigurationAdmin) caService, event.getServiceReference());
    423                     }
    424                 }
    425                 break;
    426             case ServiceEvent.UNREGISTERING :
    427                 //get replacement config admin service if available
    428                 ServiceReference caRef = bc.getServiceReference(ConfigurationAdmin.class.getName());
    429                 if (caRef != null) {
    430                     configAdmin = (ConfigurationAdmin) bc.getService(caRef);
    431                 } else {
    432                     configAdmin = null;
    433                 }
    434                 break;
    435         }
    436     }
    437 }
    Activator
  • One example of Service-Component
    • MAINFEST.INF
    •  1 Manifest-Version: 1.0
       2 Bundle-ManifestVersion: 2
       3 Bundle-Name: Toast Back End Emergency
       4 Bundle-SymbolicName: org.eclipse.examples.toast.backend.emergency
       5 Bundle-Version: 1.0.0
       6 Bundle-RequiredExecutionEnvironment: J2SE-1.4
       7 Service-Component: OSGI-INF/component.xml,OSGI-INF/emergencyCenter.xml
       8 Import-Package: javax.servlet;version="2.4.0",
       9  javax.servlet.http;version="2.4.0",
      10  org.eclipse.examples.toast.backend.controlcenter,
      11  org.eclipse.examples.toast.backend.data,
      12  org.eclipse.examples.toast.backend.data.internal,
      13  org.eclipse.examples.toast.core;version="[1.0.0,2.0.0)",
      14  org.eclipse.examples.toast.core.emergency;version="[1.0.0,2.0.0)",
      15  org.osgi.service.http;version="[1.2.0,2.0.0)"
      16 Export-Package: org.eclipse.examples.toast.internal.backend.emergency;version="1.0.0";x-internal:=true,
      17
    • Service-Component: OSGI-INF/component.xml,OSGI-INF/emergencyCenter.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="startup" deactivate="shutdown" name="org.eclipse.examples.toast.backend.emergency">
 3   <implementation class="org.eclipse.examples.toast.internal.backend.emergency.bundle.Component"/>
 4   <reference bind="setHttp" interface="org.osgi.service.http.HttpService" name="http"/>
 5   <reference bind="setEmergencyCenter" interface="org.eclipse.examples.toast.core.emergency.IEmergencyCenter" name="emergencyCenter"/>
 6 </scr:component>
 7 
 8 
 9 <?xml version="1.0" encoding="UTF-8"?>
10 <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="startup" deactivate="shutdown" name="org.eclipse.examples.toast.backend.emergency.center">
11    <implementation class="org.eclipse.examples.toast.internal.backend.emergency.EmergencyCenter"/>
12    <service>
13       <provide interface="org.eclipse.examples.toast.core.emergency.IEmergencyCenter"/>
14    </service>
15    <reference bind="setData" cardinality="1..1" interface="org.eclipse.examples.toast.backend.controlcenter.IData" name="IData" policy="static"/>
16 </scr:component>
  • start method  implemented by Activator
  •  1 public void start(BundleContext bundleContext) throws Exception {
     2         Activator.bc = bundleContext;
     3         startup = getBoolean("equinox.measurements.bundles", false); //$NON-NLS-1$
     4         if (startup) {
     5             long tmp = System.currentTimeMillis();
     6             time = new long[] {tmp, 0, tmp};
     7         }
     8         // initialize the logging routines
     9         log = new Log(bundleContext, false);
    10ServiceTracker debugTracker = new ServiceTracker(bundleContext, DebugOptions.class.getName(), null);
    11         debugTracker.open();
    12         DebugOptions debugOptions = (DebugOptions) debugTracker.getService();
    13         DEBUG = getBooleanDebugOption(debugOptions, "org.eclipse.equinox.ds/debug", false) || getBoolean("equinox.ds.debug", false); //$NON-NLS-1$ //$NON-NLS-2$
    14         PERF = getBooleanDebugOption(debugOptions, "org.eclipse.equinox.ds/performance", false) || getBoolean("equinox.ds.perf", false); //$NON-NLS-1$ //$NON-NLS-2$
    15         INSTANTIATE_ALL = getBooleanDebugOption(debugOptions, "org.eclipse.equinox.ds/instantiate_all", false) || getBoolean("equinox.ds.instantiate_all", false); //$NON-NLS-1$ //$NON-NLS-2$
    16 
    17         DBSTORE = getBooleanDebugOption(debugOptions, "org.eclipse.equinox.ds/cache_descriptions", true) || getBoolean("equinox.ds.dbstore", true); //$NON-NLS-1$ //$NON-NLS-2$
    18         boolean print = getBooleanDebugOption(debugOptions, "org.eclipse.equinox.ds/print_on_console", false) || getBoolean("equinox.ds.print", false); //$NON-NLS-1$ //$NON-NLS-2$
    19         log.setDebug(DEBUG);
    20         log.setPrintOnConsole(print);
    21         //DebugOptions no longer needed
    22         debugTracker.close();
    23         ServiceReference fwRef = bc.getServiceReference(FrameworkLog.class.getName());
    24         if (fwRef != null) {
    25             fwLog = (FrameworkLog) bc.getService(fwRef);
    26         }
    27 
    28         if (startup)
    29             timeLog("[BEGIN - start method] Creating Log instance and initializing log system took "); //$NON-NLS-1$
    30 
    31         security = Log.security();
    32         boolean hasHeaders = false;
    33         Bundle[] allBundles = bundleContext.getBundles();
    34         for (int i = 0; i < allBundles.length; i++) {
    35             Dictionary allHeaders = allBundles[i].getHeaders(""); //$NON-NLS-1$
    36             if (allHeaders.get(ComponentConstants.SERVICE_COMPONENT) != null) {
    37                 hasHeaders = true;
    38                 break;
    39             }
    40         }
    41 
    42         if (hasHeaders) {
    43             initSCR();
    44         } else {
    45             // there are no bundles holding components - SCR will not be
    46             // initialized yet
    47             bundleContext.addBundleListener(this);
    48         }
    49         ServiceReference envInfoRef = bc.getServiceReference(EnvironmentInfo.class.getName());
    50         EnvironmentInfo envInfo = null;
    51         if (envInfoRef != null) {
    52             envInfo = (EnvironmentInfo) bc.getService(envInfoRef);
    53         }
    54         if (envInfo != null) {
    55             envInfo.setProperty("equinox.use.ds", "true"); //$NON-NLS-1$//$NON-NLS-2$
    56             bc.ungetService(envInfoRef);
    57         } else {
    58             System.setProperty("equinox.use.ds", "true"); //$NON-NLS-1$ //$NON-NLS-2$
    59         }
    60 
    61         scrService = new ScrServiceImpl();
    62         scrServiceReg = bc.registerService(ScrService.class.getName(), scrService, null);
    63 
    64         if (startup) {
    65             log.debug("[END - start method] Activator.start() method executed for " + String.valueOf(time[0] - time[2]), null); //$NON-NLS-1$
    66             time = null;
    67         }
    68     }
  • initSCR() in Activator
  •  1     private void initSCR() {
     2         synchronized (this) {
     3             if (inited)
     4                 return;
     5             inited = true;
     6         }
     7 
     8         boolean lazyIniting = false;
     9         if (startup && time == null) {
    10             long tmp = System.currentTimeMillis();
    11             time = new long[] {tmp, 0, tmp};
    12             lazyIniting = true;
    13             if (startup)
    14                 timeLog("[BEGIN - lazy SCR init]"); //$NON-NLS-1$
    15         }
    16 
    17         WorkThread.IDLE_TIMEOUT = getInteger("equinox.ds.idle_timeout", 1000); //$NON-NLS-1$
    18         WorkThread.BLOCK_TIMEOUT = getInteger("equinox.ds.block_timeout", 30000); //$NON-NLS-1$
    19 
    20         try {
    21             bc.addServiceListener(this, "(objectClass=" + ConfigurationAdmin.class.getName() + ')'); //$NON-NLS-1$
    22         } catch (InvalidSyntaxException e) {
    23             //should never happen
    24         }
    25         //get config admin service if available
    26         ServiceReference caRef = bc.getServiceReference(ConfigurationAdmin.class.getName());
    27         if (caRef != null) {
    28             configAdmin = (ConfigurationAdmin) bc.getService(caRef);
    29         }
    30         if (startup)
    31             timeLog("ConfigurationAdmin service getting took "); //$NON-NLS-1$
    32 
    33         scrManager = new SCRManager();
    34         if (startup)
    35             timeLog("SCRManager instantiation took "); //$NON-NLS-1$
    36 
    37         // add the configuration listener - we to receive CM events to restart
    38         // components
    39         configListenerReg = bc.registerService(ConfigurationListener.class.getName(), scrManager, null);
    40         if (startup)
    41             timeLog("ConfigurationListener service registered for "); //$NON-NLS-1$
    42         bc.addServiceListener(scrManager);
    43 
    44         scrManager.startIt();
    45         if (Activator.startup)
    46             Activator.timeLog("startIt() method took "); //$NON-NLS-1$
    47 
    48         installCommandProvider();
    49 
    50         if (startup && lazyIniting) {
    51             log.debug("[END - lazy SCR init] Activator.initSCR() method executed for " + String.valueOf(time[0] - time[2]), null); //$NON-NLS-1$
    52             time = null;
    53         }
    54     }
     1 public void startIt() {
     2         // loop through the currently installed bundles
     3         Bundle[] bundles = Activator.bc.getBundles();
     4         if (bundles != null) {
     5             for (int i = 0; i < bundles.length; i++) {
     6                 Bundle current = bundles[i];
     7                 // try to process the active ones.
     8                 if (current.getState() == Bundle.ACTIVE) {
     9                     startedBundle(current);
    10                 } else if (current.getState() == Bundle.STARTING) {
    11                     String lazy = (String) current.getHeaders("").get(Constants.BUNDLE_ACTIVATIONPOLICY); //$NON-NLS-1$
    12                     if (lazy != null && lazy.indexOf(Constants.ACTIVATION_LAZY) >= 0) {
    13                         startedBundle(current);
    14                     }
    15                 }
    16             }
    17         }
    18     }
     1 void startedBundle(Bundle bundle) {
     2         synchronized (processingBundles) {
     3             if (processingBundles.get(bundle) != null) {
     4                 //the bundle is already being processed
     5                 return;
     6             }
     7             processingBundles.put(bundle, ""); //$NON-NLS-1$
     8         }
     9         try {
    10             startedBundle2(bundle);
    11         } finally {
    12             processingBundles.remove(bundle);
    13         }
    14     }
     1     void startedBundle2(Bundle bundle) {
     2         long start = 0l;
     3         if (Activator.PERF) {
     4             start = System.currentTimeMillis();
     5         }
     6         if (bundleToServiceComponents != null && bundleToServiceComponents.get(bundle) != null) {
     7             // the bundle is already processed - skipping it
     8             return;
     9         }
    10 
    11         String dsHeader = null;
    12         Dictionary allHeaders = bundle.getHeaders(""); //$NON-NLS-1$
    13 
    14         if (!((dsHeader = (String) allHeaders.get(ComponentConstants.SERVICE_COMPONENT)) != null)) {
    15             // no component descriptions in this bundle
    16             return;
    17         }
    18 
    19         Vector components = storage.loadComponentDefinitions(bundle, dsHeader);
    20         if (components != null && !components.isEmpty()) {
    21             if (!hasRegisteredServiceListener) {
    22                 hasRegisteredServiceListener = true;
    23                 Activator.bc.addServiceListener(this);
    24                 resolver.synchronizeServiceReferences();
    25             }
    26             if (Activator.PERF) {
    27                 start = System.currentTimeMillis() - start;
    28                 Activator.log.info("[DS perf] The components of bundle " + bundle + " are parsed for " + start + "ms"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    29             }
    30             if (bundleToServiceComponents == null) {
    31                 synchronized (this) {
    32                     if (bundleToServiceComponents == null) {
    33                         bundleToServiceComponents = new Hashtable(11);
    34                     }
    35                 }
    36             }
    37 
    38             //check whether component's names are unique
    39             ServiceComponent comp;
    40             ServiceComponent comp2;
    41             L1: for (int i = 0; i < components.size(); i++) {
    42                 comp = (ServiceComponent) components.elementAt(i);
    43                 //check if unique in its bundle
    44                 for (int j = i + 1; j < components.size(); j++) {
    45                     comp2 = (ServiceComponent) components.elementAt(j);
    46                     if (comp.name.equals(comp2.name)) {
    47                         Activator.log(comp.bc, LogService.LOG_ERROR, NLS.bind(Messages.FOUND_COMPONENTS_WITH_DUPLICATED_NAMES, comp), null);
    48                         //removing one of the components
    49                         components.remove(i);
    50                         i--;
    51                         continue L1;
    52                     }
    53                 }
    54                 //check if the component is globally unique
    55                 Enumeration keys = bundleToServiceComponents.keys();
    56                 while (keys.hasMoreElements()) {
    57                     Vector components2 = (Vector) bundleToServiceComponents.get(keys.nextElement());
    58                     for (int j = 0; j < components2.size(); j++) {
    59                         comp2 = (ServiceComponent) components2.elementAt(j);
    60                         if (comp.name.equals(comp2.name)) {
    61                             Activator.log(comp.bc, LogService.LOG_WARNING, NLS.bind(Messages.FOUND_COMPONENTS_WITH_DUPLICATED_NAMES2, comp, comp2), null);
    62                         }
    63                     }
    64                 }
    65 
    66                 if (comp.autoenable) {
    67                     comp.enabled = true;
    68                 }
    69             }
    70             // store the components in the cache
    71             bundleToServiceComponents.put(bundle, components.clone());
    72             if (workThread != null && workThread.processingThread == Thread.currentThread()) {
    73                 //we are in the queue thread already. Processing synchronously the job
    74                 resolver.enableComponents(components);
    75             } else {
    76                 // this will also resolve the component dependencies!
    77                 enqueueWork(this, ENABLE_COMPONENTS, components, false);
    78                 synchronized (components) {
    79                     long startTime = System.currentTimeMillis();
    80                     try {
    81                         while (!components.isEmpty() && (System.currentTimeMillis() - startTime < WorkThread.BLOCK_TIMEOUT)) {
    82                             components.wait(1000);
    83                         }
    84                         if (System.currentTimeMillis() - startTime >= WorkThread.BLOCK_TIMEOUT) {
    85                             Activator.log(null, LogService.LOG_WARNING, NLS.bind(Messages.TIMEOUT_REACHED_ENABLING_COMPONENTS, getBundleName(bundle), Integer.toString(WorkThread.BLOCK_TIMEOUT)), null);
    86                         }
    87                     } catch (InterruptedException e) {
    88                         //do nothing
    89                     }
    90                 }
    91             }
    92         }
    93     }
  • ad
原文地址:https://www.cnblogs.com/iiiDragon/p/3327953.html