1 17 package org.apache.forrest.eclipse.job; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 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 43 public class ForrestRunner extends ForrestJob implements 44 IJavaLaunchConfigurationConstants { 45 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 workingDir; 54 55 private static final int BROWSER_ERROR = 200; 56 57 private static final int IO_EXCEPTION = 300; 58 59 65 protected ForrestRunner(String workingDir) { 66 super("Forrest Runner"); 67 68 this.workingDir = workingDir; 69 } 70 71 79 public IStatus run(IProgressMonitor monitor) { 80 IStatus status = Status.OK_STATUS; 81 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 fhome = ForrestPlugin.getDefault().getPluginPreferences() 92 .getString(ForrestPreferences.FORREST_HOME); 93 StringBuffer sb = new StringBuffer ("-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 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 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 long endTime = System.currentTimeMillis() + (1000 * 30); 129 while (Utilities.isPortFree(port) 130 && endTime < System.currentTimeMillis()) { 131 try { 132 Thread.sleep(250); 133 } catch (InterruptedException e) { 134 } 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 171 private boolean openBrowser(IProgressMonitor monitor) { 172 173 monitor.subTask("Open index page"); 174 Display.getDefault().syncExec(new Runnable () { 176 public void run() { 177 URL url; 178 try { 179 url = new URL ("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 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 |