001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.util;
022:
023: import com.liferay.portal.kernel.log.Log;
024: import com.liferay.portal.kernel.log.LogFactoryUtil;
025:
026: import java.util.HashMap;
027: import java.util.Map;
028:
029: /**
030: * <a href="InstancePool.java.html"><b><i>View Source</i></b></a>
031: *
032: * @author Brian Wing Shun Chan
033: *
034: */
035: public class InstancePool {
036:
037: public static Object get(String className) {
038: return _instance._get(className);
039: }
040:
041: public static void put(String className, Object obj) {
042: _instance._put(className, obj);
043: }
044:
045: private InstancePool() {
046: _classPool = new HashMap();
047: }
048:
049: private Object _get(String className) {
050: className = className.trim();
051:
052: Object obj = _classPool.get(className);
053:
054: if (obj == null) {
055: ClassLoader portalClassLoader = PortalClassLoaderUtil
056: .getClassLoader();
057:
058: try {
059: Class classObj = portalClassLoader.loadClass(className);
060:
061: obj = classObj.newInstance();
062:
063: _put(className, obj);
064: } catch (Exception e1) {
065: if (_log.isWarnEnabled()) {
066: _log.warn("Unable to load " + className
067: + " with the portal class loader", e1);
068: }
069:
070: ClassLoader contextClassLoader = Thread.currentThread()
071: .getContextClassLoader();
072:
073: try {
074: Class classObj = contextClassLoader
075: .loadClass(className);
076:
077: obj = classObj.newInstance();
078:
079: _put(className, obj);
080: } catch (Exception e2) {
081: _log
082: .error(
083: "Unable to load "
084: + className
085: + " with the portal class loader or the current "
086: + "context class loader",
087: e2);
088: }
089: }
090: }
091:
092: return obj;
093: }
094:
095: private void _put(String className, Object obj) {
096: _classPool.put(className, obj);
097: }
098:
099: private static Log _log = LogFactoryUtil.getLog(InstancePool.class);
100:
101: private static InstancePool _instance = new InstancePool();
102:
103: private Map _classPool;
104:
105: }
|