KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > runtime > CompatibilityHelper


1 /*******************************************************************************
2  * Copyright (c) 2003, 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 package org.eclipse.core.internal.runtime;
12
13 import java.lang.reflect.Method JavaDoc;
14 import org.eclipse.core.runtime.*;
15 import org.osgi.framework.Bundle;
16
17 /**
18  * This class isolates calls to the backward compatibility layer.
19  * It uses reflection so it can be loaded with success even in the absence of
20  * the compatibility plugin.
21  *
22  * @deprecated Marked as deprecated to suppress deprecation warnings.
23  */

24 public class CompatibilityHelper {
25     private static final String JavaDoc OPTION_DEBUG_COMPATIBILITY = Platform.PI_RUNTIME + "/compatibility/debug"; //$NON-NLS-1$
26
public static final boolean DEBUG = Boolean.TRUE.toString().equalsIgnoreCase(InternalPlatform.getDefault().getOption(OPTION_DEBUG_COMPATIBILITY));
27     public static final String JavaDoc PI_RUNTIME_COMPATIBILITY = "org.eclipse.core.runtime.compatibility"; //$NON-NLS-1$
28
private static Bundle compatibility = null;
29
30     public synchronized static void nullCompatibility() {
31         compatibility = null;
32     }
33     
34     public synchronized static Bundle initializeCompatibility() {
35         // if compatibility is stale (has been uninstalled or unresolved)
36
// then we try to get a new resolved compatibility bundle
37
if (compatibility == null || (compatibility.getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED | Bundle.STOPPING)) != 0)
38             compatibility = org.eclipse.core.internal.runtime.InternalPlatform.getDefault().getBundle(PI_RUNTIME_COMPATIBILITY);
39         return compatibility;
40     }
41
42     public static void setPlugin(IPluginDescriptor descriptor, Plugin plugin) {
43         //Here we use reflection so the runtime code can run without the compatibility plugin
44
if (initializeCompatibility() == null)
45             throw new IllegalStateException JavaDoc();
46
47         try {
48             Method JavaDoc setPlugin = descriptor.getClass().getMethod("setPlugin", new Class JavaDoc[] {Plugin.class}); //$NON-NLS-1$
49
setPlugin.invoke(descriptor, new Object JavaDoc[] {plugin});
50         } catch (Exception JavaDoc e) {
51             //Ignore the exceptions
52
}
53     }
54
55     public synchronized static IPluginDescriptor getPluginDescriptor(String JavaDoc pluginId) {
56         //Here we use reflection so the runtime code can run without the compatibility
57
initializeCompatibility();
58         if (compatibility == null)
59             throw new IllegalStateException JavaDoc();
60
61         Class JavaDoc oldInternalPlatform = null;
62         try {
63             oldInternalPlatform = compatibility.loadClass("org.eclipse.core.internal.plugins.InternalPlatform"); //$NON-NLS-1$
64
Method JavaDoc getPluginDescriptor = oldInternalPlatform.getMethod("getPluginDescriptor", new Class JavaDoc[] {String JavaDoc.class}); //$NON-NLS-1$
65
return (IPluginDescriptor) getPluginDescriptor.invoke(oldInternalPlatform, new Object JavaDoc[] {pluginId});
66         } catch (Exception JavaDoc e) {
67             if (DEBUG) {
68                 String JavaDoc msg = "Error running compatibility code"; //$NON-NLS-1$
69
IStatus error = new Status(IStatus.ERROR, Platform.PI_RUNTIME, 1, msg, e);
70                 InternalPlatform.getDefault().log(error);
71             }
72             //Ignore the exceptions, return null
73
}
74         return null;
75     }
76
77     public synchronized static void setActive(IPluginDescriptor descriptor) {
78         initializeCompatibility();
79         if (compatibility == null)
80             throw new IllegalStateException JavaDoc();
81
82         try {
83             Method JavaDoc setPlugin = descriptor.getClass().getMethod("setActive", null); //$NON-NLS-1$
84
setPlugin.invoke(descriptor, null);
85         } catch (Exception JavaDoc e) {
86             //Ignore the exceptions
87
}
88     }
89
90     public synchronized static boolean hasPluginObject(IPluginDescriptor descriptor) {
91         initializeCompatibility();
92         if (compatibility == null)
93             throw new IllegalStateException JavaDoc();
94
95         Boolean JavaDoc result = Boolean.FALSE;
96         try {
97             Method JavaDoc setPlugin = descriptor.getClass().getMethod("hasPluginObject", null); //$NON-NLS-1$
98
result = (Boolean JavaDoc) setPlugin.invoke(descriptor, null);
99         } catch (Exception JavaDoc e) {
100             //Ignore the exceptions
101
}
102         return result.booleanValue();
103     }
104 }
105
Popular Tags