Search in sources :

Example 1 with LoadbalancerReader

use of org.glassfish.loadbalancer.admin.cli.reader.api.LoadbalancerReader in project Payara by payara.

the class ApplyHttpLbChanges method publish.

/**
 * publishes the loadbalancer.xml to the physical loadbalancer.
 * @throws java.io.IOException
 * @throws com.sun.enterprise.config.ConfigException
 * @throws org.netbeans.modules.schema2beans.Schema2BeansException
 */
public void publish(ConnectionManager _connectionManager, Domain domain, String lbConfigName) throws IOException, Exception {
    // check if the lb exists
    LoadbalancerReader lbr = LbConfigHelper.getLbReader(domain, appRegistry, lbConfigName);
    HttpURLConnection conn = _connectionManager.getConnection(LB_UPDATE_CONTEXT_ROOT);
    OutputStream out = null;
    try {
        conn.setDoOutput(true);
        conn.setRequestMethod(POST);
        conn.connect();
        out = conn.getOutputStream();
        LbConfigHelper.exportXml(lbr, out);
        out.flush();
        lbr.getLbConfig().setLastApplied();
    } catch (UnknownHostException uhe) {
        throw new IOException(LbLogUtil.getStringManager().getString("CannotConnectToLBHost", uhe.getMessage()), uhe);
    } catch (Exception e) {
        throw new IOException(e.getMessage(), e);
    } finally {
        if (out != null && conn != null) {
            int code = conn.getResponseCode();
            String response = conn.getResponseMessage();
            out.close();
            conn.disconnect();
            out = null;
            if (code != HttpURLConnection.HTTP_OK) {
                String url = conn.getURL().toString();
                conn = null;
                throw new IOException(LbLogUtil.getStringManager().getString("HttpError", Integer.valueOf(code), response, url));
            }
            conn = null;
        }
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) UnknownHostException(java.net.UnknownHostException) OutputStream(java.io.OutputStream) IOException(java.io.IOException) LoadbalancerReader(org.glassfish.loadbalancer.admin.cli.reader.api.LoadbalancerReader) Schema2BeansException(org.netbeans.modules.schema2beans.Schema2BeansException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 2 with LoadbalancerReader

use of org.glassfish.loadbalancer.admin.cli.reader.api.LoadbalancerReader in project Payara by payara.

the class LoadbalancerVisitor method visit.

/**
 * Visit reader class
 */
@Override
public void visit(BaseReader br) throws Exception {
    // FIXME, make as assert here about no class cast exception
    if (br instanceof LoadbalancerReader) {
        LoadbalancerReader lbRdr = (LoadbalancerReader) br;
        PropertyReader[] pRdrs = lbRdr.getProperties();
        if ((pRdrs != null) && (pRdrs.length > 0)) {
            Property[] props = new Property[pRdrs.length];
            for (int i = 0; i < pRdrs.length; i++) {
                props[i] = new Property();
                pRdrs[i].accept(new PropertyVisitor(props[i]));
            }
            _lb.setProperty2(props);
        }
        ClusterReader[] cRdrs = lbRdr.getClusters();
        if ((cRdrs != null) && (cRdrs.length > 0)) {
            Cluster[] cls = new Cluster[cRdrs.length];
            for (int i = 0; i < cRdrs.length; i++) {
                cls[i] = new Cluster();
                cRdrs[i].accept(new ClusterVisitor(cls[i]));
            }
            _lb.setCluster(cls);
        }
    }
}
Also used : ClusterReader(org.glassfish.loadbalancer.admin.cli.reader.api.ClusterReader) Cluster(org.glassfish.loadbalancer.admin.cli.beans.Cluster) LoadbalancerReader(org.glassfish.loadbalancer.admin.cli.reader.api.LoadbalancerReader) PropertyReader(org.glassfish.loadbalancer.admin.cli.reader.api.PropertyReader) Property(org.glassfish.loadbalancer.admin.cli.beans.Property)

Example 3 with LoadbalancerReader

use of org.glassfish.loadbalancer.admin.cli.reader.api.LoadbalancerReader in project Payara by payara.

the class ExportHttpLbConfig method process.

public String process(AdminCommandContext context) throws Exception {
    LoadbalancerReader lbr = null;
    if (lbName != null && lbConfigName == null && target == null) {
        LoadBalancer lb = LbConfigHelper.getLoadBalancer(domain, lbName);
        lbr = LbConfigHelper.getLbReader(domain, appRegistry, lb.getLbConfigName());
    } else if (lbConfigName != null && lbName == null && target == null) {
        lbr = LbConfigHelper.getLbReader(domain, appRegistry, lbConfigName);
    } else if (target != null && lbName == null && lbConfigName == null) {
        Set<String> clusters = new HashSet<String>();
        clusters.addAll(target);
        lbr = new LoadbalancerReaderImpl(domain, appRegistry, clusters, properties);
    } else {
        String msg = LbLogUtil.getStringManager().getString("ExportHttpLbConfigInvalidArgs");
        throw new Exception(msg);
    }
    if (fileName == null) {
        String configName = lbr.getName();
        if (configName != null) {
            fileName = DEFAULT_LB_XML_FILE_NAME + "." + configName;
        } else {
            fileName = DEFAULT_LB_XML_FILE_NAME;
        }
    }
    File lbConfigFile = new File(fileName);
    if (!lbConfigFile.isAbsolute() && !retrieveFile) {
        File loadbalancerDir = new File(env.getInstanceRoot(), "load-balancer");
        if (!loadbalancerDir.exists()) {
            boolean isMkdirSuccess = loadbalancerDir.mkdir();
            if (!isMkdirSuccess) {
                String msg = LbLogUtil.getStringManager().getString("directoryCreationFailed");
                throw new Exception(msg);
            }
        }
        lbConfigFile = new File(loadbalancerDir, fileName);
    }
    File tmpLbXmlFile = null;
    if (retrieveFile) {
        tmpLbXmlFile = File.createTempFile("load-balancer", ".xml");
        tmpLbXmlFile.deleteOnExit();
    } else {
        if (lbConfigFile.exists()) {
            String msg = LbLogUtil.getStringManager().getString("FileExists", lbConfigFile.getPath());
            throw new Exception(msg);
        }
        if (!(lbConfigFile.getParentFile().exists())) {
            String msg = LbLogUtil.getStringManager().getString("ParentFileMissing", lbConfigFile.getParent());
            throw new Exception(msg);
        }
        tmpLbXmlFile = lbConfigFile;
    }
    FileOutputStream fo = null;
    try {
        fo = new FileOutputStream(tmpLbXmlFile);
        LbConfigHelper.exportXml(lbr, fo);
        if (retrieveFile) {
            retrieveLbConfig(context, lbConfigFile, tmpLbXmlFile);
        }
        LbConfig lbConfig = lbr.getLbConfig();
        // In such a case, lbconfig will be null
        if (lbConfig != null) {
            lbConfig.setLastExported();
        }
        String msg = LbLogUtil.getStringManager().getString("GeneratedFileLocation", lbConfigFile.toString());
        return msg;
    } finally {
        if (fo != null) {
            fo.close();
            fo = null;
        }
    }
}
Also used : LoadbalancerReaderImpl(org.glassfish.loadbalancer.admin.cli.reader.impl.LoadbalancerReaderImpl) FileOutputStream(java.io.FileOutputStream) LbConfig(org.glassfish.loadbalancer.config.LbConfig) LoadBalancer(org.glassfish.loadbalancer.config.LoadBalancer) LoadbalancerReader(org.glassfish.loadbalancer.admin.cli.reader.api.LoadbalancerReader) File(java.io.File) IOException(java.io.IOException) HashSet(java.util.HashSet)

Aggregations

LoadbalancerReader (org.glassfish.loadbalancer.admin.cli.reader.api.LoadbalancerReader)3 IOException (java.io.IOException)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 UnknownHostException (java.net.UnknownHostException)1 HashSet (java.util.HashSet)1 Cluster (org.glassfish.loadbalancer.admin.cli.beans.Cluster)1 Property (org.glassfish.loadbalancer.admin.cli.beans.Property)1 ClusterReader (org.glassfish.loadbalancer.admin.cli.reader.api.ClusterReader)1 PropertyReader (org.glassfish.loadbalancer.admin.cli.reader.api.PropertyReader)1 LoadbalancerReaderImpl (org.glassfish.loadbalancer.admin.cli.reader.impl.LoadbalancerReaderImpl)1 LbConfig (org.glassfish.loadbalancer.config.LbConfig)1 LoadBalancer (org.glassfish.loadbalancer.config.LoadBalancer)1 Schema2BeansException (org.netbeans.modules.schema2beans.Schema2BeansException)1