You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2008/08/25 09:56:25 UTC

svn commit: r688641 - in /incubator/sling/trunk/extensions/event: ./ src/main/java/org/apache/sling/event/impl/ src/main/resources/SLING-INF/nodetypes/ src/test/java/org/apache/sling/event/impl/

Author: cziegeler
Date: Mon Aug 25 00:56:24 2008
New Revision: 688641

URL: http://svn.apache.org/viewvc?rev=688641&view=rev
Log:
Simplify node type handling and store jobs into sub trees depending on the job topic.

Modified:
    incubator/sling/trunk/extensions/event/pom.xml
    incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/AbstractRepositoryEventHandler.java
    incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/EventHelper.java
    incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java
    incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/TimedJobHandler.java
    incubator/sling/trunk/extensions/event/src/main/resources/SLING-INF/nodetypes/event.cnd
    incubator/sling/trunk/extensions/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java

Modified: incubator/sling/trunk/extensions/event/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/event/pom.xml?rev=688641&r1=688640&r2=688641&view=diff
==============================================================================
--- incubator/sling/trunk/extensions/event/pom.xml (original)
+++ incubator/sling/trunk/extensions/event/pom.xml Mon Aug 25 00:56:24 2008
@@ -117,6 +117,11 @@
         </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.jcr.resource</artifactId>
+            <version>2.0.3-incubator-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.commons.scheduler</artifactId>
             <version>2.0.2-incubator</version>
         </dependency>

Modified: incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/AbstractRepositoryEventHandler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/AbstractRepositoryEventHandler.java?rev=688641&r1=688640&r2=688641&view=diff
==============================================================================
--- incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/AbstractRepositoryEventHandler.java (original)
+++ incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/AbstractRepositoryEventHandler.java Mon Aug 25 00:56:24 2008
@@ -23,7 +23,6 @@
 import java.util.Calendar;
 import java.util.Dictionary;
 import java.util.List;
-import java.util.StringTokenizer;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.LinkedBlockingQueue;
 
@@ -32,6 +31,7 @@
 import javax.jcr.Session;
 import javax.jcr.observation.EventListener;
 
+import org.apache.jackrabbit.JcrConstants;
 import org.apache.sling.commons.osgi.OsgiUtil;
 import org.apache.sling.commons.threads.ThreadPool;
 import org.apache.sling.commons.threads.ThreadPoolConfig;
@@ -40,6 +40,7 @@
 import org.apache.sling.event.EventPropertiesMap;
 import org.apache.sling.event.EventUtil;
 import org.apache.sling.jcr.api.SlingRepository;
+import org.apache.sling.jcr.resource.JcrResourceUtil;
 import org.osgi.service.component.ComponentContext;
 import org.osgi.service.event.Event;
 import org.osgi.service.event.EventAdmin;
@@ -217,7 +218,7 @@
     protected void startWriterSession() throws RepositoryException {
         this.writerSession = this.createSession();
         if ( this.repositoryPath != null ) {
-            this.createRepositoryPath();
+            this.ensureRepositoryPath();
         }
     }
 
@@ -238,56 +239,39 @@
     }
 
     /**
-     * Create the repository path in the repository.
+     * Check if the repository path already exists. If not, create it.
      */
