You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2010/07/10 05:53:36 UTC

svn commit: r962752 - in /openejb/branches/openejb-3.1.x: ./ container/openejb-core/src/main/java/org/apache/openejb/util/ container/openejb-core/src/test/java/org/apache/openejb/config/ container/openejb-core/src/test/java/org/apache/openejb/util/ exa...

Author: dblevins
Date: Sat Jul 10 03:53:36 2010
New Revision: 962752

URL: http://svn.apache.org/viewvc?rev=962752&view=rev
Log:
svn merge -r 962749:962750 https://svn.apache.org/repos/asf/openejb/trunk/openejb3

http://svn.apache.org/viewvc?rev=962750&view=rev
------------------------------------------------------------------------
r962750 | dblevins | 2010-07-09 20:47:06 -0700 (Fri, 09 Jul 2010) | 4 lines

Patch from Ivan Xu, OPENEJB-1309: Make the DependOn sort algorithm stable
Relates to OPENEJB-1296: Allow @DependsOn usage by all session bean types
Thanks again, Ivan!

------------------------------------------------------------------------

Modified:
    openejb/branches/openejb-3.1.x/   (props changed)
    openejb/branches/openejb-3.1.x/container/openejb-core/src/main/java/org/apache/openejb/util/References.java
    openejb/branches/openejb-3.1.x/container/openejb-core/src/test/java/org/apache/openejb/config/BusinessInterfacesTest.java   (props changed)
    openejb/branches/openejb-3.1.x/container/openejb-core/src/test/java/org/apache/openejb/util/ReferencesTest.java
    openejb/branches/openejb-3.1.x/examples/alternate-descriptors/src/main/resources/META-INF/test.ejb-jar.xml   (props changed)

Propchange: openejb/branches/openejb-3.1.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Sat Jul 10 03:53:36 2010
@@ -1,2 +1,2 @@
 /openejb/branches/openejb-3.1.1:779593
-/openejb/trunk/openejb3:943472,943862,943965,944757,945989,946399,946485,946489,946705,946792,946805,946814,946861,946863-946864,947010,947017,947042,948022,948241,948243,948548,949014,949233,950391,950801,951611,953191,953556,955104,955496,962382
+/openejb/trunk/openejb3:943472,943862,943965,944757,945989,946399,946485,946489,946705,946792,946805,946814,946861,946863-946864,947010,947017,947042,948022,948241,948243,948548,949014,949233,950391,950801,951611,953191,953556,955104,955496,962382,962750

Modified: openejb/branches/openejb-3.1.x/container/openejb-core/src/main/java/org/apache/openejb/util/References.java
URL: http://svn.apache.org/viewvc/openejb/branches/openejb-3.1.x/container/openejb-core/src/main/java/org/apache/openejb/util/References.java?rev=962752&r1=962751&r2=962752&view=diff
==============================================================================
--- openejb/branches/openejb-3.1.x/container/openejb-core/src/main/java/org/apache/openejb/util/References.java (original)
+++ openejb/branches/openejb-3.1.x/container/openejb-core/src/main/java/org/apache/openejb/util/References.java Sat Jul 10 03:53:36 2010
@@ -16,16 +16,16 @@
  */
 package org.apache.openejb.util;
 
-import javax.ejb.Stateless;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.Iterator;
 
 /**
  * @version $Rev$ $Date$
@@ -38,18 +38,8 @@ public class References {
         Set<String> getReferences(T t);
     }
 
-    /**
-     * Items will be sorted from least amount of references to most amount of references.
-     * Original order will be maintained only within those bounds.
-     *
-     * If one or more circular references are detected a CircularReferenceException will
-     * be thrown containing lists of all circuits sorted from lowest to highest.
-     *
-     * @param objects
-     * @param visitor
-     * @return
-     */
     public static <T> List<T> sort(List<T> objects, Visitor<T> visitor) {
+
         final Map<String, Node> nodes = new LinkedHashMap<String, Node>();
 
         // Create nodes
@@ -65,42 +55,23 @@ public class References {
                 Node ref = nodes.get(name);
                 if (ref == null) throw new IllegalArgumentException("No such object in list: "+name);
                 node.references.add(ref);
-                ref.refernceCount++;
+                node.initialReferences.add(ref);
             }
 
         }
 
