Salome HOME
Run Salome with separated parameters. Read output and error streams.
[tools/siman.git] / Workspace / Siman / src / org / splat / launcher / ToolbarApplet.java
index 3a7c5903d7c89e5240fefc851c52b99a9c67b680..d5f2a757ab32cff2d39897d5f160f61a6f357f47 100644 (file)
@@ -5,11 +5,19 @@ import java.awt.ComponentOrientation;
 import java.awt.GridLayout;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStreamReader;
 import java.io.ObjectInputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLConnection;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
 
 /**
  * Applet providing a set of buttons. Each button is defined by three parameters:<BR>
@@ -27,6 +35,19 @@ public class ToolbarApplet extends java.applet.Applet implements ActionListener
         */
        private static final long serialVersionUID = 3243053622061086715L;
 
+       /**
+        * The name of the script to run Salome.
+        */
+       private static final String RUN_SALOME_SCRIPT = "run_salome_siman";
+       /**
+        * Possible script extensions.
+        */
+       private static final String[] SCRIPT_EXT = { "", ".sh", ".bat", ".cmd" };
+       /**
+        * The response key string: '<i>"canCheckout"</i> :'.
+        */
+       public static final String CHECKOUT_RES = "\"canCheckout\" :";
+
        // ==============================================================================================================================
        // Overridden functions
        // ==============================================================================================================================
@@ -106,50 +127,8 @@ public class ToolbarApplet extends java.applet.Applet implements ActionListener
 
                        // Opening an application on the local machine
 