-    protected void createRepositoryPath()
+    protected Node ensureRepositoryPath()
     throws RepositoryException {
-        if ( !this.writerSession.itemExists(this.repositoryPath) ) {
-            Node node = this.writerSession.getRootNode();
-            String path = this.repositoryPath.substring(1);
-            int pos = path.lastIndexOf('/');
-            if ( pos != -1 ) {
-                final StringTokenizer st = new StringTokenizer(path.substring(0, pos), "/");
-                while ( st.hasMoreTokens() ) {
-                    final String token = st.nextToken();
-                    if ( !node.hasNode(token) ) {
-                        try {
-                            node.addNode(token, "nt:folder");
-                            node.save();
-                        } catch (RepositoryException re) {
-                            // we ignore this as this folder might be created from a different task
-                            node.refresh(false);
-                        }
-                    }
-                    node = node.getNode(token);
-                }
-                path = path.substring(pos + 1);
-            }
-            if ( !node.hasNode(path) ) {
-                node.addNode(path, this.getContainerNodeType());
-                node.save();
-            }
+        final Node node = JcrResourceUtil.createPath(this.repositoryPath,
+                                   EventHelper.NODETYPE_FOLDER,
+                                   EventHelper.NODETYPE_FOLDER,
+                                   this.writerSession, true);
+        if ( !node.isNodeType(JcrConstants.MIX_LOCKABLE) ) {
+            node.addMixin(JcrConstants.MIX_LOCKABLE);
+            node.save();
         }
+        return node;
     }
 
-    protected String getContainerNodeType() {
-        return EventHelper.EVENTS_NODE_TYPE;
-    }
-
+    /**
+     * Return the node type for the event.
+     */
     protected String getEventNodeType() {
         return EventHelper.EVENT_NODE_TYPE;
     }
 
     /**
      * Write an event to the repository.
-     * @param e
+     * @param e The event
+     * @param suggestName A suggest name/path for the node.
      * @throws RepositoryException
      * @throws IOException
      */
     protected Node writeEvent(Event e, String suggestedName)
     throws RepositoryException {
         // create new node with name of topic
-        final Node rootNode = (Node) this.writerSession.getItem(this.repositoryPath);
+        final Node rootNode = this.ensureRepositoryPath();
 
         final String nodeType = this.getEventNodeType();
         final String nodeName;
@@ -298,7 +282,10 @@
             final int sepPos = nodeType.indexOf(':');
             nodeName = nodeType.substring(sepPos+1) + "-" + this.applicationId + "-" + now.getTime().getTime();
         }
-        final Node eventNode = rootNode.addNode(nodeName, nodeType);
+        final Node eventNode = JcrResourceUtil.createPath(rootNode,
+                nodeName,
+                EventHelper.NODETYPE_FOLDER,
+                nodeType, false);
 
         eventNode.setProperty(EventHelper.NODE_PROPERTY_CREATED, Calendar.getInstance());
         eventNode.setProperty(EventHelper.NODE_PROPERTY_TOPIC, e.getTopic());
@@ -343,6 +330,12 @@
         }
     }
 
+    /**
+     * Add properties from the node to the event properties.
+     * @param eventNode The repository node.
+     * @param properties The event properties.
+     * @throws RepositoryException
+     */
     protected void addEventProperties(Node eventNode, Dictionary<String, Object> properties)
     throws RepositoryException {
         // nothing to do

Modified: incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/EventHelper.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/EventHelper.java?rev=688641&r1=688640&r2=688641&view=diff
==============================================================================
--- incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/EventHelper.java (original)
+++ incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/EventHelper.java Mon Aug 25 00:56:24 2008
@@ -45,4 +45,6 @@
     public static final String JOB_NODE_TYPE = "slingevent:Job";
     public static final String TIMED_EVENTS_NODE_TYPE = "slingevent:TimedEvents";
     public static final String TIMED_EVENT_NODE_TYPE = "slingevent:TimedEvent";
+
+    public static final String NODETYPE_FOLDER = "sling:Folder";
 }

Modified: incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java?rev=688641&r1=688640&r2=688641&view=diff
==============================================================================
--- incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java (original)
+++ incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/JobEventHandler.java Mon Aug 25 00:56:24 2008
@@ -153,7 +153,7 @@
     /**
      * Return the query string for the clean up.
      */
