KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > JonasLauncherPlugin


1 package com.bull.eclipse.jonas;
2
3 /*
4  * (c) Copyright Bull SA 2003.
5  * All Rights Reserved.
6  */

7
8 import java.io.PrintWriter JavaDoc;
9 import java.io.StringWriter JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.MissingResourceException JavaDoc;
14 import java.util.PropertyResourceBundle JavaDoc;
15 import java.util.ResourceBundle JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
17
18 import org.eclipse.core.resources.IResourceChangeEvent;
19 import org.eclipse.core.resources.IWorkspace;
20 import org.eclipse.core.resources.ResourcesPlugin;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.ILog;
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.core.runtime.IPluginDescriptor;
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.core.runtime.Path;
27 import org.eclipse.core.runtime.Status;
28 import org.eclipse.debug.core.DebugEvent;
29 import org.eclipse.debug.core.DebugPlugin;
30 import org.eclipse.debug.core.IDebugEventSetListener;
31 import org.eclipse.debug.core.model.IProcess;
32 import org.eclipse.jdt.core.JavaCore;
33 import org.eclipse.jdt.core.JavaModelException;
34 import org.eclipse.jdt.launching.JavaRuntime;
35 import org.eclipse.jface.preference.IPreferenceStore;
36 import org.eclipse.ui.plugin.AbstractUIPlugin;
37
38 import com.bull.eclipse.jonas.actions.StartActionDelegate;
39 import com.bull.eclipse.jonas.editors.ProjectListElement;
40
41
42 /**
43  * The main plugin class to be used in the desktop.
44  */

