KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lateralnz > launchpad > Launcher


1 /* ====================================================================
2  * The LateralNZ Software License, Version 1.0
3  *
4  * Copyright (c) 2003 LateralNZ. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * LateralNZ (http://www.lateralnz.org/) and other third parties."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "LateralNZ" must not be used to endorse or promote
26  * products derived from this software without prior written
27  * permission. For written permission, please
28  * contact oss@lateralnz.org.
29  *
30  * 5. Products derived from this software may not be called "Panther",
31  * or "Lateral" or "LateralNZ", nor may "PANTHER" or "LATERAL" or
32  * "LATERALNZ" appear in their name, without prior written
33  * permission of LateralNZ.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of LateralNZ. For more
51  * information on Lateral, please see http://www.lateralnz.com/ or
52  * http://www.lateralnz.org
53  *
54  */

55 package org.lateralnz.launchpad;
56
57 import java.io.IOException JavaDoc;
58 import java.util.Arrays JavaDoc;
59 import java.util.Properties JavaDoc;
60
61 import javax.servlet.ServletConfig JavaDoc;
62 import javax.servlet.ServletException JavaDoc;
63 import javax.servlet.http.HttpServlet JavaDoc;
64 import javax.servlet.http.HttpServletRequest JavaDoc;
65 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
76  *
77  *
78  * @author J R Briggs
79  */

80 public class Launcher extends HttpServlet JavaDoc {
81   private static final Logger log = Logger.getLogger(Launcher.class.getName());
82   
83   private PythonInterpreter interp = new PythonInterpreter();
84   
85   private Properties JavaDoc props;
86   private String JavaDoc launchpadHome;
87   private String JavaDoc initDir;
88   
89   public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
90     try {
91       Properties JavaDoc props = ServletUtils.toProperties(config);
92
93       launch(props);
94     }
95     catch (Exception JavaDoc e) {
96       log.error(e);
97       throw new ServletException JavaDoc(e);
98     }
99   }
100     
101   protected void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
102     throw new ServletException JavaDoc("not available");
103   }
104   
105   protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
106     processRequest(request, response);
107   }
108   
109   protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
110     processRequest(request, response);
111   }
112   
113   public String JavaDoc getServletInfo() {
114     return "Short description";
115   }
116   
117   private void launch(Properties JavaDoc props) throws Exception JavaDoc {
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 JavaDoc("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     // startup scripts
140
String JavaDoc[] 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 JavaDoc[] files, boolean throwErrors) throws Exception JavaDoc {
148     for (int i = 0; i < files.length; i++) {
149       if (log.isInfoEnabled()) {
150         log.info("executing : " + files[i]);
151       }
152       
153       String JavaDoc py = StringUtils.readFromFile(launchpadHome + initDir + files[i]);
154       interp.set("sysprops", props);
155       try {
156         interp.exec(py);
157       }
158       catch (Exception JavaDoc 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 JavaDoc {
171     public void run() {
172       try {
173         //shutdown scripts
174
String JavaDoc[] filelist = SystemUtils.getFileList(launchpadHome + initDir, "^k.*\\.py$" );
175         Arrays.sort(filelist);
176         exec(filelist, false);
177       }
178       catch (Exception JavaDoc e) {
179         log.error(e);
180       }
181     }
182   }
183   
184  /**
185   * for running a standalone server
186   */

187   public static final void main(String JavaDoc[] args) {
188     try {
189       Properties JavaDoc 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 JavaDoc e) { }
196       }
197
198     }
199     catch (Exception JavaDoc e) {
200       e.printStackTrace();
201     }
202   }
203 }
Popular Tags