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.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    }