KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > core > UpdateCore


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.update.internal.core;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.net.URL JavaDoc;
16
17 import org.eclipse.core.net.proxy.IProxyService;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Platform;
21 import org.eclipse.core.runtime.Plugin;
22 import org.eclipse.osgi.internal.provisional.verifier.CertificateVerifierFactory;
23 import org.eclipse.update.configuration.IInstallConfiguration;
24 import org.eclipse.update.configurator.ConfiguratorUtils;
25 import org.eclipse.update.configurator.IPlatformConfiguration;
26 import org.eclipse.update.core.IFeature;
27 import org.eclipse.update.core.IImport;
28 import org.eclipse.update.core.JarContentReference;
29 import org.eclipse.update.core.Utilities;
30 import org.eclipse.update.internal.core.connection.ConnectionThreadManagerFactory;
31 import org.osgi.framework.BundleContext;
32 import org.osgi.service.packageadmin.PackageAdmin;
33 import org.osgi.util.tracker.ServiceTracker;
34
35 /**
36  * The main plugin class to be used in the desktop.
37  */

38 public class UpdateCore extends Plugin {
39
40     // debug options
41
public static boolean DEBUG;
42     public static boolean DEBUG_SHOW_INSTALL;
43     public static boolean DEBUG_SHOW_PARSING;
44     public static boolean DEBUG_SHOW_WARNINGS;
45     public static boolean DEBUG_SHOW_CONFIGURATION;
46     public static boolean DEBUG_SHOW_TYPE;
47     public static boolean DEBUG_SHOW_WEB;
48     public static boolean DEBUG_SHOW_IHANDLER;
49     public static boolean DEBUG_SHOW_RECONCILER;
50         
51     private static final String JavaDoc PREFIX = "org.eclipse.update.core"; //$NON-NLS-1$
52
public static final String JavaDoc P_HISTORY_SIZE = PREFIX + ".historySize"; //$NON-NLS-1$
53
public static final String JavaDoc P_CHECK_SIGNATURE = PREFIX + ".checkSignature"; //$NON-NLS-1$
54
public static final String JavaDoc P_AUTOMATICALLY_CHOOSE_MIRROR = PREFIX + ".automaticallyChooseMirror"; //$NON-NLS-1$
55
public static final String JavaDoc P_UPDATE_VERSIONS = PREFIX + ".updateVersions"; //$NON-NLS-1$
56
public static final String JavaDoc EQUIVALENT_VALUE = "equivalent"; //$NON-NLS-1$
57
public static final String JavaDoc COMPATIBLE_VALUE = "compatible"; //$NON-NLS-1$
58

59     public static int DEFAULT_HISTORY = 100;//Integer.MAX_VALUE;
60

61     //The shared instance.
62
private static UpdateCore plugin;
63
64     //log
65
private static UpdateManagerLogWriter log;
66     private static final String JavaDoc LOG_FILE="install.log"; //$NON-NLS-1$
67

68     // bundle data
69
private BundleContext context;
70     private ServiceTracker pkgAdminTracker;
71     private ServiceTracker verifierFactoryTracker;
72     private ServiceTracker proxyTracker;
73     
74     // Session
75
private UpdateSession updateSession = null;
76     
77     /**
78      * The constructor.
79      */

80     public UpdateCore() {
81         plugin = this;
82     }
83     
84
85     /**
86      * Returns the shared instance.
87      */

88     public static UpdateCore getPlugin() {
89         return plugin;
90     }
91     
92
93     private boolean getBooleanDebugOption(String JavaDoc flag, boolean dflt) {
94         String JavaDoc result = Platform.getDebugOption(flag);
95         if (result == null)
96             return dflt;
97         else
98             return result.trim().equalsIgnoreCase("true"); //$NON-NLS-1$
99
}
100
101     /**
102      * dumps a String in the trace
103      */

104     public static void debug(String JavaDoc s) {
105         StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
106         msg.append(getPlugin().toString());
107         msg.append("^"); //$NON-NLS-1$
108
msg.append(Integer.toHexString(Thread.currentThread().hashCode()));
109         msg.append(" "); //$NON-NLS-1$
110
msg.append(s);
111         System.out.println(msg.toString());
112     }
113     
114     /**
115      * Dumps a String in the log if WARNING is set to true
116      */

117     public static void warn(String JavaDoc s) {
118         if (DEBUG && DEBUG_SHOW_WARNINGS) {
119             if (s!=null){
120                 s="WARNING: "+s; //$NON-NLS-1$
121
}
122             log(s, null);
123         }
124     }
125
126     /**
127      * Dumps an exception in the log if WARNING is set to true
128      *
129      * @param s log string
130      * @param e exception to be logged
131      * @since 2.0
132      */

133     public static void warn(String JavaDoc s, Throwable JavaDoc e) {
134         if (DEBUG && DEBUG_SHOW_WARNINGS){
135             if (s!=null){
136                 s="UPDATE MANAGER INFO: "+s; //$NON-NLS-1$
137
}
138             log(s,e);
139         }
140     }
141             
142     /**
143      * Logs a status
144      */

145     public static void log(IStatus status){
146         UpdateCore.getPlugin().getLog().log(status);
147     }
148     
149     /**
150      * Logs an error
151      */

152     public static void log(Throwable JavaDoc e){
153         log("",e); //$NON-NLS-1$
154
}
155     
156     /**
157      * Logs a string and an error
158      */

159     public static void log(String JavaDoc msg, Throwable JavaDoc e){
160         IStatus status = null;
161         if (e instanceof CoreException)
162             status = ((CoreException)e).getStatus();
163         else
164             status = Utilities.newCoreException(msg,e).getStatus();
165         if (status!=null)
166             log(status);
167     }
168     /*
169      * Method log.
170      * @param newConfiguration
171      */

172     public static void log(IInstallConfiguration newConfiguration) {
173         if (log!=null)
174             log.log(newConfiguration);
175     }
176
177     /*
178      * Get update log location relative to platform configuration
179      */

180     private static File JavaDoc getInstallLogFile() throws IOException JavaDoc {
181         
182         IPlatformConfiguration config = ConfiguratorUtils.getCurrentPlatformConfiguration();
183         URL JavaDoc configurationLocation = config.getConfigurationLocation();
184         if (configurationLocation==null){
185             warn("Unable to retrieve location for update manager log file"); //$NON-NLS-1$
186
return null;
187         }
188 // URL configLocation = Platform.resolve(configurationLocation);
189
File JavaDoc updateStateLocation = null;
190
191         if ("file".equalsIgnoreCase(configurationLocation.getProtocol())) { //$NON-NLS-1$
192
File JavaDoc path = new File JavaDoc(configurationLocation.getFile());
193             updateStateLocation = new File JavaDoc(path.getParentFile(), LOG_FILE);
194         }
195         return updateStateLocation;
196     }
197
198     /**
199      * Sends the GET request to the server and returns the server's
200      * response.
201      *
202      * @param url the URL to open on the server
203      * @return the server's response
204      * @throws IOException if an I/O error occurs. Reasons include:
205      * <ul>
206      * <li>The client is closed.
207      * <li>The client could not connect to the server
208      * <li>An I/O error occurs while communicating with the server
209      * <ul>
210      */

211
212     
213     /*
214      * Returns true if the feature is a patch
215      */

216     public static boolean isPatch(IFeature candidate) {
217         IImport[] imports = candidate.getImports();
218
219         for (int i = 0; i < imports.length; i++) {
220             IImport iimport = imports[i];
221             if (iimport.isPatch())
222                 return true;
223         }
224         return false;
225     }
226     
227     /* (non-Javadoc)
228      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
229      */

230     public void start(BundleContext context) throws Exception JavaDoc {
231         super.start(context);
232         this.context = context;
233
234         DEBUG = getBooleanDebugOption("org.eclipse.update.core/debug", false); //$NON-NLS-1$
235

236         if (DEBUG) {
237             DEBUG_SHOW_WARNINGS = getBooleanDebugOption("org.eclipse.update.core/debug/warning", false); //$NON-NLS-1$
238
DEBUG_SHOW_PARSING = getBooleanDebugOption("org.eclipse.update.core/debug/parsing", false); //$NON-NLS-1$
239
DEBUG_SHOW_INSTALL = getBooleanDebugOption("org.eclipse.update.core/debug/install", false); //$NON-NLS-1$
240
DEBUG_SHOW_CONFIGURATION = getBooleanDebugOption("org.eclipse.update.core/debug/configuration", false); //$NON-NLS-1$
241
DEBUG_SHOW_TYPE = getBooleanDebugOption("org.eclipse.update.core/debug/type", false); //$NON-NLS-1$
242
DEBUG_SHOW_WEB = getBooleanDebugOption("org.eclipse.update.core/debug/web", false); //$NON-NLS-1$
243
DEBUG_SHOW_IHANDLER = getBooleanDebugOption("org.eclipse.update.core/debug/installhandler", false); //$NON-NLS-1$
244
DEBUG_SHOW_RECONCILER = getBooleanDebugOption("org.eclipse.update.core/debug/reconciler", false); //$NON-NLS-1$
245
}
246         
247         //
248
try {
249             File JavaDoc logFile = getInstallLogFile();
250             if (logFile!=null)
251                 log = new UpdateManagerLogWriter(logFile);
252         } catch (IOException JavaDoc e){
253             warn("",e); //$NON-NLS-1$
254
}
255     }
256     /* (non-Javadoc)
257      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
258      */

259     public void stop(BundleContext context) throws Exception JavaDoc {
260         super.stop(context);
261         
262         JarContentReference.shutdown(); // make sure we are not leaving jars open
263
Utilities.shutdown(); // cleanup temp area
264
if (log!=null)
265             log.shutdown();
266         
267         ConnectionThreadManagerFactory.getConnectionManager().shutdown();
268
269         
270         this.context = null;
271         if (pkgAdminTracker != null) {
272             pkgAdminTracker.close();
273             pkgAdminTracker = null;
274         }
275         if (verifierFactoryTracker != null) {
276             verifierFactoryTracker.close();
277             verifierFactoryTracker = null;
278         }
279         if (proxyTracker != null) {
280             proxyTracker.close();
281             proxyTracker = null;
282         }
283     }
284     
285     BundleContext getBundleContext() {
286         return context;
287     }
288     
289     PackageAdmin getPackageAdmin() {
290         if (pkgAdminTracker == null) {
291             pkgAdminTracker = new ServiceTracker(context, PackageAdmin.class.getName(), null);
292             pkgAdminTracker.open();
293         }
294         return (PackageAdmin)pkgAdminTracker.getService();
295     }
296     
297     public IProxyService getProxyService() {
298         if (proxyTracker == null) {
299             proxyTracker=new ServiceTracker(getBundle().getBundleContext(),
300                     IProxyService.class.getName(), null);
301             proxyTracker.open();
302         }
303         return (IProxyService)proxyTracker.getService();
304     }
305
306
307     public CertificateVerifierFactory getVerifierFactory() {
308         if (verifierFactoryTracker == null) {
309             verifierFactoryTracker = new ServiceTracker(context, CertificateVerifierFactory.class.getName(), null);
310             verifierFactoryTracker.open();
311         }
312         return (CertificateVerifierFactory)verifierFactoryTracker.getService();
313     }
314     
315     public UpdateSession getUpdateSession() {
316         synchronized(UpdateSession.class) {
317             if (updateSession == null) {
318                 updateSession = new UpdateSession();
319             }
320         }
321         return updateSession;
322     }
323 }
324
Popular Tags