1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  /**
32   * <a href="InstancePool.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class InstancePool {
37  
38      public static boolean contains(String className) {
39          return _instance._contains(className);
40      }
41  
42      public static Object get(String className) {
43          return _instance._get(className);
44      }
45  
46      public static void put(String className, Object obj) {
47          _instance._put(className, obj);
48      }
49  
50      private InstancePool() {
51          _classPool = new HashMap<String, Object>();
52      }
53  
54      private boolean _contains(String className) {
55          className = className.trim();
56  
57          return _classPool.containsKey(className);
58      }
59  
60      private Object _get(String className) {
61          className = className.trim();
62  
63          Object obj = _classPool.get(className);
64  
65          if (obj == null) {
66              ClassLoader portalClassLoader =
67                  PortalClassLoaderUtil.getClassLoader();
68  
69              try {
70                  Class<?> classObj = portalClassLoader.loadClass(className);
71  
72                  obj = classObj.newInstance();
73  
74                  _put(className, obj);
75              }
76              catch (Exception e1) {
77                  if (_log.isWarnEnabled()) {
78                      _log.warn(
79                          "Unable to load " + className +
80                              " with the portal class loader",
81                          e1);
82                  }
83  
84                  Thread currentThread = Thread.currentThread();
85  
86                  ClassLoader contextClassLoader =
87                      currentThread.getContextClassLoader();
88  
89                  try {
90                      Class<?> classObj = contextClassLoader.loadClass(className);
91  
92                      obj = classObj.newInstance();
93  
94                      _put(className, obj);
95                  }
96                  catch (Exception e2) {
97                      _log.error(
98                          "Unable to load " + className +
99                              " with the portal class loader or the current " +
100                                 "context class loader",
101                         e2);
102                 }
103             }
104         }
105 
106         return obj;
107     }
108 
109     private void _put(String className, Object obj) {
110         className = className.trim();
111 
112         _classPool.put(className, obj);
113     }
114 
115     private static Log _log = LogFactoryUtil.getLog(InstancePool.class);
116 
117     private static InstancePool _instance = new InstancePool();
118 
119     private Map<String, Object> _classPool;
120 
121 }