KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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 org.eclipse.core.runtime.adaptor.*;
18 import org.eclipse.osgi.baseadaptor.*;
19 import org.eclipse.osgi.baseadaptor.hooks.AdaptorHook;
20 import org.eclipse.osgi.framework.internal.core.Constants;
21 import org.eclipse.osgi.framework.internal.core.FrameworkProperties;
22 import org.eclipse.osgi.framework.log.FrameworkLog;
23 import org.eclipse.osgi.internal.baseadaptor.AdaptorUtil;
24 import org.eclipse.osgi.service.datalocation.Location;
25 import org.osgi.framework.BundleContext;
26 import org.osgi.framework.BundleException;
27
28 public class EclipseLogHook implements HookConfigurator, AdaptorHook {
29     // The eclipse log file extension */
30
private static final String JavaDoc LOG_EXT = ".log"; //$NON-NLS-1$
31
BaseAdaptor adaptor;
32
33     public void addHooks(HookRegistry hookRegistry) {
34         hookRegistry.addAdaptorHook(this);
35     }
36
37     public void initialize(BaseAdaptor adaptor) {
38         this.adaptor = adaptor;
39     }
40
41     public void frameworkStart(BundleContext context) throws BundleException {
42         AdaptorUtil.register(FrameworkLog.class.getName(), adaptor.getFrameworkLog(), context);
43         registerPerformanceLog(context);
44     }
45
46     public void frameworkStop(BundleContext context) throws BundleException {
47         // TODO should unregister service registered a frameworkStart
48
}
49
50     public void frameworkStopping(BundleContext context) {
51         // do nothing
52

53     }
54
55     public void addProperties(Properties properties) {
56         // do nothing
57
}
58
59     public URLConnection JavaDoc mapLocationToURLConnection(String JavaDoc location) throws IOException {
60         // do nothing
61
return null;
62     }
63
64     public void handleRuntimeError(Throwable JavaDoc error) {
65         // TODO Auto-generated method stub
66

67     }
68
69     public boolean matchDNChain(String JavaDoc pattern, String JavaDoc[] dnChain) {
70         // do nothing
71
return false;
72     }
73
74     public FrameworkLog createFrameworkLog() {
75         FrameworkLog frameworkLog;
76         String JavaDoc logFileProp = FrameworkProperties.getProperty(EclipseStarter.PROP_LOGFILE);
77         if (logFileProp != null) {
78             frameworkLog = new EclipseLog(new File(logFileProp));
79         } else {
80             Location location = LocationManager.getConfigurationLocation();
81             File configAreaDirectory = null;
82             if (location != null)
83                 // TODO assumes the URL is a file: url
84
configAreaDirectory = new File(location.getURL().getFile());
85
86             if (configAreaDirectory != null) {
87                 String JavaDoc logFileName = Long.toString(System.currentTimeMillis()) + EclipseLogHook.LOG_EXT;
88                 File logFile = new File(configAreaDirectory, logFileName);
89                 FrameworkProperties.setProperty(EclipseStarter.PROP_LOGFILE, logFile.getAbsolutePath());
90                 frameworkLog = new EclipseLog(logFile);
91             } else
92                 frameworkLog = new EclipseLog();
93         }
94         if ("true".equals(FrameworkProperties.getProperty(EclipseStarter.PROP_CONSOLE_LOG))) //$NON-NLS-1$
95
frameworkLog.setConsoleLog(true);
96         return frameworkLog;
97     }
98
99     private void registerPerformanceLog(BundleContext context) {
100         Object JavaDoc service = createPerformanceLog();
101         String JavaDoc serviceName = FrameworkLog.class.getName();
102         Hashtable serviceProperties = new Hashtable(7);
103         Dictionary headers = context.getBundle().getHeaders();
104
105         serviceProperties.put(Constants.SERVICE_VENDOR, headers.get(Constants.BUNDLE_VENDOR));
106         serviceProperties.put(Constants.SERVICE_RANKING, new Integer JavaDoc(Integer.MIN_VALUE));
107         serviceProperties.put(Constants.SERVICE_PID, context.getBundle().getBundleId() + '.' + service.getClass().getName());
108         serviceProperties.put(FrameworkLog.SERVICE_PERFORMANCE, Boolean.TRUE.toString());
109
110         context.registerService(serviceName, service, serviceProperties);
111     }
112
113     private FrameworkLog createPerformanceLog() {
114         String JavaDoc logFileProp = FrameworkProperties.getProperty(EclipseStarter.PROP_LOGFILE);
115         if (logFileProp != null) {
116             int lastSlash = logFileProp.lastIndexOf(File.separatorChar);
117             if (lastSlash > 0) {
118                 String JavaDoc logFile = logFileProp.substring(0, lastSlash + 1) + "performance.log"; //$NON-NLS-1$
119
return new EclipseLog(new File(logFile));
120             }
121         }
122         //if all else fails, write to std err
123
return new EclipseLog(new PrintWriter(System.err));
124     }
125 }
126
Popular Tags