Search in sources :

Example 1 with Loadbalancer

use of org.glassfish.loadbalancer.admin.cli.beans.Loadbalancer in project Payara by payara.

the class LbConfigHelper method exportOtdProperties.

/**
 * exports the otd.properties from the config to the outputstream provided
 * @param ctx ConfigContext
 * @param lbConfigName name of lb-config
 */
public static void exportOtdProperties(LoadbalancerReader lbRdr, OutputStream out) throws Exception {
    // tranform the data using visitor pattern
    Loadbalancer _lb = new Loadbalancer();
    Properties props = new Properties();
    String CLUSTER = "cluster";
    String LISTENER = "listeners";
    String WEB = "web-modules";
    String SEPARATOR = ".";
    StringBuffer buffer = new StringBuffer();
    LoadbalancerVisitor lbVstr = new LoadbalancerVisitor(_lb);
    lbRdr.accept(lbVstr);
    ClusterReader[] clusterReaders = lbRdr.getClusters();
    buffer.append("otd.properties");
    for (int i = 0; i < clusterReaders.length; i++) {
        StringBuffer clusterHostList = new StringBuffer();
        String clusterWebList = "";
        ClusterReader clusterReader = clusterReaders[i];
        String clusterName = clusterReader.getName();
        WebModuleReader[] webmoduleReaders = clusterReader.getWebModules();
        InstanceReader[] instanceReaders = clusterReader.getInstances();
        for (int j = 0; j < instanceReaders.length; j++) {
            InstanceReader instanceReader = instanceReaders[j];
            String listenerHost = "";
            String listenerPort = "";
            StringTokenizer st = new StringTokenizer(instanceReader.getListeners(), " ");
            while (st.hasMoreElements()) {
                String listener = st.nextToken();
                if (listener.contains("http://")) {
                    listenerHost = listener.substring(listener.lastIndexOf("/") + 1, listener.lastIndexOf(":"));
                    listenerPort = listener.substring(listener.lastIndexOf(":") + 1, listener.length());
                    break;
                }
            }
            clusterHostList = clusterHostList.append(j > 0 ? "," : "").append(listenerHost).append(":").append(listenerPort);
        }
        props.setProperty(CLUSTER + SEPARATOR + clusterName + SEPARATOR + LISTENER, clusterHostList.toString());
        for (int m = 0; m < webmoduleReaders.length; m++) {
            clusterWebList = clusterWebList + (m > 0 ? "," : "") + webmoduleReaders[m].getContextRoot();
        }
        props.setProperty(CLUSTER + SEPARATOR + clusterName + SEPARATOR + WEB, clusterWebList);
    }
    try {
        props.store(out, buffer.toString());
    } finally {
        if (out != null) {
            out.close();
            out = null;
        }
    }
}
Also used : WebModuleReader(org.glassfish.loadbalancer.admin.cli.reader.api.WebModuleReader) Loadbalancer(org.glassfish.loadbalancer.admin.cli.beans.Loadbalancer) Properties(java.util.Properties) InstanceReader(org.glassfish.loadbalancer.admin.cli.reader.api.InstanceReader) StringTokenizer(java.util.StringTokenizer) ClusterReader(org.glassfish.loadbalancer.admin.cli.reader.api.ClusterReader) LoadbalancerVisitor(org.glassfish.loadbalancer.admin.cli.transform.LoadbalancerVisitor)

Example 2 with Loadbalancer

use of org.glassfish.loadbalancer.admin.cli.beans.Loadbalancer in project Payara by payara.

the class LbConfigHelper method exportWorkerProperties.

/**
 * exports the workser.properties from the config to the outputstream provided
 * @param ctx ConfigContext
 * @param lbConfigName name of lb-config
 * @param out OutputStream into which the loadbalancer.xml is written
 */
