001: /*
002: Copyright (C) 2003-2006 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.http;
034:
035: import javax.servlet.*;
036: import javax.servlet.http.*;
037:
038: import java.sql.SQLException;
039: import java.io.IOException;
040: import java.lang.ClassNotFoundException;
041:
042: import com.knowgate.debug.DebugFile;
043: import com.knowgate.misc.Environment;
044: import com.knowgate.misc.Gadgets;
045: import com.knowgate.scheduler.SchedulerDaemon;
046:
047: /**
048: * @author Sergio Montoro Ten
049: * @version 3.0
050: */
051:
052: public class HttpSchedulerServlet extends HttpServlet {
053:
054: // ---------------------------------------------------------------------------
055:
056: private SchedulerDaemon oDaemon;
057: private String sProfile;
058:
059: // ---------------------------------------------------------------------------
060:
061: public HttpSchedulerServlet() {
062: oDaemon = null;
063: sProfile = null;
064: }
065:
066: // ---------------------------------------------------------------------------
067:
068: private static boolean isVoid(String sParam) {
069: if (null == sParam)
070: return true;
071: else
072: return (sParam.length() == 0);
073: }
074:
075: // ---------------------------------------------------------------------------
076:
077: public void init() throws ServletException {
078: ServletConfig sconfig = getServletConfig();
079:
080: if (DebugFile.trace) {
081: DebugFile.writeln("Begin HttpSchedulerServlet.init()");
082: DebugFile.incIdent();
083: }
084:
085: sProfile = sconfig.getInitParameter("profile");
086:
087: if (isVoid(sProfile)) {
088: sProfile = Gadgets.chomp(Environment.getEnvVar(
089: "KNOWGATE_PROFILES",
090: Environment.DEFAULT_PROFILES_DIR),
091: java.io.File.separator)
092: + "hipergate.cnf";
093: }
094:
095: if (DebugFile.trace)
096: DebugFile.writeln("profile is " + sProfile);
097:
098: if (DebugFile.trace) {
099: DebugFile.decIdent();
100: DebugFile.writeln("End HttpSchedulerServlet.init()");
101: }
102: } // init
103:
104: // ---------------------------------------------------------------------------
105:
106: public void destroy() {
107: if (DebugFile.trace) {
108: DebugFile.writeln("Begin HttpSchedulerServlet.destroy()");
109: DebugFile.incIdent();
110: }
111: if (null != oDaemon) {
112: try {
113: oDaemon.stopAll();
114: } catch (IllegalStateException ise) {
115: if (DebugFile.trace)
116: DebugFile.writeln("IllegalStateException "
117: + ise.getMessage());
118: } catch (SQLException sql) {
119: if (DebugFile.trace)
120: DebugFile.writeln("SQLException "
121: + sql.getMessage());
122: }
123: oDaemon = null;
124: }
125: if (DebugFile.trace) {
126: DebugFile.decIdent();
127: DebugFile.writeln("End HttpSchedulerServlet.destroy()");
128: }
129: } // destroy
130:
131: // ---------------------------------------------------------------------------
132:
133: private static void writeXML(HttpServletResponse response,
134: String sTxt) throws ServletException {
135: response.setContentType("text/xml");
136: ServletOutputStream oOut = null;
137: try {
138: oOut = response.getOutputStream();
139: oOut.print(sTxt);
140: } catch (java.io.IOException ioe) {
141: throw new ServletException(ioe.getMessage(), ioe);
142: }
143: } // writeXML
144:
145: // ---------------------------------------------------------------------------
146:
147: /**
148: * <p>Perform action on SchedulerDaemon and return resulting status</p>
149: * The output written to ServletOutputStream is an XML string of the form:<br>
150: * <scheduler><status>{start|stop|running|stopped}</status><startdate>Thu Jul 14 23:04:33 CEST 2005</startdate><stopdate></stopdate><runningtime>98767</runningtime><poolsize>2</poolsize><livethreads>1</livethreads><queuelength>4</queuelength></scheduler>
151: * @param request Contains parameters: action = { start, stop, restart, info }
152: * @param response HttpServletResponse
153: * @throws ServletException
154: */
155: public void doGet(HttpServletRequest request,
156: HttpServletResponse response) throws ServletException {
157:
158: String sAction = request.getParameter("action");
159: if (null == sAction)
160: sAction = "";
161:
162: if (DebugFile.trace) {
163: DebugFile.writeln("Begin HttpSchedulerServlet.doGet("
164: + sAction + ")");
165: DebugFile.incIdent();
166: }
167:
168: if ((null != oDaemon) && sAction.equals("restart")) {
169: try {
170: oDaemon.stopAll();
171: oDaemon = null;
172: } catch (Exception xcpt) {
173: writeXML(response, "<scheduler><error><![CDATA["
174: + xcpt.getClass().getName() + " "
175: + xcpt.getMessage() + "]]></error></scheduler>");
176: }
177: if (null != oDaemon) {
178: if (DebugFile.trace) {
179: DebugFile
180: .writeln("HttpSchedulerServlet.doGet() : No scheduler instance found. Re-start failed");
181: DebugFile.decIdent();
182: }
183: return;
184: }
185: } // fi (restart)
186:
187: String sStatus = "", sSize = "", sLive = "", sQueue = "", sStartDate = "", sStopDate = "", sRunning = "";
188:
189: if (sAction.equals("start") || sAction.equals("restart")) {
190: if (null == oDaemon) {
191: try {
192: oDaemon = new SchedulerDaemon(sProfile);
193: if (DebugFile.trace) {
194: DebugFile.writeln("SchedulerDaemon.start()");
195: }
196: oDaemon.start();
197: sStatus = "start";
198: } catch (Exception xcpt) {
199: if (DebugFile.trace) {
200: DebugFile
201: .writeln("HttpSchedulerServlet.doGet() : "
202: + xcpt.getClass().getName()
203: + " " + xcpt.getMessage());
204: }
205: if (oDaemon != null) {
206: try {
207: oDaemon.stopAll();
208: } catch (Exception ignore) {
209: DebugFile
210: .writeln("HttpSchedulerServlet.doGet() : "
211: + ignore.getClass()
212: .getName()
213: + " "
214: + ignore.getMessage());
215: }
216: oDaemon = null;
217: }
218: writeXML(response, "<scheduler><error><![CDATA["
219: + xcpt.getClass().getName() + " "
220: + xcpt.getMessage()
221: + "]]></error></scheduler>");
222: } // catch
223: if (sStatus.length() == 0) {
224: if (DebugFile.trace) {
225: DebugFile
226: .writeln("Scheduler status is unknown");
227: DebugFile.decIdent();
228: }
229: return;
230: }
231: } else {
232: sStatus = "running";
233: }
234: } else if (sAction.equals("stop")) {
235: if (null != oDaemon) {
236: try {
237: oDaemon.stopAll();
238: if (null != oDaemon.stopDate())
239: sStopDate = oDaemon.stopDate().toString();
240: oDaemon = null;
241: sStatus = "stop";
242: } catch (IllegalStateException ist) {
243: DebugFile
244: .writeln("HttpSchedulerServlet.doGet() : IllegalStateException "
245: + ist.getMessage());
246: writeXML(response, "<scheduler><error><![CDATA["
247: + ist.getMessage()
248: + "]]></error></scheduler>");
249: } catch (SQLException sql) {
250: DebugFile
251: .writeln("HttpSchedulerServlet.doGet() : SQLException "
252: + sql.getMessage());
253: writeXML(response, "<scheduler><error><![CDATA["
254: + sql.getMessage()
255: + "]]></error></scheduler>");
256: }
257: if (null != oDaemon) {
258: if (oDaemon.isAlive()) {
259: try {
260: oDaemon.stop();
261: } catch (Exception ignore) {
262: }
263: }
264: oDaemon = null;
265: }
266: if (sStatus.length() == 0) {
267: if (DebugFile.trace) {
268: DebugFile
269: .writeln("Scheduler status is unknown");
270: DebugFile.decIdent();
271: }
272: return;
273: }
274: } else {
275: sStatus = "stopped";
276: }
277: } else if (sAction.equals("info")) {
278: if (null == oDaemon) {
279: sStatus = "stopped";
280: } else {
281: if (oDaemon.isAlive())
282: sStatus = "running";
283: else
284: sStatus = "death";
285: if (null != oDaemon.threadPool()) {
286: sSize = String.valueOf(oDaemon.threadPool().size());
287: sLive = String.valueOf(oDaemon.threadPool()
288: .livethreads());
289: }
290: if (null != oDaemon.atomQueue())
291: sQueue = String.valueOf(oDaemon.atomQueue().size());
292: }
293: }
294:
295: if (null != oDaemon) {
296: if (null != oDaemon.startDate())
297: sStartDate = oDaemon.startDate().toString();
298: if (null != oDaemon.stopDate())
299: sStopDate = oDaemon.stopDate().toString();
300: if (null != oDaemon.threadPool())
301: sRunning = String.valueOf(oDaemon.threadPool()
302: .getRunningTimeMS());
303: }
304:
305: writeXML(response, "<scheduler><error/><status>" + sStatus
306: + "</status><startdate>" + sStartDate
307: + "</startdate><stopdate>" + sStopDate
308: + "</stopdate><runningtime>" + sRunning
309: + "</runningtime><poolsize>" + sSize
310: + "</poolsize><livethreads>" + sLive
311: + "</livethreads><queuelength>" + sQueue
312: + "</queuelength></scheduler>");
313:
314: if (DebugFile.trace) {
315: DebugFile.writeln("start date=" + sStartDate);
316: DebugFile.writeln("stop date=" + sStopDate);
317: DebugFile.writeln("pool size=" + sSize);
318: DebugFile.writeln("live threads=" + sLive);
319: DebugFile.writeln("queue length=" + sQueue);
320: DebugFile.decIdent();
321: DebugFile.writeln("End HttpSchedulerServlet.doGet() : "
322: + sStatus);
323: }
324: } // doGet
325:
326: // ---------------------------------------------------------------------------
327: } // HttpSchedulerServlet
|