001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    
021    package org.apache.geronimo.jetty6.connector;
022    
023    import java.util.concurrent.RejectedExecutionException;
024    import java.util.concurrent.TimeUnit;
025    
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    import org.apache.geronimo.system.threads.ThreadPool;
029    
030    /**
031     * @version $Rev: 543715 $ $Date: 2007-06-02 04:10:16 -0400 (Sat, 02 Jun 2007) $
032     */
033    public class JettyThreadPool implements org.mortbay.thread.ThreadPool {
034    
035        private static final Log log = LogFactory.getLog(JettyThreadPool.class);
036        private final ThreadPool executor;
037        private final String name;
038    
039    
040        public JettyThreadPool(ThreadPool executor, String name) {
041            this.executor = executor;
042            this.name = name;
043        }
044    
045        public boolean dispatch(Runnable runnable) {
046            try {
047                executor.execute(name, runnable);
048                return true;
049            } catch (RejectedExecutionException e) {
050                log.warn(e);
051                return false;
052            } catch (InterruptedException e) {
053                log.warn(e);
054                return false;
055            }
056        }
057    
058        public void join() throws InterruptedException {
059            //umm, this is doubtful
060            executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
061        }
062    
063        public int getThreads() {
064            return executor.getPoolSize();
065        }
066    
067        public int getIdleThreads() {
068            return executor.getMaximumPoolSize() - executor.getPoolSize();
069        }
070    
071        public boolean isLowOnThreads() {
072            return executor.getPoolSize() >= executor.getMaximumPoolSize();
073        }
074    }