KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > registry > osgi > EquinoxRegistryStrategy


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.registry.osgi;
12
13 import java.io.File JavaDoc;
14 import java.util.Map JavaDoc;
15 import org.eclipse.core.internal.registry.RegistryMessages;
16 import org.eclipse.core.internal.runtime.RuntimeLog;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.osgi.framework.BundleContext;
20 import org.osgi.framework.ServiceReference;
21
22 /**
23  * The registry strategy used by the Equinox extension registry. Adds to the
24  * OSGi registry:
25  * <p><ul>
26  * <li>Use debug information supplied via .options files</li>
27  * <li>Use Eclipse logging - Use Eclipse platform state for cache validation</li>
28  * <li>Event scheduling is done using Eclipse job scheduling mechanism</li>
29  * </ul></p>
30  *
31  * @since org.eclipse.equinox.registry 3.2
32  */

33 public class EquinoxRegistryStrategy extends RegistryStrategyOSGI {
34
35     public static final String JavaDoc PLUGIN_NAME = "org.eclipse.equinox.registry"; //$NON-NLS-1$
36
public static final String JavaDoc OPTION_DEBUG = PLUGIN_NAME + "/debug"; //$NON-NLS-1$
37
public static final String JavaDoc OPTION_DEBUG_EVENTS = PLUGIN_NAME + "/debug/events"; //$NON-NLS-1$
38

39     private static boolean DEBUG_ECLIPSE_REGISTRY = OSGIUtils.getDefault().getBooleanDebugOption(OPTION_DEBUG, false);
40     private static boolean DEBUG_ECLIPSE_EVENTS = OSGIUtils.getDefault().getBooleanDebugOption(OPTION_DEBUG_EVENTS, false);
41
42     private boolean useJobs = true;
43
44     public EquinoxRegistryStrategy(File JavaDoc[] theStorageDir, boolean[] cacheReadOnly, Object JavaDoc key) {
45         super(theStorageDir, cacheReadOnly, key);
46     }
47
48     public boolean debug() {
49         return DEBUG_ECLIPSE_REGISTRY;
50     }
51
52     public boolean debugRegistryEvents() {
53         return DEBUG_ECLIPSE_EVENTS;
54     }
55
56     public final void log(IStatus status) {
57         RuntimeLog.log(status);
58     }
59
60     public long getContainerTimestamp() {
61         BundleContext context = Activator.getContext();
62         if (context == null) {
63             RuntimeLog.log(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, 0, RegistryMessages.bundle_not_activated, null));
64             return -1;
65         }
66         // use a string here instead of the class to prevent class loading.
67
ServiceReference ref = context.getServiceReference("org.eclipse.osgi.service.resolver.PlatformAdmin"); //$NON-NLS-1$
68
if (ref == null)
69             return -1;
70         return EquinoxUtils.getContainerTimestamp(context, ref);
71     }
72
73     /**
74      * This method will attempt to use Eclipse Jobs mechanism to schedule registry events. If, at any time,
75      * Eclipse Jobs mechanism is missing, this method will fallback on the "internal" event scheduling
76      * provided by the registry strategy.
77      *
78      * Once the switch to the fallback mechanism occurred, no further attempt to use scheduling from the Jobs bundle
79      * will be made (until registry bundle is restarted). Avoiding repeated checks in this scenario will ensure that
80      * most users see no performance degradation and that order of registry events remains consistent.
81      */

82     public final void scheduleChangeEvent(Object JavaDoc[] listeners, Map JavaDoc deltas, Object JavaDoc registry) {
83         if (useJobs) {
84             try {
85                 new ExtensionEventDispatcherJob(listeners, deltas, registry).schedule();
86                 return; // all done - most typical use case
87
} catch (NoClassDefFoundError JavaDoc e) {
88                 useJobs = false; // Jobs are missing
89
}
90         }
91         super.scheduleChangeEvent(listeners, deltas, registry);
92     }
93
94 }
95
Popular Tags