public static void exportWorkerProperties(LoadbalancerReader lbRdr, OutputStream out) throws Exception {
    // tranform the data using visitor pattern
    Loadbalancer _lb = new Loadbalancer();
    Properties props = new Properties();
    String WORKER = "worker";
    String SEPARATOR = ".";
    String HOST = "host";
    String PORT = "port";
    String LIST = "list";
    String TYPE = "type";
    String TYPE_VALUE = "ajp13";
    String LBFACTOR = "lbfactor";
    String LBFACTOR_VALUE = "1";
    String SOCKET_KEEPALIVE = "socket_keepalive";
    String SOCKET_TIMEOUT = "socket_timeout";
    String SOCKET_KEEPALIVE_VALUE = "1";
    String SOCKET_TIMEOUT_VALUE = "300";
    String LOADBALANCER = "-lb";
    String BALANCER_WORKERS = "balance_workers";
    String LB = "lb";
    String CONTEXT_ROOT_MAPPING = "CONTEXT_ROOT_MAPPING";
    String APP = "APP";
    StringBuffer buffer = new StringBuffer();
    String workerList = "";
    LoadbalancerVisitor lbVstr = new LoadbalancerVisitor(_lb);
    lbRdr.accept(lbVstr);
    ClusterReader[] clusterReaders = lbRdr.getClusters();
    int c;
    buffer.append("worker.properties");
    for (int i = 0; i < clusterReaders.length; i++) {
        String clusterWorkerList = "";
        ClusterReader clusterReader = clusterReaders[i];
        String clusterName = clusterReader.getName();
        WebModuleReader[] webmoduleReaders = clusterReader.getWebModules();
        InstanceReader[] instanceReaders = clusterReader.getInstances();
        for (int j = 0; j < instanceReaders.length; j++) {
            InstanceReader instanceReader = instanceReaders[j];
            String listenerHost = "";
            String listenerPort = "";
            StringTokenizer st = new StringTokenizer(instanceReader.getListeners(), " ");
            while (st.hasMoreElements()) {
                String listener = st.nextToken();
                if (listener.contains("ajp://")) {
                    listenerHost = listener.substring(listener.lastIndexOf("/") + 1, listener.lastIndexOf(":"));
                    listenerPort = listener.substring(listener.lastIndexOf(":") + 1, listener.length());
                    break;
                }
            }
            String listenterName = instanceReader.getName();
            props.setProperty(WORKER + SEPARATOR + listenterName + SEPARATOR + HOST, listenerHost);
            props.setProperty(WORKER + SEPARATOR + listenterName + SEPARATOR + PORT, listenerPort);
            props.setProperty(WORKER + SEPARATOR + listenterName + SEPARATOR + TYPE, TYPE_VALUE);
            props.setProperty(WORKER + SEPARATOR + listenterName + SEPARATOR + LBFACTOR, LBFACTOR_VALUE);
            props.setProperty(WORKER + SEPARATOR + listenterName + SEPARATOR + SOCKET_KEEPALIVE, SOCKET_KEEPALIVE_VALUE);
            props.setProperty(WORKER + SEPARATOR + listenterName + SEPARATOR + SOCKET_TIMEOUT, SOCKET_TIMEOUT_VALUE);
            workerList = workerList + listenterName + ",";
            clusterWorkerList = clusterWorkerList + listenterName + ",";
        }
        workerList = workerList + clusterName + LOADBALANCER + ",";
        props.setProperty(WORKER + SEPARATOR + LIST, workerList.substring(0, workerList.length() - 1));
        props.setProperty(WORKER + SEPARATOR + clusterName + LOADBALANCER + SEPARATOR + TYPE, LB);
        props.setProperty(WORKER + SEPARATOR + clusterName + LOADBALANCER + SEPARATOR + BALANCER_WORKERS, clusterWorkerList.substring(0, clusterWorkerList.length() - 1));
        for (int m = 0; m < webmoduleReaders.length; m++) {
            buffer.append("\n" + CONTEXT_ROOT_MAPPING + SEPARATOR + webmoduleReaders[m].getContextRoot() + "=" + clusterName + LOADBALANCER);
        }
    }
    try {
        props.store(out, buffer.toString());
    } finally {
        if (out != null) {
            out.close();
            out = null;
        }
    }
}
Also used : WebModuleReader(org.glassfish.loadbalancer.admin.cli.reader.api.WebModuleReader) Loadbalancer(org.glassfish.loadbalancer.admin.cli.beans.Loadbalancer) Properties(java.util.Properties) InstanceReader(org.glassfish.loadbalancer.admin.cli.reader.api.InstanceReader) StringTokenizer(java.util.StringTokenizer) ClusterReader(org.glassfish.loadbalancer.admin.cli.reader.api.ClusterReader) LoadbalancerVisitor(org.glassfish.loadbalancer.admin.cli.transform.LoadbalancerVisitor)

Example 3 with Loadbalancer

use of org.glassfish.loadbalancer.admin.cli.beans.Loadbalancer in project Payara by payara.

the class LbConfigHelper method exportXml.

/**
 * exports the loadbalancer.xml from the config to the outputstream provided
 * @param ctx ConfigContext
 * @param lbConfigName name of lb-config
 * @param out OutputStream into which the loadbalancer.xml is written
 */
public static void exportXml(LoadbalancerReader lbRdr, OutputStream out) throws Exception {
    // tranform the data using visitor pattern
    Loadbalancer _lb = new Loadbalancer();
    LoadbalancerVisitor lbVstr = new LoadbalancerVisitor(_lb);
    lbRdr.accept(lbVstr);
    try {
        String footer = LbLogUtil.getStringManager().getString("GeneratedFileFooter", new Date().toString());
        // write the header
        _lb.graphManager().setDoctype(PUBLICID, SYSTEMID);
        _lb.write(out);
        out.write(footer.getBytes());
    } finally {
        if (out != null) {
            out.close();
            out = null;
        }
    }
}
Also used : Loadbalancer(org.glassfish.loadbalancer.admin.cli.beans.Loadbalancer) LoadbalancerVisitor(org.glassfish.loadbalancer.admin.cli.transform.LoadbalancerVisitor) Date(java.util.Date)

Aggregations

Loadbalancer (org.glassfish.loadbalancer.admin.cli.beans.Loadbalancer)3 LoadbalancerVisitor (org.glassfish.loadbalancer.admin.cli.transform.LoadbalancerVisitor)3 Properties (java.util.Properties)2 StringTokenizer (java.util.StringTokenizer)2 ClusterReader (org.glassfish.loadbalancer.admin.cli.reader.api.ClusterReader)2 InstanceReader (org.glassfish.loadbalancer.admin.cli.reader.api.InstanceReader)2 WebModuleReader (org.glassfish.loadbalancer.admin.cli.reader.api.WebModuleReader)2 Date (java.util.Date)1