| InstancePool.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 * The contents of this file are subject to the terms of the Liferay Enterprise
5 * Subscription License ("License"). You may not use this file except in
6 * compliance with the License. You can obtain a copy of the License by
7 * contacting Liferay, Inc. See the License for the specific language governing
8 * permissions and limitations under the License, including but not limited to
9 * distribution rights of the Software.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20 package com.liferay.portal.kernel.util;
21
22 import com.liferay.portal.kernel.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24
25 import java.util.HashMap;
26 import java.util.Map;
27
28 /**
29 * <a href="InstancePool.java.html"><b><i>View Source</i></b></a>
30 *
31 * @author Brian Wing Shun Chan
32 *
33 */
34 public class InstancePool {
35
36 public static boolean contains(String className) {
37 return _instance._contains(className);
38 }
39
40 public static Object get(String className) {
41 return _instance._get(className);
42 }
43
44 public static void put(String className, Object obj) {
45 _instance._put(className, obj);
46 }
47
48 private InstancePool() {
49 _classPool = new HashMap<String, Object>();
50 }
51
52 private boolean _contains(String className) {
53 className = className.trim();
54
55 return _classPool.containsKey(className);
56 }
57
58 private Object _get(String className) {
59 className = className.trim();
60
61 Object obj = _classPool.get(className);
62
63 if (obj == null) {
64 ClassLoader portalClassLoader =
65 PortalClassLoaderUtil.getClassLoader();
66
67 try {
68 Class<?> classObj = portalClassLoader.loadClass(className);
69
70 obj = classObj.newInstance();
71
72 _put(className, obj);
73 }
74 catch (Exception e1) {
75 if (_log.isWarnEnabled()) {
76 _log.warn(
77 "Unable to load " + className +
78 " with the portal class loader",
79 e1);
80 }
81
82 Thread currentThread = Thread.currentThread();
83
84 ClassLoader contextClassLoader =
85 currentThread.getContextClassLoader();
86
87 try {
88 Class<?> classObj = contextClassLoader.loadClass(className);
89
90 obj = classObj.newInstance();
91
92 _put(className, obj);
93 }
94 catch (Exception e2) {
95 _log.error(
96 "Unable to load " + className +
97 " with the portal class loader or the current " +
98 "context class loader",
99 e2);
100 }
101 }
102 }
103
104 return obj;
105 }
106
107 private void _put(String className, Object obj) {
108 className = className.trim();
109
110 _classPool.put(className, obj);
111 }
112
113 private static Log _log = LogFactoryUtil.getLog(InstancePool.class);
114
115 private static InstancePool _instance = new InstancePool();
116
117 private Map<String, Object> _classPool;
118
119 }