|
Lines 15-50
Link Here
|
| 15 |
|
15 |
|
| 16 |
package org.eclipse.core.internal.net; |
16 |
package org.eclipse.core.internal.net; |
| 17 |
|
17 |
|
|
|
18 |
import java.util.ArrayList; |
| 18 |
import java.util.Hashtable; |
19 |
import java.util.Hashtable; |
| 19 |
|
|
|
| 20 |
import org.eclipse.core.net.proxy.IProxyService; |
20 |
import org.eclipse.core.net.proxy.IProxyService; |
| 21 |
import org.eclipse.core.runtime.*; |
21 |
import org.eclipse.core.runtime.*; |
| 22 |
import org.eclipse.core.runtime.preferences.ConfigurationScope; |
22 |
import org.eclipse.core.runtime.preferences.ConfigurationScope; |
|
|
23 |
import org.eclipse.osgi.framework.log.FrameworkLog; |
| 24 |
import org.eclipse.osgi.framework.log.FrameworkLogEntry; |
| 23 |
import org.eclipse.osgi.service.datalocation.Location; |
25 |
import org.eclipse.osgi.service.datalocation.Location; |
| 24 |
import org.osgi.framework.BundleContext; |
26 |
import org.osgi.framework.*; |
| 25 |
import org.osgi.framework.Filter; |
|
|
| 26 |
import org.osgi.framework.InvalidSyntaxException; |
| 27 |
import org.osgi.framework.ServiceRegistration; |
| 28 |
import org.osgi.util.tracker.ServiceTracker; |
27 |
import org.osgi.util.tracker.ServiceTracker; |
| 29 |
|
28 |
|
| 30 |
public class Activator extends Plugin { |
29 |
public class Activator implements BundleActivator { |
| 31 |
/** |
30 |
/** |
| 32 |
* The identifier of the descriptor of this plugin in plugin.xml. |
31 |
* The identifier of the descriptor of this plugin in plugin.xml. |
| 33 |
*/ |
32 |
*/ |
| 34 |
public static final String ID = "org.eclipse.core.net"; //$NON-NLS-1$ |
33 |
public static final String ID = "org.eclipse.core.net"; //$NON-NLS-1$ |
| 35 |
|
|
|
| 36 |
public static final String PT_AUTHENTICATOR = "authenticator"; //$NON-NLS-1$ |
| 37 |
|
| 38 |
private static final String PROP_REGISTER_SERVICE = "org.eclipse.net.core.enableProxyService"; //$NON-NLS-1$ |
| 39 |
|
34 |
|
| 40 |
/** |
35 |
/** |
| 41 |
* The instance of this plugin. |
36 |
* The instance of this plugin. |
| 42 |
*/ |
37 |
*/ |
| 43 |
private static Activator instance; |
38 |
private static Activator instance; |
| 44 |
|
39 |
|
| 45 |
private ServiceRegistration proxyService; |
40 |
private static ServiceTracker logTracker; |
| 46 |
|
41 |
|
|
|
42 |
private static final String PROP_REGISTER_SERVICE = "org.eclipse.net.core.enableProxyService"; //$NON-NLS-1$ |
| 43 |
|
| 44 |
public static final String PT_AUTHENTICATOR = "authenticator"; //$NON-NLS-1$ |
| 45 |
private BundleContext bundleContext; |
| 47 |
private ServiceTracker instanceLocationTracker; |
46 |
private ServiceTracker instanceLocationTracker; |
|
|
47 |
private ServiceRegistration proxyService; |
| 48 |
|
| 49 |
/** |
| 50 |
* Returns the instance of this plugin. |
| 51 |
* @return the singleton instance of this plug-in class |
| 52 |
*/ |
| 53 |
static public Activator getInstance() { |
| 54 |
return instance; |
| 55 |
} |
| 56 |
|
| 57 |
public static void log(int severity, String message, Throwable throwable) { |
| 58 |
getInstance().log(new Status(severity, ID, 0, message, throwable)); |
| 59 |
} |
| 60 |
|
| 61 |
public static void logError(String message, Throwable exc) { |
| 62 |
getInstance().log(new Status(IStatus.ERROR, ID, 0, message, exc)); |
| 63 |
} |
| 64 |
|
| 65 |
public static void logInfo(String message, Throwable exc) { |
| 66 |
getInstance().log(new Status(IStatus.INFO, ID, 0, message, exc)); |
| 67 |
} |
| 48 |
|
68 |
|
| 49 |
/** |
69 |
/** |
| 50 |
* Constructor for use by the Eclipse platform only. |
70 |
* Constructor for use by the Eclipse platform only. |
|
Lines 55-123
Link Here
|
| 55 |
} |
75 |
} |
| 56 |
|
76 |
|
| 57 |
/** |
77 |
/** |
| 58 |
* Returns the instance of this plugin. |
78 |
* Copied from PlatformLogWriter in core runtime. |
| 59 |
* @return the singleton instance of this plug-in class |
|
|
| 60 |
*/ |
79 |
*/ |
| 61 |
static public Activator getInstance() { |
80 |
private FrameworkLogEntry getLog(IStatus status) { |
| 62 |
return instance; |
81 |
Throwable t = status.getException(); |
|
|
82 |
ArrayList childlist = new ArrayList(); |
| 83 |
|
| 84 |
int stackCode = t instanceof CoreException ? 1 : 0; |
| 85 |
// ensure a substatus inside a CoreException is properly logged |
| 86 |
if (stackCode == 1) { |
| 87 |
IStatus coreStatus = ((CoreException) t).getStatus(); |
| 88 |
if (coreStatus != null) { |
| 89 |
childlist.add(getLog(coreStatus)); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if (status.isMultiStatus()) { |
| 94 |
IStatus[] children = status.getChildren(); |
| 95 |
for (int i = 0; i < children.length; i++) { |
| 96 |
childlist.add(getLog(children[i])); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
FrameworkLogEntry[] children = (FrameworkLogEntry[]) (childlist.size() == 0 ? null : childlist.toArray(new FrameworkLogEntry[childlist.size()])); |
| 101 |
|
| 102 |
return new FrameworkLogEntry(status.getPlugin(), status.getSeverity(), status.getCode(), status.getMessage(), stackCode, t, children); |
| 103 |
} |
| 104 |
|
| 105 |
public org.osgi.service.prefs.Preferences getPreferences() { |
| 106 |
return new ConfigurationScope().getNode(ID); |
| 107 |
} |
| 108 |
|
| 109 |
public boolean instanceLocationAvailable() { |
| 110 |
Location instanceLocation = (Location) instanceLocationTracker.getService(); |
| 111 |
return (instanceLocation != null && instanceLocation.isSet()); |
| 112 |
} |
| 113 |
|
| 114 |
/* |
| 115 |
* Log the given status to the log file. If the log is not available, log the status to the console. |
| 116 |
*/ |
| 117 |
private void log(IStatus status) { |
| 118 |
if (logTracker == null) { |
| 119 |
logTracker = new ServiceTracker(bundleContext, FrameworkLog.class.getName(), null); |
| 120 |
logTracker.open(); |
| 121 |
} |
| 122 |
FrameworkLog log = (FrameworkLog) logTracker.getService(); |
| 123 |
if (log != null) { |
| 124 |
log.log(getLog(status)); |
| 125 |
} else { |
| 126 |
System.out.println(status.getMessage()); |
| 127 |
if (status.getException() != null) |
| 128 |
status.getException().printStackTrace(); |
| 129 |
} |
| 63 |
} |
130 |
} |
| 64 |
|
131 |
|
| 65 |
public void start(BundleContext context) throws Exception { |
132 |
public void start(BundleContext context) throws Exception { |
| 66 |
super.start(context); |
133 |
this.bundleContext = context; |
| 67 |
|
|
|
| 68 |
Filter filter = null; |
134 |
Filter filter = null; |
| 69 |
try { |
135 |
try { |
| 70 |
filter = context.createFilter(Location.INSTANCE_FILTER); |
136 |
filter = context.createFilter(Location.INSTANCE_FILTER); |
| 71 |
} catch (InvalidSyntaxException e) { |
137 |
} catch (InvalidSyntaxException e) { |
| 72 |
// ignore this. It should never happen as we have tested the above format. |
138 |
// ignore this. It should never happen as we have tested the above format. |
| 73 |
} |
139 |
} |
| 74 |
instanceLocationTracker = new ServiceTracker(context, filter, null); |
140 |
instanceLocationTracker = new ServiceTracker(context, filter, null); |
| 75 |
instanceLocationTracker.open(); |
141 |
instanceLocationTracker.open(); |
| 76 |
|
142 |
|
| 77 |
|
|
|
| 78 |
if (Boolean.valueOf(System.getProperty(PROP_REGISTER_SERVICE, "true")).booleanValue()) { //$NON-NLS-1$ |
143 |
if (Boolean.valueOf(System.getProperty(PROP_REGISTER_SERVICE, "true")).booleanValue()) { //$NON-NLS-1$ |
| 79 |
ProxyManager proxyManager = (ProxyManager)ProxyManager.getProxyManager(); |
144 |
ProxyManager proxyManager = (ProxyManager) ProxyManager.getProxyManager(); |
| 80 |
proxyManager.initialize(); |
145 |
proxyManager.initialize(); |
| 81 |
proxyService = context.registerService(IProxyService.class.getName(), proxyManager, new Hashtable()); |
146 |
proxyService = context.registerService(IProxyService.class.getName(), proxyManager, new Hashtable()); |
| 82 |
} |
147 |
} |
| 83 |
} |
148 |
} |
| 84 |
|
149 |
|
| 85 |
public void stop(BundleContext context) throws Exception { |
150 |
public void stop(BundleContext context) throws Exception { |
| 86 |
if (proxyService != null) { |
151 |
if (proxyService != null) { |
| 87 |
proxyService.unregister(); |
152 |
proxyService.unregister(); |
| 88 |
proxyService = null; |
153 |
proxyService = null; |
| 89 |
} |
154 |
} |
| 90 |
|
155 |
|
| 91 |
if (instanceLocationTracker != null) { |
156 |
if (instanceLocationTracker != null) { |
| 92 |
instanceLocationTracker.close(); |
157 |
instanceLocationTracker.close(); |
| 93 |
instanceLocationTracker = null; |
158 |
instanceLocationTracker = null; |
| 94 |
} |
159 |
} |
| 95 |
|
|
|
| 96 |
super.stop(context); |
| 97 |
} |
| 98 |
|
| 99 |
public static void logError(String message, Throwable exc) { |
| 100 |
IStatus status = new Status(IStatus.ERROR, ID, 0, message, exc); |
| 101 |
|
| 102 |
getInstance().getLog().log(status); |
| 103 |
} |
| 104 |
|
| 105 |
public static void logInfo(String message, Throwable exc) { |
| 106 |
IStatus status = new Status(IStatus.INFO, ID, 0, message, exc); |
| 107 |
|
| 108 |
getInstance().getLog().log(status); |
| 109 |
} |
| 110 |
|
| 111 |
public org.osgi.service.prefs.Preferences getPreferences() { |
| 112 |
return new ConfigurationScope().getNode(getBundle().getSymbolicName()); |
| 113 |
} |
| 114 |
|
| 115 |
public static void log(int severity, String message, Throwable throwable) { |
| 116 |
getInstance().getLog().log(new Status(severity, ID, 0, message, throwable)); |
| 117 |
} |
| 118 |
|
| 119 |
public boolean instanceLocationAvailable() { |
| 120 |
Location instanceLocation = (Location) instanceLocationTracker.getService(); |
| 121 |
return (instanceLocation != null && instanceLocation.isSet()); |
| 122 |
} |
160 |
} |
| 123 |
} |
161 |
} |