You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ace.apache.org by ja...@apache.org on 2013/10/23 11:32:30 UTC

svn commit: r1534965 - in /ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo: execute/ExecuteCommands.java queue/QueueCommands.java

Author: jawi
Date: Wed Oct 23 09:32:29 2013
New Revision: 1534965

URL: http://svn.apache.org/r1534965
Log:
ACE-376 - add support for recalculation jobs:

- extended the queue to support maps as definition of a
  script, allowing any sort of properties to be defined
  along with a script. This way, we can retrieve scripts
  that match certain criteria (and support reordering of
  script execution);
- extended the execute command to accept multiple script
  definitions.


Modified:
    ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/execute/ExecuteCommands.java
    ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/queue/QueueCommands.java

Modified: ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/execute/ExecuteCommands.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/execute/ExecuteCommands.java?rev=1534965&r1=1534964&r2=1534965&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/execute/ExecuteCommands.java (original)
+++ ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/execute/ExecuteCommands.java Wed Oct 23 09:32:29 2013
@@ -16,9 +16,10 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-
 package org.apache.ace.gogo.execute;
 
+import java.util.Map;
+
 import org.apache.felix.service.command.CommandProcessor;
 import org.apache.felix.service.command.CommandSession;
 import org.apache.felix.service.command.Descriptor;
@@ -33,14 +34,28 @@ public class ExecuteCommands {
     // Injected by Felix DM...
     private volatile CommandProcessor m_processor;
 
-    @Descriptor("executes a given Gogo script")
-    public void execute(CommandSession session, String script) throws Exception {
-        CommandSession newSession = m_processor.createSession(session.getKeyboard(), session.getConsole(), System.err);
-        try {
-            newSession.execute(script);
+    @Descriptor("executes a script definition")
+    public void execute(CommandSession session, @Descriptor("the script definition(s) to execute, which consists of a map with at least a 'script' key") Map<String, String>... defs) throws Exception {
+        if (defs == null || defs.length == 0) {
+            throw new IllegalArgumentException("Need at least one script definition!");
         }
-        finally {
-            newSession.close();
+        // Check whether all definitions are valid...
+        for (Map<String, String> def : defs) {
+            String script = def.get("script");
+            if (script == null || "".equals(script.trim())) {
+                throw new IllegalArgumentException("Script definition *must* define at least a 'script' property!");
+            }
+        }
+
+        // Execute all scripts, in order...
+        for (Map<String, String> def : defs) {
+            CommandSession newSession = m_processor.createSession(session.getKeyboard(), session.getConsole(), System.err);
+            try {
+                newSession.execute(def.get("script"));
+            }
+            finally {
+                newSession.close();
+            }
         }
     }
 }

Modified: ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/queue/QueueCommands.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/queue/QueueCommands.java?rev=1534965&r1=1534964&r2=1534965&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/queue/QueueCommands.java (original)
+++ ace/trunk/org.apache.ace.gogo/src/org/apache/ace/gogo/queue/QueueCommands.java Wed Oct 23 09:32:29 2013
@@ -18,30 +18,101 @@
  */
 package org.apache.ace.gogo.queue;
 
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.*;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.LinkedBlockingQueue;
 
 import org.apache.felix.service.command.Descriptor;
+import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkUtil;
 
 /**
  * Provides the commands for putting and removing scripts from the queue.
  */
 public class QueueCommands {
     public final static String SCOPE = "queue";
-    public final static String[] FUNCTIONS = new String[] { "put", "get" };
+    public final static String[] FUNCTIONS = new String[] { "put", "get", "contains" };
 
-    private final BlockingQueue<String> m_queue = new LinkedBlockingQueue<String>();
+    private final BlockingQueue<Dictionary<String, String>> m_queue = new LinkedBlockingQueue<Dictionary<String, String>>();
 
-    @Descriptor("puts a new script on the queue")
-    public void put(String script) throws Exception {
-        if (script == null) {
-            throw new IllegalArgumentException("Script cannot be null!");
+    @Descriptor("puts a new script definition on the queue")
+    public void put(@Descriptor("the script definition to put onto the queue, which consists of a map with at least a 'script' key") Map<String, String> def) throws Exception {
+        if (def == null) {
+            throw new IllegalArgumentException("Script definition cannot be null!");
         }
-        m_queue.put(script);
+        String script = def.get("script");
+        if (script == null || "".equals(script.trim())) {
+            throw new IllegalArgumentException("Script definition *must* define at least a 'script' property!");
+        }
+        m_queue.put(toDictionary(def));
+    }
+
+    @Descriptor("returns whether anything is present on the queue")
+    public boolean contains() throws Exception {
+        return contains(null);
+    }
+
+    @Descriptor("returns whether anything is present on the queue matchin a given filter definition")
+    public boolean contains(@Descriptor("Represents an OSGi-filter to search with") String filter) throws Exception {
+        List<Dictionary<String, String>> copy = new ArrayList<Dictionary<String, String>>(m_queue);
+        if (filter == null || "".equals(filter.trim())) {
+            return !copy.isEmpty();
+        }
+
+        Filter f = FrameworkUtil.createFilter(filter);
+        for (Dictionary<String, String> entry : copy) {
+            if (f.match(entry)) {
+                return true;
+            }
+        }
+
+        return false;
     }
 
-    @Descriptor("returns the first script from the queue, if available")
-    public String get() throws Exception {
-        return m_queue.poll();
+    @Descriptor("returns the first script definition from the queue, if available")
+    public Map<String, String> get() throws Exception {
+        Dictionary<String, String> dict = m_queue.poll();
+        return (dict == null) ? null : toMap(dict);
+    }
+
+    @Descriptor("returns the script definition from the queue matching a given filter, if available")
+    public Map<String, String>[] get(@Descriptor("Represents an OSGi-filter to match against the script definitions") String filter) throws Exception {
+        List<Dictionary<String, String>> copy = new ArrayList<Dictionary<String, String>>(m_queue);
+
+        List<Map<String, String>> result = new ArrayList<Map<String, String>>();
+
+        Filter f = FrameworkUtil.createFilter(filter);
+        for (Dictionary<String, String> entry : copy) {
+            if (f.match(entry)) {
+                result.add(toMap(entry));
+                m_queue.remove(entry);
+            }
+        }
+
+        return result.toArray(new Map[result.size()]);
+    }
+
+    private static Dictionary<String, String> toDictionary(Map<String, String> map) {
+        Dictionary<String, String> result = new Hashtable<String, String>();
+        for (Map.Entry<String, String> entry : map.entrySet()) {
+            result.put(entry.getKey(), entry.getValue());
+        }
+        return result;
+    }
+
+    private static Map<String, String> toMap(Dictionary<String, String> dict) {
+        Map<String, String> result = new HashMap<String, String>();
+        Enumeration<String> keys = dict.keys();
+        while (keys.hasMoreElements()) {
+            String key = keys.nextElement();
+            result.put(key, dict.get(key));
+        }
+        return result;
     }
 }