KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > jobs > JobOSGiUtils


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.jobs;
12
13 import org.eclipse.core.runtime.jobs.IJobManager;
14 import org.eclipse.osgi.service.debug.DebugOptions;
15 import org.osgi.framework.Bundle;
16 import org.osgi.framework.BundleContext;
17 import org.osgi.service.packageadmin.PackageAdmin;
18 import org.osgi.util.tracker.ServiceTracker;
19
20 /**
21  * The class contains a set of helper methods for the runtime Jobs plugin.
22  * The following utility methods are supplied:
23  * - provides access to debug options
24  * - provides some bundle discovery functionality
25  *
26  * The closeServices() method should be called before the plugin is stopped.
27  *
28  * @since org.eclipse.core.jobs 3.2
29  */

30 class JobOSGiUtils {
31     private ServiceTracker debugTracker = null;
32     private ServiceTracker bundleTracker = null;
33
34     private static final JobOSGiUtils singleton = new JobOSGiUtils();
35
36     /**
37      * Accessor for the singleton instance
38      * @return The JobOSGiUtils instance
39      */

40     public static JobOSGiUtils getDefault() {
41         return singleton;
42     }
43
44     /**
45      * Private constructor to block instance creation.
46      */

47     private JobOSGiUtils() {
48         super();
49     }
50
51     void openServices() {
52         BundleContext context = JobActivator.getContext();
53         if (context == null) {
54             if (JobManager.DEBUG)
55                 JobMessages.message("JobsOSGiUtils called before plugin started"); //$NON-NLS-1$
56
return;
57         }
58
59         debugTracker = new ServiceTracker(context, DebugOptions.class.getName(), null);
60         debugTracker.open();
61
62         bundleTracker = new ServiceTracker(context, PackageAdmin.class.getName(), null);
63         bundleTracker.open();
64     }
65
66     void closeServices() {
67         if (debugTracker != null) {
68             debugTracker.close();
69             debugTracker = null;
70         }
71         if (bundleTracker != null) {
72             bundleTracker.close();
73             bundleTracker = null;
74         }
75     }
76
77     public boolean getBooleanDebugOption(String JavaDoc option, boolean defaultValue) {
78         if (debugTracker == null) {
79             if (JobManager.DEBUG)
80                 JobMessages.message("Debug tracker is not set"); //$NON-NLS-1$
81
return defaultValue;
82         }
83         DebugOptions options = (DebugOptions) debugTracker.getService();
84         if (options != null) {
85             String JavaDoc value = options.getOption(option);
86             if (value != null)
87                 return value.equalsIgnoreCase("true"); //$NON-NLS-1$
88
}
89         return defaultValue;
90     }
91
92     /**
93      * Returns the bundle id of the bundle that contains the provided object, or
94      * <code>null</code> if the bundle could not be determined.
95      */

96     public String JavaDoc getBundleId(Object JavaDoc object) {
97         if (bundleTracker == null) {
98             if (JobManager.DEBUG)
99                 JobMessages.message("Bundle tracker is not set"); //$NON-NLS-1$
100
return null;
101         }
102         PackageAdmin packageAdmin = (PackageAdmin) bundleTracker.getService();
103         if (object == null)
104             return null;
105         if (packageAdmin == null)
106             return null;
107         Bundle source = packageAdmin.getBundle(object.getClass());
108         if (source != null && source.getSymbolicName() != null)
109             return source.getSymbolicName();
110         return null;
111     }
112
113     /**
114      * Calculates whether the job plugin should set worker threads to be daemon
115      * threads. When workers are daemon threads, the job plugin does not need
116      * to be explicitly shut down because the VM can exit while workers are still
117      * alive.
118      * @return <code>true</code> if all worker threads should be daemon threads,
119      * and <code>false</code> otherwise.
120      */

121     boolean useDaemonThreads() {
122         BundleContext context = JobActivator.getContext();
123         if (context == null) {
124             //we are running stand-alone, so consult global system property
125
String JavaDoc value = System.getProperty(IJobManager.PROP_USE_DAEMON_THREADS);
126             //default to use daemon threads if property is absent
127
if (value == null)
128                 return true;
129             return "true".equalsIgnoreCase(value); //$NON-NLS-1$
130
}
131         //only use daemon threads if the property is defined
132
final String JavaDoc value = context.getProperty(IJobManager.PROP_USE_DAEMON_THREADS);
133         //if value is absent, don't use daemon threads to maintain legacy behaviour
134
if (value == null)
135             return false;
136         return "true".equalsIgnoreCase(value); //$NON-NLS-1$
137
}
138 }
139
Popular Tags