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