Package org.jvnet.glassfish.comms.admin.reporter

Source Code of org.jvnet.glassfish.comms.admin.reporter.SMIAdapterHandler$PropertyConfigListener

/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
* Copyright (c) Ericsson AB, 2004-2009. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License").  You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code.  If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license."  If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above.  However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.jvnet.glassfish.comms.admin.reporter;

import java.io.File;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jvnet.glassfish.comms.util.LogUtil;

import com.ericsson.ssa.config.ConfigFactory;
import com.ericsson.ssa.config.SimpleConfig;
import com.ericsson.ssa.config.event.ConfigAddEvent;
import com.ericsson.ssa.config.event.ConfigChangeListener;
import com.ericsson.ssa.config.event.ConfigRemoveEvent;
import com.ericsson.ssa.config.event.ConfigUpdateEvent;
import com.sun.enterprise.config.ConfigException;
import com.sun.enterprise.config.serverbeans.Config;
import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
import com.sun.enterprise.server.ApplicationServer;
import com.sun.enterprise.server.ServerContext;

public class SMIAdapterHandler {
    private static Logger logger = LogUtil.SMI_LOGGER.getLogger();;
    private ConfigChangeListener listener;
    private static final String CFG_SIP_SERVICE = "/SipService";
    private static final String SIP_SERVICE_CFG_REGEXP = CFG_SIP_SERVICE + "/ElementProperty";
    private static SMIAdapterHandler mySingleton = new SMIAdapterHandler();

    private static final String SMI_SD_CFG_KEY = "smiServletAdapter";
    private static final String SMI_LAYER_CFG_KEY = "smiLayerAdapter";
    private static final String SMI_NM_CFG_KEY = "smiNetworkManagerAdapter";

    private SMILogMessageAdapter smiServletAdapter = null;
    private SMILogMessageAdapter smiLayerAdapter = null;
    private SMILogMessageAdapter smiNetworkManagerAdapter = null;

    public static SMIAdapterHandler getInstance() {
        return mySingleton;
    }

    public SMIAdapterHandler() {
        init();
    }

    public SMILogMessageAdapter getServletAdapter() {
        return smiServletAdapter;
    }

    public SMILogMessageAdapter getLayerAdapter() {
        return smiLayerAdapter;
    }

    public SMILogMessageAdapter getNetworkManagerAdapter() {
        return smiNetworkManagerAdapter;
    }

    private void init() {
        try {
            // Read the property elements below the sip-service
            ServerContext sc = ApplicationServer.getServerContext();
            Config cfg = ServerBeansFactory.getConfigBean(sc.getConfigContext());

            String servletAdapterClassName = ConfigFactory.getConfig().get(CFG_SIP_SERVICE, SMI_SD_CFG_KEY);
            smiServletAdapter = setAdapterClass(servletAdapterClassName, smiServletAdapter);

            String layerAdapterClassName = ConfigFactory.getConfig().get(CFG_SIP_SERVICE, SMI_NM_CFG_KEY);
            smiLayerAdapter = setAdapterClass(layerAdapterClassName, smiLayerAdapter);

            String nmAdapterClassName = ConfigFactory.getConfig().get(CFG_SIP_SERVICE, SMI_NM_CFG_KEY);
            smiNetworkManagerAdapter = setAdapterClass(nmAdapterClassName, smiNetworkManagerAdapter);

            // Add listener to configuration changes in the sip-listener element
            listener = new PropertyConfigListener();
            ConfigFactory.instance().registerConfigChangeListener(CFG_SIP_SERVICE, listener);

        } catch (ConfigException e) {
            if (logger.isLoggable(Level.FINE)){
                logger.log(Level.FINE, "Failed to SipMessageReporter adapter property: {1}", e);
            }
        }
    }

