1 32 33 package com.knowgate.http; 34 35 import javax.servlet.*; 36 import javax.servlet.http.*; 37 38 import java.sql.SQLException ; 39 import java.io.IOException ; 40 import java.lang.ClassNotFoundException ; 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 51 52 public class HttpSchedulerServlet extends HttpServlet { 53 54 56 private SchedulerDaemon oDaemon; 57 private String sProfile; 58 59 61 public HttpSchedulerServlet() { 62 oDaemon=null; 63 sProfile=null; 64 } 65 66 68 private static boolean isVoid(String sParam) { 69 if (null==sParam) 70 return true; 71 else 72 return (sParam.length()==0); 73 } 74 75 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 } 99 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 ise) { 111 if (DebugFile.trace) 112 DebugFile.writeln("IllegalStateException " + ise.getMessage()); 113 } 114 catch (SQLException 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 } 126 128 private static void writeXML(HttpServletResponse response, String 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 ioe) { 135 throw new ServletException(ioe.getMessage(), ioe); 136 } 137 } 139 141 149 public void doGet(HttpServletRequest request, HttpServletResponse response) 150 throws ServletException { 151 152 String 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 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 } 176 String 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 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 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 } 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 ist) { 221 DebugFile.writeln("HttpSchedulerServlet.doGet() : IllegalStateException " + ist.getMessage()); 222 writeXML(response, "<scheduler><error><![CDATA["+ist.getMessage()+"]]></error></scheduler>"); 223 } catch (SQLException 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 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 } 280 } | Popular Tags |