View Javadoc
1 /* 2 * Scope: a generic MVC framework. 3 * Copyright (c) 2000-2002, The Scope team 4 * All rights reserved. 5 * 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * Neither the name "Scope" nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * 35 * 36 * $Id: BasicObjectPool.java,v 1.5 2002/09/05 15:41:46 ludovicc Exp $ 37 */ 38 package org.scopemvc.util; 39 40 41 import java.util.LinkedList; 42 import org.apache.commons.logging.Log; 43 import org.apache.commons.logging.LogFactory; 44 45 /*** 46 * Pools objects given a factory and optional maximum pool size. 47 * 48 * @author <A HREF="mailto:haruki_zaemon@users.sourceforge.net">Simon Harris</A> 49 * @created 05 September 2002 50 * @version $Revision: 1.5 $ 51 */ 52 public class BasicObjectPool implements ObjectPool { 53 54 private final static Log LOG = LogFactory.getLog(BasicObjectPool.class); 55 56 /*** 57 * Used for synchronisation 58 */ 59 private final Object lock = new Object(); 60 61 /*** 62 * Queue of free instances 63 */ 64 private final LinkedList freeList = new LinkedList(); 65 66 /*** 67 * The factory used to create pooled objects 68 */ 69 private final PoolableObjectFactory factory; 70 71 /*** 72 * The maximum number of objects to pool 73 */ 74 private final int maxSize; 75 76 /*** 77 * Construct a basic object pool with the specified factory and a maximum 78 * pool size of Integer.MAX_VALUE. 79 * 80 * @param inFactory TODO: Describe the Parameter 81 */ 82 public BasicObjectPool(PoolableObjectFactory inFactory) { 83 this(inFactory, Integer.MAX_VALUE); 84 } 85 86 /*** 87 * Construct a basic object pool with the specified factory and maximum pool 88 * size. 89 * 90 * @param inFactory TODO: Describe the Parameter 91 * @param inMaxSize TODO: Describe the Parameter 92 */ 93 public BasicObjectPool(PoolableObjectFactory inFactory, int inMaxSize) { 94 factory = inFactory; 95 maxSize = inMaxSize; 96 } 97 98 /*** 99 * Obtains an instance of the pooled class. Blocks if no free instances. 100 * 101 * @return TODO: Describe the Return Value 102 */ 103 public Object borrowObject() { 104 while (true) { 105 // Get an instance 106 Object object = borrowObjectIfExists(); 107 108 // Was there one? 109 if (object != null) { 110 // Yes-> 111 return object; 112 } 113 114 // No, wait for one to be freed 115 synchronized (lock) { 116 try { 117 lock.wait(); 118 } catch (InterruptedException ie) { 119 // Ignore it 120 } 121 } 122 } 123 } 124 125 /*** 126 * Obtains an instance of the pooled class. Returns null if no free 127 * instances. 128 * 129 * @return TODO: Describe the Return Value 130 */ 131 public Object borrowObjectIfExists() { 132 synchronized (lock) { 133 Object object = null; 134 135 // Any free objects? 136 if (!freeList.isEmpty()) { 137 // Yes, get the next one 138 object = freeList.removeFirst(); 139 } else { 140 // No, are we allowed to create another? 141 if (freeList.size() < maxSize) { 142 // Yes, create one 143 object = factory.createObject(); 144 } 145 } 146 147 // Finished, return the instance (or null) to the caller-> 148 factory.activateObject(object); 149 return object; 150 } 151 } 152 153 /*** 154 * Returns an instance to the pool. 155 * 156 * @param object A previously borrowed instance. 157 * @throw IllegalArgumentException if object was not previosuly borrowed 158 * from the pool. 159 */ 160 public void returnObject(Object object) { 161 synchronized (lock) { 162 factory.passivateObject(object); 163 freeList.addLast(object); 164 lock.notifyAll(); 165 } 166 } 167 }

This page was automatically generated by Maven