Package org.jvnet.glassfish.comms.admin.clbadmin.reader.impl

Source Code of org.jvnet.glassfish.comms.admin.clbadmin.reader.impl.InstanceReaderImpl

/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
* Copyright (c) Ericsson AB, 2004-2008. 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.clbadmin.reader.impl;

import com.sun.enterprise.admin.util.JMXConnectorConfig;
import com.sun.enterprise.config.ConfigContext;
import com.sun.enterprise.config.ConfigException;
import com.sun.enterprise.config.serverbeans.Config;
import com.sun.enterprise.config.serverbeans.HttpListener;
import com.sun.enterprise.config.serverbeans.HttpService;
import com.sun.enterprise.config.serverbeans.SipListener;
import com.sun.enterprise.config.serverbeans.SipService;
import com.sun.enterprise.config.serverbeans.PropertyResolver;
import com.sun.enterprise.config.serverbeans.ServerHelper;
import com.sun.enterprise.config.serverbeans.ServerHelper;
import com.sun.enterprise.config.serverbeans.ServerRef;
import com.sun.enterprise.util.i18n.StringManager;

import java.net.InetAddress;
import java.net.UnknownHostException;
import org.jvnet.glassfish.comms.admin.clbadmin.reader.api.InstanceReader;
import org.jvnet.glassfish.comms.admin.clbadmin.reader.api.LbReaderException;
import org.jvnet.glassfish.comms.admin.clbadmin.transform.InstanceVisitor;
import org.jvnet.glassfish.comms.admin.clbadmin.transform.Visitor;

/**
* Provides instance information relavant to Load balancer tier.
*
* @author Vijaya Gadhamsetty
*/
public class InstanceReaderImpl implements InstanceReader {
    private static final StringManager _localStrMgr = StringManager.getManager(InstanceReaderImpl.class);

    // --- PRIVATE VARS -------
    ConfigContext _configCtx = null;
    ServerRef _serverRef = null;
    final static private String HTTP_PROTO = "http://";
    final static private String HTTPS_PROTO = "https://";

    final static private String SIP_PROTO = "sip://";
    final static private String SIPS_PROTO = "sips://";
   
    final static private String SECURE_TRANSPORT = "tls";

    /**
     * Constructor
     */
    public InstanceReaderImpl(ConfigContext ctx, ServerRef sRef) {
        if ((ctx == null) || (sRef == null)) {
            String msg = _localStrMgr.getString("ConfigBeanAndNameNull");
            throw new IllegalArgumentException(msg);
        }

        _configCtx = ctx;
        _serverRef = sRef;
    }

    /**
     * Return server instance's name.
     *
     * @return String           instance' name
     */
    public String getName() throws LbReaderException {
        return _serverRef.getRef();
    }

    /**
     * Returns if the server is enabled in the load balancer or not.
     *
     * @return boolean          true if enabled in LB; false if disabled
     */
    public boolean getLbEnabled() throws LbReaderException {
        return _serverRef.isLbEnabled();
    }

    /**
     * This is used in quicescing. Timeouts after this interval and disables the
     * instance in the load balancer.
     *
     * @return String           Disable time out in minutes
     */
    public String getDisableTimeoutInMinutes() throws LbReaderException {
        return _serverRef.getDisableTimeoutInMinutes();
    }