-        // find all initial leaf nodes (and islands)
-        List<Node> sortedNodes = new ArrayList<Node>(nodes.size());
-        LinkedList<Node> leafNodes = new LinkedList<Node>();
-        for (Node n : nodes.values()) {
-            if (n.refernceCount == 0) {
-                // if the node is totally isolated (no in or out refs),
-                // move it directly to the finished list, so they are first
-                if (n.references.size() == 0) {
-                    sortedNodes.add(n);
-                } else {
-                    leafNodes.add(n);
-                }
-            }
-        }
-
-        // pluck the leaves until there are no leaves remaining
-        while (!leafNodes.isEmpty()) {
-            Node node = leafNodes.removeFirst();
-            sortedNodes.add(node);
-            for (Node ref : node.references) {
-                ref.refernceCount--;
-                if (ref.refernceCount == 0) {
-                    leafNodes.add(ref);
-                }
+        boolean circuitFounded = false;
+        for (Node node : nodes.values()) {
+            Set<Node> visitedNodes = new HashSet<Node>();
+            if (!normalizeNodeReferences(node, node, visitedNodes)) {
+                circuitFounded = true;
+                break;
             }
+            node.references.addAll(visitedNodes);
         }
 
-        // There are no more leaves so if there are there still
-        // unprocessed nodes in the graph, we have one or more curcuits
-        if (sortedNodes.size() != nodes.size()) {
-
+        //detect circus
+        if (circuitFounded) {
             Set<Circuit> circuits = new LinkedHashSet<Circuit>();
 
             for (Node node : nodes.values()) {
@@ -112,15 +83,61 @@ public class References {
 
             List<List> all = new ArrayList<List>();
             for (Circuit circuit : list) {
-                all.add((List) unwrap(circuit.nodes));
+                all.add(unwrap(circuit.nodes));
             }
 
             throw new CircularReferencesException(all);
         }
 
-        List<T> sortedObjects = unwrap(sortedNodes);
-        Collections.reverse(sortedObjects);
-        return sortedObjects;
+        LinkedList<Node> sortedNodes = new LinkedList<Node>();
+        sortedNodes.addAll(nodes.values());
+
+        for (Node node : nodes.values()) {
+            for (Node reference : node.references) {
+                final int i = sortedNodes.indexOf(node);
+                swap(node.name, reference.name, sortedNodes);
+            }
+        }
+        return unwrap(sortedNodes);
+    }
+
+    private static boolean normalizeNodeReferences(Node rootNode, Node node, Set<Node> referenceNodes) {
+        if (node.references.contains(rootNode)) {
+            return false;
+        }
+        for (Node reference : node.references) {
+            if (!referenceNodes.add(reference)) {
+                //this reference node has been visited in the past
+                continue;
+            }
+            if (!normalizeNodeReferences(rootNode, reference, referenceNodes)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private static void swap(String shouldAfterNodeName, String shouldBeforeNodeName, LinkedList<Node> nodes) {
+        int shouldAfterIndex = -1;
+        int shouldBeforeIndex = -1;
+        int index = 0;
+        for (Node node : nodes) {
+            if (shouldAfterIndex == -1 || shouldBeforeIndex == -1) {
+                if (shouldAfterNodeName.equals(node.name)) {
+                    shouldAfterIndex = index;
+                } else if (shouldBeforeNodeName.equals(node.name)) {
+                    shouldBeforeIndex = index;
+                }
+                index++;
+            } else {
+                break;
+            }
+        }
+        System.out.println(shouldAfterNodeName + "=" + shouldAfterIndex + "\t" + shouldBeforeNodeName + "=" + shouldBeforeIndex);
+        if (shouldAfterIndex < shouldBeforeIndex) {
+            Node node = nodes.remove(shouldAfterIndex);
+            nodes.add(shouldBeforeIndex, node);
+        }
     }
 
     private static <T> List<T> unwrap(List<Node> nodes) {
@@ -149,7 +166,7 @@ public class References {
 
         stack.push(node);
 
-        for (Node reference : node.references) {
+        for (Node reference : node.initialReferences) {
             findCircuits(circuits, reference, stack);
         }
 
@@ -159,8 +176,9 @@ public class References {
     private static class Node implements Comparable<Node> {
         private final String name;
         private Object object;
-        private final List<Node> references = new ArrayList<Node>();
-        private int refernceCount;
+        private final List<Node> initialReferences = new ArrayList<Node>();
+        private final Set<Node> references = new HashSet<Node>();
+        //private int refernceCount;
 
         public Node(String name, Object object) {
             this.name = name;
@@ -185,7 +203,8 @@ public class References {
         }
 
         public String toString() {
-            return "Node("+ name +" : "+ Join.join(",", unwrap(references))+")";
+            return name;
+            //return "Node("+ name +" : "+ Join.join(",", unwrap(initialReferences))+")";
         }
     }
 

Propchange: openejb/branches/openejb-3.1.x/container/openejb-core/src/test/java/org/apache/openejb/config/BusinessInterfacesTest.java
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Sat Jul 10 03:53:36 2010
@@ -1,2 +1,2 @@
 /openejb/branches/openejb-3.1.1/container/openejb-core/src/test/java/org/apache/openejb/config/UberInterfaceTest.java:779593
-/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/BusinessInterfacesTest.java:943472,943862,943965,944757,945989,946399,946485,946489,946705,946792,946805,946814,946861,946863-946864,947010,947017,947042,948022,948241,948548,949014,949233,950391,950801,951611,953191,953556,955104,955496,962382
+/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/BusinessInterfacesTest.java:943472,943862,943965,944757,945989,946399,946485,946489,946705,946792,946805,946814,946861,946863-946864,947010,947017,947042,948022,948241,948548,949014,949233,950391,950801,951611,953191,953556,955104,955496,962382,962750

Modified: openejb/branches/openejb-3.1.x/container/openejb-core/src/test/java/org/apache/openejb/util/ReferencesTest.java
URL: http://svn.apache.org/viewvc/openejb/branches/openejb-3.1.x/container/openejb-core/src/test/java/org/apache/openejb/util/ReferencesTest.java?rev=962752&r1=962751&r2=962752&view=diff
==============================================================================
--- openejb/branches/openejb-3.1.x/container/openejb-core/src/test/java/org/apache/openejb/util/ReferencesTest.java (original)
+++ openejb/branches/openejb-3.1.x/container/openejb-core/src/test/java/org/apache/openejb/util/ReferencesTest.java Sat Jul 10 03:53:36 2010
@@ -17,15 +17,14 @@
 package org.apache.openejb.util;
 
 import static org.apache.openejb.util.References.sort;
-import junit.framework.TestCase;
-
-import java.util.LinkedHashSet;
-import java.util.Set;
 import java.util.ArrayList;
-import java.util.List;
 import java.util.Arrays;
-import java.util.ListIterator;
 import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+
+import junit.framework.TestCase;
 
 /**
  * @version $Rev$ $Date$
@@ -75,7 +74,8 @@ public class ReferencesTest extends Test
 
         List<Bean> actual = sort(beans, visitor);
 
-        assertEquals(expected(d, a, e, b, f, c), actual);
+        //assertEquals(expected(d, a, e, b, f, c), actual);
+        assertEquals(expected(a, b, c, d, e, f), actual);
     }
 
     public void testOrder2() {
@@ -103,7 +103,6 @@ public class ReferencesTest extends Test
         Bean a = bean("a", "c");
         Bean b = bean("b", "a");
         Bean c = bean("c", "b");
-
         try {
             sort(beans, visitor);
             fail("Ciruit should have been detected");
@@ -184,7 +183,7 @@ public class ReferencesTest extends Test
 
     // proof that this sort is not a stable sort -- i.e. disrupts order unnecessarily
     // this test needs to pass
-    public void _testNoReferences() {
+    public void testNoReferences() {
 
         beans = new ArrayList<Bean>();
 
@@ -202,7 +201,7 @@ public class ReferencesTest extends Test
 
     // items are already in the right order
     // this should pass but doesn't
-    public void _testOrderedReferences() {
+    public void testOrderedReferences() {
 
         beans = new ArrayList<Bean>();
 
@@ -210,8 +209,8 @@ public class ReferencesTest extends Test
         Bean a = bean("a");
         Bean d = bean("d","a", "b");
         Bean c = bean("c");
-        Bean f = bean("f");
         Bean e = bean("e", "f");
+        Bean f = bean("f");
 
         List<Bean> actual = sort(beans, visitor);
 

Propchange: openejb/branches/openejb-3.1.x/examples/alternate-descriptors/src/main/resources/META-INF/test.ejb-jar.xml
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Sat Jul 10 03:53:36 2010
@@ -1,2 +1,2 @@
 /openejb/branches/openejb-3.1.1/examples/alternate-descriptors/src/main/resources/META-INF/ejb-jar.xml:779593
-/openejb/trunk/openejb3/examples/alternate-descriptors/src/main/resources/META-INF/test.ejb-jar.xml:943472,943862,943965,944757,945989,946399,946485,946489,946705,946792,946805,946814,946861,946863-946864,947010,947017,947042,948022,948241,948243,948548,949014,949233,950391,950801,951611,953191,953556,955104,955496,962382
+/openejb/trunk/openejb3/examples/alternate-descriptors/src/main/resources/META-INF/test.ejb-jar.xml:943472,943862,943965,944757,945989,946399,946485,946489,946705,946792,946805,946814,946861,946863-946864,947010,947017,947042,948022,948241,948243,948548,949014,949233,950391,950801,951611,953191,953556,955104,955496,962382,962750