]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/execution/JcrExecutionProcess.java
Improve runtime
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / execution / JcrExecutionProcess.java
index 526b5ebc4e5fb10add998c3b635088423dd59031..5825351771e207ca03f692f3546dbf904a42c89a 100644 (file)
@@ -1,5 +1,24 @@
+/*
+ * Copyright (C) 2007-2012 Mathieu Baudier
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.argeo.slc.jcr.execution;
 
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.List;
+
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 
@@ -8,20 +27,24 @@ import org.apache.commons.logging.LogFactory;
 import org.argeo.jcr.JcrUtils;
 import org.argeo.slc.SlcException;
 import org.argeo.slc.execution.ExecutionProcess;
+import org.argeo.slc.execution.ExecutionStep;
 import org.argeo.slc.jcr.SlcNames;
+import org.argeo.slc.jcr.SlcTypes;
 
 /** Execution process implementation based on a JCR node. */
-public class JcrExecutionProcess implements ExecutionProcess {
+public class JcrExecutionProcess implements ExecutionProcess, SlcNames {
        private Log log = LogFactory.getLog(JcrExecutionProcess.class);
        private final Node node;
 
+       private Long nextLogLine = 1l;
+
        public JcrExecutionProcess(Node node) {
                this.node = node;
        }
 
        public String getUuid() {
                try {
-                       return node.getProperty(SlcNames.SLC_UUID).getString();
+                       return node.getProperty(SLC_UUID).getString();
                } catch (RepositoryException e) {
                        throw new SlcException("Cannot get uuid for " + node, e);
                }
@@ -29,7 +52,7 @@ public class JcrExecutionProcess implements ExecutionProcess {
 
        public String getStatus() {
                try {
-                       return node.getProperty(SlcNames.SLC_STATUS).getString();
+                       return node.getProperty(SLC_STATUS).getString();
                } catch (RepositoryException e) {
                        log.error("Cannot get status: " + e);
                        // we should re-throw exception because this information can
@@ -41,17 +64,13 @@ public class JcrExecutionProcess implements ExecutionProcess {
 
        public void setStatus(String status) {
                try {
-                       node.setProperty(SlcNames.SLC_STATUS, status);
+                       node.setProperty(SLC_STATUS, status);
                        // last modified properties needs to be manually updated
                        // see https://issues.apache.org/jira/browse/JCR-2233
                        JcrUtils.updateLastModified(node);
                        node.getSession().save();
                } catch (RepositoryException e) {
-                       try {
-                               JcrUtils.discardQuietly(node.getSession());
-                       } catch (RepositoryException e1) {
-                               // silent
-                       }
+                       JcrUtils.discardUnderlyingSessionQuietly(node);
                        // we should re-throw exception because this information can
                        // probably used for monitoring in case there are already unexpected
                        // exceptions
@@ -59,6 +78,60 @@ public class JcrExecutionProcess implements ExecutionProcess {
                }
        }
 
+       /**
+        * Synchronized in order to make sure that there is no concurrent
+        * modification of {@link #nextLogLine}.
+        */
+       public synchronized void addSteps(List<ExecutionStep> steps) {
+               try {
+                       steps: for (ExecutionStep step : steps) {
+                               String type;
+                               if (step.getType().equals(ExecutionStep.TRACE))
+                                       type = SlcTypes.SLC_LOG_TRACE;
+                               else if (step.getType().equals(ExecutionStep.DEBUG))
+                                       type = SlcTypes.SLC_LOG_DEBUG;
+                               else if (step.getType().equals(ExecutionStep.INFO))
+                                       type = SlcTypes.SLC_LOG_INFO;
+                               else if (step.getType().equals(ExecutionStep.WARNING))
+                                       type = SlcTypes.SLC_LOG_WARNING;
+                               else if (step.getType().equals(ExecutionStep.ERROR))
+                                       type = SlcTypes.SLC_LOG_ERROR;
+                               else
+                                       // skip
+                                       continue steps;
+
+                               String relPath = SLC_LOG + '/'
+                                               + step.getThread().replace('/', '_') + '/'
+                                               + step.getLocation().replace('.', '/');
+                               String path = node.getPath() + '/' + relPath;
+                               // clean special character
+                               // TODO factorize in JcrUtils
+                               path = path.replace('@', '_');
+
+                               Node location = JcrUtils.mkdirs(node.getSession(), path);
+                               Node logEntry = location.addNode(Long.toString(nextLogLine),
+                                               type);
+                               logEntry.setProperty(SLC_MESSAGE, step.getLog());
+                               Calendar calendar = new GregorianCalendar();
+                               calendar.setTime(step.getTimestamp());
+                               logEntry.setProperty(SLC_TIMESTAMP, calendar);
+
+                               // System.out.println("Logged " + logEntry.getPath());
+
+                               nextLogLine++;
+                       }
+
+                       // last modified properties needs to be manually updated
+                       // see https://issues.apache.org/jira/browse/JCR-2233
+                       JcrUtils.updateLastModified(node);
+
+                       node.getSession().save();
+               } catch (Exception e) {
+                       JcrUtils.discardUnderlyingSessionQuietly(node);
+                       e.printStackTrace();
+               }
+       }
+
        public Node getNode() {
                return node;
        }