001: package dalma.webui;
002:
003: import dalma.container.Container;
004: import dalma.container.FailedOperationException;
005: import dalma.container.WorkflowApplication;
006: import org.apache.commons.fileupload.DiskFileUpload;
007: import org.apache.commons.fileupload.FileItem;
008: import org.apache.commons.fileupload.FileUploadException;
009: import org.kohsuke.stapler.StaplerRequest;
010: import org.kohsuke.stapler.StaplerResponse;
011:
012: import javax.servlet.ServletException;
013: import java.io.IOException;
014: import java.util.ArrayList;
015: import java.util.List;
016: import java.util.Collections;
017: import java.util.logging.LogRecord;
018: import java.util.logging.Logger;
019:
020: /**
021: * {@link Container} wrapper for the web UI.
022: *
023: * @author Kohsuke Kawaguchi
024: */
025: public class WContainer extends UIObject {
026: public final Container core;
027:
028: private final LogRecorder logRecorder;
029:
030: public WContainer(Container core, LogRecorder logRecorder) {
031: this .core = core;
032: this .logRecorder = logRecorder;
033: }
034:
035: public String getDisplayName() {
036: return "Dalma";
037: }
038:
039: public String getUrl() {
040: return "";
041: }
042:
043: public boolean isUseSecurity() {
044: return false; // TODO
045: }
046:
047: public List<LogRecord> getLogs() {
048: return logRecorder.getLogRecords();
049: }
050:
051: public void doCreateApp(StaplerRequest req, StaplerResponse resp)
052: throws IOException, ServletException {
053: String appName = null;
054: byte[] contents = null;
055:
056: try {
057: DiskFileUpload fu = new DiskFileUpload();
058: for (FileItem fi : (List<FileItem>) fu.parseRequest(req)) {
059: if (fi.isFormField()) {
060: if (fi.getFieldName() != null
061: && fi.getFieldName().equals("name"))
062: appName = fi.getString();
063: } else {
064: contents = fi.get();
065: }
066: }
067: } catch (FileUploadException e) {
068: sendError(req, e, resp);
069: return;
070: }
071:
072: if (appName == null || contents == null
073: || appName.length() == 0 || contents.length == 0) {
074: sendError(req, "form data incomplete", resp);
075: return;
076: }
077:
078: try {
079: WorkflowApplication wa = core.deploy(appName, contents);
080: resp.sendRedirect(req.getContextPath() + '/'
081: + WWorkflow.wrap(wa).getUrl());
082: } catch (FailedOperationException e) {
083: sendError(req, e, resp);
084: } catch (InterruptedException e) {
085: sendError(req, e, resp);
086: }
087: }
088:
089: public List<WWorkflow> getWorkflows() {
090: List<WWorkflow> r = new ArrayList<WWorkflow>();
091: for (WorkflowApplication a : core.getApplications()) {
092: r.add(WWorkflow.wrap(a));
093: }
094: Collections.sort(r);
095: return r;
096: }
097:
098: public WWorkflow getWorkflow(String name) {
099: return WWorkflow.wrap(core.getApplication(name));
100: }
101:
102: public void shutdown() {
103: Logger.getLogger("dalma").removeHandler(logRecorder);
104: core.stop();
105: }
106: }
|