You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@river.apache.org by pe...@apache.org on 2011/09/21 17:35:47 UTC

svn commit: r1173721 - in /river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river: api/isolate/ api/isolate/RemoteExecutor.java impl/security/policy/util/Segment.java

Author: peter_firmstone
Date: Wed Sep 21 15:35:47 2011
New Revision: 1173721

URL: http://svn.apache.org/viewvc?rev=1173721&view=rev
Log:
Reference Collection Utilities

Added:
    river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/api/isolate/
    river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/api/isolate/RemoteExecutor.java   (with props)
    river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/security/policy/util/Segment.java   (with props)

Added: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/api/isolate/RemoteExecutor.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/api/isolate/RemoteExecutor.java?rev=1173721&view=auto
==============================================================================
--- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/api/isolate/RemoteExecutor.java (added)
+++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/api/isolate/RemoteExecutor.java Wed Sep 21 15:35:47 2011
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.river.api.isolate;
+
+import java.io.IOException;
+import java.rmi.Remote;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+/**
+ *
+ * @author Peter Firmstone
+ */
+public interface RemoteExecutor extends Remote {
+
+    public void shutdown() throws IOException;
+
+    /**
+     * 
+     * @return List of Runnable tasks that haven't been run.
+     * @throws IOException
+     */
+    public List shutdownNow() throws IOException;
+
+    public boolean isShutdown() throws IOException;
+
+    public boolean isTerminated() throws IOException;
+
+    public boolean awaitTermination(long timeout, TimeUnit unit) 
+            throws InterruptedException, IOException;
+
+    public Future submit(Callable task) throws IOException;
+
+    public Future submit(Runnable task, Object result) throws IOException;
+
+    public Future submit(Runnable task) throws IOException;
+
+    /**
+     * 
+     * @param tasks Collection of Callable tasks.
+     * @return List<Future> 
+     * @throws InterruptedException
+     * @throws IOException
+     */
+    public List invokeAll(Collection tasks) 
+            throws InterruptedException, IOException;
+
+    public List invokeAll(Collection tasks,
+            long timeout, TimeUnit unit) throws InterruptedException, IOException;
+
+    public Object invokeAny(Collection tasks) 
+            throws InterruptedException, ExecutionException, IOException;
+
+    public Object invokeAny(Collection tasks, long timeout,
+    TimeUnit unit) throws InterruptedException, ExecutionException, 
+    TimeoutException, IOException;
+
+    public void execute(Runnable command) throws IOException;
+    
+}

