1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  /**
24   * <a href="InstancePool.java.html"><b><i>View Source</i></b></a>
25   *
26   * @author Brian Wing Shun Chan
27   */
28  public class InstancePool {
29  
30      public static boolean contains(String className) {
31          return _instance._contains(className);
32      }
33  
34      public static Object get(String className) {
35          return _instance._get(className);
36      }
37  
38      public static Object get(String className, boolean logErrors) {
39          return _instance._get(className, logErrors);
40      }
41  
42      public static void put(String className, Object obj) {
43          _instance._put(className, obj);
44      }
45  
46      public static void reset() {
47          _instance._reset();
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          return _get(className, true);
62      }
63  
64      private Object _get(String className, boolean logErrors) {
65          className = className.trim();
66  
67          Object obj = _classPool.get(className);
68  
69          if (obj == null) {
70              ClassLoader portalClassLoader =
71                  PortalClassLoaderUtil.getClassLoader();
72  
73              try {
74                  Class<?> classObj = portalClassLoader.loadClass(className);
75  
76                  obj = classObj.newInstance();
77  
78                  _put(className, obj);
79              }
80              catch (Exception e1) {
81                  if (logErrors && _log.isWarnEnabled()) {
82                      _log.warn(
83                          "Unable to load " + className +
84                              " with the portal class loader",
85                          e1);
86                  }
87  
88                  Thread currentThread = Thread.currentThread();
89  
90                  ClassLoader contextClassLoader =
91                      currentThread.getContextClassLoader();
92  
93                  try {
94                      Class<?> classObj = contextClassLoader.loadClass(className);
95  
96                      obj = classObj.newInstance();
97  
98                      _put(className, obj);
99                  }
100                 catch (Exception e2) {
101                     if (logErrors) {
102                         _log.error(
103                             "Unable to load " + className +
104                                 " with the portal class loader or the " +
105                                     "current context class loader",
106                             e2);
107                     }
108                 }
109             }
110         }
111 
112         return obj;
113     }
114 
115     private void _put(String className, Object obj) {
116         className = className.trim();
117 
118         _classPool.put(className, obj);
119     }
120 
121     private void _reset() {
122         _classPool.clear();
123     }
124 
125     private static Log _log = LogFactoryUtil.getLog(InstancePool.class);
126 
127     private static InstancePool _instance = new InstancePool();
128 
129     private Map<String, Object> _classPool;
130 
131 }