KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > server > ServicePool


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: ServicePool.java 2499 2006-02-24 21:33:16Z dblevins $
44  */

45 package org.openejb.server;
46
47 import java.io.*;
48 import java.net.*;
49 import java.util.*;
50 import org.openejb.*;
51 import org.openejb.util.Logger;
52 import edu.emory.mathcs.backport.java.util.concurrent.Executor;
53 import edu.emory.mathcs.backport.java.util.concurrent.LinkedBlockingQueue;
54 import edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor;
55 import edu.emory.mathcs.backport.java.util.concurrent.ThreadFactory;
56 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
57
58 /**
59  * The Server will call the following methods.
60  *
61  * newInstance()
62  * init( port, properties)
63  * start()
64  * stop()
65  *
66  * All ServerService implementations must have a no argument
67  * constructor.
68  *
69  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
70  */

71 public class ServicePool implements ServerService {
72
73     private Logger log = Logger.getInstance(ServicePool.class.getName(), ServicePool.class.getName());
74     private ServerService next;
75     private Executor executor;
76
77
78     public ServicePool(ServerService next){
79         this.next = next;
80     }
81
82     /**
83      * Pulls out the access log information
84      *
85      * @param props
86      *
87      * @exception ServiceException
88      */

89     public void init(Properties props) throws Exception JavaDoc{
90
91         // Do our stuff
92
String JavaDoc threadsString = props.getProperty("threads", "200");
93         int threads = Integer.parseInt(threadsString);
94         final String JavaDoc name = props.getProperty("name", "unknown");
95
96         ThreadPoolExecutor p = new ThreadPoolExecutor(threads, threads, 5000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue());
97         p.setThreadFactory(new ThreadFactory() {
98             private volatile int id = 0;
99
100             public Thread JavaDoc newThread(Runnable JavaDoc arg0) {
101                 Thread JavaDoc thread = new Thread JavaDoc(arg0, name + " " + getNextID());
102                 return thread;
103             }
104
105             private int getNextID() {
106                 return id++;
107             }
108
109         });
110         executor = p;
111
112         // Then call the next guy
113
next.init(props);
114     }
115     
116     public void start() throws ServiceException{
117         // Do our stuff
118

119         // Then call the next guy
120
next.start();
121     }
122     
123     public void stop() throws ServiceException{
124         // Do our stuff
125

126         // Then call the next guy
127
next.stop();
128     }
129
130     public void service(final Socket socket) throws ServiceException, IOException{
131         final Runnable JavaDoc service = new Runnable JavaDoc() {
132             public void run() {
133                 try {
134                     next.service(socket);
135                 } catch (SecurityException JavaDoc e) {
136                     log.error("Security error: " + e.getMessage(), e);
137                 } catch (Throwable JavaDoc e) {
138                     log.error("Unexpected error", e);
139                 } finally {
140                     try {
141                         if (socket != null) {
142                             socket.close();
143                         }
144                     } catch (Throwable JavaDoc t) {
145                         log.warning("Error while closing connection with client", t);
146                     }
147                 }
148             }
149         };
150
151         final ClassLoader JavaDoc tccl = Thread.currentThread().getContextClassLoader();
152         Runnable JavaDoc ctxCL = new Runnable JavaDoc() {
153             public void run() {
154                 ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
155                 Thread.currentThread().setContextClassLoader(tccl);
156                 try {
157                     service.run();
158                 } finally {
159                     Thread.currentThread().setContextClassLoader(cl);
160                 }
161             }
162         };
163
164         executor.execute(ctxCL);
165     }
166
167     public void service(InputStream in, OutputStream out) throws ServiceException, IOException {
168         throw new UnsupportedOperationException JavaDoc("service(in,out)");
169     }
170
171     /**
172      * Gets the name of the service.
173      * Used for display purposes only
174      */

175     public String JavaDoc getName(){
176         return next.getName();
177     }
178
179     /**
180      * Gets the ip number that the
181      * daemon is listening on.
182      */

183     public String JavaDoc getIP(){
184         return next.getIP();
185     }
186     
187     /**
188      * Gets the port number that the
189      * daemon is listening on.
190      */

191     public int getPort(){
192         return next.getPort();
193     }
194
195 }
196
Popular Tags