some refactoring in the code analysis
[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 = PHPeclipsePlugin.getDefault().getPreferenceStore();
33     String documentRoot = store.getString(PHPeclipsePlugin.DOCUMENTROOT_PREF);
34     // replace backslash with slash in the DocumentRoot under Windows
35     documentRoot = documentRoot.replace('\\', '/');
36     String[] arguments = { documentRoot };
37     MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.APACHE_START_PREF));
38     execute(
39       "apache_start",
40       store.getString(PHPeclipsePlugin.APACHE_RUN_PREF),
41       form.format(arguments),
42       store.getBoolean(PHPeclipsePlugin.APACHE_START_BACKGROUND));
43   }
44
45   //    public static void execute(String command, String consoleMessage) {
46   //            //              MessageDialog.openInformation(activeWindow.getShell(), "Exec command: ", command);
47   //            try {
48   //                    PHPConsole console = PHPConsole.getInstance();
49   //                    console.write(consoleMessage + command + "\n");
50   //                    Runtime runtime = Runtime.getRuntime(); 
51   //
52   //                    // runs the command
53   //                    Process p = runtime.exec(command);
54   //
55   //                    if (PHPeclipsePlugin.getDefault().getPreferenceStore().getBoolean(PHPeclipsePlugin.SHOW_OUTPUT_IN_CONSOLE) == true) {
56   //
57   //                                    OutputThread out = new OutputThread(p.getInputStream(), console);
58   //                                    OutputThread err = new OutputThread(p.getErrorStream(), console);
59   //                                    out.start();
60   //                                    err.start();
61   //        
62   //                    }
63   //
64   //            } catch (IOException e) {
65   //
66   //                    System.err.println("Problem");
67   //                    e.printStackTrace();
68   //
69   //            }
70   //
71   //    }
72
73   /**
74          * Executes an external progam and saves the LaunchConfiguration under external tools 
75          * @param command external tools command name
76          * @param executable executable path i.e.c:\apache\apache.exe
77          * @param arguments arguments for this configuration
78          * @param background run this configuration in background mode
79          */
80   public static void execute(String command, String executable, String arguments, boolean background) {
81     PHPConsole console = PHPConsole.getInstance();
82     String consoleMessage;
83     if (background) {
84       consoleMessage = "run in background mode-" + command + ": " + executable + " " + arguments;
85     } else {
86       consoleMessage = "run in foreground mode-" + command + ": " + executable + " " + arguments;
87     }
88     console.write(consoleMessage + "\n");
89
90     ExternalToolsUtil.execute(command, executable, arguments, background);
91     //    MessageDialog.openInformation(activeWindow.getShell(), "Exec command: ", command);
92     //          try {
93     //                  PHPConsole console = PHPConsole.getInstance();
94     //                  console.write(consoleMessage + command + "\n");
95     //                  
96     //                  ExternalToolsUtil.execute()
97     //                  Runtime runtime = Runtime.getRuntime();
98     //
99     //                  // runs the command
100     //                  Process p = runtime.exec(command);
101     //
102     //                  // gets the input stream to have the post-compile-time information
103     //                  InputStream stream = p.getInputStream();
104     //
105     //                  // get the string from Stream
106     //                  String consoleOutput = PHPConsole.getStringFromStream(stream);
107     //
108     //                  // prints out the information
109     //                  console.write(consoleOutput);
110     //                  return consoleOutput;
111     //
112     //          } catch (IOException e) {
113     //
114     //                  System.err.println("Problem");
115     //                  e.printStackTrace();
116     //
117     //          }
118     //          return "";
119   }
120   public static String getParserOutput(String command, String consoleMessage) {
121     //    MessageDialog.openInformation(activeWindow.getShell(), "Exec command: ", command);
122     try {
123       PHPConsole console = PHPConsole.getInstance();
124       if (console != null) {
125         console.write(consoleMessage + command + "\n");
126       }
127
128       Runtime runtime = Runtime.getRuntime();
129
130       // runs the command
131       Process p = runtime.exec(command);
132
133       // gets the input stream to have the post-compile-time information
134       InputStream stream = p.getInputStream();
135
136       // get the string from Stream
137       String consoleOutput = PHPConsole.getStringFromStream(stream);
138
139       // prints out the information
140       if (console != null) {
141         console.write(consoleOutput);
142       }
143       return consoleOutput;
144
145     } catch (IOException e) {
146
147       System.err.println("Problem");
148       e.printStackTrace();
149
150     }
151     return "";
152   }
153
154   public void selectionChanged(IAction action, ISelection selection) {
155
156   }
157
158   public void init(IWorkbenchWindow window) {
159     this.activeWindow = window;
160   }
161
162   public void dispose() {
163
164   }
165
166   //    static class OutputThread extends Thread {
167   //            InputStream fInputStream;
168   //            PHPConsole console;
169   //
170   //            OutputThread(InputStream inputStream, PHPConsole console) {
171   //                    this.fInputStream = inputStream;
172   //                    this.console = console;
173   //            }
174   //
175   //            public void run() {
176   //                    try {
177   //                            BufferedReader bin = new BufferedReader(new InputStreamReader(fInputStream));
178   //
179   //                            String bufferRow;
180   //                            while ((bufferRow = bin.readLine()) != null) {
181   //
182   //                                    // prints out the information
183   //                                    console.write( bufferRow );
184   //
185   //                            }
186   //                            bin.close();
187   //
188   //                    } catch (IOException e) {
189   //                            MessageDialog.openError(null, "Error in output", e.toString());
190   //                    } finally {
191   //
192   //                    }
193   //            }
194   //    }
195
196 }