KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > forrest > eclipse > job > ForrestRunner


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation or its licensors,
3  * as applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.forrest.eclipse.job;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23
24 import org.apache.forrest.eclipse.ForrestPlugin;
25 import org.apache.forrest.eclipse.actions.Utilities;
26 import org.apache.forrest.eclipse.preference.ForrestPreferences;
27 import org.apache.log4j.Logger;
28 import org.burrokeet.servletEngine.Jetty;
29 import org.burrokeet.servletEngine.Server;
30 import org.eclipse.core.runtime.IProgressMonitor;
31 import org.eclipse.core.runtime.IStatus;
32 import org.eclipse.core.runtime.Status;
33 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
34 import org.eclipse.swt.widgets.Display;
35 import org.eclipse.ui.browser.IWebBrowser;
36 import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
37 import org.eclipse.wst.internet.webbrowser.internal.Trace;
38 import org.eclipse.wst.internet.webbrowser.internal.WebBrowserUIPlugin;
39
40 /**
41  * Run a version of Forrest
42  */

43 public class ForrestRunner extends ForrestJob implements
44         IJavaLaunchConfigurationConstants {
45     /**
46      * Logger for this class
47      */

48     protected static final Logger logger = Logger
49             .getLogger(ForrestRunner.class);
50
51     private static final int EXCEPTION_UNABLE_TO_START = 2010;
52
53     private String JavaDoc workingDir;
54
55     private static final int BROWSER_ERROR = 200;
56
57     private static final int IO_EXCEPTION = 300;
58
59     /**
60      * Create a Forrest runner that will run a Jetty server on a given directory
61      *
62      * @param workingDir -
63      * the working directory for the command
64      */

65     protected ForrestRunner(String JavaDoc workingDir) {
66         super("Forrest Runner");
67
68         this.workingDir = workingDir;
69     }
70
71     /*
72      * Run the Forrest server in a separate thread and return that thread to the
73      * Forrest manager.
74      *
75      * @see java.lang.Runnable#run() @refactor lots of potential to tidy up this
76      * code, for example extract a few methods, move relevant code to
77      * ForrestManager
78      */

79     public IStatus run(IProgressMonitor monitor) {
80         IStatus status = Status.OK_STATUS;
81         // FIXME: PORT should be retrieved from properties
82
int port = 8888;
83
84         if (Utilities.isPortFree(port)) {
85
86             if (logger.isDebugEnabled()) {
87                 logger.debug("run(IProgressMonitor) - start");
88             }
89
90             monitor.subTask("Initialising project");
91             String JavaDoc fhome = ForrestPlugin.getDefault().getPluginPreferences()
92                     .getString(ForrestPreferences.FORREST_HOME);
93             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("-Dproject.home=");
94             sb.append(workingDir);
95             sb.append(" -Dbasedir=");
96             sb.append(fhome + File.separatorChar + "main");
97             sb.append(" ");
98             sb.append("init");
99             status = runAnt(monitor, sb.toString());
100
101             monitor.subTask("Starting Server");
102             if (status.isOK()) {
103                 ForrestManager forrestManager = ForrestManager.getInstance();
104                 String JavaDoc confPath = ForrestManager.FORREST_CORE_WEBAPP
105                         + File.separatorChar + "jettyconf.xml";
106                 try {
107                     Server jetty = new Jetty(confPath,
108                             ForrestManager.FORREST_CORE_WEBAPP,
109                             ForrestManager.FORREST_ENDORSED_LIB, forrestManager
110                                     .getProperties(workingDir), ForrestManager
111                                     .getClasspathFiles());
112                     status = jetty.start(monitor);
113                     if (status.isOK()) {
114                         ForrestManager.setServerLaunch(jetty.getLaunch());
115                     } else {
116                         ForrestManager.setServerLaunch(null);
117                     }
118                 } catch (IOException JavaDoc e) {
119                     logger.error("run(IProgressMonitor)", e);
120                     status = new Status(Status.ERROR, ForrestPlugin.ID,
121                             IO_EXCEPTION, "Unable to start Jetty server", e);
122                 }
123             } else {
124                 return status;
125             }
126
127             // FIXME: Timeout delay should be a preference
128
long endTime = System.currentTimeMillis() + (1000 * 30);
129             while (Utilities.isPortFree(port)
130                     && endTime < System.currentTimeMillis()) {
131                 try {
132                     Thread.sleep(250);
133                 } catch (InterruptedException JavaDoc e) {
134                     // no problem (I think ;-))
135
}
136             }
137
138             if (!Utilities.isPortFree(port)) {
139                 status = new Status(
140                         Status.WARNING,
141                         ForrestPlugin.ID,
142                         EXCEPTION_UNABLE_TO_START,
143                         "Server did not start within specified timeout period, have not tried to open browser.",
144                         null);
145                 return status;
146             }
147
148             if (!openBrowser(monitor)) {
149                 status = new Status(Status.WARNING, ForrestPlugin.ID,
150                         BROWSER_ERROR, "Unable to open browser", null);
151             }
152
153             if (logger.isDebugEnabled()) {
154                 logger.debug("run(IProgressMonitor) - end");
155             }
156         } else {
157             status = new Status(Status.WARNING, ForrestPlugin.ID,
158                     EXCEPTION_UNABLE_TO_START, "Unable to start Forrest, port "
159                             + port + " already in use", null);
160         }
161         return status;
162     }
163
164     /**
165      * Open a web browser on the index page.
166      *
167      * @param monitor -
168      * the progress monitor for this job
169      * @return boolean - true if browser opened OK
170      */

171     private boolean openBrowser(IProgressMonitor monitor) {
172
173         monitor.subTask("Open index page");
174         // FIXME: port should come from the config files
175
Display.getDefault().syncExec(new Runnable JavaDoc() {
176             public void run() {
177                 URL JavaDoc url;
178                 try {
179                     url = new URL JavaDoc("http://localhost:8888");
180                     IWorkbenchBrowserSupport browserSupport = WebBrowserUIPlugin
181                             .getInstance().getWorkbench().getBrowserSupport();
182                     IWebBrowser browser = browserSupport.createBrowser(
183                             IWorkbenchBrowserSupport.LOCATION_BAR
184                                     | IWorkbenchBrowserSupport.NAVIGATION_BAR,
185                             null, null, null);
186                     browser.openURL(url);
187                 } catch (Exception JavaDoc e) {
188                     Trace.trace(Trace.SEVERE, "Error opening browser", e);
189                 }
190             }
191         });
192
193         monitor.worked(3);
194         return true;
195     }
196 }
Popular Tags