001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.util.HashMap;
021    import java.util.Map;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class InstancePool {
027    
028            public static boolean contains(String className) {
029                    return _instance._contains(className);
030            }
031    
032            public static Object get(String className) {
033                    return _instance._get(className);
034            }
035    
036            public static Object get(String className, boolean logErrors) {
037                    return _instance._get(className, logErrors);
038            }
039    
040            public static void put(String className, Object obj) {
041                    _instance._put(className, obj);
042            }
043    
044            public static void reset() {
045                    _instance = new InstancePool();
046            }
047    
048            private InstancePool() {
049                    _classPool = new HashMap<String, Object>();
050            }
051    
052            private boolean _contains(String className) {
053                    className = className.trim();
054    
055                    return _classPool.containsKey(className);
056            }
057    
058            private Object _get(String className) {
059                    return _get(className, true);
060            }
061    
062            private Object _get(String className, boolean logErrors) {
063                    className = className.trim();
064    
065                    Object obj = _classPool.get(className);
066    
067                    if (obj == null) {
068                            ClassLoader portalClassLoader =
069                                    PortalClassLoaderUtil.getClassLoader();
070    
071                            try {
072                                    Class<?> classObj = portalClassLoader.loadClass(className);
073    
074                                    obj = classObj.newInstance();
075    
076                                    _put(className, obj);
077                            }
078                            catch (Exception e1) {
079                                    if (logErrors && _log.isWarnEnabled()) {
080                                            _log.warn(
081                                                    "Unable to load " + className +
082                                                            " with the portal class loader",
083                                                    e1);
084                                    }
085    
086                                    Thread currentThread = Thread.currentThread();
087    
088                                    ClassLoader contextClassLoader =
089                                            currentThread.getContextClassLoader();
090    
091                                    try {
092                                            Class<?> classObj = contextClassLoader.loadClass(className);
093    
094                                            obj = classObj.newInstance();
095    
096                                            _put(className, obj);
097                                    }
098                                    catch (Exception e2) {
099                                            if (logErrors) {
100                                                    _log.error(
101                                                            "Unable to load " + className +
102                                                                    " with the portal class loader or the " +
103                                                                            "current context class loader",
104                                                            e2);
105                                            }
106                                    }
107                            }
108                    }
109    
110                    return obj;
111            }
112    
113            private void _put(String className, Object obj) {
114                    className = className.trim();
115    
116                    _classPool.put(className, obj);
117            }
118    
119            private static Log _log = LogFactoryUtil.getLog(InstancePool.class);
120    
121            private static InstancePool _instance = new InstancePool();
122    
123            private Map<String, Object> _classPool;
124    
125    }