/* ====================================================================
* The LateralNZ Software License, Version 1.0
*
* Copyright (c) 2003 LateralNZ. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by
* LateralNZ (http://www.lateralnz.org/) and other third parties."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "LateralNZ" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please
* contact oss@lateralnz.org.
*
* 5. Products derived from this software may not be called "Panther",
* or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
* "LATERALNZ" appear in their name, without prior written
* permission of LateralNZ.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of LateralNZ. For more
* information on Lateral, please see http://www.lateralnz.com/ or
* http://www.lateralnz.org
*
*/
package org.lateralnz.launchpad;
import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.python.util.PythonInterpreter;
import org.lateralnz.common.util.ResourceUtils;
import org.lateralnz.common.util.ServletUtils;
import org.lateralnz.common.util.StringUtils;
import org.lateralnz.common.util.SystemUtils;
/**
*
*
* @author J R Briggs
*/
public class Launcher extends HttpServlet {
private static final Logger log = Logger.getLogger(Launcher.class.getName());
private PythonInterpreter interp = new PythonInterpreter();
private Properties props;
private String launchpadHome;
private String initDir;
public void init(ServletConfig config) throws ServletException {
try {
Properties props = ServletUtils.toProperties(config);
launch(props);
}
catch (Exception e) {
log.error(e);
throw new ServletException(e);
}
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
throw new ServletException("not available");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
public String getServletInfo() {
return "Short description";
}
private void launch(Properties props) throws Exception {
this.props = props;
launchpadHome = System.getProperty("LAUNCHPAD_HOME");
if (StringUtils.isEmpty(launchpadHome)) {
launchpadHome = System.getProperty("launchpad_home");
}
if (StringUtils.isEmpty(launchpadHome)) {
throw new Exception("property 'launchpad_home' is required");
}
launchpadHome = StringUtils.toDirectory(launchpadHome);
initDir = props.getProperty("conf_directory");
if (!StringUtils.isEmpty(initDir)) {
initDir = StringUtils.toDirectory(initDir);
}
if (log.isInfoEnabled()) {
log.info("Launchpad (" + launchpadHome + initDir + ")");
}
// startup scripts
String[] filelist = SystemUtils.getFileList(launchpadHome + initDir, "^s.*\\.py$" );
Arrays.sort(filelist);
exec(filelist, true);
Runtime.getRuntime().addShutdownHook(new ShutdownThread());
}
private final void exec(String[] files, boolean throwErrors) throws Exception {
for (int i = 0; i < files.length; i++) {
if (log.isInfoEnabled()) {
log.info("executing : " + files[i]);
}
String py = StringUtils.readFromFile(launchpadHome + initDir + files[i]);
interp.set("sysprops", props);
try {
interp.exec(py);
}
catch (Exception e) {
log.warn("unable to execute " + files[i]);
if (throwErrors) {
throw e;
}
else {
e.printStackTrace();
}
}
}
}
class ShutdownThread extends Thread {
public void run() {
try {
//shutdown scripts
String[] filelist = SystemUtils.getFileList(launchpadHome + initDir, "^k.*\\.py$" );
Arrays.sort(filelist);
exec(filelist, false);
}
catch (Exception e) {
log.error(e);
}
}
}
/**
* for running a standalone server
*/
public static final void main(String[] args) {
try {
Properties props = System.getProperties();
Launcher launcher = new Launcher();
launcher.launch(props);
while (true) {
try { Thread.currentThread().sleep(9999999999999L); } catch (Exception e) { }
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|