View Javadoc

1   package org.apache.turbine.services.pool;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.turbine.services.TurbineServices;
23  import org.apache.turbine.util.TurbineException;
24  
25  /***
26   * This is a static accessor to common pooling tasks.
27   *
28   * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
29   * @version $Id: TurbinePool.java 534527 2007-05-02 16:10:59Z tv $
30   */
31  public abstract class TurbinePool
32  {
33      /***
34       * Gets an instance of a named class either from the pool
35       * or by calling the Factory Service if the pool is empty.
36       *
37       * @param className the name of the class.
38       * @return the instance.
39       * @throws TurbineException if recycling fails.
40       */
41      public static Object getInstance(String className)
42              throws TurbineException
43      {
44          return getService().getInstance(className);
45      }
46  
47      /***
48       * Gets an instance of a named class either from the pool
49       * or by calling the Factory Service if the pool is empty.
50       * The specified class loader will be passed to the Factory Service.
51       *
52       * @param className the name of the class.
53       * @param loader the class loader.
54       * @return the instance.
55       * @throws TurbineException if recycling fails.
56       */
57      public static Object getInstance(String className,
58                                       ClassLoader loader)
59              throws TurbineException
60      {
61          return getService().getInstance(className, loader);
62      }
63  
64      /***
65       * Gets an instance of a named class either from the pool
66       * or by calling the Factory Service if the pool is empty.
67       * Parameters for its constructor are given as an array of objects,
68       * primitive types must be wrapped with a corresponding class.
69       *
70       * @param className the name of the class.
71       * @param loader the class loader.
72       * @param params an array containing the parameters of the constructor.
73       * @param signature an array containing the signature of the constructor.
74       * @return the instance.
75       * @throws TurbineException if recycling fails.
76       */
77      public static Object getInstance(String className,
78                                       Object[] params,
79                                       String[] signature)
80              throws TurbineException
81      {
82          return getService().getInstance(className, params, signature);
83      }
84  
85      /***
86       * Gets an instance of a named class either from the pool
87       * or by calling the Factory Service if the pool is empty.
88       * Parameters for its constructor are given as an array of objects,
89       * primitive types must be wrapped with a corresponding class.
90       * The specified class loader will be passed to the Factory Service.
91       *
92       * @param className the name of the class.
93       * @param loader the class loader.
94       * @param params an array containing the parameters of the constructor.
95       * @param signature an array containing the signature of the constructor.
96       * @return the instance.
97       * @throws TurbineException if recycling fails.
98       */
99      public static Object getInstance(String className,
100                                      ClassLoader loader,
101                                      Object[] params,
102                                      String[] signature)
103             throws TurbineException
104     {
105         return getService().getInstance(className, loader, params, signature);
106     }
107 
108     /***
109      * Gets an instance of a specified class either from the pool
110      * or by instatiating from the class if the pool is empty.
111      *
112      * @param clazz the class.
113      * @return the instance.
114      * @throws TurbineException if recycling fails.
115      */
116     public static Object getInstance(Class clazz)
117             throws TurbineException
118     {
119         return getService().getInstance(clazz);
120     }
121 
122     /***
123      * Gets an instance of a specified class either from the pool
124      * or by instatiating from the class if the pool is empty.
125      *
126      * @param clazz the class.
127      * @param params an array containing the parameters of the constructor.
128      * @param signature an array containing the signature of the constructor.
129      * @return the instance.
130      * @throws TurbineException if recycling fails.
131      */
132     public static Object getInstance(Class clazz,
133                                      Object params[],
134                                      String signature[])
135             throws TurbineException
136     {
137         return getService().getInstance(clazz, params, signature);
138     }
139 
140     /***
141      * Puts a used object back to the pool. Objects implementing
142      * the Recyclable interface can provide a recycle method to
143      * be called when they are reused and a dispose method to be
144      * called when they are returned to the pool.
145      *
146      * @param instance the object instance to recycle.
147      * @return true if the instance was accepted.
148      */
149     public static boolean putInstance(Object instance)
150     {
151         return getService().putInstance(instance);
152     }
153 
154     /***
155      * Gets the pool service implementation.
156      *
157      * @return the pool service implementation.
158      */
159     public static PoolService getService()
160     {
161         return (PoolService) TurbineServices.
162                 getInstance().getService(PoolService.SERVICE_NAME);
163     }
164 }