1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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      private InstancePool() {
47          _classPool = new HashMap<String, Object>();
48      }
49  
50      private boolean _contains(String className) {
51          className = className.trim();
52  
53          return _classPool.containsKey(className);
54      }
55  
56      private Object _get(String className) {
57          return _get(className, true);
58      }
59  
60      private Object _get(String className, boolean logErrors) {
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 (logErrors && _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                      if (logErrors) {
98                          _log.error(
99                              "Unable to load " + className +
100                                 " with the portal class loader or the " +
101                                     "current context class loader",
102                             e2);
103                     }
104                 }
105             }
106         }
107 
108         return obj;
109     }
110 
111     private void _put(String className, Object obj) {
112         className = className.trim();
113 
114         _classPool.put(className, obj);
115     }
116 
117     private static Log _log = LogFactoryUtil.getLog(InstancePool.class);
118 
119     private static InstancePool _instance = new InstancePool();
120 
121     private Map<String, Object> _classPool;
122 
123 }