private PortletRequestDispatcher helpView;
public void processAction(ActionRequest actionRequest,
ActionResponse actionResponse) throws PortletException, IOException {
if (!PortletFileUpload.isMultipartContent(actionRequest)) {
throw new PortletException("Expected file upload");
}
File rootDir = new File(System.getProperty("java.io.tmpdir"));
PortletFileUpload uploader = new PortletFileUpload(new DiskFileItemFactory(10240, rootDir));
File moduleFile = null;
File planFile = null;
String startApp = null;
String redeploy = null;
try {
List items = uploader.parseRequest(actionRequest);
for (Iterator i = items.iterator(); i.hasNext();) {
FileItem item = (FileItem) i.next();
if (!item.isFormField()) {
String fieldName = item.getFieldName();
String name = item.getName().trim();
File file;
if (name.length() == 0) {
file = null;
} else {
// Firefox sends basename, IE sends full path
int index = name.lastIndexOf('\\');
if (index != -1) {
name = name.substring(index + 1);
}
file = new File(rootDir, name);
}
if ("module".equals(fieldName)) {
moduleFile = file;
} else if ("plan".equals(fieldName)) {
planFile = file;
}
if (file != null) {
try {
item.write(file);
} catch (Exception e) {
throw new PortletException(e);
}
}
} else {
// retrieve 'startApp' form field value
if ("startApp".equalsIgnoreCase(item.getFieldName())) {
startApp = item.getString();
} else if ("redeploy".equalsIgnoreCase(item.getFieldName())) {
redeploy = item.getString();
}
}
}
} catch (FileUploadException e) {
throw new PortletException(e);
}
DeploymentFactoryManager dfm = DeploymentFactoryManager.getInstance();
FileInputStream fis = null;
try {
DeploymentManager mgr = dfm.getDeploymentManager("deployer:geronimo:inVM", null, null);
try {
boolean isRedeploy = redeploy != null && !redeploy.equals("");
if(mgr instanceof JMXDeploymentManager) {
((JMXDeploymentManager)mgr).setLogConfiguration(false, true);
}
Target[] all = mgr.getTargets();
if (null == all) {
throw new IllegalStateException("No target to distribute to");
}
ProgressObject progress;
if(isRedeploy) {
TargetModuleID[] targets = identifyTargets(moduleFile, planFile, mgr.getAvailableModules(null, all));
if(targets.length == 0) {
throw new PortletException("Unable to identify modules to replace. Please include a Geronimo deployment plan or use the command-line deployment tool.");
}
progress = mgr.redeploy(targets, moduleFile, planFile);
} else {
progress = mgr.distribute(new Target[] {all[0]}, moduleFile, planFile);
}
while(progress.getDeploymentStatus().isRunning()) {
Thread.sleep(100);
}
String abbrStatusMessage;
String fullStatusMessage = null;
if(progress.getDeploymentStatus().isCompleted()) {
abbrStatusMessage = "The application was successfully "+(isRedeploy ? "re" : "")+"deployed.<br/>";
// start installed app/s
if (!isRedeploy && startApp != null && !startApp.equals("")) {
progress = mgr.start(progress.getResultTargetModuleIDs());
while(progress.getDeploymentStatus().isRunning()) {
Thread.sleep(100);
}
abbrStatusMessage+="The application was successfully started";
}
} else {
fullStatusMessage = progress.getDeploymentStatus().getMessage();
// for the abbreviated status message clip off everything
// after the first line, which in most cases means the gnarly stacktrace
abbrStatusMessage = "Deployment failed:<br/>"
+ fullStatusMessage.substring(0, fullStatusMessage.indexOf('\n'));
// try to provide an upgraded version of the plan
try {
if (planFile != null && planFile.exists()) {
byte[] plan = new byte[(int) planFile.length()];
fis = new FileInputStream(planFile);
fis.read(plan);
DocumentBuilder documentBuilder = XmlUtil.newDocumentBuilderFactory().newDocumentBuilder();
Document doc = documentBuilder.parse(new ByteArrayInputStream(plan));
// v1.1 switched from configId to moduleId
String configId = doc.getDocumentElement().getAttribute("configId");
if (configId != null && !("".equals(configId))) {
StringWriter sw = new StringWriter();
new Upgrade1_0To1_1().upgrade(new ByteArrayInputStream(plan), sw);
// have to store the original and upgraded plans in the session
// because the buffer size for render parameters is sometimes not
// big enough
actionRequest.getPortletSession().setAttribute(MIGRATED_PLAN_PARM, sw.getBuffer());
actionRequest.getPortletSession().setAttribute(ORIGINAL_PLAN_PARM, new String(plan));
}
}
} catch (Exception e) {
// cannot provide a migrated plan in this case, most likely
// because the deployment plan would not parse. a valid
// status message has already been provided in this case
}
}
// have to store the status messages in the portlet session
// because the buffer size for render parameters is sometimes not big enough
actionRequest.getPortletSession().setAttribute(FULL_STATUS_PARM, fullStatusMessage);
actionRequest.getPortletSession().setAttribute(ABBR_STATUS_PARM, abbrStatusMessage);
} finally {
mgr.release();
if (fis!=null) fis.close();
if(moduleFile != null && moduleFile.exists()) {
if(!moduleFile.delete()) {
log.debug("Unable to delete temporary file "+moduleFile);
moduleFile.deleteOnExit();
}
}
if(planFile != null && planFile.exists()) {
if(!planFile.delete()) {
log.debug("Unable to delete temporary file "+planFile);
planFile.deleteOnExit();
}
}
}
} catch (Exception e) {
throw new PortletException(e);
}
}