KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > http > HttpSchedulerServlet


1 /*
2   Copyright (C) 2003-2006 Know Gate S.L. All rights reserved.
3                            C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.http;
34
35 import javax.servlet.*;
36 import javax.servlet.http.*;
37
38 import java.sql.SQLException JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.lang.ClassNotFoundException JavaDoc;
41
42 import com.knowgate.debug.DebugFile;
43 import com.knowgate.misc.Environment;
44 import com.knowgate.misc.Gadgets;
45 import com.knowgate.scheduler.SchedulerDaemon;
46
47 /**
48  * @author Sergio Montoro Ten
49  * @version 3.0
50  */

51
52 public class HttpSchedulerServlet extends HttpServlet {
53
54   // ---------------------------------------------------------------------------
55

56   private SchedulerDaemon oDaemon;
57   private String JavaDoc sProfile;
58
59   // ---------------------------------------------------------------------------
60

61   public HttpSchedulerServlet() {
62     oDaemon=null;
63     sProfile=null;
64   }
65
66   // ---------------------------------------------------------------------------
67

68   private static boolean isVoid(String JavaDoc sParam) {
69     if (null==sParam)
70       return true;
71     else
72       return (sParam.length()==0);
73   }
74
75   // ---------------------------------------------------------------------------
76

77   public void init() throws ServletException {
78     ServletConfig sconfig = getServletConfig();
79
80     if (DebugFile.trace) {
81       DebugFile.writeln("Begin HttpSchedulerServlet.init()");
82       DebugFile.incIdent();
83     }
84
85     sProfile = sconfig.getInitParameter("profile");
86
87     if (isVoid(sProfile)) {
88       sProfile = Gadgets.chomp(Environment.getEnvVar("KNOWGATE_PROFILES",Environment.DEFAULT_PROFILES_DIR), java.io.File.separator) + "hipergate.cnf";
89     }
90
91     if (DebugFile.trace) DebugFile.writeln("profile is " + sProfile);
92
93     if (DebugFile.trace) {
94       DebugFile.decIdent();
95       DebugFile.writeln("End HttpSchedulerServlet.init()");
96     }
97   } // init
98

99   // ---------------------------------------------------------------------------
100

101   public void destroy() {
102     if (DebugFile.trace) {
103       DebugFile.writeln("Begin HttpSchedulerServlet.destroy()");
104       DebugFile.incIdent();
105     }
106     if (null!=oDaemon) {
107       try {
108         oDaemon.stopAll();
109       }
110       catch (IllegalStateException JavaDoc ise) {
111         if (DebugFile.trace)
112           DebugFile.writeln("IllegalStateException " + ise.getMessage());
113       }
114       catch (SQLException JavaDoc sql) {
115         if (DebugFile.trace)
116           DebugFile.writeln("SQLException " + sql.getMessage());
117       }
118       oDaemon = null;
119     }
120     if (DebugFile.trace) {
121       DebugFile.decIdent();
122       DebugFile.writeln("End HttpSchedulerServlet.destroy()");
123     }
124   } // destroy
125

126   // ---------------------------------------------------------------------------
127

128   private static void writeXML(HttpServletResponse response, String JavaDoc sTxt) throws ServletException {
129     response.setContentType("text/xml");
130     ServletOutputStream oOut = null;
131     try {
132       oOut = response.getOutputStream();
133       oOut.print(sTxt);
134     } catch (java.io.IOException JavaDoc ioe) {
135       throw new ServletException(ioe.getMessage(), ioe);
136     }
137   } // writeXML
138

139   // ---------------------------------------------------------------------------
140

141   /**
142    * <p>Perform action on SchedulerDaemon and return resulting status</p>
143    * The output written to ServletOutputStream is an XML string of the form:<br>
144    * &lt;scheduler&gt;&lt;status&gt;{start|stop|running|stopped}&lt;/status&gt;&lt;startdate&gt;Thu Jul 14 23:04:33 CEST 2005&lt;/startdate&gt;&lt;stopdate&gt;&lt;/stopdate&gt;&lt;runningtime&gt;98767&lt;/runningtime&gt;&lt;poolsize&gt;2&lt;/poolsize&gt;&lt;livethreads&gt;1&lt;/livethreads&gt;&lt;queuelength&gt;4&lt;/queuelength&gt;&lt;/scheduler&gt;
145    * @param request Contains parameters: action = { start, stop, restart, info }
146    * @param response HttpServletResponse
147    * @throws ServletException
148    */

149   public void doGet(HttpServletRequest request, HttpServletResponse response)
150      throws ServletException {
151
152    String JavaDoc sAction = request.getParameter("action");
153    if (null==sAction) sAction = "";
154
155     if (DebugFile.trace) {
156       DebugFile.writeln("Begin HttpSchedulerServlet.doGet("+sAction+")");
157       DebugFile.incIdent();
158     }
159
160     if ((null!=oDaemon) && sAction.equals("restart")) {
161       try {
162         oDaemon.stopAll();
163         oDaemon = null;
164       } catch (Exception JavaDoc xcpt) {
165         writeXML(response, "<scheduler><error><![CDATA["+xcpt.getClass().getName()+" "+xcpt.getMessage()+"]]></error></scheduler>");
166       }
167       if (null!=oDaemon) {
168         if (DebugFile.trace) {
169           DebugFile.writeln("HttpSchedulerServlet.doGet() : No scheduler instance found. Re-start failed");
170           DebugFile.decIdent();
171         }
172         return;
173       }
174     } // fi (restart)
175

176     String JavaDoc sStatus = "", sSize = "", sLive = "", sQueue = "", sStartDate = "", sStopDate = "", sRunning = "";
177
178     if (sAction.equals("start") || sAction.equals("restart")) {
179       if (null==oDaemon) {
180         try {
181           oDaemon = new SchedulerDaemon(sProfile);
182           if (DebugFile.trace) {
183             DebugFile.writeln("SchedulerDaemon.start()");
184           }
185           oDaemon.start();
186           sStatus = "start";
187         }
188         catch (Exception JavaDoc xcpt) {
189           if (DebugFile.trace) {
190             DebugFile.writeln("HttpSchedulerServlet.doGet() : "+xcpt.getClass().getName()+" "+xcpt.getMessage());
191           }
192           if (oDaemon!=null) {
193             try { oDaemon.stopAll(); }
194             catch (Exception JavaDoc ignore) {
195               DebugFile.writeln("HttpSchedulerServlet.doGet() : " + ignore.getClass().getName() + " " + ignore.getMessage());
196             }
197             oDaemon=null;
198           }
199           writeXML(response, "<scheduler><error><![CDATA["+xcpt.getClass().getName()+" "+xcpt.getMessage()+"]]></error></scheduler>");
200         } // catch
201
if (sStatus.length()==0) {
202           if (DebugFile.trace) {
203             DebugFile.writeln("Scheduler status is unknown");
204             DebugFile.decIdent();
205           }
206           return;
207         }
208       }
209       else {
210         sStatus = "running";
211       }
212     }
213     else if (sAction.equals("stop")) {
214       if (null!=oDaemon) {
215         try {
216           oDaemon.stopAll();
217           if (null!=oDaemon.stopDate()) sStopDate = oDaemon.stopDate().toString();
218           oDaemon=null;
219           sStatus = "stop";
220         } catch (IllegalStateException JavaDoc ist) {
221             DebugFile.writeln("HttpSchedulerServlet.doGet() : IllegalStateException " + ist.getMessage());
222             writeXML(response, "<scheduler><error><![CDATA["+ist.getMessage()+"]]></error></scheduler>");
223         } catch (SQLException JavaDoc sql) {
224             DebugFile.writeln("HttpSchedulerServlet.doGet() : SQLException " + sql.getMessage());
225             writeXML(response, "<scheduler><error><![CDATA["+sql.getMessage()+"]]></error></scheduler>");
226         }
227         if (null!=oDaemon) {
228           if (oDaemon.isAlive()) { try { oDaemon.stop(); } catch (Exception JavaDoc ignore) {} }
229           oDaemon=null;
230         }
231         if (sStatus.length()==0) {
232           if (DebugFile.trace) {
233             DebugFile.writeln("Scheduler status is unknown");
234             DebugFile.decIdent();
235           }
236           return;
237         }
238       }
239       else {
240         sStatus = "stopped";
241       }
242     }
243     else if (sAction.equals("info")) {
244       if (null==oDaemon) {
245         sStatus = "stopped";
246       } else {
247         if (oDaemon.isAlive())
248           sStatus = "running";
249         else
250           sStatus = "death";
251         if (null!=oDaemon.threadPool()) {
252           sSize = String.valueOf(oDaemon.threadPool().size());
253           sLive = String.valueOf(oDaemon.threadPool().livethreads());
254         }
255         if (null!=oDaemon.atomQueue())
256           sQueue = String.valueOf(oDaemon.atomQueue().size());
257       }
258     }
259
260     if (null!=oDaemon) {
261       if (null!=oDaemon.startDate()) sStartDate = oDaemon.startDate().toString();
262       if (null!=oDaemon.stopDate() ) sStopDate = oDaemon.stopDate().toString();
263       if (null!=oDaemon.threadPool())
264         sRunning = String.valueOf(oDaemon.threadPool().getRunningTimeMS());
265     }
266
267     writeXML(response, "<scheduler><error/><status>"+sStatus+"</status><startdate>"+sStartDate+"</startdate><stopdate>"+sStopDate+"</stopdate><runningtime>"+sRunning+"</runningtime><poolsize>"+sSize+"</poolsize><livethreads>"+sLive+"</livethreads><queuelength>"+sQueue+"</queuelength></scheduler>");
268
269     if (DebugFile.trace) {
270       DebugFile.writeln("start date="+sStartDate);
271       DebugFile.writeln("stop date="+sStopDate);
272       DebugFile.writeln("pool size="+sSize);
273       DebugFile.writeln("live threads="+sLive);
274       DebugFile.writeln("queue length="+sQueue);
275       DebugFile.decIdent();
276       DebugFile.writeln("End HttpSchedulerServlet.doGet() : " + sStatus);
277     }
278   } // doGet
279

280   // ---------------------------------------------------------------------------
281
} // HttpSchedulerServlet
282
Popular Tags