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