KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > runtime > internal > adaptor > EclipseAdaptorHook


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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
12 package org.eclipse.core.runtime.internal.adaptor;
13
14 import java.io.*;
15 import java.net.URLConnection JavaDoc;
16 import java.util.*;
17 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
18 import javax.xml.parsers.SAXParserFactory JavaDoc;
19 import org.eclipse.core.runtime.adaptor.LocationManager;
20 import org.eclipse.osgi.baseadaptor.*;
21 import org.eclipse.osgi.baseadaptor.hooks.AdaptorHook;
22 import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor;
23 import org.eclipse.osgi.framework.console.CommandProvider;
24 import org.eclipse.osgi.framework.debug.Debug;
25 import org.eclipse.osgi.framework.debug.FrameworkDebugOptions;
26 import org.eclipse.osgi.framework.internal.core.Constants;
27 import org.eclipse.osgi.framework.log.FrameworkLog;
28 import org.eclipse.osgi.framework.log.FrameworkLogEntry;
29 import org.eclipse.osgi.internal.baseadaptor.AdaptorUtil;
30 import org.eclipse.osgi.service.datalocation.Location;
31 import org.eclipse.osgi.service.pluginconversion.PluginConverter;
32 import org.eclipse.osgi.service.resolver.PlatformAdmin;
33 import org.eclipse.osgi.service.runnable.ApplicationLauncher;
34 import org.eclipse.osgi.service.urlconversion.URLConverter;
35 import org.osgi.framework.*;
36
37 public class EclipseAdaptorHook implements AdaptorHook, HookConfigurator {
38     /** The SAX factory name */
39     public static final String JavaDoc SAXFACTORYNAME = "javax.xml.parsers.SAXParserFactory"; //$NON-NLS-1$
40
/** The DOM factory name */
41     public static final String JavaDoc DOMFACTORYNAME = "javax.xml.parsers.DocumentBuilderFactory"; //$NON-NLS-1$
42
private static final String JavaDoc RUNTIME_ADAPTOR = FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME + "/eclipseadaptor"; //$NON-NLS-1$
43
private static final String JavaDoc OPTION_CONVERTER = RUNTIME_ADAPTOR + "/converter/debug"; //$NON-NLS-1$
44
private static final String JavaDoc OPTION_LOCATION = RUNTIME_ADAPTOR + "/debug/location"; //$NON-NLS-1$
45

46     private BaseAdaptor adaptor;
47     private boolean noXML = false;
48
49     public void frameworkStart(BundleContext context) throws BundleException {
50         registerEndorsedXMLParser(context);
51         Location location = LocationManager.getUserLocation();
52         Hashtable locationProperties = new Hashtable(1);
53         if (location != null) {
54             locationProperties.put("type", LocationManager.PROP_USER_AREA); //$NON-NLS-1$
55
context.registerService(Location.class.getName(), location, locationProperties);
56         }
57         location = LocationManager.getInstanceLocation();
58         if (location != null) {
59             locationProperties.put("type", LocationManager.PROP_INSTANCE_AREA); //$NON-NLS-1$
60
context.registerService(Location.class.getName(), location, locationProperties);
61         }
62         location = LocationManager.getConfigurationLocation();
63         if (location != null) {
64             locationProperties.put("type", LocationManager.PROP_CONFIG_AREA); //$NON-NLS-1$
65
context.registerService(Location.class.getName(), location, locationProperties);
66         }
67         location = LocationManager.getInstallLocation();
68         if (location != null) {
69             locationProperties.put("type", LocationManager.PROP_INSTALL_AREA); //$NON-NLS-1$
70
context.registerService(Location.class.getName(), location, locationProperties);
71         }
72
73         Dictionary urlProperties = new Hashtable();
74         urlProperties.put("protocol", new String JavaDoc[] {"bundleentry", "bundleresource"}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
75
context.registerService(URLConverter.class.getName(), new URLConverterImpl(), urlProperties);
76
77         AdaptorUtil.register(org.eclipse.osgi.service.environment.EnvironmentInfo.class.getName(), EclipseEnvironmentInfo.getDefault(), context);
78         AdaptorUtil.register(PlatformAdmin.class.getName(), adaptor.getPlatformAdmin(), context);
79         PluginConverter converter = PluginConverterImpl.getDefault();
80         if (converter == null)
81             converter = new PluginConverterImpl(adaptor, context);
82         AdaptorUtil.register(PluginConverter.class.getName(), converter, context);
83         AdaptorUtil.register(CommandProvider.class.getName(), new EclipseCommandProvider(context), context);
84         AdaptorUtil.register(org.eclipse.osgi.service.localization.BundleLocalization.class.getName(), new BundleLocalizationImpl(), context);
85     }
86
87     private void registerEndorsedXMLParser(BundleContext bc) {
88         try {
89             Class.forName(SAXFACTORYNAME);
90             bc.registerService(SAXFACTORYNAME, new SaxParsingService(), new Hashtable());
91             Class.forName(DOMFACTORYNAME);
92             bc.registerService(DOMFACTORYNAME, new DomParsingService(), new Hashtable());
93         } catch (ClassNotFoundException JavaDoc e) {
94             noXML = true;
95             if (Debug.DEBUG && Debug.DEBUG_ENABLED) {
96                 String JavaDoc message = EclipseAdaptorMsg.ECLIPSE_ADAPTOR_ERROR_XML_SERVICE;
97                 adaptor.getFrameworkLog().log(new FrameworkLogEntry(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, FrameworkLogEntry.ERROR, 0, message, 0, e, null));
98             }
99         }
100     }
101
102     private class SaxParsingService implements ServiceFactory {
103         public Object JavaDoc getService(Bundle bundle, ServiceRegistration registration) {
104             return SAXParserFactory.newInstance();
105         }
106
107         public void ungetService(Bundle bundle, ServiceRegistration registration, Object JavaDoc service) {
108             // Do nothing.
109
}
110     }
111
112     private class DomParsingService implements ServiceFactory {
113         public Object JavaDoc getService(Bundle bundle, ServiceRegistration registration) {
114             return DocumentBuilderFactory.newInstance();
115         }
116
117         public void ungetService(Bundle bundle, ServiceRegistration registration, Object JavaDoc service) {
118             // Do nothing.
119
}
120     }
121
122     public void frameworkStop(BundleContext context) throws BundleException {
123         // TODO should unregister the services here
124
printStats();
125         if (!noXML)
126             PluginParser.releaseXMLParsing();
127     }
128
129     private void printStats() {
130         FrameworkDebugOptions debugOptions = FrameworkDebugOptions.getDefault();
131         if (debugOptions == null)
132             return;
133         String JavaDoc registryParsing = debugOptions.getOption("org.eclipse.core.runtime/registry/parsing/timing/value"); //$NON-NLS-1$
134
if (registryParsing != null)
135             MessageHelper.debug("Time spent in registry parsing: " + registryParsing); //$NON-NLS-1$
136
String JavaDoc packageAdminResolution = debugOptions.getOption("debug.packageadmin/timing/value"); //$NON-NLS-1$
137
if (packageAdminResolution != null)
138             System.out.println("Time spent in package admin resolve: " + packageAdminResolution); //$NON-NLS-1$
139
String JavaDoc constraintResolution = debugOptions.getOption("org.eclipse.core.runtime.adaptor/resolver/timing/value"); //$NON-NLS-1$
140
if (constraintResolution != null)
141             System.out.println("Time spent resolving the dependency system: " + constraintResolution); //$NON-NLS-1$
142
}
143
144     public void frameworkStopping(BundleContext context) {
145         // Shutdown the ApplicationLauncher service if it is available.
146
ServiceReference launcherRef = context.getServiceReference(ApplicationLauncher.class.getName());
147         if (launcherRef != null) {
148             ApplicationLauncher launcher = (ApplicationLauncher) context.getService(launcherRef);
149             // this will force a currently running application to stop.
150
launcher.shutdown();
151             context.ungetService(launcherRef);
152         }
153     }
154
155     public void addProperties(Properties properties) {
156         // default to not verify EE at install time
157
if (properties.getProperty(Constants.ECLIPSE_EE_INSTALL_VERIFY) == null)
158             properties.put(Constants.ECLIPSE_EE_INSTALL_VERIFY, "false"); //$NON-NLS-1$
159
// default to enable compatibility boot delegation
160
if (properties.getProperty(Constants.OSGI_COMPATIBILITY_BOOTDELEGATION) == null)
161             properties.put(Constants.OSGI_COMPATIBILITY_BOOTDELEGATION, "true"); //$NON-NLS-1$
162
}
163
164     public URLConnection JavaDoc mapLocationToURLConnection(String JavaDoc location) throws IOException {
165         // do nothing
166
return null;
167     }
168
169     public void handleRuntimeError(Throwable JavaDoc error) {
170         // do nothing
171
}
172
173     public boolean matchDNChain(String JavaDoc pattern, String JavaDoc[] dnChain) {
174         // do nothing
175
return false;
176     }
177
178     public FrameworkLog createFrameworkLog() {
179         // do nothing
180
return null;
181     }
182
183     public void initialize(BaseAdaptor adaptor) {
184         this.adaptor = adaptor;
185         // EnvironmentInfo has to be initialized first to compute defaults for system context (see bug 88925)
186
EclipseEnvironmentInfo.getDefault();
187         setDebugOptions();
188     }
189
190     private void setDebugOptions() {
191         FrameworkDebugOptions options = FrameworkDebugOptions.getDefault();
192         // may be null if debugging is not enabled
193
if (options == null)
194             return;
195         PluginConverterImpl.DEBUG = options.getBooleanOption(OPTION_CONVERTER, false);
196         BasicLocation.DEBUG = options.getBooleanOption(OPTION_LOCATION, false);
197     }
198
199     public void addHooks(HookRegistry hookRegistry) {
200         hookRegistry.addAdaptorHook(this);
201     }
202
203 }
204
Popular Tags