    /**
     * Enlists both http and https listeners of this server instance
     * It will be form "http:<hostname>:<port> https:<hostname>:<port>"
     *
     * @return String   Listener(s) info.
     */
    public String getListeners() throws LbReaderException {
        String listenerStr = "";

        String sName = _serverRef.getRef();
        Config c = null;

        try {
            c = ServerHelper.getConfigForServer(_configCtx, sName);
        } catch (ConfigException ce) {
            String msg = _localStrMgr.getString("ConfigNotFound", sName);
            throw new LbReaderException(msg, ce);
        }

        // http-listeners
        HttpService httpSvc = c.getHttpService();
        HttpListener[] lstnrs = httpSvc.getHttpListener();

        for (int i = 0; i < lstnrs.length; i++) {
            String hostName = resolveAddress(lstnrs[i].getAddress(), sName, c);
            if (lstnrs[i].isEnabled() && !(lstnrs[i].getType().equals("external"))) {
                if (lstnrs[i].isSecurityEnabled())
                    listenerStr += HTTPS_PROTO;
                else
                    listenerStr += HTTP_PROTO;

                // XXX actual hostname
                listenerStr += (hostName + ":");
                // resolve the port name
                listenerStr += resolvePort(lstnrs[i].getPort());
                // space between listener names
                listenerStr += " ";
            }
        }

        // sip-listeners
        SipService sipSvc = c.getSipService();
        SipListener[] sipListeners = sipSvc.getSipListener();

        for (int i = 0; i < sipListeners.length; i++) {
            String hostName = resolveAddress(sipListeners[i].getAddress(), sName, c);
            if (sipListeners[i].isEnabled() && !(sipListeners[i].getType().equals("external"))) {
                if (sipListeners[i].getTransport().equals(SECURE_TRANSPORT))
                    listenerStr += SIPS_PROTO;
                else
                    listenerStr += SIP_PROTO;

                listenerStr += (hostName + ":");
                listenerStr += resolvePort(sipListeners[i].getPort());
                listenerStr += " ";
            }
        }

        return listenerStr.trim();
    }

    private String resolveAddress(String hostName, String sName, Config c) throws LbReaderException {
        InetAddress address = null;
        try {
            // If it is system variable, resolve it
            if ((hostName != null) && (hostName.length() > 1) &&
                (hostName.charAt(0) == '$') && (hostName.charAt(1) == '{') &&
                (hostName.charAt(hostName.length() - 1) == '}')) {
                String hostVar = hostName.substring(2, hostName.length() - 1);
                // resolve hostName since it is a token from system-property
                PropertyResolver propResolver = null;

                try {
                    propResolver = new PropertyResolver(_configCtx, _serverRef.getRef());
                } catch (ConfigException ce) {
                    // ignore this exception
                }

                if (propResolver != null) {
                    hostName = propResolver.getPropertyValue(hostVar);
                }
            }
            else {
                address = InetAddress.getByName(hostName);
                if (address.isAnyLocalAddress())
                    hostName = getHostNameForServerInstance(_configCtx, sName);
            }
        } catch (UnknownHostException ue) {
            // ignore it
        }
        return hostName;
    }
   
    private String resolvePort(String port) {

        // If it is system variable, resolve it
        if ((port != null) && (port.length() > 1) &&
                (port.charAt(0) == '$') && (port.charAt(1) == '{') &&
                (port.charAt(port.length() - 1) == '}')) {
            String portVar = port.substring(2, port.length() - 1);
            String sVar = null;
            PropertyResolver propResolver = null;

            try {
                propResolver = new PropertyResolver(_configCtx, _serverRef.getRef());
            } catch (ConfigException ce) {
                // ignore this exception
            }

            if (propResolver != null) {
                sVar = propResolver.getPropertyValue(portVar);
            }

            if (sVar != null) {
                return sVar;
            } else {
                return port;
            }
        } else {
            return port;
        }
    }

    // --- VISITOR IMPLEMENTATION ---
    public void accept(Visitor v) {
        InstanceVisitor iv = (InstanceVisitor) v;
        iv.visit(this);
    }

    private String getHostNameForServerInstance(ConfigContext ctx,
        String serverName) throws LbReaderException {
        try {
            JMXConnectorConfig info = ServerHelper.getJMXConnectorInfo(ctx,
                    serverName);
            String host = info.getHost();

            return host;
        } catch (Exception e) {
            String msg = _localStrMgr.getString("GetHostNameFailed");
            throw new LbReaderException(msg, e);
        }
    }

}
TOP

Related Classes of org.jvnet.glassfish.comms.admin.clbadmin.reader.impl.InstanceReaderImpl

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.