001: package org.apache.turbine.services.pool;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * 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, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import org.apache.turbine.services.TurbineServices;
020: import org.apache.turbine.util.TurbineException;
021:
022: /**
023: * This is a static accessor to common pooling tasks.
024: *
025: * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
026: * @version $Id: TurbinePool.java 264148 2005-08-29 14:21:04Z henning $
027: */
028: public abstract class TurbinePool {
029: /**
030: * Gets an instance of a named class either from the pool
031: * or by calling the Factory Service if the pool is empty.
032: *
033: * @param className the name of the class.
034: * @return the instance.
035: * @throws TurbineException if recycling fails.
036: */
037: public static Object getInstance(String className)
038: throws TurbineException {
039: return getService().getInstance(className);
040: }
041:
042: /**
043: * Gets an instance of a named class either from the pool
044: * or by calling the Factory Service if the pool is empty.
045: * The specified class loader will be passed to the Factory Service.
046: *
047: * @param className the name of the class.
048: * @param loader the class loader.
049: * @return the instance.
050: * @throws TurbineException if recycling fails.
051: */
052: public static Object getInstance(String className,
053: ClassLoader loader) throws TurbineException {
054: return getService().getInstance(className, loader);
055: }
056:
057: /**
058: * Gets an instance of a named class either from the pool
059: * or by calling the Factory Service if the pool is empty.
060: * Parameters for its constructor are given as an array of objects,
061: * primitive types must be wrapped with a corresponding class.
062: *
063: * @param className the name of the class.
064: * @param loader the class loader.
065: * @param params an array containing the parameters of the constructor.
066: * @param signature an array containing the signature of the constructor.
067: * @return the instance.
068: * @throws TurbineException if recycling fails.
069: */
070: public static Object getInstance(String className, Object[] params,
071: String[] signature) throws TurbineException {
072: return getService().getInstance(className, params, signature);
073: }
074:
075: /**
076: * Gets an instance of a named class either from the pool
077: * or by calling the Factory Service if the pool is empty.
078: * Parameters for its constructor are given as an array of objects,
079: * primitive types must be wrapped with a corresponding class.
080: * The specified class loader will be passed to the Factory Service.
081: *
082: * @param className the name of the class.
083: * @param loader the class loader.
084: * @param params an array containing the parameters of the constructor.
085: * @param signature an array containing the signature of the constructor.
086: * @return the instance.
087: * @throws TurbineException if recycling fails.
088: */
089: public static Object getInstance(String className,
090: ClassLoader loader, Object[] params, String[] signature)
091: throws TurbineException {
092: return getService().getInstance(className, loader, params,
093: signature);
094: }
095:
096: /**
097: * Gets an instance of a specified class either from the pool
098: * or by instatiating from the class if the pool is empty.
099: *
100: * @param clazz the class.
101: * @return the instance.
102: * @throws TurbineException if recycling fails.
103: */
104: public static Object getInstance(Class clazz)
105: throws TurbineException {
106: return getService().getInstance(clazz);
107: }
108:
109: /**
110: * Gets an instance of a specified class either from the pool
111: * or by instatiating from the class if the pool is empty.
112: *
113: * @param clazz the class.
114: * @param params an array containing the parameters of the constructor.
115: * @param signature an array containing the signature of the constructor.
116: * @return the instance.
117: * @throws TurbineException if recycling fails.
118: */
119: public static Object getInstance(Class clazz, Object params[],
120: String signature[]) throws TurbineException {
121: return getService().getInstance(clazz, params, signature);
122: }
123:
124: /**
125: * Puts a used object back to the pool. Objects implementing
126: * the Recyclable interface can provide a recycle method to
127: * be called when they are reused and a dispose method to be
128: * called when they are returned to the pool.
129: *
130: * @param instance the object instance to recycle.
131: * @return true if the instance was accepted.
132: */
133: public static boolean putInstance(Object instance) {
134: return getService().putInstance(instance);
135: }
136:
137: /**
138: * Gets the pool service implementation.
139: *
140: * @return the pool service implementation.
141: */
142: public static PoolService getService() {
143: return (PoolService) TurbineServices.getInstance().getService(
144: PoolService.SERVICE_NAME);
145: }
146: }
|