Adding listener for setting executable flag on the web application start up
[jabaws.git] / webservices / compbio / ws / server / SetExecutableFlag.java
1 package compbio.ws.server;\r
2 \r
3 import java.io.File;\r
4 import java.io.IOException;\r
5 \r
6 import javax.servlet.ServletContextEvent;\r
7 import javax.servlet.ServletContextListener;\r
8 \r
9 import org.apache.log4j.Logger;\r
10 \r
11 import compbio.engine.client.Executable.ExecProvider;\r
12 import compbio.engine.client.Util;\r
13 import compbio.engine.conf.PropertyHelperManager;\r
14 import compbio.runner.disorder.IUPred;\r
15 import compbio.runner.msa.Muscle;\r
16 import compbio.util.SysPrefs;\r
17 \r
18 /**\r
19  * Run setexecflag.sh script if executable flag is not set\r
20  * \r
21  * @author pvtroshin\r
22  * \r
23  */\r
24 public class SetExecutableFlag implements ServletContextListener {\r
25 \r
26         private final Logger log = Logger.getLogger(SetExecutableFlag.class);\r
27 \r
28         @Override\r
29         public void contextDestroyed(ServletContextEvent arg0) {\r
30                 // do nothing\r
31         }\r
32 \r
33         /**\r
34          * This listener is designed to run only once when the web application is\r
35          * deployed to set executable flag for binaries.\r
36          * \r
37          * @param arg0\r
38          *            - ignored\r
39          */\r
40         @Override\r
41         public void contextInitialized(ServletContextEvent arg0) {\r
42                 // Assume at least one of these is configured\r
43                 // Do not even bother with cluster execution, sysadmins set the flag\r
44                 // themselves\r
45                 String command = Util.getCommand(ExecProvider.Local, Muscle.class);\r
46                 if (compbio.util.Util.isEmpty(command)) {\r
47                         command = Util.getCommand(ExecProvider.Local, IUPred.class);\r
48                 }\r
49                 boolean isExec = true;\r
50                 if (!compbio.util.Util.isEmpty(command)) {\r
51                         File file = new File(command);\r
52                         isExec = file.canExecute();\r
53                 }\r
54 \r
55                 String workDir = PropertyHelperManager.getLocalPath() + "binaries/src";\r
56                 String script = "setexecflag.sh";\r
57 \r
58                 // Run only one once if not on Windows\r
59                 if (!SysPrefs.isWindows && !isExec) {\r
60 \r
61                         // verify script exist\r
62                         File scriptFile = new File(workDir, script);\r
63                         if (!scriptFile.exists()) {\r
64                                 log.debug("Setexecflag.sh script is NOT found in "\r
65                                                 + scriptFile.getAbsolutePath());\r
66                                 return;\r
67                         } else {\r
68                                 scriptFile.setExecutable(true);\r
69                                 log.debug("Setexecflag.sh script is found");\r
70                         }\r
71 \r
72                         try {\r
73                                 log.debug("Executable flag is NOT set for the binaries - attemping to set it");\r
74                                 ProcessBuilder pb = new ProcessBuilder(scriptFile.getAbsolutePath());\r
75                                 pb.directory(new File(workDir));\r
76                                 Process process = pb.start();\r
77                                 process.waitFor();\r
78                                 process.destroy();\r
79                         } catch (IOException e) {\r
80                                 log.debug("Failed to execute set executable flag script due to IOException! Please run it manually!");\r
81                                 log.debug(e.getMessage(), e);\r
82                         } catch (InterruptedException e) {\r
83                                 log.debug("Failed to execute set executable flag script due to Interruption! Please run it manually!");\r
84                         }\r
85                 } else {\r
86                         log.debug("Executable flag is already set for the binaries.");\r
87                 }\r
88         }\r
89 }\r