--- /dev/null
+package compbio.ws.server;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+\r
+import javax.servlet.ServletContextEvent;\r
+import javax.servlet.ServletContextListener;\r
+\r
+import org.apache.log4j.Logger;\r
+\r
+import compbio.engine.client.Executable.ExecProvider;\r
+import compbio.engine.client.Util;\r
+import compbio.engine.conf.PropertyHelperManager;\r
+import compbio.runner.disorder.IUPred;\r
+import compbio.runner.msa.Muscle;\r
+import compbio.util.SysPrefs;\r
+\r
+/**\r
+ * Run setexecflag.sh script if executable flag is not set\r
+ * \r
+ * @author pvtroshin\r
+ * \r
+ */\r
+public class SetExecutableFlag implements ServletContextListener {\r
+\r
+ private final Logger log = Logger.getLogger(SetExecutableFlag.class);\r
+\r
+ @Override\r
+ public void contextDestroyed(ServletContextEvent arg0) {\r
+ // do nothing\r
+ }\r
+\r
+ /**\r
+ * This listener is designed to run only once when the web application is\r
+ * deployed to set executable flag for binaries.\r
+ * \r
+ * @param arg0\r
+ * - ignored\r
+ */\r
+ @Override\r
+ public void contextInitialized(ServletContextEvent arg0) {\r
+ // Assume at least one of these is configured\r
+ // Do not even bother with cluster execution, sysadmins set the flag\r
+ // themselves\r
+ String command = Util.getCommand(ExecProvider.Local, Muscle.class);\r
+ if (compbio.util.Util.isEmpty(command)) {\r
+ command = Util.getCommand(ExecProvider.Local, IUPred.class);\r
+ }\r
+ boolean isExec = true;\r
+ if (!compbio.util.Util.isEmpty(command)) {\r
+ File file = new File(command);\r
+ isExec = file.canExecute();\r
+ }\r
+\r
+ String workDir = PropertyHelperManager.getLocalPath() + "binaries/src";\r
+ String script = "setexecflag.sh";\r
+\r
+ // Run only one once if not on Windows\r
+ if (!SysPrefs.isWindows && !isExec) {\r
+\r
+ // verify script exist\r
+ File scriptFile = new File(workDir, script);\r
+ if (!scriptFile.exists()) {\r
+ log.debug("Setexecflag.sh script is NOT found in "\r
+ + scriptFile.getAbsolutePath());\r
+ return;\r
+ } else {\r
+ scriptFile.setExecutable(true);\r
+ log.debug("Setexecflag.sh script is found");\r
+ }\r
+\r
+ try {\r
+ log.debug("Executable flag is NOT set for the binaries - attemping to set it");\r
+ ProcessBuilder pb = new ProcessBuilder(scriptFile.getAbsolutePath());\r
+ pb.directory(new File(workDir));\r
+ Process process = pb.start();\r
+ process.waitFor();\r
+ process.destroy();\r
+ } catch (IOException e) {\r
+ log.debug("Failed to execute set executable flag script due to IOException! Please run it manually!");\r
+ log.debug(e.getMessage(), e);\r
+ } catch (InterruptedException e) {\r
+ log.debug("Failed to execute set executable flag script due to Interruption! Please run it manually!");\r
+ }\r
+ } else {\r
+ log.debug("Executable flag is already set for the binaries.");\r
+ }\r
+ }\r
+}\r