Home » openejb-3.1.2-src » org.apache » openejb » server » [javadoc | source]

    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.openejb.server;
   18   
   19   import java.util.concurrent.Executor;
   20   import java.util.concurrent.LinkedBlockingQueue;
   21   import java.util.concurrent.ThreadPoolExecutor;
   22   import java.util.concurrent.ThreadFactory;
   23   import java.util.concurrent.TimeUnit;
   24   import java.util.concurrent.atomic.AtomicBoolean;
   25   
   26   import org.apache.openejb.util.LogCategory;
   27   import org.apache.openejb.util.Logger;
   28   import org.apache.openejb.loader.Options;
   29   import org.apache.openejb.loader.SystemInstance;
   30   
   31   import java.io.IOException;
   32   import java.io.InputStream;
   33   import java.io.OutputStream;
   34   import java.net.Socket;
   35   import java.util.Properties;
   36   
   37   public class ServicePool implements ServerService {
   38       private static final Logger log = Logger.getInstance(LogCategory.SERVICEPOOL, "org.apache.openejb.util.resources");
   39   
   40       private final ServerService next;
   41       private final Executor executor;
   42       private final ThreadPoolExecutor threadPool;
   43       private final AtomicBoolean stop = new AtomicBoolean();
   44   
   45       public ServicePool(ServerService next, String name, Properties properties) {
   46           this(next, name, new Options(properties).get("threads", 100));
   47       }
   48   
   49       public ServicePool(ServerService next, final String name, int threads) {
   50           this.next = next;
   51   
   52           final int keepAliveTime = (1000 * 60 * 5);
   53   
   54           threadPool = new ThreadPoolExecutor(threads, threads, keepAliveTime, TimeUnit.MILLISECONDS, new LinkedBlockingQueue());
   55           threadPool.setThreadFactory(new ThreadFactory() {
   56               private volatile int id = 0;
   57   
   58               public Thread newThread(Runnable arg0) {
   59                   Thread thread = new Thread(arg0, name + " " + getNextID());
   60                   return thread;
   61               }
   62   
   63               private int getNextID() {
   64                   return id++;
   65               }
   66   
   67           });
   68   
   69           executor = threadPool;
   70           SystemInstance.get().setComponent(ServicePool.class, this);
   71       }
   72   
   73       public ServicePool(ServerService next, Executor executor) {
   74           this.next = next;
   75           this.executor = executor;
   76           this.threadPool = null;
   77       }
   78   
   79       public ThreadPoolExecutor getThreadPool() {
   80           return threadPool;
   81       }
   82   
   83       public void service(InputStream in, OutputStream out) throws ServiceException, IOException {
   84       }
   85   
   86       public void service(final Socket socket) throws ServiceException, IOException {
   87           final Runnable service = new Runnable() {
   88               public void run() {
   89                   try {
   90                       if (stop.get()) return;
   91                       next.service(socket);
   92                   } catch (SecurityException e) {
   93                       log.error("Security error: " + e.getMessage(), e);
   94                   } catch (Throwable e) {
   95                       log.error("Unexpected error", e);
   96                   } finally {
   97                       try {
   98                           // Once the thread is done with the socket, clean it up
   99                           // The ServiceDaemon does not close the sockets as it is
  100                           // single threaded and only accepts sockets and then
  101                           // hands them off to be proceeceed.  As the thread doing
  102                           // that processing it is our job to close the socket
  103                           // when we are finished with it.
  104                           if (socket != null) {
  105                               socket.close();
  106                           }
  107                       } catch (Throwable t) {
  108                           log.warning("Error while closing connection with client", t);
  109                       }
  110                   }
  111               }
  112           };
  113   
  114           final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
  115           Runnable ctxCL = new Runnable() {
  116               public void run() {
  117                   ClassLoader cl = Thread.currentThread().getContextClassLoader();
  118                   Thread.currentThread().setContextClassLoader(tccl);
  119                   try {
  120                       service.run();
  121                   } finally {
  122                       Thread.currentThread().setContextClassLoader(cl);
  123                   }
  124               }
  125           };
  126   
  127           executor.execute(ctxCL);
  128       }
  129   
  130       /**
  131        * Pulls out the access log information
  132        *
  133        * @param props
  134        * @throws ServiceException
  135        */
  136       public void init(Properties props) throws Exception {
  137           // Do our stuff
  138   
  139           // Then call the next guy
  140           next.init(props);
  141       }
  142   
  143       public void start() throws ServiceException {
  144           // Do our stuff
  145   
  146           // Then call the next guy
  147           next.start();
  148       }
  149   
  150       public void stop() throws ServiceException {
  151           // Do our stuff
  152   
  153           // Then call the next guy
  154           next.stop();
  155       }
  156   
  157   
  158       /**
  159        * Gets the name of the service.
  160        * Used for display purposes only
  161        */
  162       public String getName() {
  163           return next.getName();
  164       }
  165   
  166       /**
  167        * Gets the ip number that the
  168        * daemon is listening on.
  169        */
  170       public String getIP() {
  171           return next.getIP();
  172       }
  173   
  174       /**
  175        * Gets the port number that the
  176        * daemon is listening on.
  177        */
  178       public int getPort() {
  179           return next.getPort();
  180       }
  181   
  182   }

Home » openejb-3.1.2-src » org.apache » openejb » server » [javadoc | source]