001    /**
002     * Copyright (c) 2000-2013 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.Map;
021    import java.util.concurrent.ConcurrentHashMap;
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._reset();
046            }
047    
048            private InstancePool() {
049                    _instances = new ConcurrentHashMap<String, Object>();
050            }
051    
052            private boolean _contains(String className) {
053                    className = className.trim();
054    
055                    return _instances.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 instance = _instances.get(className);
066    
067                    if (instance != null) {
068                            return instance;
069                    }
070    
071                    ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();
072    
073                    try {
074                            Class<?> clazz = portalClassLoader.loadClass(className);
075    
076                            instance = clazz.newInstance();
077    
078                            _instances.put(className, instance);
079                    }
080                    catch (Exception e1) {
081                            if (logErrors && _log.isWarnEnabled()) {
082                                    _log.warn(
083                                            "Unable to load " + className +
084                                                    " with the portal class loader",
085                                            e1);
086                            }
087    
088                            Thread currentThread = Thread.currentThread();
089    
090                            ClassLoader contextClassLoader =
091                                    currentThread.getContextClassLoader();
092    
093                            try {
094                                    Class<?> clazz = contextClassLoader.loadClass(className);
095    
096                                    instance = clazz.newInstance();
097    
098                                    _instances.put(className, instance);
099                            }
100                            catch (Exception e2) {
101                                    if (logErrors) {
102                                            _log.error(
103                                                    "Unable to load " + className +
104                                                            " with the portal class loader or the " +
105                                                                    "current context class loader",
106                                                    e2);
107                                    }
108                            }
109                    }
110    
111                    return instance;
112            }
113    
114            private void _put(String className, Object obj) {
115                    className = className.trim();
116    
117                    _instances.put(className, obj);
118            }
119    
120            private void _reset() {
121                    _instances.clear();
122            }
123    
124            private static Log _log = LogFactoryUtil.getLog(InstancePool.class);
125    
126            private static InstancePool _instance = new InstancePool();
127    
128            private final Map<String, Object> _instances;
129    
130    }