X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.slc.core%2Fsrc%2Forg%2Fargeo%2Fslc%2Fcore%2Fexecution%2Fhttp%2FRunnerServlet.java;h=eb9b435682e1fd869535c0485adf765401026a45;hb=b707a615d88b14bab7c75467e0acfcccd8aa3d6e;hp=c3f22ccb4398f0a756790338d9ee4e95c97ee8fe;hpb=179731de58f2030d14511cf26b94fdf01f334e71;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc.core/src/org/argeo/slc/core/execution/http/RunnerServlet.java b/org.argeo.slc.core/src/org/argeo/slc/core/execution/http/RunnerServlet.java index c3f22ccb4..eb9b43568 100644 --- a/org.argeo.slc.core/src/org/argeo/slc/core/execution/http/RunnerServlet.java +++ b/org.argeo.slc.core/src/org/argeo/slc/core/execution/http/RunnerServlet.java @@ -55,6 +55,7 @@ public class RunnerServlet extends HttpServlet { private Path baseDir; private BundleContext bc; private ExecutorService executor; + private boolean persistProcesses = true; public RunnerServlet(BundleContext bc, Path baseDir, ExecutorService executor) { this.bc = bc; @@ -62,6 +63,10 @@ public class RunnerServlet extends HttpServlet { this.executor = executor; } + protected void setPersistProcesses(boolean persistProcesses) { + this.persistProcesses = persistProcesses; + } + @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.service(req, resp); @@ -80,18 +85,6 @@ public class RunnerServlet extends HttpServlet { in = req.getInputStream(); } - // InputStream in = req.getInputStream(); - // Gson gson = new Gson(); - // JsonParser jsonParser = new JsonParser(); - // BufferedReader reader = new BufferedReader(new InputStreamReader(in, - // Charset.forName("UTF-8"))); - // JsonElement payload = jsonParser.parse(reader); - // String payloadStr = gson.toJson(payload); - // - // log.debug(payloadStr); - // if (true) - // return; - String path = req.getPathInfo(); // InputStream in = req.getInputStream(); OutputStream out = resp.getOutputStream(); @@ -128,54 +121,58 @@ public class RunnerServlet extends HttpServlet { String flowName = sb.toString(); String ext = FilenameUtils.getExtension(flowName.toString()); - // JCR - Repository repository; - try { - ServiceReference sr = bc.getServiceReferences(Repository.class, "(cn=home)").iterator().next(); - repository = bc.getService(sr); + Session session = null; + Node realizedFlowNode = null; + if (persistProcesses) { + // JCR + Repository repository; + try { + ServiceReference sr = bc.getServiceReferences(Repository.class, "(cn=home)").iterator() + .next(); + repository = bc.getService(sr); - } catch (InvalidSyntaxException e2) { - throw new SlcException("Cannot find home repository", e2); - } - Session session = Subject.doAs(subject, new PrivilegedAction() { + } catch (InvalidSyntaxException e2) { + throw new SlcException("Cannot find home repository", e2); + } + session = Subject.doAs(subject, new PrivilegedAction() { - @Override - public Session run() { - try { - return repository.login(); - } catch (RepositoryException e) { - throw new RuntimeException("Cannot login", e); + @Override + public Session run() { + try { + return repository.login(); + } catch (RepositoryException e) { + throw new RuntimeException("Cannot login", e); + } } - } - }); - UUID processUuid = UUID.randomUUID(); - GregorianCalendar started = new GregorianCalendar(); - Node groupHome = NodeUtils.getGroupHome(session, workgroup); - if (groupHome == null) { - groupHome = NodeUtils.getUserHome(session); - } - String processPath = SlcNames.SLC_SYSTEM + "/" + SlcNames.SLC_PROCESSES + "/" - + JcrUtils.dateAsPath(started, true) + processUuid; - Node processNode = JcrUtils.mkdirs(groupHome, processPath, SlcTypes.SLC_PROCESS); - Node realizedFlowNode; - try { - processNode.setProperty(SlcNames.SLC_UUID, processUuid.toString()); - processNode.setProperty(SlcNames.SLC_STATUS, ExecutionProcess.RUNNING); - realizedFlowNode = processNode.addNode(SlcNames.SLC_FLOW); - realizedFlowNode.addMixin(SlcTypes.SLC_REALIZED_FLOW); - realizedFlowNode.setProperty(SlcNames.SLC_STARTED, started); - realizedFlowNode.setProperty(SlcNames.SLC_NAME, flowName); - Node addressNode = realizedFlowNode.addNode(SlcNames.SLC_ADDRESS, NodeType.NT_ADDRESS); - addressNode.setProperty(Property.JCR_PATH, flowName); - processNode.getSession().save(); - } catch (RepositoryException e1) { - throw new SlcException("Cannot register SLC process", e1); + }); + UUID processUuid = UUID.randomUUID(); + GregorianCalendar started = new GregorianCalendar(); + Node groupHome = NodeUtils.getGroupHome(session, workgroup); + if (groupHome == null) { + groupHome = NodeUtils.getUserHome(session); + } + String processPath = SlcNames.SLC_SYSTEM + "/" + SlcNames.SLC_PROCESSES + "/" + + JcrUtils.dateAsPath(started, true) + processUuid; + Node processNode = JcrUtils.mkdirs(groupHome, processPath, SlcTypes.SLC_PROCESS); + try { + processNode.setProperty(SlcNames.SLC_UUID, processUuid.toString()); + processNode.setProperty(SlcNames.SLC_STATUS, ExecutionProcess.RUNNING); + realizedFlowNode = processNode.addNode(SlcNames.SLC_FLOW); + realizedFlowNode.addMixin(SlcTypes.SLC_REALIZED_FLOW); + realizedFlowNode.setProperty(SlcNames.SLC_STARTED, started); + realizedFlowNode.setProperty(SlcNames.SLC_NAME, flowName); + Node addressNode = realizedFlowNode.addNode(SlcNames.SLC_ADDRESS, NodeType.NT_ADDRESS); + addressNode.setProperty(Property.JCR_PATH, flowName); + processNode.getSession().save(); + } catch (RepositoryException e1) { + throw new SlcException("Cannot register SLC process", e1); + } + if (log.isTraceEnabled()) + log.trace(session.getUserID() + " " + workgroup + " " + flowName); } - if (log.isTraceEnabled()) - log.trace(session.getUserID() + " " + workgroup + " " + flowName); - + boolean isFailed = false; try (ServiceChannel serviceChannel = new ServiceChannel(Channels.newChannel(in), Channels.newChannel(out), executor)) { resp.setHeader("Content-Type", "application/json"); @@ -194,34 +191,42 @@ public class RunnerServlet extends HttpServlet { // execute Future f = executor.submit(task); int written = f.get(); + + if (written < 0) {// error + isFailed = true; + } + if (log.isTraceEnabled()) log.trace("Written " + written + " bytes"); - try { - processNode.setProperty(SlcNames.SLC_STATUS, ExecutionProcess.COMPLETED); - realizedFlowNode.setProperty(SlcNames.SLC_COMPLETED, new GregorianCalendar()); - processNode.getSession().save(); - } catch (RepositoryException e1) { - throw new SlcException("Cannot update SLC process status", e1); - } + if (persistProcesses) + try { + Node processNode = realizedFlowNode.getParent(); + processNode.setProperty(SlcNames.SLC_STATUS, + isFailed ? ExecutionProcess.ERROR : ExecutionProcess.COMPLETED); + realizedFlowNode.setProperty(SlcNames.SLC_COMPLETED, new GregorianCalendar()); + processNode.getSession().save(); + } catch (RepositoryException e1) { + throw new SlcException("Cannot update SLC process status", e1); + } } catch (Exception e) { - try { - processNode.setProperty(SlcNames.SLC_STATUS, ExecutionProcess.ERROR); - realizedFlowNode.setProperty(SlcNames.SLC_COMPLETED, new GregorianCalendar()); - processNode.getSession().save(); - } catch (RepositoryException e1) { - throw new SlcException("Cannot update SLC process status", e1); - } - throw new SlcException("Task " + flowName + " failed", e); + if (persistProcesses) + try { + Node processNode = realizedFlowNode.getParent(); + processNode.setProperty(SlcNames.SLC_STATUS, ExecutionProcess.ERROR); + realizedFlowNode.setProperty(SlcNames.SLC_COMPLETED, new GregorianCalendar()); + processNode.getSession().save(); + } catch (RepositoryException e1) { + throw new SlcException("Cannot update SLC process status", e1); + } + // throw new SlcException("Task " + flowName + " failed", e); + isFailed = true; } finally { JcrUtils.logoutQuietly(session); } - // JsonElement answer = jsonParser.parse(answerStr); - // resp.setHeader("Content-Type", "application/json"); - // JsonWriter jsonWriter = gson.newJsonWriter(resp.getWriter()); - // jsonWriter.setIndent(" "); - // gson.toJson(answer, jsonWriter); - // jsonWriter.flush(); + if (isFailed) { + resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + } } protected Callable createTask(ServiceChannel serviceChannel, String flowName) { @@ -301,4 +306,8 @@ public class RunnerServlet extends HttpServlet { } + protected ExecutorService getExecutor() { + return executor; + } + }