Tom
[EMAIL PROTECTED] wrote:
Hi, I'm intrested on testing your servlet capabilities, can u send it to me??
For the Open Source Power ...
Ahmed ALAMI DQAI/SDV Tel : 05 57 75 60 52
------------------------------------------------------------------------
Post-scriptum La Poste
Ce message est confidentiel. Sous réserve de tout accord conclu par écrit entre vous et La Poste, son contenu ne représente en aucun cas un engagement de la part de La Poste. Toute publication, utilisation ou diffusion, même partielle, doit être autorisée préalablement. Si vous n'êtes pas destinataire de ce message, merci d'en avertir immédiatement l'expéditeur.
------------------------------------------------------------------------
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
/* * Copyright: (c) TBEE.ORG * Modified: $Date: 2003/12/18 12:26:40 $ */
package org.tbee.servlets.log4j; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.util.Properties; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Category; import org.apache.log4j.PropertyConfigurator; /** * This servlet just initializes Log4j based on a propery file. * The file may be specified in the init parameters as "log4j.properties". * Per default "/WEB-INF/log4j.properties" and "/WEB-INF/classes/log4j.properties" are checked. * This servlet will reexamine the properties file every 10 seconds or when the get method is called. * A complete log4j properties file may be provided as part of the doPost submit. * * @version $Revision: 1.5 $ */ public class InitServlet extends HttpServlet { // Log4J private static Category cLog4J = Category.getInstance(InitServlet.class.getName()); // Frequence for looking for new properties static int cLog4jInterval = 10000 ; // 10 Seconds // the worker thread WorkerThread iWorkerThread = null; // the parameter for the configuration file static private String PARAMETER_CONFIGFILE = "configfile"; /** * initialize log4j * start the worker thread */ public void init() throws ServletException { // set up LOG4J { // determine properties file String lPrefix = getServletContext().getRealPath("/WEB-INF") + File.separator; String lFilePath = getInitParameter("log4j.properties"); if (lFilePath == null) lFilePath = "log4j.properties"; if (!(new File(lPrefix + lFilePath)).exists()) lFilePath = "log4j.properties"; if (!(new File(lPrefix + lFilePath)).exists()) lFilePath = "classes/log4j.properties"; if (!(new File(lPrefix + lFilePath)).exists()) { System.err.println("Log4j InitServlet: could not determine log4j file, reverting to auto configuration"); return; } // Set up the configuration PropertyConfigurator.configure(lPrefix + lFilePath); if (cLog4J.isInfoEnabled()) cLog4J.info("Log4J initialized using: " + lPrefix + lFilePath); // if debugging must be disabled entirely if ("true".equals(getInitParameter("disabled")) || "yes".equals(getInitParameter("disabled"))) { // disable if (cLog4J.isDebugEnabled()) cLog4J.debug("Log4J: further logging disabled!"); Category.getDefaultHierarchy().disableAll(); } else { // create worker thread if(cLog4J.isDebugEnabled()) cLog4J.debug("Starting worker thread"); iWorkerThread = new WorkerThread(lPrefix + lFilePath, 10000); // 10 seconds, TODO: make configurable iWorkerThread.start(); } } } /** * Stop the worker thread when the servlet is unloaded */ public void destroy() { // stop worker thread if(cLog4J.isDebugEnabled()) cLog4J.debug("Stopping worker thread"); iWorkerThread.stopAsSoonAsPossible(); } /** * reexamine the log4j file now */ public void doGet(HttpServletRequest pRequest, HttpServletResponse pResponse) { // interrupt the worker thread so it will loop immediatly iWorkerThread.interrupt(); try { pResponse.getOutputStream().println("<html>"); pResponse.getOutputStream().println(" <body>"); // basic feedback pResponse.getOutputStream().println(" Configuration file is reexamined<br/>"); // upload form pResponse.getOutputStream().println(" <form method=\"POST\">"); pResponse.getOutputStream().println(" Paste configuration file here:"); pResponse.getOutputStream().println(" <textarea name=\"" + PARAMETER_CONFIGFILE + "\"></textarea>"); pResponse.getOutputStream().println(" <input type=\"submit\" value=\"configure\"/>"); pResponse.getOutputStream().println(" </form>"); pResponse.getOutputStream().println(" </body>"); pResponse.getOutputStream().println("</html>"); } catch (IOException lException) { cLog4J.error(lException); } } /** * posting will overload the log4j settings until the configuration file is renewed or a new post is made */ public void doPost(HttpServletRequest pRequest, HttpServletResponse pResponse) { // interrupt the worker thread so it will loop immediatly iWorkerThread.interrupt(); try { // feedback pResponse.getOutputStream().print("<html><body>"); pResponse.getOutputStream().print("configuration file is reexamined<br/>"); // is the parameter submitted? if (pRequest.getParameter(PARAMETER_CONFIGFILE) != null) { // push file contents into log4j Properties lProperties = new Properties(); lProperties.load(new ByteArrayInputStream(pRequest.getParameter(PARAMETER_CONFIGFILE).toString().getBytes())); PropertyConfigurator.configure(lProperties); if (cLog4J.isInfoEnabled()) cLog4J.info("Log4J reinitialized using submitted text: " + pRequest.getParameter(PARAMETER_CONFIGFILE).toString().length() + " bytes"); pResponse.getOutputStream().print("Log4J reinitialized using submitted text<br/>"); } // feedback pResponse.getOutputStream().print("</body></html>"); } catch (IOException lException) { cLog4J.error("EXCEPTION: " + lException.getMessage() + " at " + lException.getStackTrace()[0].toString(), lException ); } } // // ================================================================= // /** * This is the actual thread checking the file */ private class WorkerThread extends Thread { /** * constructor */ public WorkerThread(String pConfigFilePath, int pSleep) { super(); iConfigFilePath = pConfigFilePath; iSleep = pSleep; } // the file to keep an eye on private String iConfigFilePath = null; // the timeout period private int iSleep = 10000; // the stop flag private boolean iStop = false; /** * make this thread stop asap */ public void stopAsSoonAsPossible() { // just set the flag iStop = true; if(cLog4J.isDebugEnabled()) cLog4J.debug("Stop flag set to " + iStop); // wake the thread up iWorkerThread.interrupt(); } /** * check the file and optionally reload */ public void run() { // remember last changed data long lLastModified = (new File(iConfigFilePath)).lastModified(); // endless loop while(true) { // stop check if(cLog4J.isDebugEnabled()) cLog4J.debug("Checking stop flag"); if (iStop) { if(cLog4J.isDebugEnabled()) cLog4J.debug("Stopping!"); break; } else { try { // sleep a while Thread.sleep(iSleep); } catch (InterruptedException lException) {} // do nothing, this is an early wake-up call } // was the file changed? long lCurrentModified = (new File(iConfigFilePath)).lastModified(); if(cLog4J.isDebugEnabled()) cLog4J.debug("Checking " + iConfigFilePath + ", current = " + lCurrentModified + ", last = " + lLastModified); if (lLastModified < lCurrentModified) { // reconfigure log4j PropertyConfigurator.configure(iConfigFilePath); if (cLog4J.isInfoEnabled()) cLog4J.info("Log4J reinitialized using: " + iConfigFilePath); // remember new datetime lLastModified = lCurrentModified; } } } } } /* * $Log: InitServlet.java,v $ * Revision 1.5 2003/12/18 12:26:40 cvs * cleanup * * Revision 1.4 2003/12/15 20:19:12 cvs * own worker thread * doGet and doPost * * Revision 1.3 2003/12/10 15:28:19 cvs * *** empty log message *** * * Revision 1.2 2002/09/25 08:25:10 tom * *** empty log message *** * */
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]