KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > standalone > Eclipse


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.help.internal.standalone;
12
13 import java.io.*;
14 import java.util.List JavaDoc;
15
16 /**
17  * Eclipse launcher. Spawns eclipse executable or
18  * org.eclipse.core.launcher.Main.
19  */

20 public class Eclipse extends Thread JavaDoc {
21     // Eclipse exit codes
22
private static final int NEEDS_RESTART = 23;
23     // Launching status
24
public static final int STATUS_INIT = 0;
25     public static final int STATUS_STARTED = 1;
26     public static final int STATUS_ERROR = 2;
27
28     File dir;
29     String JavaDoc[] cmdarray;
30     private int status = STATUS_INIT;
31     private Exception JavaDoc exception;
32     Process JavaDoc pr;
33     private EclipseLifeCycleListener lifeCycleListener;
34     /**
35      * Constructor
36      */

37     public Eclipse(EclipseLifeCycleListener listener) {
38         super();
39         this.lifeCycleListener = listener;
40         this.setName("Eclipse"); //$NON-NLS-1$
41
this.dir = Options.getEclipseHome();
42     }
43     private void prepareCommand() throws Exception JavaDoc {
44         if (Options.useExe()) {
45             prepareEclipseCommand();
46             ensureEclipseExeExists();
47         } else {
48             prepareJavaCommand();
49         }
50         ensureVmExists();
51     }
52     private void prepareEclipseCommand() {
53         List JavaDoc vmArgs = Options.getVmArgs();
54         List JavaDoc eclipseArgs = Options.getEclipseArgs();
55         cmdarray = new String JavaDoc[3 + vmArgs.size() + 1 + eclipseArgs.size()];
56         cmdarray[0] = new File(Options.getEclipseHome(), "eclipse").getAbsolutePath(); //$NON-NLS-1$
57
cmdarray[1] = "-vm"; //$NON-NLS-1$
58
cmdarray[2] = Options.getVm();
59         for (int i = 0; i < eclipseArgs.size(); i++) {
60             cmdarray[3 + i] = (String JavaDoc) eclipseArgs.get(i);
61         }
62         cmdarray[3 + eclipseArgs.size()] = "-vmargs"; //$NON-NLS-1$
63
for (int i = 0; i < vmArgs.size(); i++) {
64             cmdarray[4 + eclipseArgs.size() + i] = (String JavaDoc) vmArgs.get(i);
65         }
66     }
67     private void prepareJavaCommand() throws Exception JavaDoc {
68         List JavaDoc vmArgs = Options.getVmArgs();
69         List JavaDoc eclipseArgs = Options.getEclipseArgs();
70         cmdarray = new String JavaDoc[1 + vmArgs.size() + 3 + eclipseArgs.size()];
71         cmdarray[0] = Options.getVm();
72         for (int i = 0; i < vmArgs.size(); i++) {
73             cmdarray[1 + i] = (String JavaDoc) vmArgs.get(i);
74         }
75         cmdarray[1 + vmArgs.size()] = "-cp"; //$NON-NLS-1$
76
cmdarray[2 + vmArgs.size()] = getStartupJar();
77         cmdarray[3 + vmArgs.size()] = "org.eclipse.core.launcher.Main"; //$NON-NLS-1$
78
for (int i = 0; i < eclipseArgs.size(); i++) {
79             cmdarray[4 + vmArgs.size() + i] = (String JavaDoc) eclipseArgs.get(i);
80         }
81     }
82     /**
83      * Launches Eclipse process and waits for it.
84      */

85     public void run() {
86         try {
87             prepareCommand();
88             if (Options.isDebug()) {
89                 printCommand();
90             }
91             do {
92                 pr = Runtime.getRuntime().exec(cmdarray, (String JavaDoc[]) null, dir);
93                 (new StreamConsumer(pr.getInputStream())).start();
94                 (new StreamConsumer(pr.getErrorStream())).start();
95                 if (status == STATUS_INIT) {
96                     // started first time
97
status = STATUS_STARTED;
98                 }
99                 try {
100                     pr.waitFor();
101                 } catch (InterruptedException JavaDoc e) {
102                 }
103                 if (Options.isDebug()) {
104                     System.out
105                             .println("Eclipse exited with status code " + pr.exitValue()); //$NON-NLS-1$
106
if (pr.exitValue() == NEEDS_RESTART) {
107                         System.out
108                                 .println("Updates are installed, Eclipse will be restarted."); //$NON-NLS-1$
109
}
110                 }
111             } while (pr.exitValue() == NEEDS_RESTART);
112         } catch (Exception JavaDoc exc) {
113             exception = exc;
114             status = STATUS_ERROR;
115         } finally {
116             if (status == STATUS_INIT) {
117                 status = STATUS_ERROR;
118             }
119             if (status == STATUS_ERROR && exception == null) {
120                 exception = new Exception JavaDoc("Unknown exception."); //$NON-NLS-1$
121
}
122             lifeCycleListener.eclipseEnded();
123         }
124     }
125     /**
126      * Reads a stream
127      */

128     public class StreamConsumer extends Thread JavaDoc {
129         BufferedReader bReader;
130         public StreamConsumer(InputStream inputStream) {
131             super();
132             this.setName("Eclipse out/err consumer"); //$NON-NLS-1$
133
this.setDaemon(true);
134             bReader = new BufferedReader(new InputStreamReader(inputStream));
135         }
136         public void run() {
137             try {
138                 String JavaDoc line;
139                 while (null != (line = bReader.readLine())) {
140                     System.out.println(line);
141                 }
142                 bReader.close();
143             } catch (IOException ioe) {
144                 ioe.printStackTrace();
145             }
146         }
147     }
148     private void ensureVmExists() throws Exception JavaDoc {
149         File vmExe = new File(Options.getVm());
150         if (vmExe.exists() && !vmExe.isDirectory()) {
151             return;
152         }
153         vmExe = new File(Options.getVm() + ".exe"); //$NON-NLS-1$
154
if (vmExe.exists() && !vmExe.isDirectory()) {
155             return;
156         }
157         throw new Exception JavaDoc("File " + vmExe.getAbsolutePath() //$NON-NLS-1$
158
+ " does not exists. Pass a correct -vm option"); //$NON-NLS-1$
159
}
160     private void ensureEclipseExeExists() throws Exception JavaDoc {
161         File eclipseExe = new File(Options.getEclipseHome(), "eclipse" //$NON-NLS-1$
162
+ (System.getProperty("os.name").startsWith("Win") //$NON-NLS-1$ //$NON-NLS-2$
163
? ".exe" //$NON-NLS-1$
164
: "")); //$NON-NLS-1$
165
if (eclipseExe.exists() && !eclipseExe.isDirectory()) {
166             return;
167         }
168         throw new Exception JavaDoc("File " + eclipseExe.getAbsolutePath() //$NON-NLS-1$
169
+ " does not exists. Pass a correct -eclipsehome option"); //$NON-NLS-1$
170
}
171     private String JavaDoc getStartupJar() throws Exception JavaDoc {
172         File pluginsDir = new File(Options.getEclipseHome(), "plugins/"); //$NON-NLS-1$
173
if (!pluginsDir.exists() || !pluginsDir.isDirectory())
174             throw new Exception JavaDoc("Plugins directory " + pluginsDir.getAbsolutePath() //$NON-NLS-1$
175
+ " does not exists. Pass a correct -eclipsehome option"); //$NON-NLS-1$
176
File[] plugins = pluginsDir.listFiles();
177         for (int i = 0; i < plugins.length; i++) {
178             String JavaDoc file = plugins[i].getName();
179             if (file.startsWith("org.eclipse.equinox.launcher") && file.endsWith(".jar") && !plugins[i].isDirectory()) //$NON-NLS-1$ //$NON-NLS-2$
180
return "plugins/" + file; //$NON-NLS-1$
181
}
182         throw new Exception JavaDoc("Plugins directory " + pluginsDir.getAbsolutePath() //$NON-NLS-1$
183
+ " does not contain a valid startup jar. Pass a correct -eclipsehome option"); //$NON-NLS-1$
184
}
185     /**
186      * @return Exception
187      */

188     public Exception JavaDoc getException() {
189         return exception;
190     }
191
192     /**
193      * @return int
194      */

195     public int getStatus() {
196         return status;
197     }
198     private void printCommand() {
199         System.out.println("Launch command is:"); //$NON-NLS-1$
200
for (int i = 0; i < cmdarray.length; i++) {
201             System.out.println(" " + cmdarray[i]); //$NON-NLS-1$
202
}
203
204     }
205     /**
206      * Forcibly kill Eclipse process.
207      */

208     public void killProcess() {
209         if (pr != null) {
210             pr.destroy();
211         }
212     }
213 }
214
Popular Tags