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

Source Code of org.jvnet.glassfish.comms.admin.clbadmin.ClbAdminEventHelper

/*
* 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;

import com.sun.enterprise.admin.servermgmt.pe.PEFileLayout;
import com.sun.enterprise.config.ConfigContext;
import com.sun.enterprise.config.serverbeans.ConvergedLoadBalancer;
import com.sun.enterprise.util.SystemPropertyConstants;

import org.jvnet.glassfish.comms.clb.admin.Loadbalancer;
import org.jvnet.glassfish.comms.clb.admin.ObjectFactory;
import org.jvnet.glassfish.comms.admin.clbadmin.reader.api.LoadbalancerReader;
import org.jvnet.glassfish.comms.admin.clbadmin.transform.LoadbalancerVisitor;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.math.BigInteger;
import java.util.Properties;

/**
* Helper Class to Load Balancer Admin Event
* @author Srinivas Krishnan
*/
public class ClbAdminEventHelper {
   
    private static final String DIGEST_FILE = "digestfile.properties";
    private ClbConfigPublisher _clbp = null;
    private String _name = null;
    private String _lbName = null;
    private String _configName = null;
    private ConfigContext _ctx = null;
   
    /**
     * Default constructor.
     */
    public ClbAdminEventHelper(ConfigContext ctx, ClbConfigPublisher clbp, ConvergedLoadBalancer clb, String configName) {
        _clbp = clbp;
        _name = clb.getConvergedLbConfigName();
        _lbName = clb.getName();
        _ctx = ctx;
        _configName = configName;
    }

    public boolean isApplyChangesRequired() {
        boolean applyChanges = false;
        String iRoot = System.getProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY);
        String digestFilePath = iRoot + File.separator + PEFileLayout.CONFIG_DIR + File.separator + _configName + File.separator + DIGEST_FILE;
        String key = _name + "." + _lbName;
        String str = getClbXML();
        String lastDigestValue = getLastDigestValue(digestFilePath, key);
        String currentDigestValue = getMessageDigest(str);

        if (!currentDigestValue.equals(lastDigestValue)) {
            // write the latest digest value into the digest file
            storeLatestDigestValue(digestFilePath, currentDigestValue, key);
            applyChanges = true;
        }
        return applyChanges;
    }
   
    public String getClbXML() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            LoadbalancerReader lbr = _clbp.getLbReader(_ctx, _name);
            // tranform the data using visitor pattern
            Loadbalancer _lb = new Loadbalancer();
            LoadbalancerVisitor lbVstr = new LoadbalancerVisitor(_lb);
            lbr.accept(lbVstr);
            ObjectFactory clbFactory = new ObjectFactory();
            clbFactory.store(_lb, baos);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return baos.toString();
       
    }
   
    public String getMessageDigest(String lbxml) {
        String hash = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(lbxml.getBytes());
            hash = new BigInteger(md.digest()).toString(16);
        }catch(NoSuchAlgorithmException e){
            System.out.println(e.getMessage());
        }
        return hash;
    }
   
    public String getLastDigestValue(String sPath, String keyName) {
        Properties prop = new Properties();
        String lastDigestValue = null;
        try {
            File digestFile = new File(sPath);
            FileInputStream digestInputStream = new FileInputStream(digestFile);
            if (digestInputStream != null) {
                prop.load(digestInputStream);
                lastDigestValue = prop.getProperty(keyName);
            }
        } catch (IOException ioe) {
            lastDigestValue = "";
        }
        return lastDigestValue;
    }
   
    public static void storeLatestDigestValue(String sPath, String digestValue, String keyName) {
        Properties prop = new Properties();
        prop.setProperty(keyName, digestValue);
        boolean created = false;
        File digestFile = new File(sPath);
        try {
            if (!digestFile.exists())
                created = digestFile.createNewFile();
            if (digestFile.exists() || created) {
                FileOutputStream digestOutputStream = new FileOutputStream(digestFile);
                if (digestOutputStream != null)
                    prop.store(digestOutputStream, "");
            }
        }catch (IOException ioe) {
            System.out.println(ioe.getMessage());
        }
    }
   
    public static String getConfigFileNewValue(String fileName) {
        if (fileName == null)
            return "";
        int versionNumPos = fileName.lastIndexOf(".v");
        String versionNum = "0";
        if (versionNumPos != -1) {
            versionNum = fileName.substring(versionNumPos+2);
            try {
                int ver = Integer.parseInt(versionNum);
                ver++;
                versionNum = String.valueOf(ver);
            } catch (Exception e) {
                versionNum = "0";
            }
            fileName = fileName.substring(0,versionNumPos);
        }
        versionNum = ".v" + versionNum;
        String newFileName = fileName + versionNum;
        return newFileName;
    }
   
    public static boolean isClbDebug() {
        return Boolean.parseBoolean(System.getProperty("clbreconfig.debug"));
    }
}
TOP

Related Classes of org.jvnet.glassfish.comms.admin.clbadmin.ClbAdminEventHelper

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.