Search in sources :

Example 1 with LoadbalancerReaderImpl

use of org.glassfish.loadbalancer.admin.cli.reader.impl.LoadbalancerReaderImpl in project Payara by payara.

the class LbConfigHelper method getLbReader.

/**
 * 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 LoadbalancerReader getLbReader(Domain domain, ApplicationRegistry appRegistry, String lbConfigName) throws Exception {
    // reads the load balancer related data
    LbConfigs lbConfigs = domain.getExtensionByType(LbConfigs.class);
    if (lbConfigs == null) {
        throw new Exception(LbLogUtil.getStringManager().getString("UnableToGetLbConfig", lbConfigName));
    }
    LbConfig lbConfig = lbConfigs.getLbConfig(lbConfigName);
    if (lbConfig == null) {
        throw new Exception(LbLogUtil.getStringManager().getString("UnableToGetLbConfig", lbConfigName));
    }
    return new LoadbalancerReaderImpl(domain, appRegistry, lbConfig);
}
Also used : LbConfigs(org.glassfish.loadbalancer.config.LbConfigs) LoadbalancerReaderImpl(org.glassfish.loadbalancer.admin.cli.reader.impl.LoadbalancerReaderImpl) LbConfig(org.glassfish.loadbalancer.config.LbConfig)

Example 2 with LoadbalancerReaderImpl

use of org.glassfish.loadbalancer.admin.cli.reader.impl.LoadbalancerReaderImpl 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

LoadbalancerReaderImpl (org.glassfish.loadbalancer.admin.cli.reader.impl.LoadbalancerReaderImpl)2 LbConfig (org.glassfish.loadbalancer.config.LbConfig)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 LoadbalancerReader (org.glassfish.loadbalancer.admin.cli.reader.api.LoadbalancerReader)1 LbConfigs (org.glassfish.loadbalancer.config.LbConfigs)1 LoadBalancer (org.glassfish.loadbalancer.config.LoadBalancer)1