You are viewing a plain text version of this content. The canonical link for it is here.
Posted to svn@forrest.apache.org by rg...@apache.org on 2004/12/13 01:49:25 UTC
svn commit: r111683 - in forrest/trunk/tools/eclipse/src/org/apache/forrest: . eclipse/popup/actions
Author: rgardler
Date: Sun Dec 12 16:49:24 2004
New Revision: 111683
URL: http://svn.apache.org/viewcvs?view=rev&rev=111683
Log:
ForrestRunner is now an eclipse job - makes for better scheduling of the task
Modified:
forrest/trunk/tools/eclipse/src/org/apache/forrest/ForrestRunner.java
forrest/trunk/tools/eclipse/src/org/apache/forrest/eclipse/popup/actions/BuildSite.java
forrest/trunk/tools/eclipse/src/org/apache/forrest/eclipse/popup/actions/StartForrest.java
forrest/trunk/tools/eclipse/src/org/apache/forrest/eclipse/popup/actions/StopForrest.java
Modified: forrest/trunk/tools/eclipse/src/org/apache/forrest/ForrestRunner.java
Url: http://svn.apache.org/viewcvs/forrest/trunk/tools/eclipse/src/org/apache/forrest/ForrestRunner.java?view=diff&rev=111683&p1=forrest/trunk/tools/eclipse/src/org/apache/forrest/ForrestRunner.java&r1=111682&p2=forrest/trunk/tools/eclipse/src/org/apache/forrest/ForrestRunner.java&r2=111683
==============================================================================
--- forrest/trunk/tools/eclipse/src/org/apache/forrest/ForrestRunner.java (original)
+++ forrest/trunk/tools/eclipse/src/org/apache/forrest/ForrestRunner.java Sun Dec 12 16:49:24 2004
@@ -15,94 +15,108 @@
*/
package org.apache.forrest;
-import java.io.BufferedReader;
+import org.apache.log4j.Logger;
+import org.apache.log4j.xml.DOMConfigurator;
+
+
import java.io.File;
-import java.io.IOException;
-import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
+import java.net.URL;
+
+import org.apache.forrest.eclipse.ForrestPlugin;
+import org.apache.forrest.eclipse.preference.ForrestPreferences;
+import org.eclipse.ant.core.AntRunner;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
/**
* Run and monitor a version of Forrest..
*/
-public class ForrestRunner implements Runnable {
- /** Indicates the ForrestRunner is initialised and ready to be started */
- public static final int INITIALISED = 0;
- /** Indicates the ForrestRunner is confiuguring the environment for the server */
- public static final int CONFIGURING = 5;
- /** Indicates the Forrest server is starting up */
- public static final int STARTING = 10;
- /** Indicates the Forrest server is running */
- public static final int RUNNING = 20;
- /** Indicates the Forrest server is stopping */
- public static final int STOPPING = 30;
- /** Indicates the Forrest server has stopped */
- public static final int STOPPED = 40;
- /** Indicates the Forrest server has hit an exception condition */
- public static final int EXCEPTION = 101;
+public class ForrestRunner extends Job {
+ /**
+ * Logger for this class
+ */
+ private static final Logger logger = Logger.getLogger(ForrestRunner.class);
+
+ public static final String COMMAND_STOP = "stop";
+
+ public static final int EXCEPTION_UNIDENTIFIED = 1001;
+ public static final int EXCEPTION_UNABLE_TO_STOP = 1010;
private int stop_port = Integer.getInteger("STOP.PORT",8079).intValue();
private String _key = System.getProperty("STOP.KEY","mortbay");
- protected String exceptionMessage;
-
private static ForrestRunner INSTANCE;
- protected File workingDirectory;
protected String cmdString;
- Process forrestProcess;
- protected Thread forrestThread;
- protected BufferedReader forrestOutput;
- protected int status;
-
+
/**
- * Create a JettyRunner to execute Jetty on the
- * supplied working directory.
+ * Create a Forrest runner to execute a specified command on a given directory
+ * @param workingDir - the working directory for the command
+ * @param cmd - the forrest command
*/
- public ForrestRunner(String workingDir) {
- this.workingDirectory = new File(workingDir);
- if (System.getProperty("os.name").toLowerCase().startsWith("linux")) {
- cmdString = "forrest -Dbasedir=" + workingDir
- + " run";
- } else if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
- cmdString = "cmd /c forrest -Dbasedir=" + workingDir
- + " run";
- } // FIXME: implement for Mac and other OS's
-
- forrestThread = new Thread(this);
+ public ForrestRunner(String workingDir, String cmd) {
+ super("Forrest Runner");
+
+ ForrestPlugin plugin = ForrestPlugin.getDefault();
+ URL urlPluginDir = plugin.getBundle().getEntry("/");
+ String strLog4jConf = "D:\\projects\\burrokeet\\forrestplugin\\conf\\log4j.xml";
+ DOMConfigurator.configure(strLog4jConf);
+
+ if (cmd.equals(COMMAND_STOP)) {
+ cmdString=COMMAND_STOP;
+ } else {
+ String fhome = ForrestPlugin.getDefault().getPluginPreferences()
+ .getString(ForrestPreferences.FORREST_HOME);
+ StringBuffer sb = new StringBuffer("-Dproject.home=");
+ sb.append(workingDir);
+ sb.append(" -Dbasedir=");
+ sb.append(fhome + File.separatorChar + "main");
+ sb.append(" ");
+ sb.append(cmd);
+ cmdString = sb.toString();
+ }
INSTANCE = this;
- status = INITIALISED;
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
- public void run() {
- try {
- String str;
- while ((str = forrestOutput.readLine()) != null) {
- // FIXME: Output should be sent to log not console
- System.out.println(str);
- if (str.indexOf("init-props:") != -1) {
- status = CONFIGURING;
- } else if (str.indexOf("run_default_jetty:") != -1 || str.indexOf("run_custom_jetty:") != -1) {
- status = STARTING;
- } else if (str.indexOf("EVENT Started org.mortbay.jetty.Server") != -1) {
- status = RUNNING;
- } else if (str.indexOf("FIXME: what is the message that indicates server is stopping") != -1) {
- status = STOPPING;
- } else if (str.indexOf("FIXME: what is the message that indicates server has stopped") != -1) {
- status = STOPPED;
- } else if (str.indexOf("org.mortbay.util.MultiException[java.net.BindException: Address already in use: JVM_Bind]") != -1) {
- exceptionMessage = str;
- status = EXCEPTION;
- }
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ public IStatus run(IProgressMonitor monitor) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("run(IProgressMonitor) - start");
+ }
+
+ IStatus status;
+
+ if (cmdString.equals("COMMAND_STOP")) {
+ status = this.stop();
+ } else {
+ String fhome = ForrestPlugin.getDefault().getPluginPreferences()
+ .getString(ForrestPreferences.FORREST_HOME);
+ AntRunner runner = new AntRunner();
+ String antFile = fhome + File.separatorChar + "main" + File.separatorChar + "forrest.build.xml";
+ try {
+ runner.setBuildFileLocation(antFile);
+ runner.setArguments(cmdString);
+ logger.info("run() - Running ANT with command string = " + cmdString);
+ runner.run(monitor);
+ status = Status.OK_STATUS;
+ } catch (CoreException e) {
+ logger.error("run(IProgressMonitor)", e);
+ status = new Status(Status.ERROR, null, ForrestRunner.EXCEPTION_UNIDENTIFIED, "Forrest Exception", null);
+ }
+ }
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("run(IProgressMonitor) - end");
}
+ return status;
}
/**
@@ -110,24 +124,25 @@
*
*/
public void start(){
- System.out.println("Starting Forrest with the command " + cmdString);
- try {
- forrestProcess = Runtime.getRuntime().exec(cmdString, null, workingDirectory);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ if (logger.isDebugEnabled()) {
+ logger.debug("start() - start");
+ }
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("start() - end");
}
-
- forrestOutput = new BufferedReader(
- new InputStreamReader(forrestProcess.getInputStream()));
- forrestThread.start();
}
/**
* Stop this version of Forrest.
*
*/
- public void stop() {
+ public Status stop() {
+ if (logger.isDebugEnabled()) {
+ logger.debug("stop() - start");
+ }
+ IStatus status;
+
try {
Socket s=new Socket(InetAddress.getByName("127.0.0.1"), stop_port);
OutputStream out = s.getOutputStream();
@@ -135,14 +150,16 @@
out.flush();
s.shutdownOutput();
s.close();
+ status = Status.OK_STATUS;
} catch (Exception e) {
- exceptionMessage = "Unable to Stop Server (" + e.getMessage() + ")";
- status = EXCEPTION;
- // TODO: log this error correctly
- e.printStackTrace();
+ logger.error("stop()", e);
+ status = new Status(Status.ERROR, null, ForrestRunner.EXCEPTION_UNABLE_TO_STOP, "Forrest Exception", null);
}
- forrestProcess.destroy();
- forrestThread.stop();
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("stop() - end");
+ }
+ return (Status)status;
}
@@ -150,22 +167,13 @@
* @return Returns the running Forrest instance.
*/
public static ForrestRunner getInstance() {
+ if (logger.isDebugEnabled()) {
+ logger.debug("getInstance() - start");
+ }
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("getInstance() - end");
+ }
return INSTANCE;
- }
-
- /**
- * Get the current status of this ForrestRunner.
- * @return Returns the status.
- */
- public int getStatus() {
- return status;
- }
-
- /**
- * Get the message associated with the last exception thrown by this version of Forrest.
- * @return A message describing the last exception
- */
- public String getExceptionMessage() {
- return exceptionMessage;
}
}
Modified: forrest/trunk/tools/eclipse/src/org/apache/forrest/eclipse/popup/actions/BuildSite.java
Url: http://svn.apache.org/viewcvs/forrest/trunk/tools/eclipse/src/org/apache/forrest/eclipse/popup/actions/BuildSite.java?view=diff&rev=111683&p1=forrest/trunk/tools/eclipse/src/org/apache/forrest/eclipse/popup/actions/BuildSite.java&r1=111682&p2=forrest/trunk/tools/eclipse/src/org/apache/forrest/eclipse/popup/actions/BuildSite.java&r2=111683
==============================================================================
--- forrest/trunk/tools/eclipse/src/org/apache/forrest/eclipse/popup/actions/BuildSite.java (original)
+++ forrest/trunk/tools/eclipse/src/org/apache/forrest/eclipse/popup/actions/BuildSite.java Sun Dec 12 16:49:24 2004
@@ -15,13 +15,13 @@
*/
package org.apache.forrest.eclipse.popup.actions;
-import java.io.IOException;
-
+import org.apache.forrest.ForrestRunner;
import org.apache.forrest.eclipse.ForrestPlugin;
import org.apache.forrest.eclipse.preference.ForrestPreferences;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.jface.action.IAction;
@@ -58,14 +58,13 @@
public void run(IAction action) {
String cmdString = null;
IPath path = JavaCore.getClasspathVariable("ECLIPSE_HOME");
- // TODO: This should be a progress dialog
- Shell dialog = new Shell(new Shell());
// TODO: move preferences code to utilities class
String fhome = ForrestPlugin.getDefault().getPluginPreferences()
.getString(ForrestPreferences.FORREST_HOME);
if (fhome.equals("")) {
+ Shell dialog = new Shell(new Shell());
dialog.setText("Configure Forrest");
dialog.setSize(400, 100);
Label statusMsg = new Label(dialog, SWT.NONE);
@@ -79,33 +78,11 @@
return;
}
- dialog.setText("Forrest Server");
- dialog.setSize(500, 250);
- Label statusMsg = new Label(dialog, SWT.NONE);
- StringBuffer sb = new StringBuffer("Forrest is building the site.\n");
- sb.append("\n\nPlease wait...");
- statusMsg.setText(sb.toString());
- statusMsg.setLocation(30, 25);
- statusMsg.pack();
- dialog.open();
-
IPath workingDirectory = activeProject.getLocation();
-
- if (System.getProperty("os.name").toLowerCase().startsWith("linux")) {
- cmdString = "forrest -Dbasedir=" + workingDirectory
- + " site";
- } else if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
- cmdString = "cmd /c forrest -Dbasedir=" + workingDirectory
- + " site";
- }
- try {
- Runtime.getRuntime().exec(cmdString);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- dialog.close();
+
+ Job forrest = new ForrestRunner(workingDirectory.toOSString(), "site");
+ forrest.setUser(true);
+ forrest.schedule();
}
/**
Modified: forrest/trunk/tools/eclipse/src/org/apache/forrest/eclipse/popup/actions/StartForrest.java
Url: http://svn.apache.org/viewcvs/forrest/trunk/tools/eclipse/src/org/apache/forrest/eclipse/popup/actions/StartForrest.java?view=diff&rev=111683&p1=forrest/trunk/tools/eclipse/src/org/apache/forrest/eclipse/popup/actions/StartForrest.java&r1=111682&p2=forrest/trunk/tools/eclipse/src/org/apache/forrest/eclipse/popup/actions/StartForrest.java&r2=111683
==============================================================================
--- forrest/trunk/tools/eclipse/src/org/apache/forrest/eclipse/popup/actions/StartForrest.java (original)
+++ forrest/trunk/tools/eclipse/src/org/apache/forrest/eclipse/popup/actions/StartForrest.java Sun Dec 12 16:49:24 2004
@@ -15,18 +15,12 @@
*/
package org.apache.forrest.eclipse.popup.actions;
-import java.net.MalformedURLException;
-import java.net.URL;
-
import org.apache.forrest.ForrestRunner;
import org.apache.forrest.eclipse.ForrestPlugin;
import org.apache.forrest.eclipse.preference.ForrestPreferences;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
@@ -39,8 +33,6 @@
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
-import org.eclipse.webbrowser.WebBrowser;
-import org.eclipse.webbrowser.WebBrowserEditorInput;
public class StartForrest
implements IObjectActionDelegate, IJavaLaunchConfigurationConstants {
@@ -94,7 +86,7 @@
return;
}
- Job forrest = new ForrestStarter(workingDirectory.toOSString());
+ Job forrest = new ForrestRunner(workingDirectory.toOSString(), "run");
forrest.schedule();
}
@@ -111,52 +103,4 @@
}
}
- /**
- * Manage the starting a Forrest server in a specific working directory.
- * Once Forrest has started open a web browser for the server.
- */
- class ForrestStarter extends Job {
- ForrestRunner forrestRunner;
- IProgressMonitor monitor;
-
- /**
- * Create a new Forrest starter for the given working directory.
- * @param string
- * @refactor Need a factory to create instances for each Working Directory, run Forrest on a different port for each
- */
- public ForrestStarter(String wdir) {
- super("Server Starter");
- this.setUser(true);
- forrestRunner = new ForrestRunner(wdir);
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
- */
- public IStatus run(IProgressMonitor monitor) {
- this.monitor = monitor;
- monitor.beginTask("Starting Test Server", 2);
- monitor.subTask("Start Server");
- forrestRunner.start();
- while (forrestRunner.getStatus() != ForrestRunner.RUNNING
- && forrestRunner.getStatus() != ForrestRunner.EXCEPTION) {
- }
- monitor.worked(1);
- if (forrestRunner.getStatus() != ForrestRunner.EXCEPTION) {
- //FIXME: Handle startup exception
- }
- monitor.subTask("Open index page");
- URL url;
- try {
- url = new URL("http://localhost:8888");
- WebBrowserEditorInput browserInput = new WebBrowserEditorInput(url, WebBrowserEditorInput.SHOW_ALL);
- WebBrowser.openURL(browserInput);
- } catch (MalformedURLException e1) {
- // Should never be thrown
- e1.printStackTrace();
- }
- monitor.worked(2);
- return Status.OK_STATUS;
- }
- }
}
Modified: forrest/trunk/tools/eclipse/src/org/apache/forrest/eclipse/popup/actions/StopForrest.java
Url: http://svn.apache.org/viewcvs/forrest/trunk/tools/eclipse/src/org/apache/forrest/eclipse/popup/actions/StopForrest.java?view=diff&rev=111683&p1=forrest/trunk/tools/eclipse/src/org/apache/forrest/eclipse/popup/actions/StopForrest.java&r1=111682&p2=forrest/trunk/tools/eclipse/src/org/apache/forrest/eclipse/popup/actions/StopForrest.java&r2=111683
==============================================================================
--- forrest/trunk/tools/eclipse/src/org/apache/forrest/eclipse/popup/actions/StopForrest.java (original)
+++ forrest/trunk/tools/eclipse/src/org/apache/forrest/eclipse/popup/actions/StopForrest.java Sun Dec 12 16:49:24 2004
@@ -17,11 +17,11 @@
import org.apache.forrest.ForrestRunner;
import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.jface.action.IAction;
-import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
@@ -48,10 +48,10 @@
* @see IActionDelegate#run(IAction)
*/
public void run(IAction action) {
- ForrestRunner.getInstance().stop();
- // FIXME: only provide this feedback when we *know* it has stopped
- MessageDialog.openInformation(new Shell(), "Forrest",
- "Forrest server stopped");
+ IPath workingDirectory = activeProject.getLocation();
+ Job forrest = new ForrestRunner(workingDirectory.toOSString(), ForrestRunner.COMMAND_STOP);
+ forrest.setUser(true);
+ forrest.schedule();
}
/**