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 Object get(String className, boolean logErrors) {
47          return _instance._get(className, logErrors);
48      }
49  
50      public static void put(String className, Object obj) {
51          _instance._put(className, obj);
52      }
53  
54      private InstancePool() {
55          _classPool = new HashMap<String, Object>();
56      }
57  
58      private boolean _contains(String className) {
59          className = className.trim();
60  
61          return _classPool.containsKey(className);
62      }
63  
64      private Object _get(String className) {
65          return _get(className, true);
66      }
67  
68      private Object _get(String className, boolean logErrors) {
69          className = className.trim();
70  
71          Object obj = _classPool.get(className);
72  
73          if (obj == null) {
74              ClassLoader portalClassLoader =
75                  PortalClassLoaderUtil.getClassLoader();
76  
77              try {
78                  Class<?> classObj = portalClassLoader.loadClass(className);
79  
80                  obj = classObj.newInstance();
81  
82                  _put(className, obj);
83              }
84              catch (Exception e1) {
85                  if (logErrors && _log.isWarnEnabled()) {
86                      _log.warn(
87                          "Unable to load " + className +
88                              " with the portal class loader",
89                          e1);
90                  }
91  
92                  Thread currentThread = Thread.currentThread();
93  
94                  ClassLoader contextClassLoader =
95                      currentThread.getContextClassLoader();
96  
97                  try {
98                      Class<?> classObj = contextClassLoader.loadClass(className);
99  
100                     obj = classObj.newInstance();
101 
102                     _put(className, obj);
103                 }
104                 catch (Exception e2) {
105                     if (logErrors) {
106                         _log.error(
107                             "Unable to load " + className +
108                                 " with the portal class loader or the " +
109                                     "current context class loader",
110                             e2);
111                     }
112                 }
113             }
114         }
115 
116         return obj;
117     }
118 
119     private void _put(String className, Object obj) {
120         className = className.trim();
121 
122         _classPool.put(className, obj);
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 }