001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.pool.performance;
019    
020    import org.apache.commons.pool.PoolableObjectFactory;
021    
022    /**
023     * Sleepy ObjectFactory (everything takes a while longer)
024     * 
025     * @author Dirk Verbeeck
026     * @version $Revision: 777749 $ $Date: 2009-05-22 20:04:54 -0400 (Fri, 22 May 2009) $ 
027     */
028    public class SleepingObjectFactory implements PoolableObjectFactory {
029    
030        private int counter = 0;
031        private boolean debug = false;
032    
033        public Object makeObject() throws Exception {
034            Object obj = new Integer(counter++);
035            debug("makeObject", obj);
036            sleep(500);
037            return obj;
038        }
039    
040        public void destroyObject(Object obj) throws Exception {
041            debug("destroyObject", obj);
042            sleep(250);
043        }
044    
045        public boolean validateObject(Object obj) {
046            debug("validateObject", obj);
047            sleep(30);
048            return true;
049        }
050    
051        public void activateObject(Object obj) throws Exception {
052            debug("activateObject", obj);
053            sleep(10);
054        }
055    
056        public void passivateObject(Object obj) throws Exception {
057            debug("passivateObject", obj);
058            sleep(10);
059        }
060        
061        private void debug(String method, Object obj) {
062            if (debug) {
063                String thread = "thread" + Thread.currentThread().getName();
064                System.out.println(thread + ": " + method + " " + obj);
065            }
066        }
067        
068        private void sleep(long millis) {
069            try {
070                Thread.sleep(millis);
071            }
072            catch (InterruptedException e) {
073            }
074        }
075    
076        public boolean isDebug() {
077            return debug;
078        }
079    
080        public void setDebug(boolean b) {
081            debug = b;
082        }
083    }