-                       if (module.equals("runSalome")) {
-                               // Run SALOME
-                               // To get the SALOME_HOME environment variable
-                               String SALOME_HOME = System.getenv("SALOME_ROOT_DIR");
-                               // If SALOME_ROOT_DIR does not exist the SALOME_HOME equals to null.
-                               if (SALOME_HOME == null) {
-                                       // TODO: Use logger to be more user friendly
-                                       getAppletContext()
-                                                       .showDocument(
-                                                                       new URL(
-                                                                                       getCodeBase().toString()
-                                                                                                       + "error.jsp?message=launch&value="
-                                                                                                       + "SALOME_ROOT_DIR environment variable is not defined."),
-                                                                       "_blank");
-                                       throw new Exception(
-                                                       "SALOME_ROOT_DIR nevironment variable is not defined.");
-                               } else if (!SALOME_HOME.endsWith(File.separator)) {
-                                       SALOME_HOME += File.separator;
-                               }
-                               String pathToScript = SALOME_HOME + "runSalome";
-
-                               File script = new File(pathToScript);
-                               if (!script.exists()) {
-                                       script = new File(pathToScript + ".bat");
-                                       if (!script.exists()) {
-                                               script = new File(pathToScript + ".cmd");
-                                       }
-                               }
-                               
-                               if (script.exists()) {
-                                       // Execute the runSalome script.
-                                       // filename here indeed a string containing parameters for runSalome.
-                                       Runtime.getRuntime().exec(
-                                                       new String[] { script.getAbsolutePath(), filename });
-                               } else {
-                                       // TODO: Use logger to be more user friendly
-                                       getAppletContext().showDocument(
-                                                       new URL(getCodeBase().toString()
-                                                                       + "error.jsp?message=launch&value="
-                                                                       + "SALOME module is not found: "
-                                                                       + script.getAbsolutePath()), "_blank");
-                                       throw new FileNotFoundException(
-                                                       "SALOME module is not found: " + script.getAbsolutePath());
-                               }
+                       if ("runSalome".equals(module)) {
+                               salomeCheckout(filename);
                        } else
 
                        if (module.endsWith(".exe") || module.endsWith(".EXE")) {
@@ -202,7 +181,129 @@ public class ToolbarApplet extends java.applet.Applet implements ActionListener
                                }
                        }
                } catch (Exception error) {
-                       error.printStackTrace(); // RKV: NOPMD: TODO: try to use logger
+                       error.printStackTrace(); // RKV: NOPMD: Applet output
+               }
+       }
+
+       /**
+        * Checkout a scenario and start Salome with it.
+        * 
+        * @param params
+        *            Salome parameters
+        * @throws IOException
+        *             if URL connection opening/reading is failed
+        * @throws ConfigurationException
+        *             if SALOME_ROOT_DIR environment variable is not defined or Salome launcher script is not found
+        */
+       private void salomeCheckout(final String params)
+                       throws ConfigurationException, IOException {
+               // Run SALOME
+               // To get the SALOME_HOME environment variable
+               String SALOME_HOME = System.getenv("SALOME_ROOT_DIR");
+               // If SALOME_ROOT_DIR does not exist the SALOME_HOME equals to null.
+               if (SALOME_HOME == null) {
+                       // TODO: Use logger to be more user friendly
+                       showError("SALOME_ROOT_DIR environment variable is not defined.");
+                       throw new ConfigurationException(
+                                       "SALOME_ROOT_DIR nevironment variable is not defined.");
+               } else if (!SALOME_HOME.endsWith(File.separator)) {
+                       SALOME_HOME += File.separator;
                }
+               String pathToScript = SALOME_HOME + File.separator + RUN_SALOME_SCRIPT;
+               File script = new File(pathToScript);
+               String extensions = "";
+               // Look for the launching script in the file system
+               for (String ext : SCRIPT_EXT) {
+                       script = new File(pathToScript + ext); // RKV: NOPMD: There are not so much extensions
+                       if (script.exists()) {
+                               break;
+                       }
+                       extensions += "|" + ext;
+               }
+
+               if (script.exists()) {
+
+                       // Call to the Siman server to checkout the scenario
+                       URL checkoutUrl = new URL(getCodeBase().toString()
+                                       + "checkout.action?"
+                                       + params.replaceAll("--siman-", "&").replaceAll("\\s", ""));
+
+                       BufferedReader buffer = new BufferedReader(new InputStreamReader(
+                                       checkoutUrl.openStream()));
+
+                       // Read the response of the Siman server
+                       boolean isOk = false;
+                       String response = buffer.readLine();
+                       while ((response != null)) {
+                               if (response.contains(CHECKOUT_RES)) {
+                                       isOk = response.contains(CHECKOUT_RES + " \"true\"");
+                                       break;
+                               }
+                               response = buffer.readLine();
+                       }
+                       buffer.close();
+
+                       if (isOk) {
+                               // Execute the runSalome script.
+                               // filename here indeed a string containing parameters for runSalome.
+                               List<String> cmd = new ArrayList<String>(Arrays.asList(params
+                                               .split("\\s")));
+                               cmd.add(0, script.getAbsolutePath());
+                               Process proc = Runtime.getRuntime().exec(
+                                               cmd.toArray(new String[] {}), null,
+                                               new File(SALOME_HOME));
+                               // Refresh the current scenario view
+                               getAppletContext().showDocument(
+                                               new URL(this.getParameter("refresh")));
+                               BufferedReader bri = new BufferedReader(new InputStreamReader(
+                                               proc.getInputStream()));
+                               BufferedReader bre = new BufferedReader(new InputStreamReader(
+                                               proc.getErrorStream()));
+                               String line = bri.readLine();
+                               while (line != null) {
+                                       System.out.println(line); // RKV: NOPMD: Applet output
+                                       line = bri.readLine();
+                               }
+                               bri.close();
+                               line = bre.readLine();
+                               while (line != null) {
+                                       System.out.println(line); // RKV: NOPMD: Applet output
+                                       line = bre.readLine();
+                               }
+                               bre.close();
+                       } else {
+                               // Checkout of the scenario is failed at the beginning.
+                               if (response != null) {
+                                       response = response.substring(
+                                                       response.indexOf(CHECKOUT_RES)
+                                                                       + CHECKOUT_RES.length()).replace('}', ' ')
+                                                       .replace('"', ' ');
+                               }
+                               showError(response);
+                       }
+               } else {
+                       extensions = "[" + extensions.replaceAll("\\|\\|", "") + "]";
+                       showError("SALOME module is not found: " + pathToScript
+                                       + extensions);
+                       throw new ConfigurationException("SALOME module is not found: "
+                                       + pathToScript + extensions);
+               }
+       }
+
+       /**
+        * Show error message in the new window.
+        * 
+        * @param message
+        *            the error message
+        * @throws MalformedURLException
+        *             if built URL is incorrect
+        * @throws UnsupportedEncodingException
+        *             if UTF-8 encoding is not supported
+        */
+       private void showError(final String message) throws MalformedURLException,
+                       UnsupportedEncodingException {
+               getAppletContext().showDocument(
+                               new URL(getCodeBase().toString() + "../error.jsp?error="
+                                               + URLEncoder.encode(message, "UTF-8")), "_blank");
        }
 }
\ No newline at end of file