001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.resiliency.spi.agent;
016    
017    import com.liferay.portal.kernel.nio.intraband.RegistrationReference;
018    import com.liferay.portal.kernel.resiliency.spi.SPIConfiguration;
019    
020    import java.lang.reflect.Constructor;
021    
022    import java.util.Map;
023    import java.util.Set;
024    import java.util.concurrent.ConcurrentHashMap;
025    
026    /**
027     * @author Shuyang Zhou
028     */
029    public class SPIAgentFactoryUtil {
030    
031            public static SPIAgent createSPIAgent(
032                    SPIConfiguration spiConfiguration,
033                    RegistrationReference registrationReference) {
034    
035                    String spiAgentClassName = spiConfiguration.getSPIAgentClassName();
036    
037                    if (spiAgentClassName == null) {
038                            throw new NullPointerException("Missing SPI agent class name");
039                    }
040    
041                    Class<? extends SPIAgent> spiAgentClass = _spiAgentClasses.get(
042                            spiAgentClassName);
043    
044                    if (spiAgentClass == null) {
045                            throw new IllegalArgumentException(
046                                    "Unkown SPI agent class name " + spiAgentClassName);
047                    }
048    
049                    try {
050                            Constructor<? extends SPIAgent> constructor =
051                                    spiAgentClass.getConstructor(
052                                            SPIConfiguration.class, RegistrationReference.class);
053    
054                            return constructor.newInstance(
055                                    spiConfiguration, registrationReference);
056                    }
057                    catch (Exception e) {
058                            throw new RuntimeException(
059                                    "Unable to instantiate " + spiAgentClass, e);
060                    }
061            }
062    
063            public static Set<String> getSPIAgentClassNames() {
064                    return _spiAgentClasses.keySet();
065            }
066    
067            public static Class<? extends SPIAgent> registerSPIAgentClass(
068                    Class<? extends SPIAgent> spiAgentClass) {
069    
070                    return _spiAgentClasses.put(spiAgentClass.getName(), spiAgentClass);
071            }
072    
073            public static Class<? extends SPIAgent> unregisterSPIAgentClass(
074                    String spiAgentClassName) {
075    
076                    return _spiAgentClasses.remove(spiAgentClassName);
077            }
078    
079            public void setSPIAgentClasses(Set<String> spiAgentClassNames)
080                    throws ClassNotFoundException {
081    
082                    Thread currentThread = Thread.currentThread();
083    
084                    ClassLoader classLoader = currentThread.getContextClassLoader();
085    
086                    for (String spiAgentClassName : spiAgentClassNames) {
087                            Class<? extends SPIAgent> agentClass =
088                                    (Class<? extends SPIAgent>)classLoader.loadClass(
089                                            spiAgentClassName);
090    
091                            _spiAgentClasses.put(spiAgentClassName, agentClass);
092                    }
093            }
094    
095            private static final Map<String, Class<? extends SPIAgent>>
096                    _spiAgentClasses =
097                            new ConcurrentHashMap<String, Class<? extends SPIAgent>>();
098    
099    }