Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members

ServerThreadPool.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  *  2004 Modified by Romain Beaugrand
00027  * 
00028  */
00029 
00030 package org.openmobileis.embedded.webserver;
00031 
00032 import org.openmobileis.common.util.ObjectPool;
00033 import org.openmobileis.common.util.log.*;
00034 import org.openmobileis.common.util.exception.ServerException;
00035 import java.net.*;
00036 
00037 import javax.servlet.ServletContext;
00038 
00046 public class ServerThreadPool extends ObjectPool {
00047   class HTTPConnexionMaintener implements Runnable {
00048     public java.util.ArrayList connList;
00049     
00050     HTTPConnexionMaintener()  {
00051       connList = new java.util.ArrayList(5);
00052     }
00053     
00054     public void run() {
00055       while (true)  {
00056         try {
00057           Thread.currentThread().sleep(60000);       
00058         } catch (Throwable ex)  {
00059           LogManager.trace(new ServerException("Error in Thread that maintain ServletConnexion open", ex));          
00060         }
00061         long timeout = System.currentTimeMillis() - 60000;
00062         WebServerConnection conn = null;
00063         java.io.OutputStream stream = null;
00064         for (int i=0; i<connList.size(); i++)  {
00065           conn = (WebServerConnection)connList.get(i);
00066           if(conn.connexionLifeTime < timeout) { 
00067             try {
00068               stream = conn.getOutputStream();
00069               stream.write(' ');
00070             } catch (Throwable ex) {
00071               this.unregisterConnexion(conn);
00072             }
00073           }
00074         }
00075       }
00076     }
00077     
00078     public void registerConnexion(WebServerConnection connexion) {
00079       connList.add(connexion);
00080     }
00081     
00082     public void unregisterConnexion(WebServerConnection connexion) {
00083       connList.remove(connexion);
00084     }
00085     
00086   }
00087   
00088  // private HTTPConnexionMaintener maintener;
00089 
00090   public ServerThreadPool()  throws ClassNotFoundException, IllegalAccessException, InstantiationException  {
00091     super("org.openmobileis.embedded.webserver.ServerThread", 5, 5);
00092   //  maintener = new HTTPConnexionMaintener();
00093   }
00094 
00095   public synchronized void disposeObject(Object object) {
00096   //  maintener.unregisterConnexion(((ServerThread)object).getConnexion());
00097     super.disposeObject(object);
00098   }
00099   
00100   public void runServerConnexion(WebServer webserver, Socket socket) {
00101     ServerThread thread = (ServerThread) super.getObject();
00102     if (thread == null) {
00103       LogManager.traceEmergency(LogServices.SYNCHROSERVICE, "Object pool error ServerThread from pool NULL. Create one. System must be repared.");
00104       thread = new ServerThread();
00105     }
00106 
00107     try {
00108    //   maintener.registerConnexion(thread.getConnexion());
00109       thread.init(webserver, socket, this);
00110     } catch (Exception ex)  {
00111       LogManager.trace(new ServerException("Error during threadPool init", ex));
00112       LogManager.traceError(0, ex);
00113     }
00114   }
00115 }

Generated on Thu Oct 6 10:06:33 2005 for OpenMobileIS by  doxygen 1.4.3