| 1 55 package org.lateralnz.launchpad; 56 57 import java.io.IOException ; 58 import java.util.Arrays ; 59 import java.util.Properties ; 60 61 import javax.servlet.ServletConfig ; 62 import javax.servlet.ServletException ; 63 import javax.servlet.http.HttpServlet ; 64 import javax.servlet.http.HttpServletRequest ; 65 import javax.servlet.http.HttpServletResponse ; 66 67 import org.apache.log4j.Logger; 68 import org.python.util.PythonInterpreter; 69 70 import org.lateralnz.common.util.ResourceUtils; 71 import org.lateralnz.common.util.ServletUtils; 72 import org.lateralnz.common.util.StringUtils; 73 import org.lateralnz.common.util.SystemUtils; 74 75 80 public class Launcher extends HttpServlet { 81 private static final Logger log = Logger.getLogger(Launcher.class.getName()); 82 83 private PythonInterpreter interp = new PythonInterpreter(); 84 85 private Properties props; 86 private String launchpadHome; 87 private String initDir; 88 89 public void init(ServletConfig config) throws ServletException { 90 try { 91 Properties props = ServletUtils.toProperties(config); 92 93 launch(props); 94 } 95 catch (Exception e) { 96 log.error(e); 97 throw new ServletException (e); 98 } 99 } 100 101 protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 102 throw new ServletException ("not available"); 103 } 104 105 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 106 processRequest(request, response); 107 } 108 109 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 110 processRequest(request, response); 111 } 112 113 public String getServletInfo() { 114 return "Short description"; 115 } 116 117 private void launch(Properties props) throws Exception { 118 this.props = props; 119 120 launchpadHome = System.getProperty("LAUNCHPAD_HOME"); 121 if (StringUtils.isEmpty(launchpadHome)) { 122 launchpadHome = System.getProperty("launchpad_home"); 123 } 124 125 if (StringUtils.isEmpty(launchpadHome)) { 126 throw new Exception ("property 'launchpad_home' is required"); 127 } 128 launchpadHome = StringUtils.toDirectory(launchpadHome); 129 130 initDir = props.getProperty("conf_directory"); 131 if (!StringUtils.isEmpty(initDir)) { 132 initDir = StringUtils.toDirectory(initDir); 133 } 134 135 if (log.isInfoEnabled()) { 136 log.info("Launchpad (" + launchpadHome + initDir + ")"); 137 } 138 139 String [] filelist = SystemUtils.getFileList(launchpadHome + initDir, "^s.*\\.py$" ); 141 Arrays.sort(filelist); 142 exec(filelist, true); 143 144 Runtime.getRuntime().addShutdownHook(new ShutdownThread()); 145 } 146 147 private final void exec(String [] files, boolean throwErrors) throws Exception { 148 for (int i = 0; i < files.length; i++) { 149 if (log.isInfoEnabled()) { 150 log.info("executing : " + files[i]); 151 } 152 153 String py = StringUtils.readFromFile(launchpadHome + initDir + files[i]); 154 interp.set("sysprops", props); 155 try { 156 interp.exec(py); 157 } 158 catch (Exception e) { 159 log.warn("unable to execute " + files[i]); 160 if (throwErrors) { 161 throw e; 162 } 163 else { 164 e.printStackTrace(); 165 } 166 } 167 } 168 } 169 170 class ShutdownThread extends Thread { 171 public void run() { 172 try { 173 String [] filelist = SystemUtils.getFileList(launchpadHome + initDir, "^k.*\\.py$" ); 175 Arrays.sort(filelist); 176 exec(filelist, false); 177 } 178 catch (Exception e) { 179 log.error(e); 180 } 181 } 182 } 183 184 187 public static final void main(String [] args) { 188 try { 189 Properties props = System.getProperties(); 190 191 Launcher launcher = new Launcher(); 192 launcher.launch(props); 193 194 while (true) { 195 try { Thread.currentThread().sleep(9999999999999L); } catch (Exception e) { } 196 } 197 198 } 199 catch (Exception e) { 200 e.printStackTrace(); 201 } 202 } 203 } | Popular Tags |