    private SMILogMessageAdapter setAdapterClass(String value, SMILogMessageAdapter currentAdapter) {
        if (logger.isLoggable(Level.FINEST)){
            logger.log(Level.FINEST, "changed config value:" + value);
        }

        if (value == null || "null".equalsIgnoreCase(value)) {
            return null;
        }

        if (currentAdapter != null && currentAdapter.getClass().getName().equals(value)) {
            // same adapter value as currently used, ignore for now, maybe add logic to check version/timestamp....
            // avoid repeated classLoading when value is same as previously
            return currentAdapter;
        } else {
            SMILogMessageAdapter adapter = loadAdapterClass(value);
            if (adapter != null) {
                if (logger.isLoggable(Level.FINE)){
                    logger.log(Level.FINE, "Successfully changed adapter to:" + value);
                }
            }
            return adapter;
        }
    }

    private void scanConfigEvent(SimpleConfig cfg) {
        Map<String, String> properties = cfg.getAll(SIP_SERVICE_CFG_REGEXP);

        for (Map.Entry<String, String> entry : properties.entrySet()) {
            if (entry.getKey().equals(SMI_SD_CFG_KEY)) {
                smiServletAdapter = setAdapterClass(entry.getValue(), smiServletAdapter);
            } else if (entry.getKey().equals(SMI_LAYER_CFG_KEY)) {
                smiLayerAdapter = setAdapterClass(entry.getValue(), smiLayerAdapter);
            } else if (entry.getKey().equals(SMI_NM_CFG_KEY)) {
                smiNetworkManagerAdapter = setAdapterClass(entry.getValue(), smiNetworkManagerAdapter);
            }
        }
    }

    private SMILogMessageAdapter loadAdapterClass(String arg) {
        try {
            int sepIndex = arg.indexOf(';');

            String packageAndClass = null;
            ClassLoader classLoader = null;

            if (sepIndex == -1) {
                packageAndClass = arg;
                classLoader = SMIAdapterHandler.class.getClassLoader();

            } else {
                packageAndClass = arg.substring(0, sepIndex);
                String classRoot = arg.substring(sepIndex + 1);
                if (logger.isLoggable(Level.FINE)){
                    logger.log(Level.FINE, "package:" + packageAndClass + " root: " + classRoot);
                }
                File f = new File(classRoot);
                if (f.exists() == false) {
                    if (logger.isLoggable(Level.FINE)){
                        logger.log(Level.FINE, "Cannot find adapter class path root: " + classRoot);
                    }
                    return null;
                }

                URL[] urls = { f.toURL() };
                classLoader = URLClassLoader.newInstance(urls, SMIAdapterHandler.class.getClassLoader());
            }

            Class<?> handlerClass = Class.forName(packageAndClass, true, classLoader);
            Constructor<?> constructor = handlerClass.getConstructor((Class<?>[]) null);
            Object o = constructor.newInstance((Object[]) null);

            if (!(o instanceof SMILogMessageAdapter)) {
                if (logger.isLoggable(Level.FINE)){
                    logger.log(Level.FINE, "specified class does not implement SmiLogMessageAdapter interface :" + arg);
                }
                return null;
            }
            return (SMILogMessageAdapter) o;

        } catch (Exception e) {
            if (logger.isLoggable(Level.FINE)){
                logger.log(Level.FINE, "failed to load Class:" + arg, e);
            }
        }
        return null;
    }

    private final class PropertyConfigListener implements ConfigChangeListener {
        public void handleConfigEvent(ConfigAddEvent event) {
            SimpleConfig cfg = (SimpleConfig) event.getSource();

            scanConfigEvent(cfg);
        }

        public void handleConfigEvent(ConfigUpdateEvent event) {
            SimpleConfig cfg = (SimpleConfig) event.getSource();

            scanConfigEvent(cfg);
        }

        public void handleConfigEvent(ConfigRemoveEvent event) {
            SimpleConfig cfg = (SimpleConfig) event.getSource();

            scanConfigEvent(cfg);
        }
    }

}
TOP

Related Classes of org.jvnet.glassfish.comms.admin.reporter.SMIAdapterHandler$PropertyConfigListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.