incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r686186 - in /incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event: JobStatusProvider.java impl/AbstractRepositoryEventHandler.java impl/JobEventHandler.java
Date Fri, 15 Aug 2008 09:54:48 GMT
Author: cziegeler
Date: Fri Aug 15 02:54:47 2008
New Revision: 686186

URL: http://svn.apache.org/viewvc?rev=686186&view=rev
Log:
SLING-616: Improved search: filter properties.

Modified:
    incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/JobStatusProvider.java
    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/JobEventHandler.java

Modified: incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/JobStatusProvider.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/JobStatusProvider.java?rev=686186&r1=686185&r2=686186&view=diff
==============================================================================
--- incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/JobStatusProvider.java
(original)
+++ incubator/sling/trunk/extensions/event/src/main/java/org/apache/sling/event/JobStatusProvider.java
Fri Aug 15 02:54:47 2008
@@ -19,6 +19,7 @@
 package org.apache.sling.event;
 
 import java.util.Collection;
+import java.util.Map;
 
 import org.osgi.service.event.Event;
 
@@ -28,11 +29,17 @@
 public interface JobStatusProvider {
 
     /**
+     * @deprecated Use {@link #getScheduledJobs(String)} instead.
+     */
+    @Deprecated
+    Collection<Event> scheduledJobs(String topic);
+
+    /**
      * Return a list of currently schedulded jobs.
      * @param topic Topic can be used as a filter, if it is non-null, only jobs with this
topic will be returned.
      * @return A non null collection.
      */
-    Collection<Event> scheduledJobs(String topic);
+    Collection<Event> getScheduledJobs(String topic);
 
     /**
      * Return the jobs which are currently in processing. If there are several application
nodes
@@ -41,4 +48,21 @@
      * @return A non null collection.
      */
     Collection<Event> getCurrentJobs(String topic);
+
+    /**
+     * Return a list of currently schedulded jobs.
+     * @param topic Topic can be used as a filter, if it is non-null, only jobs with this
topic will be returned.
+     * @param filterProps An optional map of filter props that act like a template.
+     * @return A non null collection.
+     */
+    Collection<Event> getScheduledJobs(String topic, Map<String, Object> filterProps);
+
+    /**
+     * Return the jobs which are currently in processing. If there are several application
nodes
+     * in the cluster, there could be more than one job in processing
+     * @param topic Topic can be used as a filter, if it is non-null, only jobs with this
topic will be returned.
+     * @param filterProps An optional map of filter props that act like a template.
+     * @return A non null collection.
+     */
+    Collection<Event> getCurrentJobs(String topic, Map<String, Object> filterProps);
 }

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=686186&r1=686185&r2=686186&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
Fri Aug 15 02:54:47 2008
@@ -349,6 +349,43 @@
     }
 
     /**
+     * Return the converted repository property name
+     * @param name The OSGi event property name
+     * @return The converted name or null if not possible.
+     */
+    protected String getNodePropertyName(final String name) {
+        // if name contains a colon, we can't set it as a property
+        if ( name.indexOf(':') != -1 ) {
+            return null;
+        }
+        return ISO9075.encode(name);
+    }
+
+    /**
+     * Return the converted repository property value
+     * @param valueFactory The value factory
+     * @param eventValue The event value
+     * @return The converted value or null if not possible
+     */
+    protected Value getNodePropertyValue(final ValueFactory valueFactory, final Object eventValue)
{
+        final Value val;
+        if (eventValue.getClass().isAssignableFrom(Calendar.class)) {
+            val = valueFactory.createValue((Calendar)eventValue);
+        } else if (eventValue.getClass().isAssignableFrom(Long.class)) {
+            val = valueFactory.createValue((Long)eventValue);
+        } else if (eventValue.getClass().isAssignableFrom(Double.class)) {
+            val = valueFactory.createValue(((Double)eventValue).doubleValue());
+        } else if (eventValue.getClass().isAssignableFrom(Boolean.class)) {
+            val = valueFactory.createValue((Boolean) eventValue);
+        } else if (eventValue instanceof String) {
+            val = valueFactory.createValue((String)eventValue);
+        } else {
+            val = null;
+        }
+        return val;
+    }
+
+    /**
      * Try to set the OSGi event property as a property of the node.
      * @param name
      * @param value
@@ -358,27 +395,14 @@
      */
     private boolean setProperty(String name, Object value, Node node)
     throws RepositoryException {
-        // if name contains a colon, we can't set it as a property
-        if ( name.indexOf(':') != -1 ) {
+        final String propName = this.getNodePropertyName(name);
+        if ( propName == null ) {
             return false;
         }
         final ValueFactory fac = node.getSession().getValueFactory();
-        final Value val;
-        if (value.getClass().isAssignableFrom(Calendar.class)) {
-            val = fac.createValue((Calendar)value);
-        } else if (value.getClass().isAssignableFrom(Long.class)) {
-            val = fac.createValue((Long)value);
-        } else if (value.getClass().isAssignableFrom(Double.class)) {
-            val = fac.createValue(((Double)value).doubleValue());
-        } else if (value.getClass().isAssignableFrom(Boolean.class)) {
-            val = fac.createValue((Boolean) value);
-        } else if (value instanceof String) {
-            val = fac.createValue((String)value);
-        } else {
-            val = null;
-        }
+        final Value val = this.getNodePropertyValue(fac, value);
         if ( val != null ) {
-            node.setProperty(ISO9075.encode(name), val);
+            node.setProperty(propName, val);
             return true;
         }
         return false;

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=686186&r1=686185&r2=686186&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
Fri Aug 15 02:54:47 2008
@@ -26,6 +26,7 @@
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Hashtable;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -36,6 +37,7 @@
 import javax.jcr.NodeIterator;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
+import javax.jcr.Value;
 import javax.jcr.observation.EventIterator;
 import javax.jcr.query.Query;
 import javax.jcr.query.QueryManager;
@@ -933,12 +935,16 @@
     }
 
     /**
-     * Search for active nodes
-     * @param topic
+     * Search for job nodes
+     * @param topic The job topic
+     * @param filterProps optional filter props
+     * @param locked only active jobs?
      * @return
      * @throws RepositoryException
      */
-    protected Collection<Event> queryCurrentJobs(String topic, boolean locked)  {
+    private Collection<Event> queryCurrentJobs(final String topic,
+                                               final Map<String, Object> filterProps,
+                                               final boolean locked)  {
         // we create a new session
         Session s = null;
         final List<Event> jobs = new ArrayList<Event>();
@@ -949,9 +955,11 @@
             buffer.append(this.repositoryPath);
             buffer.append("//element(*, ");
             buffer.append(this.getEventNodeType());
+            buffer.append(") [not(@");
+            buffer.append(EventHelper.NODE_PROPERTY_FINISHED);
             buffer.append(")");
             if ( topic != null ) {
-                buffer.append(" [");
+                buffer.append(" @");
                 buffer.append(EventHelper.NODE_PROPERTY_TOPIC);
                 buffer.append(" = '");
                 buffer.append(topic);
@@ -959,10 +967,32 @@
             }
             if ( locked ) {
                 buffer.append(" and ");
-                buffer.append("jcr:lockOwner");
+                buffer.append("@jcr:lockOwner");
+            }
+            if ( filterProps != null ) {
+                final Iterator<Map.Entry<String, Object>> i = filterProps.entrySet().iterator();
+                while ( i.hasNext() ) {
+                    final Map.Entry<String, Object> current = i.next();
+                    // check prop name first
+                    final String propName = this.getNodePropertyName(current.getKey());
+                    if ( propName != null ) {
+                        // check value
+                        final Value value = this.getNodePropertyValue(s.getValueFactory(),
current.getValue());
+                        if ( value != null ) {
+                            buffer.append(" and @");
+                            buffer.append(propName);
+                            buffer.append(" = '");
+                            buffer.append(current.getValue());
+                            buffer.append("'");
+                        }
+                    }
+                }
             }
             buffer.append("]");
-            final Query q = qManager.createQuery(buffer.toString(), Query.XPATH);
+            final String queryString = buffer.toString();
+            logger.debug("Executing job query {}.", queryString);
+
+            final Query q = qManager.createQuery(queryString, Query.XPATH);
             final NodeIterator iter = q.execute().getNodes();
             while ( iter.hasNext() ) {
                 final Node eventNode = iter.nextNode();
@@ -989,13 +1019,34 @@
      * @see org.apache.sling.event.JobStatusProvider#getCurrentJobs(java.lang.String)
      */
     public Collection<Event> getCurrentJobs(String topic) {
-        return this.queryCurrentJobs(topic, true);
+        return this.getCurrentJobs(topic, null);
     }
 
     /**
-     * @see org.apache.sling.event.JobStatusProvider#scheduledJobs(java.lang.String)
+     * This is deprecated.
      */
     public Collection<Event> scheduledJobs(String topic) {
-        return this.queryCurrentJobs(topic, false);
+        return this.getScheduledJobs(topic);
+    }
+
+    /**
+     * @see org.apache.sling.event.JobStatusProvider#getScheduledJobs(java.lang.String)
+     */
+    public Collection<Event> getScheduledJobs(String topic) {
+        return this.getScheduledJobs(topic, null);
+    }
+
+    /**
+     * @see org.apache.sling.event.JobStatusProvider#getCurrentJobs(java.lang.String, java.util.Map)
+     */
+    public Collection<Event> getCurrentJobs(String topic, Map<String, Object>
filterProps) {
+        return this.queryCurrentJobs(topic, null, true);
+    }
+
+    /**
+     * @see org.apache.sling.event.JobStatusProvider#getScheduledJobs(java.lang.String, java.util.Map)
+     */
+    public Collection<Event> getScheduledJobs(String topic, Map<String, Object>
filterProps) {
+        return this.queryCurrentJobs(topic, null, false);
     }
 }



Mime
View raw message