Propchange: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/api/isolate/RemoteExecutor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/security/policy/util/Segment.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/security/policy/util/Segment.java?rev=1173721&view=auto
==============================================================================
--- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/security/policy/util/Segment.java (added)
+++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/security/policy/util/Segment.java Wed Sep 21 15:35:47 2011
@@ -0,0 +1,181 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.river.impl.security.policy.util;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Properties;
+import org.apache.river.impl.security.policy.util.PolicyUtils.ExpansionFailedException;
+
+/**
+ * Segments form a chain of String parts which represent a framework for
+ * building complex String's from Substrings.  Segments are chained at 
+ * creation time, the last Segment in a chain communicates back up the chain
+ * using the #next() method call.
+ * 
+ * The first segment in a chain must be checked each string build creation loop,
+ * until it is complete, at which point the loop should be terminated by the caller.  
+ * The last segment in a chain must have the next() method
+ * called at the end of the loop, this will propagate back up the chain, ensuring
+ * every possible unique string combination is produced, before the status of
+ * the first link becomes complete.
+ * 
+ * Segments 
+ * @author Peter Firmstone.
+ */
+public class Segment implements Comparable {
+    private Segment previous;
+    private String [] properties;
+    private String original;
+    private int length = 0;
+    int counter = 0;
+    Status stat;
+    public Segment(String s, Segment preceed){
+        previous = preceed;
+        stat = Status.STRING;
+        properties = null;
+    }
+
+    @Override
+    public String toString(){
+        if (stat.equals(Status.STRING)) return original;
+        return properties[counter];
+    }
+
+    public Status status(){
+        return stat;
+    }
+
+    public void next(){
+        if ( stat.equals(Status.COMPLETE)){
+            counter = 0;
+            stat = Status.MORE;
+            if (previous != null ){
+                previous.next();
+            }
+        } else if ( stat.equals(Status.MORE)){
+            counter ++;
+            if (counter == length){
+                stat = Status.COMPLETE;
+            }
+        } else if ( stat.equals(Status.STRING)&& previous != null){
+            previous.next();
+        }
+    }
+    
+    private int sequenceNumber(){
+        int sequence = 1;
+        if (previous != null){
+            sequence = sequence + previous.sequenceNumber();
+        }
+        return sequence;
+    }
+
+    public int compareTo(Object o) {
+        if ( !(o instanceof Segment)) 
+            throw new ClassCastException(o.toString() 
+                    + " not an instance of Segment");
+        Segment that = (Segment) o;
+        if (this.sequenceNumber() == that.sequenceNumber()) return 0;
+        if (this.sequenceNumber() < that.sequenceNumber()) return -1;
+        return 1; // It must be greater than that.
+    }
+    
+    /**
+     * Segments the current String by find Properties between the START_MARK and
+     * END_MARK and replacing them with their values, splitting them into separate
+     * Strings (that remain encapsulated in the Segment) if regex is non null.
+     * @param START_MART
+     * @param END_MARK
+     * @param splitter
+     * @param p
+     * @return 
+     */
+    public Collection<Segment> divideAndReplace(String START_MARK, String END_MARK,
+            String regex, Properties p) throws ExpansionFailedException{
+        Collection<Segment> result = new ArrayList<Segment>();
+        String orig = original; //original reference is replaced
+        Segment last = previous; //previous reference too.
+        ArrayList<String> segments = new ArrayList<String>();
+        final int START_OFFSET = START_MARK.length();
+        final int END_OFFSET = END_MARK.length();
+        int start = orig.indexOf(START_MARK);
+        int end = 0;
+        int beginning = 0;
+        while (start >= 0) {
+            // Get the segment preceeding the key, or between keys.
+            Segment seg = new Segment(orig.substring(beginning, start), last);
+            result.add(seg);
+            last = seg;
+            end = orig.indexOf(END_MARK, start);
+            if (end >= 0) {
+                String key = orig.substring(start + START_OFFSET, end);
+                String value = p.getProperty(key);
+                if (value != null) {
+                    seg = new Segment(value, last);
+                    if (regex != null) {
+                        seg.split(regex);
+                        seg.status(Status.MORE);
+                    }
+                    result.add(seg);
+                    last = seg;
+                } else {
+                    throw new ExpansionFailedException(Messages.getString("security.14F", key)); //$NON-NLS-1$
+                }
+            }
+            beginning = end;
+            start = orig.indexOf(START_MARK, start);
+        }
+        // Now there could be a trailing string.
+        if (end < orig.length()){
+            // Use this to represent it.
+            previous = last;
+            original = orig.substring(end);
+        } else if ( end == orig.length() && last != null){
+            // Replace the last Segment in the list with this, after
+            // making it equal.  The reason for doing so is that a downstream
+            // Segment may reference this.
+            result.remove(last);
+            previous = last.previous;
+            original = last.original;
+            properties = last.properties;
+            length = last.length;
+            stat = last.stat;
+            
+        }
+        result.add(this); //We must always keep this since other Segments may reference it.
+        return result;
+    }
+    
+    /**
+     * Same as String.split(String regex) except that it stores the result
+     * internally.
+     */
+    private void split(String regex){
+        properties = original.split(regex);
+    }
+    
+    private void status(Status status){
+        stat = status;
+    }
+    
+    public enum Status {
+        STRING, MORE, COMPLETE
+    }
+}
\ No newline at end of file

Propchange: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/security/policy/util/Segment.java
------------------------------------------------------------------------------
    svn:eol-style = native