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;
}
}
}
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);
}
}
}
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;
}
}
}
Aggregations