1 package com.lutris.appserver.debugger; 2 3 import java.util.Vector ; 4 import java.util.Enumeration ; 5 import java.util.Hashtable ; 6 import javax.servlet.http.*; 7 import java.io.IOException ; 8 import javax.servlet.*; 9 import javax.servlet.http.HttpServletResponse ; 10 import org.apache.catalina.connector.*; 11 import org.apache.catalina.*; 12 import org.apache.catalina.core.*; 13 import org.apache.catalina.valves.ValveBase; 14 import javax.servlet.http.HttpServletRequest ; 15 import javax.servlet.http.Cookie ; 17 import com.lutris.util.*; 18 import com.lutris.appserver.server.session.Session; 19 import com.lutris.appserver.server.sessionEnhydra.StandardSession; 20 import com.lutris.appserver.server.Application; 21 import com.lutris.appserver.server.session.SessionData; 22 import com.lutris.appserver.server.httpPresentation.servlet.*; 23 import com.lutris.appserver.server.sessionContainerAdapter.ContainerAdapterSessionManager; 24 import com.lutris.appserver.server.sessionContainerAdapter.JmxContainerAdapterSessionManager; 25 import com.lutris.appserver.server.sessionEnhydra.SimpleServletSessionManager; 26 import com.lutris.appserver.server.sessionEnhydra.StandardSessionManager; 27 import javax.management.MBeanRegistration ; 28 import javax.management.MBeanServer ; 29 import javax.management.*; 30 import org.apache.commons.modeler.Registry; 31 32 33 public class EnhydraDebuggerValve extends ValveBase implements MBeanRegistration { 34 35 private MBeanServer mBeanServer; 36 private static Object sinc = new Object (); 37 private Vector applicationsToDebug=new Vector (); 38 private String debugerAppName = "com.lutris.appserver.debugger.Debugger"; 39 40 public EnhydraDebuggerValve(){ 41 } 42 43 public void addApplication(String name) 44 { 45 if(!applicationsToDebug.contains(name)) 46 applicationsToDebug.add(name); 47 } 48 49 public void removeApplication(String name) 50 { 51 if(applicationsToDebug.contains(name)) 52 applicationsToDebug.remove(name); 53 } 54 55 public Vector getApplications() 56 { 57 return applicationsToDebug; 58 } 59 60 public String getInfo() 61 { 62 return "com.lutris.appserver.debugger.business.EnhydraDebuggerValve"; 63 } 64 65 66 public void setDebuggerAppName(String debugerAppName) 67 { 68 this.debugerAppName = debugerAppName; 69 } 70 71 public String getDebuggerAppName() 72 { 73 return debugerAppName; 74 } 75 76 77 public void invoke(Request request, Response response) 78 throws IOException , ServletException 79 { 80 String app=request.getContext().getPath(); 81 82 if(applicationsToDebug.contains(app)) 83 { 84 85 Hashtable record=new Hashtable (); 86 record.put("ServletName",app); 87 88 manageRequest(record,request); 89 getNext().invoke(request, response); 91 92 manageResponse(record,response,request); 93 94 try 95 { 96 ObjectName oname = new ObjectName("EnhydraApplications:type="+debugerAppName+",name=ServletMonitor"); 97 Attribute temp=new Attribute("Transactions",record); 98 getMBeanServer().setAttribute(oname, temp); 99 Object [] params=new Object [0]; 100 String [] signature=new String [0]; 101 getMBeanServer().invoke(oname,"newRequest",params, signature); 102 }catch(Exception e){ 103 } 105 } 106 else 107 getNext().invoke(request, response); 109 } 110 111 112 113 114 115 public StringBuffer getAttribuestAsStringBuffer(HttpSession httpsession) 116 { 117 StringBuffer stringbuffer = new StringBuffer (); 118 Enumeration enumeration = httpsession.getAttributeNames(); 119 if(httpsession != null) 120 { 121 stringbuffer.append("<UL>\n"); 122 for(; enumeration.hasMoreElements(); stringbuffer.append("\n")) 123 { 124 String s = (String )enumeration.nextElement(); 125 Object obj = httpsession.getAttribute(s); 126 stringbuffer.append("<LI> <TT>"); 127 stringbuffer.append(s); 128 stringbuffer.append(": </TT>"); 129 stringbuffer.append(obj.toString()); 130 } 131 132 stringbuffer.append("</UL>\n"); 133 } 134 return stringbuffer; 135 } 136 137 138 private static void wait(int wait) 139 { 140 141 long now = System.currentTimeMillis(); 142 long giveUpTime = now + (wait *10); 143 144 long leftToGo = giveUpTime - now; 145 146 synchronized (sinc) { 147 try { 148 sinc.wait(leftToGo); 149 } catch (InterruptedException e) { 150 151 } 152 } 153 } 154 155 156 157 private String getSessionData(Request r) 158 { 159 HttpServletRequest request=(HttpServletRequest )r; 160 Session session = null; 161 HttpSession httpsession; 162 httpsession = request.getSession(false); 163 javax.servlet.Servlet servlet; 164 StandardContext cont=(StandardContext)r.getContext(); 165 Container[] wrappers=cont.findChildren(); 166 Wrapper wrapper=(StandardWrapper)wrappers[0]; 167 168 try 169 { 170 servlet = wrapper.allocate(); 171 }catch(ServletException e) 172 { 173 return "<I>No Session</I>"; 174 } 175 176 if(servlet instanceof HttpPresentationServlet) 177 { 178 Application app = ((HttpPresentationServlet)servlet).getApplication(); 180 181 com.lutris.appserver.server.session.SessionManager sm = app.getSessionManager(); 182 183 if((sm instanceof StandardSessionManager) || (sm instanceof SimpleServletSessionManager)) 184 { 185 session = ((HttpPresentationServlet)servlet).getSession(request); 186 if(!(sm instanceof SimpleServletSessionManager) && session != null) 187 httpsession = session.getHttpSession(); 188 } 189 190 if((sm instanceof ContainerAdapterSessionManager) || (sm instanceof JmxContainerAdapterSessionManager)) 191 { 192 if(httpsession != null) 193 { 194 session = (Session)httpsession.getAttribute("session"); 195 } 196 else 197 { 198 try 199 { 200 wrapper.deallocate(servlet); 201 } 202 catch(ServletException e) 203 { 204 return "<I>No Session</I>"; 205 } 206 return "<I>No Session</I>"; 207 } 208 } 209 } 210 else 211 { 212 String s; 214 if(httpsession != null) 215 { 216 s = ""; 217 StringBuffer stringbuffer = getAttribuestAsStringBuffer(httpsession); 218 s = s + stringbuffer.toString(); 219 if(s.length() == 0) 220 s = "<I>Empty</I>"; 221 try 222 { 223 wrapper.deallocate(servlet); 224 } 225 catch(ServletException e) 226 { 227 return "<I>No Session</I>"; 228 } 229 230 return s; 231 }else{ 232 try 233 { 234 wrapper.deallocate(servlet); 235 } 236 catch(ServletException e) 237 { 238 return "<I>No Session</I>"; 239 } 240 return "<I>No Session</I>"; 241 } 242 } 243 244 try 245 { 246 wrapper.deallocate(servlet); 247 } 248 catch(ServletException e) 249 { 250 return "<I>No Session</I>"; 251 } 252 253 254 255 if(httpsession==null) 257 return "<I>No Session</I>"; 258 else 259 { 260 if(session == null) 261 { 262 String s=""; 263 StringBuffer stringbuffer = getAttribuestAsStringBuffer(httpsession); 264 s = s + stringbuffer.toString(); 265 if(s.length() == 0) 266 s = "<I>Empty</I>"; 267 return s; 268 269 }else{ 270 String s=""; 271 SessionData sessiondata = session.getSessionData(); 272 s = sessiondata == null ? "" : sessiondata.toHtml(); 273 if(s == null) 274 s = ""; 275 StringBuffer stringbuffer = getAttribuestAsStringBuffer(httpsession); 276 s = s + stringbuffer.toString(); 277 if(s.length() == 0) 278 s = "<I>Empty</I>"; 279 return s; 280 } 281 } 282 283 } 284 285 286 private String check(String data){ 287 if(data==null || data=="") 288 return "N/A"; 289 else 290 return new String (data); 291 } 292 293 294 private void manageRequest(Hashtable record,Request r) 295 { 296 ServletRequest req = r.getRequest(); 297 HttpServletRequest request = (HttpServletRequest )r; 298 String sessionDataRequest=getSessionData(r); 299 record.put("S",sessionDataRequest); 300 record.put("RemoteHost",check(req.getRemoteHost())); 301 record.put("RemoteAdr",check(req.getRemoteAddr())); 302 record.put("Method",check(request.getMethod())); 303 record.put("ServerName",check(req.getServerName())); 304 record.put("ServerPort",new Integer (req.getServerPort())); 305 record.put("RequestURI",check(request.getRequestURI())); 306 record.put("Scheme",check(req.getScheme())); 307 record.put("QueryString",check(request.getQueryString())); 308 record.put("RemoteUser",check(request.getRemoteUser())); 309 record.put("AuthType",check(request.getAuthType())); 310 record.put("ContentLength",new Integer (req.getContentLength())); 311 record.put("ContentType",check(req.getContentType())); 312 record.put("PathInfo",check(request.getPathInfo())); 313 record.put("PathTranslated",check(request.getPathTranslated())); 314 record.put("Protocol",check(req.getProtocol())); 315 316 Enumeration e = request.getHeaderNames(); 317 Vector names=new Vector (); 318 319 while (e.hasMoreElements()) 320 { 321 String name= (String )e.nextElement(); 322 String header = request.getHeader(name); 323 names.add(name); 324 record.put(name,header); 325 } 326 record.put("HeaderNames",names); 327 328 Cookie [] temp=request.getCookies(); 329 if(temp!=null) 330 record.put("Cookies",temp); 331 else 332 record.put("Cookies",new Cookie [0]); 333 334 Enumeration en = req.getParameterNames(); 335 Vector namess=new Vector (); 336 337 while (en.hasMoreElements()) 338 { 339 String name= (String )en.nextElement(); 340 String [] values = request.getParameterValues(name); 341 namess.add(name); 342 record.put(name,values); 343 } 344 record.put("ParameterNames",namess); 345 346 StringBuffer uri = new StringBuffer (); 347 uri.append(request.getRequestURI()); 348 349 if(request.getQueryString() != null) 350 { 351 uri.append('?'); 352 uri.append(request.getQueryString()); 353 } 354 355 record.put("RequestURL",uri.toString()); 356 } 357 358 359 private void manageResponse(Hashtable record,Response response,Request req) 360 { 361 362 String sessionDataResponse=getSessionData(req); 363 record.put("S1",sessionDataResponse); 364 365 Cookie [] coc=response.getCookies(); 366 367 if(coc==null) 368 record.put("CookiesResponse",new Cookie [0]); 369 else 370 record.put("CookiesResponse",coc); 371 372 Integer status=new Integer (response.getStatus()); 373 record.put("StatusCode",status); 374 record.put("Message",check(response.getMessage())); 375 376 Integer totalBytes=new Integer (response.getContentCount()); 377 378 if(totalBytes==null ||totalBytes.intValue()==0) 379 record.put("TotalBytesResponse",new Integer (-1)); 380 else 381 record.put("TotalBytesResponse",totalBytes); 382 383 record.put("ContentLengthResponse",new Integer (response.getContentLength())); 384 record.put("ContentTypeResponse",check(response.getContentType())); 385 record.put("StatusResponse",new Integer (response.getStatus())); 386 387 String [] hnames=(response.getHeaderNames()); 388 389 for(int u=0;u<hnames.length;u++) 390 { 391 String header=(response.getHeader(hnames[u])); 392 record.put("Response"+hnames[u],header); 393 } 394 record.put("HeaderNamesResponse",hnames); 395 } 396 397 protected String domain; 399 protected ObjectName oname; 400 protected MBeanServer mserver; 401 402 public ObjectName getObjectName() { 403 return oname; 404 } 405 406 public String getDomain() { 407 return domain; 408 } 409 410 public ObjectName preRegister(MBeanServer server, 411 ObjectName name) throws Exception { 412 oname=name; 413 mserver=server; 414 domain=name.getDomain(); 415 return name; 416 } 417 418 public void postRegister(Boolean registrationDone) { 419 } 420 421 public void preDeregister() throws Exception { 422 } 423 424 public void postDeregister() { 425 } 426 427 428 private MBeanServer getMBeanServer() 429 { 430 if(mBeanServer==null) 431 mBeanServer = Registry.getRegistry(null, null).getMBeanServer(); 432 433 return mBeanServer; 434 } 435 436 } 437 438 439 440 441 442 | Popular Tags |