45 public class JonasLauncherPlugin extends AbstractUIPlugin implements IDebugEventSetListener {
46
47     public static final String JavaDoc PLUGIN_ID = "com.bull.eclipse.jonas" ;
48     public static final String JavaDoc NATURE_ID = PLUGIN_ID + ".jonasnature" ;
49
50     public static final String JavaDoc JONAS_PREF_HOME_KEY = "jonasDir";
51     public static final String JavaDoc JONAS_PREF_BASE_KEY = "jonasBase";
52     public static final String JavaDoc JONAS_PREF_VERSION_KEY = "jonasVersion";
53     public static final String JavaDoc JONAS_PREF_JRE_KEY = "jonasJRE";
54     public static final String JavaDoc JONAS_PREF_JVM_PARAMETERS_KEY = "jvmParameters";
55     public static final String JavaDoc JONAS_PREF_JVM_CLASSPATH_KEY = "jvmClasspath";
56     public static final String JavaDoc JONAS_PREF_JVM_BOOTCLASSPATH_KEY = "jvmBootClasspath";
57     public static final String JavaDoc JONAS_PREF_PROJECTSINCP_KEY = "projectsInCp";
58     public static final String JavaDoc JONAS_PREF_XTRA_CLASSPATH_KEY = "xtraClasspath";
59     public static final String JavaDoc JONAS_EXTRA_RESOURCES_KEY = "extraResources";
60     public static final String JavaDoc JONAS_PREF_ORB_KEY = "orb";
61     public static final String JavaDoc JONAS_PREF_DEBUGMODE_KEY = "jonasDebugMode";
62     public static final String JavaDoc JONAS_PREF_CONSOLEIN_KEY = "jonasConsoleIn";
63     public static final String JavaDoc JONAS_VERSION30 = "jonas30";
64     public static final String JavaDoc JONAS_VERSION31 = "jonas31";
65     public static final String JavaDoc JONAS_VERSION40 = "jonas40";
66     public static final String JavaDoc JONAS_VERSION41 = "jonas41";
67     public static final String JavaDoc JONAS_VERSION414 = "jonas414";
68     public static final String JavaDoc JONAS_ORB_RMI = "RMI";
69     public static final String JavaDoc JONAS_ORB_JEREMIE = "JEREMIE";
70     public static final String JavaDoc JONAS_PREF_DATABASE_KEY = "database";
71     public static final String JavaDoc JONAS_PREF_AUTOMATICDEPLOY_KEY = "automaticDeploy";
72                 
73     private static final String JavaDoc JONAS_HOME_CLASSPATH_VARIABLE = "JONAS_ROOT";
74
75
76     //The shared instance.
77
private static JonasLauncherPlugin plugin;
78     //Resource bundle.
79
private ResourceBundle JavaDoc resourceBundle;
80     
81     // Restart management
82
private boolean eventRestart = false;
83     
84     /**
85      * The constructor.
86      */

87     public JonasLauncherPlugin(IPluginDescriptor descriptor) {
88         super(descriptor);
89         plugin = this;
90
91         try {
92             resourceBundle= PropertyResourceBundle.getBundle("resources");
93         } catch (MissingResourceException JavaDoc x) {
94             JonasLauncherPlugin.log("Problème lors du chargement du plugin");
95             x.printStackTrace();
96             resourceBundle = null;
97         }
98         
99         JonasLauncherPlugin.getWorkspace().addResourceChangeListener(new JonasProjectChangeListener(), IResourceChangeEvent.PRE_DELETE);
100     }
101
102     /**
103      * Returns the shared instance.
104      */

105     public static JonasLauncherPlugin getDefault() {
106         return plugin;
107     }
108
109     /**
110      * Returns the workspace instance.
111      */

112     public static IWorkspace getWorkspace() {
113         return ResourcesPlugin.getWorkspace();
114     }
115
116     /**
117      * Returns the string from the plugin's resource bundle,
118      * or 'key' if not found.
119      */

120     public static String JavaDoc getResourceString(String JavaDoc key) {
121         ResourceBundle JavaDoc bundle= JonasLauncherPlugin.getDefault().getResourceBundle();
122         System.out.println("bundle = " + bundle);
123         try {
124             return bundle.getString(key);
125         } catch (MissingResourceException JavaDoc e) {
126             return key;
127         }
128     }
129
130     /**
131      * Returns the plugin's resource bundle,
132      */

133     public ResourceBundle JavaDoc getResourceBundle() {
134         try {
135             resourceBundle= PropertyResourceBundle.getBundle("resources");
136         } catch (MissingResourceException JavaDoc x) {
137             resourceBundle = null;
138         }
139         return resourceBundle;
140     }
141
142
143     public String JavaDoc getJonasDir() {
144         IPreferenceStore pref = JonasLauncherPlugin.getDefault().getPreferenceStore();
145         return pref.getString(JONAS_PREF_HOME_KEY);
146     }
147
148     public String JavaDoc getBaseDir() {
149         IPreferenceStore pref = JonasLauncherPlugin.getDefault().getPreferenceStore();
150         return pref.getString(JONAS_PREF_BASE_KEY);
151     }
152     
153     public String JavaDoc getJonasVersion() {
154         IPreferenceStore pref = JonasLauncherPlugin.getDefault().getPreferenceStore();
155         String JavaDoc result = pref.getString(JONAS_PREF_VERSION_KEY);
156         if (result.equals(""))
157             result = JONAS_VERSION30;
158         
159         return result;
160     }
161
162     public String JavaDoc getJonasJRE() {
163         IPreferenceStore pref = JonasLauncherPlugin.getDefault().getPreferenceStore();
164         String JavaDoc result = pref.getString(JONAS_PREF_JRE_KEY);
165         if (result.equals(""))
166             result = JavaRuntime.getDefaultVMInstall().getId();
167             
168         return result;
169     }
170     
171     public boolean isDebugMode() {
172         IPreferenceStore pref = JonasLauncherPlugin.getDefault().getPreferenceStore();
173         return !pref.getBoolean(JONAS_PREF_DEBUGMODE_KEY);
174     }
175
176     public boolean isConsoleIn() {
177         IPreferenceStore pref = JonasLauncherPlugin.getDefault().getPreferenceStore();
178         return pref.getBoolean(JONAS_PREF_CONSOLEIN_KEY);
179     }
180
181     public String JavaDoc getJvmParamaters() {
182         IPreferenceStore pref = JonasLauncherPlugin.getDefault().getPreferenceStore();
183         return pref.getString(JONAS_PREF_JVM_PARAMETERS_KEY);
184     }
185
186     public String JavaDoc getJvmClasspath() {
187         IPreferenceStore pref = JonasLauncherPlugin.getDefault().getPreferenceStore();
188         return pref.getString(JONAS_PREF_JVM_CLASSPATH_KEY);
189     }
190
191     public String JavaDoc getJvmBootClasspath() {
192         IPreferenceStore pref = JonasLauncherPlugin.getDefault().getPreferenceStore();
193         return pref.getString(JONAS_PREF_JVM_BOOTCLASSPATH_KEY);
194     }
195
196     public String JavaDoc getDatabase() {
197         IPreferenceStore pref = JonasLauncherPlugin.getDefault().getPreferenceStore();
198         return pref.getString(JONAS_PREF_DATABASE_KEY);
199     }
200
201     public String JavaDoc getXtraClasspath() {
202             IPreferenceStore pref = JonasLauncherPlugin.getDefault().getPreferenceStore();
203             return pref.getString(JONAS_PREF_XTRA_CLASSPATH_KEY);
204     }
205
206     public static String JavaDoc getPluginId()
207     {
208         return getDefault().getDescriptor().getUniqueIdentifier();
209     }
210
211     public String JavaDoc getOrb() {
212             IPreferenceStore pref = JonasLauncherPlugin.getDefault().getPreferenceStore();
213             return pref.getString(JONAS_PREF_ORB_KEY);
214     }
215     
216     public String JavaDoc getAutomaticDeploy() {
217         IPreferenceStore pref = JonasLauncherPlugin.getDefault().getPreferenceStore();
218         return pref.getString(JONAS_PREF_AUTOMATICDEPLOY_KEY);
219     }
220             
221     public void setProjectsInCP(List JavaDoc projectsInCP) {
222         IPreferenceStore pref = JonasLauncherPlugin.getDefault().getPreferenceStore();
223         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
224         Iterator JavaDoc it = projectsInCP.iterator();
225         while(it.hasNext()) {
226             ProjectListElement each = (ProjectListElement)it.next();
227             buf.append(each.getID());
228             buf.append(';');
229         }
230         pref.setValue(JONAS_PREF_PROJECTSINCP_KEY, buf.toString());
231     }
232
233     public List JavaDoc getProjectsInCP() {
234         IPreferenceStore pref = JonasLauncherPlugin.getDefault().getPreferenceStore();
235         String JavaDoc stringList = pref.getString(JONAS_PREF_PROJECTSINCP_KEY);
236
237         List JavaDoc projectsIdList = new ArrayList JavaDoc();
238         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(stringList, ";");
239         while (tokenizer.hasMoreElements()) {
240             projectsIdList.add(tokenizer.nextToken());
241         }
242         
243         return ProjectListElement.stringsToProjectsList(projectsIdList);
244         
245     }
246     
247         
248     public JonasBootstrap getJonasBootstrap() {
249         JonasBootstrap jonasBootsrap = null;
250
251         if(getJonasVersion().equals(JONAS_VERSION30)) {
252             jonasBootsrap = new Jonas30Bootstrap();
253         }
254         
255         if(getJonasVersion().equals(JONAS_VERSION31)) {
256                     jonasBootsrap = new Jonas31Bootstrap();
257         }
258
259         if(getJonasVersion().equals(JONAS_VERSION40)) {
260             jonasBootsrap = new Jonas40Bootstrap();
261         }
262
263         if(getJonasVersion().equals(JONAS_VERSION41)) {
264             jonasBootsrap = new Jonas41Bootstrap();
265         }
266
267         if(getJonasVersion().equals(JONAS_VERSION414)) {
268             jonasBootsrap = new Jonas414Bootstrap();
269         }
270
271         return jonasBootsrap;
272     }
273
274         
275
276     static public void log(String JavaDoc msg) {
277         ILog log = JonasLauncherPlugin.getDefault().getLog();
278         Status status = new Status(IStatus.ERROR, JonasLauncherPlugin.getDefault().getDescriptor().getUniqueIdentifier(), IStatus.ERROR, msg + "\n", null);
279         log.log(status);
280     }
281     
282     static public void log(Exception JavaDoc ex) {
283         ILog log = JonasLauncherPlugin.getDefault().getLog();
284         StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
285         ex.printStackTrace(new PrintWriter JavaDoc(stringWriter));
286         String JavaDoc msg = stringWriter.getBuffer().toString();
287
288         Status status = new Status(IStatus.ERROR, JonasLauncherPlugin.getDefault().getDescriptor().getUniqueIdentifier(), IStatus.ERROR, msg, null);
289         log.log(status);
290     }
291
292     
293     public IPath getJonasIPath() {
294         IPath jonasPath = getJonasClasspathVariable();
295         if(jonasPath == null) {
296             return new Path(JonasLauncherPlugin.getDefault().getJonasDir());
297         } else {
298             return new Path(JONAS_HOME_CLASSPATH_VARIABLE);
299         }
300     }
301     
302     private IPath getJonasClasspathVariable() {
303         IPath jonasPath = JavaCore.getClasspathVariable(JONAS_HOME_CLASSPATH_VARIABLE);
304         if(jonasPath == null) {
305             this.initJonasClasspathVariable();
306             jonasPath = JavaCore.getClasspathVariable(JONAS_HOME_CLASSPATH_VARIABLE);
307         }
308         return jonasPath;
309     }
310
311     public void initJonasClasspathVariable() {
312         try {
313             JavaCore.setClasspathVariable(
314                 JONAS_HOME_CLASSPATH_VARIABLE,
315                 new Path(JonasLauncherPlugin.getDefault().getJonasDir()),
316                 null);
317         } catch (JavaModelException e) {
318             log(e);
319         }
320     }
321
322
323
324     // Restart managed with event listener.
325

326     /* (non-Javadoc)
327      * @see org.eclipse.core.runtime.Plugin#startup()
328      */

329     public void startup() throws CoreException {
330         super.startup();
331         DebugPlugin.getDefault().addDebugEventListener(this);
332     }
333
334     /* (non-Javadoc)
335      * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent[])
336      */

337     public void handleDebugEvents(DebugEvent[] events)
338     {
339         for (int i = 0; i < events.length; i++) {
340             if (events[i].getKind() == DebugEvent.CREATE) {
341                 if (events[i].getSource() instanceof IProcess) {
342                     IProcess process = (IProcess) events[i].getSource();
343                 }
344             }
345             else if (events[i].getKind() == DebugEvent.TERMINATE) {
346                     if (events[i].getSource() instanceof IProcess) {
347                         IProcess process = (IProcess) events[i].getSource();
348                         if(eventRestart == true) {
349                             eventRestart = false;
350                             StartActionDelegate start = new StartActionDelegate();
351                             start.run(start.getAction());
352                         }
353                     }
354                 }
355             }
356         }
357
358
359     public boolean isEventRestart() {
360         return eventRestart;
361     }
362
363     public void setEventRestart(boolean b) {
364         eventRestart = b;
365     }
366
367     
368 }
369
370
Popular Tags