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