some bugfixes
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / actions / PHPStartApacheAction.java
1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. and others.
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v1.0
5 which accompanies this distribution, and is available at
6 http://www.eclipse.org/legal/cpl-v10.html
7
8 Contributors:
9                 IBM Corporation - Initial implementation
10                 Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.actions;
13
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.text.MessageFormat;
17
18 import net.sourceforge.phpdt.externaltools.launchConfigurations.ExternalToolsUtil;
19 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
20 import net.sourceforge.phpeclipse.views.PHPConsole;
21
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.jface.preference.IPreferenceStore;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.ui.IWorkbenchWindow;
26 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
27
28 public class PHPStartApacheAction implements IWorkbenchWindowActionDelegate {
29   protected IWorkbenchWindow activeWindow = null;
30
31   public void run(IAction action) {
32     final IPreferenceStore store =
33       PHPeclipsePlugin.getDefault().getPreferenceStore();
34     String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
35     // replace backslash with slash in the DocumentRoot under Windows
36     documentRoot = documentRoot.replace('\\', '/');
37     String[] arguments = { documentRoot };
38     MessageFormat form =
39       new MessageFormat(store.getString(PHPeclipsePlugin.APACHE_START_PREF));
40     execute(
41       "apache_start",
42       store.getString(PHPeclipsePlugin.APACHE_RUN_PREF),
43       form.format(arguments),
44       store.getBoolean(PHPeclipsePlugin.APACHE_START_BACKGROUND));
45   }
46
47   //    public static void execute(String command, String consoleMessage) {
48   //            //              MessageDialog.openInformation(activeWindow.getShell(), "Exec command: ", command);
49   //            try {
50   //                    PHPConsole console = PHPConsole.getInstance();
51   //                    console.write(consoleMessage + command + "\n");
52   //                    Runtime runtime = Runtime.getRuntime(); 
53   //
54   //                    // runs the command
55   //                    Process p = runtime.exec(command);
56   //
57   //                    if (PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean(PHPeclipsePlugin.SHOW_OUTPUT_IN_CONSOLE) == true) {
58   //
59   //                                    OutputThread out = new OutputThread(p.getInputStream(), console);
60   //                                    OutputThread err = new OutputThread(p.getErrorStream(), console);
61   //                                    out.start();
62   //                                    err.start();
63   //        
64   //                    }
65   //
66   //            } catch (IOException e) {
67   //
68   //                    System.err.println("Problem");
69   //                    e.printStackTrace();
70   //
71   //            }
72   //
73   //    }
74
75   /**
76          * Executes an external progam and saves the LaunchConfiguration under external tools 
77          * @param command external tools command name
78          * @param executable executable path i.e.c:\apache\apache.exe
79          * @param arguments arguments for this configuration
80          * @param background run this configuration in background mode
81          */
82   public static void execute(
83     String command,
84     String executable,
85     String arguments,
86     boolean background) {
87     PHPConsole console = PHPConsole.getInstance();
88     String consoleMessage;
89     if (background) {
90       consoleMessage =
91         "run in background mode-"
92           + command
93           + ": "
94           + executable
95           + " "
96           + arguments;
97     } else {
98       consoleMessage =
99         "run in foreground mode-"
100           + command
101           + ": "
102           + executable
103           + " "
104           + arguments;
105     }
106     console.write(consoleMessage + "\n");
107
108     ExternalToolsUtil.execute(command, executable, arguments, background);
109     //    MessageDialog.openInformation(activeWindow.getShell(), "Exec command: ", command);
110     //          try {
111     //                  PHPConsole console = PHPConsole.getInstance();
112     //                  console.write(consoleMessage + command + "\n");
113     //                  
114     //                  ExternalToolsUtil.execute()
115     //                  Runtime runtime = Runtime.getRuntime();
116     //
117     //                  // runs the command
118     //                  Process p = runtime.exec(command);
119     //
120     //                  // gets the input stream to have the post-compile-time information
121     //                  InputStream stream = p.getInputStream();
122     //
123     //                  // get the string from Stream
124     //                  String consoleOutput = PHPConsole.getStringFromStream(stream);
125     //
126     //                  // prints out the information
127     //                  console.write(consoleOutput);
128     //                  return consoleOutput;
129     //
130     //          } catch (IOException e) {
131     //
132     //                  System.err.println("Problem");
133     //                  e.printStackTrace();
134     //
135     //          }
136     //          return "";
137   }
138   public static String getParserOutput(String command, String consoleMessage) {
139     //    MessageDialog.openInformation(activeWindow.getShell(), "Exec command: ", command);
140     try {
141       PHPConsole console = null;
142       try {
143         console = PHPConsole.getInstance();
144         if (console != null) {
145           console.write(consoleMessage + command + "\n");
146         }
147       } catch (Throwable th) {
148
149       }
150
151       Runtime runtime = Runtime.getRuntime();
152
153       // runs the command
154       Process p = runtime.exec(command);
155
156       // gets the input stream to have the post-compile-time information
157       InputStream stream = p.getInputStream();
158
159       // get the string from Stream
160       String consoleOutput = PHPConsole.getStringFromStream(stream);
161
162       // prints out the information
163       if (console != null) {
164         console.write(consoleOutput);
165       }
166       return consoleOutput;
167
168     } catch (IOException e) {
169
170       System.err.println("Problem");
171       e.printStackTrace();
172
173     }
174     return "";
175   }
176
177   public void selectionChanged(IAction action, ISelection selection) {
178
179   }
180
181   public void init(IWorkbenchWindow window) {
182     this.activeWindow = window;
183   }
184
185   public void dispose() {
186
187   }
188
189   //    static class OutputThread extends Thread {
190   //            InputStream fInputStream;
191   //            PHPConsole console;
192   //
193   //            OutputThread(InputStream inputStream, PHPConsole console) {
194   //                    this.fInputStream = inputStream;
195   //                    this.console = console;
196   //            }
197   //
198   //            public void run() {
199   //                    try {
200   //                            BufferedReader bin = new BufferedReader(new InputStreamReader(fInputStream));
201   //
202   //                            String bufferRow;
203   //                            while ((bufferRow = bin.readLine()) != null) {
204   //
205   //                                    // prints out the information
206   //                                    console.write( bufferRow );
207   //
208   //                            }
209   //                            bin.close();
210   //
211   //                    } catch (IOException e) {
212   //                            MessageDialog.openError(null, "Error in output", e.toString());
213   //                    } finally {
214   //
215   //                    }
216   //            }
217   //    }
218
219 }