-    protected String getCleanUpQueryString() {
+    private String getCleanUpQueryString() {
         final Calendar deleteBefore = Calendar.getInstance();
         deleteBefore.add(Calendar.MINUTE, -this.cleanupPeriod);
         final String dateString = ISO8601.format(deleteBefore);
@@ -229,26 +229,26 @@
                 }
                 final EventInfo info = new EventInfo();
                 info.event = event;
-                final String nodeName = this.getNodeName(event);
+                final String nodePath = this.getNodePath(event);
 
                 // if the job has no job id, we can just write the job to the repo and don't
                 // need locking
                 final String jobId = (String)event.getProperty(EventUtil.PROPERTY_JOB_ID);
                 if ( jobId == null ) {
                     try {
-                        final Node eventNode = this.writeEvent(event, nodeName);
+                        final Node eventNode = this.writeEvent(event, nodePath);
                         info.nodePath = eventNode.getPath();
                     } catch (RepositoryException re ) {
                         // something went wrong, so let's log it
-                        this.logger.error("Exception during writing new job '" + nodeName + "' to repository.", re);
+                        this.logger.error("Exception during writing new job '" + nodePath + "' to repository.", re);
                     }
                 } else {
                     try {
                         // let's first search for an existing node with the same id
-                        final Node parentNode = (Node)this.writerSession.getItem(this.repositoryPath);
+                        final Node parentNode = this.ensureRepositoryPath();
                         Node foundNode = null;
-                        if ( parentNode.hasNode(nodeName) ) {
-                            foundNode = parentNode.getNode(nodeName);
+                        if ( parentNode.hasNode(nodePath) ) {
+                            foundNode = parentNode.getNode(nodePath);
                         }
                         if ( foundNode != null ) {
                             // if the node is locked, someone else was quicker
@@ -268,7 +268,7 @@
                         } else {
                             // We now write the event into the repository
                             try {
-                                final Node eventNode = this.writeEvent(event, nodeName);
+                                final Node eventNode = this.writeEvent(event, nodePath);
                                 info.nodePath = eventNode.getPath();
                             } catch (ItemExistsException iee) {
                                 // someone else did already write this node in the meantime
@@ -277,7 +277,7 @@
                         }
                     } catch (RepositoryException re ) {
                         // something went wrong, so let's log it
-                        this.logger.error("Exception during writing new job '" + nodeName + "' to repository.", re);
+                        this.logger.error("Exception during writing new job '" + nodePath + "' to repository.", re);
                     }
                 }
                 // if we were able to write the event into the repository
@@ -300,13 +300,13 @@
     protected void runInBackground() throws RepositoryException {
         this.backgroundSession = this.createSession();
         this.backgroundSession.getWorkspace().getObservationManager()
-        .addEventListener(this,
-                          javax.jcr.observation.Event.PROPERTY_REMOVED,
-                          this.repositoryPath,
-                          true,
-                          null,
-                          new String[] {this.getEventNodeType()},
-                          true);
+                .addEventListener(this,
+                                  javax.jcr.observation.Event.PROPERTY_REMOVED,
+                                  this.repositoryPath,
+                                  true,
+                                  null,
+                                  new String[] {this.getEventNodeType()},
+                                  true);
         // load unprocessed jobs from repository
         this.loadJobs();
         while ( this.running ) {
@@ -429,13 +429,6 @@
     }
 
     /**
-     * @see org.apache.sling.engine.event.impl.JobPersistenceHandler#getContainerNodeType()
-     */
-    protected String getContainerNodeType() {
-        return EventHelper.JOBS_NODE_TYPE;
-    }
-
-    /**
      * @see org.apache.sling.engine.event.impl.JobPersistenceHandler#getEventNodeType()
      */
     protected String getEventNodeType() {
@@ -566,18 +559,16 @@
     }
 
     /**
-     * Create a unique node name for the job.
+     * Create a unique node path (folder and name) for the job.
      */
-    protected String getNodeName(Event event) {
+    private String getNodePath(Event event) {
+        final String jobTopic = ((String)event.getProperty(EventUtil.PROPERTY_JOB_TOPIC)).replace('/', '.');
         final String jobId = (String)event.getProperty(EventUtil.PROPERTY_JOB_ID);
-        final String name;
+
         if ( jobId != null ) {
-            final String jobTopic = ((String)event.getProperty(EventUtil.PROPERTY_JOB_TOPIC));
-            name = jobTopic + " " + jobId;
-        } else {
-            name = "Job " + UUID.randomUUID().toString();
+            return jobTopic + "/" + filter(jobId);
         }
-        return filter(name);
+        return jobTopic + "/Job " + UUID.randomUUID().toString();
     }
 
     /**
@@ -585,7 +576,7 @@
      * @param event The original event.
      * @param eventNode The node in the repository where the job is stored.
      */
-    protected void processJob(Event event, Node eventNode)  {
+    private void processJob(Event event, Node eventNode)  {
         final boolean parallelProcessing = event.getProperty(EventUtil.PROPERTY_JOB_PARALLEL) != null;
         final String jobTopic = (String)event.getProperty(EventUtil.PROPERTY_JOB_TOPIC);
         boolean unlock = true;
@@ -627,7 +618,7 @@
      * @param e
      * @return
      */
-    protected Event getJobEvent(Event e, String nodePath) {
+    private Event getJobEvent(Event e, String nodePath) {
         final String eventTopic = (String)e.getProperty(EventUtil.PROPERTY_JOB_TOPIC);
         final Dictionary<String, Object> properties = new Hashtable<String, Object>();
         final String[] propertyNames = e.getPropertyNames();
@@ -737,7 +728,7 @@
      * Load all active jobs from the repository.
      * @throws RepositoryException
      */
-    protected void loadJobs() {
+    private void loadJobs() {
         try {
             final QueryManager qManager = this.backgroundSession.getWorkspace().getQueryManager();
             final StringBuffer buffer = new StringBuffer("/jcr:root");

Modified: incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/TimedJobHandler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/TimedJobHandler.java?rev=688641&r1=688640&r2=688641&view=diff
==============================================================================
--- incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/TimedJobHandler.java (original)
+++ incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/impl/TimedJobHandler.java Mon Aug 25 00:56:24 2008
@@ -549,13 +549,6 @@
     }
 
     /**
-     * @see org.apache.sling.event.impl.AbstractRepositoryEventHandler#getContainerNodeType()
-     */
-    protected String getContainerNodeType() {
-        return EventHelper.TIMED_EVENTS_NODE_TYPE;
-    }
-
-    /**
      * @see org.apache.sling.event.impl.AbstractRepositoryEventHandler#getEventNodeType()
      */
     protected String getEventNodeType() {

Modified: incubator/sling/trunk/extensions/event/src/main/resources/SLING-INF/nodetypes/event.cnd
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/event/src/main/resources/SLING-INF/nodetypes/event.cnd?rev=688641&r1=688640&r2=688641&view=diff
==============================================================================
--- incubator/sling/trunk/extensions/event/src/main/resources/SLING-INF/nodetypes/event.cnd (original)
+++ incubator/sling/trunk/extensions/event/src/main/resources/SLING-INF/nodetypes/event.cnd Mon Aug 25 00:56:24 2008
@@ -21,15 +21,6 @@
 <nt='http://www.jcp.org/jcr/nt/1.0'>
 <mix='http://www.jcp.org/jcr/mix/1.0'>
 
-[slingevent:Events] > nt:folder, mix:lockable
-  + * (slingevent:Event)
-
-[slingevent:TimedEvents] > nt:folder, mix:lockable
- + * (slingevent:TimedEvent)
-
-[slingevent:Jobs] > nt:folder, mix:lockable
-  + * (slingevent:Job)
-  
 [slingevent:Event] > nt:unstructured
   - slingevent:topic (string)
   - slingevent:application (string)

Modified: incubator/sling/trunk/extensions/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java?rev=688641&r1=688640&r2=688641&view=diff
==============================================================================
--- incubator/sling/trunk/extensions/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java (original)
+++ incubator/sling/trunk/extensions/event/src/test/java/org/apache/sling/event/impl/AbstractRepositoryEventHandlerTest.java Mon Aug 25 00:56:24 2008
@@ -70,6 +70,7 @@
         final SlingRepository repository = RepositoryUtil.getRepository();
         session = repository.loginAdministrative(repository.getDefaultWorkspace());
         assertTrue(RepositoryUtil.registerNodeType(session, DistributingEventHandler.class.getResourceAsStream("/SLING-INF/nodetypes/event.cnd")));
+        assertTrue(RepositoryUtil.registerNodeType(session, DistributingEventHandler.class.getResourceAsStream("/SLING-INF/nodetypes/resource.cnd")));
     }
 
     @org.junit.AfterClass public static void shutdownRepository() throws Exception {