Adding listener for setting executable flag on the web application start up
authorpvtroshin <pvtroshin@e3abac25-378b-4346-85de-24260fe3988d>
Mon, 5 Sep 2011 15:11:23 +0000 (15:11 +0000)
committerpvtroshin <pvtroshin@e3abac25-378b-4346-85de-24260fe3988d>
Mon, 5 Sep 2011 15:11:23 +0000 (15:11 +0000)
git-svn-id: link to svn.lifesci.dundee.ac.uk/svn/barton/ptroshin/JABA2@4563 e3abac25-378b-4346-85de-24260fe3988d

WEB-INF/web.xml
binaries/src/setexecflag.sh
webservices/compbio/ws/server/SetExecutableFlag.java [new file with mode: 0644]

index 794c4cb..3bf22cf 100644 (file)
     <listener>\r
         <listener-class>compbio.stat.servlet.StatisticCollector</listener-class>\r
      </listener>\r
-    <listener>\r
+     <listener>\r
+        <listener-class>compbio.ws.server.SetExecutableFlag</listener-class>\r
+     </listener>\r
+     <listener>\r
         <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>\r
-    </listener>\r
+     </listener>\r
     \r
        <!-- Session timeout in minutes -->\r
        <session-config>\r
index e4d11a0..aa82b3d 100644 (file)
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 
 echo ""
 echo "Setting executable flag for Clustalw..."
diff --git a/webservices/compbio/ws/server/SetExecutableFlag.java b/webservices/compbio/ws/server/SetExecutableFlag.java
new file mode 100644 (file)
index 0000000..dea7cbe
--- /dev/null
@@ -0,0 +1,89 @@
+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