1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
37  public class InstancePool {
38  
39      public static boolean contains(String className) {
40          return _instance._contains(className);
41      }
42  
43      public static Object get(String className) {
44          return _instance._get(className);
45      }
46  
47      public static Object get(String className, boolean logErrors) {
48          return _instance._get(className, logErrors);
49      }
50  
51      public static void put(String className, Object obj) {
52          _instance._put(className, obj);
53      }
54  
55      private InstancePool() {
56          _classPool = new HashMap<String, Object>();
57      }
58  
59      private boolean _contains(String className) {
60          className = className.trim();
61  
62          return _classPool.containsKey(className);
63      }
64  
65      private Object _get(String className) {
66          return _get(className, true);
67      }
68  
69      private Object _get(String className, boolean logErrors) {
70          className = className.trim();
71  
72          Object obj = _classPool.get(className);
73  
74          if (obj == null) {
75              ClassLoader portalClassLoader =
76                  PortalClassLoaderUtil.getClassLoader();
77  
78              try {
79                  Class<?> classObj = portalClassLoader.loadClass(className);
80  
81                  obj = classObj.newInstance();
82  
83                  _put(className, obj);
84              }
85              catch (Exception e1) {
86                  if (logErrors && _log.isWarnEnabled()) {
87                      _log.warn(
88                          "Unable to load " + className +
89                              " with the portal class loader",
90                          e1);
91                  }
92  
93                  Thread currentThread = Thread.currentThread();
94  
95                  ClassLoader contextClassLoader =
96                      currentThread.getContextClassLoader();
97  
98                  try {
99                      Class<?> classObj = contextClassLoader.loadClass(className);
100 
101                     obj = classObj.newInstance();
102 
103                     _put(className, obj);
104                 }
105                 catch (Exception e2) {
106                     if (logErrors) {
107                         _log.error(
108                             "Unable to load " + className +
109                                 " with the portal class loader or the " +
110                                     "current context class loader",
111                             e2);
112                     }
113                 }
114             }
115         }
116 
117         return obj;
118     }
119 
120     private void _put(String className, Object obj) {
121         className = className.trim();
122 
123         _classPool.put(className, obj);
124     }
125 
126     private static Log _log = LogFactoryUtil.getLog(InstancePool.class);
127 
128     private static InstancePool _instance = new InstancePool();
129 
130     private Map<String, Object> _classPool;
131 
132 }