OMElement namePart = deployElement.getFirstChildWithName(new QName(null, "name"));
// "be liberal in what you accept from others"
if (namePart == null) {
namePart = OMUtils.getFirstChildWithName(deployElement, "name");
if( namePart == null ) {
throw new OdeFault("The name part is missing");
} else if (__log.isWarnEnabled()) {
__log.warn("Invalid incoming request detected for operation " + messageContext.getAxisOperation().getName() + ". Name part should have no namespace but has " + namePart.getQName().getNamespaceURI());
}
}
OMElement packagePart = deployElement.getFirstChildWithName(new QName(null, "package"));
// "be liberal in what you accept from others"
if (packagePart == null) {
packagePart = OMUtils.getFirstChildWithName(deployElement, "package");
if (packagePart != null && __log.isWarnEnabled()) {
__log.warn("Invalid incoming request detected for operation " + messageContext.getAxisOperation().getName() + ". Package part should have no namespace but has " + packagePart.getQName().getNamespaceURI());
}
}
OMElement zip = null;
if (packagePart != null) {
zip = packagePart.getFirstChildWithName(new QName(Namespaces.ODE_DEPLOYAPI_NS, "zip"));
// "be liberal in what you accept from others"
if (zip == null) {
zip = OMUtils.getFirstChildWithName(packagePart, "zip");
if (zip != null && __log.isWarnEnabled()) {
String ns = zip.getQName().getNamespaceURI() == null || zip.getQName().getNamespaceURI().length() == 0 ? "empty" : zip.getQName().getNamespaceURI();
__log.warn("Invalid incoming request detected for operation " + messageContext.getAxisOperation().getName() + ". <zip/> element namespace should be " + Namespaces.ODE_DEPLOYAPI_NS + " but was " + ns);
}
}
}
if (zip == null || packagePart == null)
throw new OdeFault("Your message should contain an element named 'package' with a 'zip' element");
String bundleName = namePart.getText().trim();
if (!validBundleName(namePart.getText()))
throw new OdeFault("Invalid bundle name, only non empty alpha-numerics and _ strings are allowed.");
OMText binaryNode = (OMText) zip.getFirstOMChild();
if (binaryNode == null) {
throw new OdeFault("Empty binary node under <zip> element");
}
binaryNode.setOptimize(true);
try {
// We're going to create a directory under the deployment root and put
// files in there. The poller shouldn't pick them up so we're asking
// it to hold on for a while.
_poller.hold();
File dest = new File(_deployPath, bundleName + "-" + _store.getCurrentVersion());
boolean createDir = dest.mkdir();
if(!createDir){
throw new OdeFault("Error while creating file " + dest.getName());
}
unzip(dest, (DataHandler) binaryNode.getDataHandler());
// Check that we have a deploy.xml
File deployXml = new File(dest, "deploy.xml");
if (!deployXml.exists())
throw new OdeFault("The deployment doesn't appear to contain a deployment " +
"descriptor in its root directory named deploy.xml, aborting.");
Collection<QName> deployed = _store.deploy(dest);
File deployedMarker = new File(_deployPath, dest.getName() + ".deployed");
if(!deployedMarker.createNewFile()) {
throw new OdeFault("Error while creating file " + deployedMarker.getName() + "deployment failed");
}
// Telling the poller what we deployed so that it doesn't try to deploy it again
_poller.markAsDeployed(dest);
__log.info("Deployment of artifact " + dest.getName() + " successful.");
OMElement response = factory.createOMElement("response", null);
if (__log.isDebugEnabled()) __log.debug("Deployed package: "+dest.getName());
OMElement d = factory.createOMElement("name", _deployapi);
d.setText(dest.getName());
response.addChild(d);
for (QName pid : deployed) {
if (__log.isDebugEnabled()) __log.debug("Deployed PID: "+pid);
d = factory.createOMElement("id", _deployapi);
d.setText(pid);
response.addChild(d);
}
sendResponse(factory, messageContext, "deployResponse", response);
} finally {
_poller.release();
}
} else if (operation.equals("undeploy")) {
OMElement part = messageContext.getEnvelope().getBody().getFirstElement().getFirstElement();
if (part == null) throw new OdeFault("Missing bundle name in undeploy message.");
String pkg = part.getText().trim();
if (!validBundleName(pkg)) {
throw new OdeFault("Invalid bundle name, only non empty alpha-numerics and _ strings are allowed.");
}
File deploymentDir = new File(_deployPath, pkg);
if (!deploymentDir.exists())
throw new OdeFault("Couldn't find deployment package " + pkg + " in directory " + _deployPath);
try {
// We're going to delete files & directories under the deployment root.
// Put the poller on hold to avoid undesired side effects
_poller.hold();
Collection<QName> undeployed = _store.undeploy(deploymentDir);
File deployedMarker = new File(deploymentDir + ".deployed");
boolean isDeleted = deployedMarker.delete();
if (!isDeleted)
__log.error("Error while deleting file " + deployedMarker.getName());
FileUtils.deepDelete(deploymentDir);
OMElement response = factory.createOMElement("response", null);
response.setText("" + (undeployed.size() > 0));
sendResponse(factory, messageContext, "undeployResponse", response);
_poller.markAsUndeployed(deploymentDir);
} finally {
_poller.release();
}
} else if (operation.equals("listDeployedPackages")) {
Collection<String> packageNames = _store.getPackages();
OMElement response = factory.createOMElement("deployedPackages", null);
for (String name : packageNames) {
OMElement nameElmt = factory.createOMElement("name", _deployapi);
nameElmt.setText(name);
response.addChild(nameElmt);
}
sendResponse(factory, messageContext, "listDeployedPackagesResponse", response);
} else if (operation.equals("listProcesses")) {
OMElement namePart = messageContext.getEnvelope().getBody().getFirstElement().getFirstElement();
List<QName> processIds = _store.listProcesses(namePart.getText());
if (processIds == null) {
throw new OdeFault("Could not find process package: " + namePart.getText());
}
OMElement response = factory.createOMElement("processIds", null);
for (QName qname : processIds) {
OMElement nameElmt = factory.createOMElement("id", _deployapi);
nameElmt.setText(qname);
response.addChild(nameElmt);
}
sendResponse(factory, messageContext, "listProcessesResponse", response);
} else if (operation.equals("getProcessPackage")) {
OMElement qnamePart = messageContext.getEnvelope().getBody().getFirstElement().getFirstElement();
ProcessConf process = _store.getProcessConfiguration(OMUtils.getTextAsQName(qnamePart));
if (process == null) {
throw new OdeFault("Could not find process: " + qnamePart.getTextAsQName());
}
String packageName = _store.getProcessConfiguration(OMUtils.getTextAsQName(qnamePart)).getPackage();
OMElement response = factory.createOMElement("packageName", null);
response.setText(packageName);
sendResponse(factory, messageContext, "getProcessPackageResponse", response);
} else unknown = true;
} catch (Throwable t) {
// Trying to extract a meaningful message
Throwable source = t;
while (source.getCause() != null && source.getCause() != source) source = source.getCause();
__log.warn("Invocation of operation " + operation + " failed", t);
throw new OdeFault("Invocation of operation " + operation + " failed: " + source.toString(), t);
}
if (unknown) throw new OdeFault("Unknown operation: '"
+ messageContext.getAxisOperation().getName() + "'");
}