|
Lines 15-29
Link Here
|
| 15 |
|
15 |
|
| 16 |
package org.eclipse.core.internal.net; |
16 |
package org.eclipse.core.internal.net; |
| 17 |
|
17 |
|
| 18 |
import java.util.Hashtable; |
18 |
import java.net.URL; |
| 19 |
|
19 |
import java.util.*; |
|
|
20 |
import org.eclipse.core.internal.runtime.auth.AuthorizationHandler; |
| 20 |
import org.eclipse.core.net.proxy.IProxyService; |
21 |
import org.eclipse.core.net.proxy.IProxyService; |
| 21 |
import org.eclipse.core.runtime.*; |
22 |
import org.eclipse.core.runtime.*; |
| 22 |
import org.eclipse.core.runtime.preferences.InstanceScope; |
23 |
import org.eclipse.core.runtime.preferences.InstanceScope; |
| 23 |
import org.osgi.framework.BundleContext; |
24 |
import org.eclipse.osgi.framework.log.FrameworkLog; |
| 24 |
import org.osgi.framework.ServiceRegistration; |
25 |
import org.eclipse.osgi.framework.log.FrameworkLogEntry; |
|
|
26 |
import org.osgi.framework.*; |
| 27 |
import org.osgi.util.tracker.ServiceTracker; |
| 25 |
|
28 |
|
| 26 |
public class Activator extends Plugin { |
29 |
public class Activator implements BundleActivator { |
| 27 |
/** |
30 |
/** |
| 28 |
* The identifier of the descriptor of this plugin in plugin.xml. |
31 |
* The identifier of the descriptor of this plugin in plugin.xml. |
| 29 |
*/ |
32 |
*/ |
|
Lines 33-95
Link Here
|
| 33 |
|
36 |
|
| 34 |
private static final String PROP_REGISTER_SERVICE = "org.eclipse.net.core.enableProxyService"; //$NON-NLS-1$ |
37 |
private static final String PROP_REGISTER_SERVICE = "org.eclipse.net.core.enableProxyService"; //$NON-NLS-1$ |
| 35 |
|
38 |
|
| 36 |
/** |
39 |
private static BundleContext bundleContext; |
| 37 |
* The instance of this plugin. |
40 |
private static ServiceTracker extensionRegistryTracker; |
| 38 |
*/ |
41 |
private static ServiceTracker logTracker; |
| 39 |
private static Activator instance; |
|
|
| 40 |
|
| 41 |
private ServiceRegistration proxyService; |
42 |
private ServiceRegistration proxyService; |
| 42 |
|
43 |
|
| 43 |
/** |
44 |
/* |
| 44 |
* Constructor for use by the Eclipse platform only. |
45 |
* Return this bundle's context, or null. |
| 45 |
*/ |
46 |
*/ |
| 46 |
public Activator() { |
47 |
public static BundleContext getBundleContext() { |
| 47 |
super(); |
48 |
return bundleContext; |
| 48 |
instance = this; |
|
|
| 49 |
} |
49 |
} |
| 50 |
|
50 |
|
| 51 |
/** |
51 |
/* (non-Javadoc) |
| 52 |
* Returns the instance of this plugin. |
52 |
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) |
| 53 |
* @return the singleton instance of this plug-in class |
|
|
| 54 |
*/ |
53 |
*/ |
| 55 |
static public Activator getInstance() { |
|
|
| 56 |
return instance; |
| 57 |
} |
| 58 |
|
| 59 |
public void start(BundleContext context) throws Exception { |
54 |
public void start(BundleContext context) throws Exception { |
| 60 |
super.start(context); |
55 |
bundleContext = context; |
| 61 |
if (Boolean.valueOf(System.getProperty(PROP_REGISTER_SERVICE, "true")).booleanValue()) { //$NON-NLS-1$ |
56 |
if (Boolean.valueOf(System.getProperty(PROP_REGISTER_SERVICE, "true")).booleanValue()) { //$NON-NLS-1$ |
| 62 |
ProxyManager proxyManager = (ProxyManager)ProxyManager.getProxyManager(); |
57 |
ProxyManager proxyManager = (ProxyManager)ProxyManager.getProxyManager(); |
| 63 |
proxyManager.initialize(); |
58 |
proxyManager.initialize(); |
| 64 |
proxyService = getBundle().getBundleContext().registerService(IProxyService.class.getName(), proxyManager, new Hashtable()); |
59 |
proxyService = getBundleContext().registerService(IProxyService.class.getName(), proxyManager, new Hashtable()); |
| 65 |
} |
60 |
} |
| 66 |
} |
61 |
} |
| 67 |
|
62 |
|
|
|
63 |
/* (non-Javadoc) |
| 64 |
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) |
| 65 |
*/ |
| 68 |
public void stop(BundleContext context) throws Exception { |
66 |
public void stop(BundleContext context) throws Exception { |
| 69 |
if (proxyService != null) { |
67 |
if (proxyService != null) { |
| 70 |
proxyService.unregister(); |
68 |
proxyService.unregister(); |
| 71 |
proxyService = null; |
69 |
proxyService = null; |
| 72 |
} |
70 |
} |
| 73 |
super.stop(context); |
71 |
if (extensionRegistryTracker != null) { |
|
|
72 |
extensionRegistryTracker.close(); |
| 73 |
extensionRegistryTracker = null; |
| 74 |
} |
| 75 |
if (logTracker != null) { |
| 76 |
logTracker.close(); |
| 77 |
logTracker = null; |
| 78 |
} |
| 79 |
bundleContext = null; |
| 74 |
} |
80 |
} |
| 75 |
|
|
|
| 76 |
public static void logError(String message, Throwable exc) { |
| 77 |
IStatus status = new Status(IStatus.ERROR, ID, 0, message, exc); |
| 78 |
|
81 |
|
| 79 |
getInstance().getLog().log(status); |
82 |
/* |
|
|
83 |
* Log the given message as an error. |
| 84 |
*/ |
| 85 |
public static void logError(String message, Throwable exc) { |
| 86 |
log(new Status(IStatus.ERROR, ID, 0, message, exc)); |
| 80 |
} |
87 |
} |
| 81 |
|
88 |
|
|
|
89 |
/* |
| 90 |
* Log the given message as an informational message. |
| 91 |
*/ |
| 82 |
public static void logInfo(String message, Throwable exc) { |
92 |
public static void logInfo(String message, Throwable exc) { |
| 83 |
IStatus status = new Status(IStatus.INFO, ID, 0, message, exc); |
93 |
log(new Status(IStatus.INFO, ID, 0, message, exc)); |
| 84 |
|
|
|
| 85 |
getInstance().getLog().log(status); |
| 86 |
} |
94 |
} |
| 87 |
|
95 |
|
| 88 |
public org.osgi.service.prefs.Preferences getInstancePreferences() { |
96 |
public static org.osgi.service.prefs.Preferences getInstancePreferences() { |
| 89 |
return new InstanceScope().getNode(getBundle().getSymbolicName()); |
97 |
return new InstanceScope().getNode(ID); |
| 90 |
} |
98 |
} |
| 91 |
|
99 |
|
| 92 |
public static void log(int severity, String message, Throwable throwable) { |
100 |
public static void log(int severity, String message, Throwable throwable) { |
| 93 |
getInstance().getLog().log(new Status(severity, ID, 0, message, throwable)); |
101 |
log(new Status(severity, ID, 0, message, throwable)); |
| 94 |
} |
102 |
} |
|
|
103 |
|
| 104 |
/* |
| 105 |
* Return the extension registry. It is acquired lazily. |
| 106 |
*/ |
| 107 |
public static IExtensionRegistry getExtensionRegistry() { |
| 108 |
if (extensionRegistryTracker == null) { |
| 109 |
extensionRegistryTracker = new ServiceTracker(getBundleContext(), IExtensionRegistry.class.getName(), null); |
| 110 |
extensionRegistryTracker.open(); |
| 111 |
} |
| 112 |
return (IExtensionRegistry) extensionRegistryTracker.getService(); |
| 113 |
} |
| 114 |
|
| 115 |
/* |
| 116 |
* TODO: This currently references internal classes but will be replaced by the new security work |
| 117 |
* to be available in Eclipse 3.4. |
| 118 |
*/ |
| 119 |
public static void addAuthorizationInfo(URL serverUrl, String realm, String authScheme, Map info) throws CoreException { |
| 120 |
AuthorizationHandler.addAuthorizationInfo(serverUrl, realm, authScheme, info); |
| 121 |
} |
| 122 |
|
| 123 |
/* |
| 124 |
* TODO: This currently references internal classes but will be replaced by the new security work |
| 125 |
* to be available in Eclipse 3.4. |
| 126 |
*/ |
| 127 |
public static Map getAuthorizationInfo(URL serverUrl, String realm, String authScheme) { |
| 128 |
return AuthorizationHandler.getAuthorizationInfo(serverUrl, realm, authScheme); |
| 129 |
} |
| 130 |
|
| 131 |
/* |
| 132 |
* TODO: This currently references internal classes but will be replaced by the new security work |
| 133 |
* to be available in Eclipse 3.4. |
| 134 |
*/ |
| 135 |
public static void flushAuthorizationInfo(URL serverUrl, String realm, String authScheme) throws CoreException { |
| 136 |
AuthorizationHandler.flushAuthorizationInfo(serverUrl, realm, authScheme); |
| 137 |
} |
| 138 |
|
| 139 |
/* |
| 140 |
* Log the given status to the log file. If the log is not available, log the status to the console. |
| 141 |
*/ |
| 142 |
public static void log(IStatus status) { |
| 143 |
if (logTracker == null) { |
| 144 |
logTracker = new ServiceTracker(getBundleContext(), FrameworkLog.class.getName(), null); |
| 145 |
logTracker.open(); |
| 146 |
} |
| 147 |
FrameworkLog log = (FrameworkLog) logTracker.getService(); |
| 148 |
if (log != null) { |
| 149 |
log.log(getLog(status)); |
| 150 |
} else { |
| 151 |
System.out.println(status.getMessage()); |
| 152 |
if (status.getException() != null) |
| 153 |
status.getException().printStackTrace(); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Copied from PlatformLogWriter in core runtime. |
| 159 |
*/ |
| 160 |
private static FrameworkLogEntry getLog(IStatus status) { |
| 161 |
Throwable t = status.getException(); |
| 162 |
ArrayList childlist = new ArrayList(); |
| 163 |
|
| 164 |
int stackCode = t instanceof CoreException ? 1 : 0; |
| 165 |
// ensure a substatus inside a CoreException is properly logged |
| 166 |
if (stackCode == 1) { |
| 167 |
IStatus coreStatus = ((CoreException) t).getStatus(); |
| 168 |
if (coreStatus != null) { |
| 169 |
childlist.add(getLog(coreStatus)); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
if (status.isMultiStatus()) { |
| 174 |
IStatus[] children = status.getChildren(); |
| 175 |
for (int i = 0; i < children.length; i++) { |
| 176 |
childlist.add(getLog(children[i])); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
FrameworkLogEntry[] children = (FrameworkLogEntry[]) (childlist.size() == 0 ? null : childlist.toArray(new FrameworkLogEntry[childlist.size()])); |
| 181 |
|
| 182 |
return new FrameworkLogEntry(status.getPlugin(), status.getSeverity(), status.getCode(), status.getMessage(), stackCode, t, children); |
| 183 |
} |
| 184 |
|
| 95 |
} |
185 |
} |