tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject tomee git commit: TOMEE-1594 fixing resource ordering for dependency management. Thanks Jon to have pointed it out and David for its References reference
Date Wed, 27 May 2015 05:44:43 GMT
Repository: tomee
Updated Branches:
  refs/heads/master 1c5a61370 -> d16c3bed8


TOMEE-1594 fixing resource ordering for dependency management. Thanks Jon to have pointed
it out and David for its References reference


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/d16c3bed
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/d16c3bed
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/d16c3bed

Branch: refs/heads/master
Commit: d16c3bed84d00d04ce624193e4155ff38126936e
Parents: 1c5a613
Author: Romain Manni-Bucau <rmannibucau@apache.org>
Authored: Wed May 27 07:44:29 2015 +0200
Committer: Romain Manni-Bucau <rmannibucau@apache.org>
Committed: Wed May 27 07:44:29 2015 +0200

----------------------------------------------------------------------
 .../org/apache/openejb/config/AutoConfig.java   |   2 +-
 .../openejb/config/ConfigurationFactory.java    | 106 +++++--------------
 .../org/apache/openejb/util/References.java     |   2 +-
 .../classic/ResourceInfoComparatorTest.java     |  81 +++++++++++---
 .../openejb/resource/ResourceSortingTest.java   |  41 +------
 5 files changed, 95 insertions(+), 137 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/d16c3bed/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
b/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
index b634ce5..f828b4c 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
@@ -1008,7 +1008,7 @@ public class AutoConfig implements DynamicDeployer, JndiConstants {
             resourcesMap.put(resourceInfo, resource);
         }
 
-        Collections.sort(resourceInfos, new ConfigurationFactory.ResourceInfoComparator(resourceInfos));
+        ConfigurationFactory.sort(resourceInfos, module.getModuleId() + "/");
         for (final ResourceInfo resourceInfo : resourceInfos) {
             final int originalSize = resourceInfo.aliases.size();
             final String id = installResource(module.getModuleId(), resourceInfo);

http://git-wip-us.apache.org/repos/asf/tomee/blob/d16c3bed/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
b/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
index 5254f72..65a80f9 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
@@ -89,6 +89,7 @@ import org.apache.openejb.util.LogCategory;
 import org.apache.openejb.util.Logger;
 import org.apache.openejb.util.Messages;
 import org.apache.openejb.util.PropertyPlaceHolderHelper;
+import org.apache.openejb.util.References;
 import org.apache.openejb.util.SuperProperties;
 import org.apache.openejb.util.URISupport;
 import org.apache.openejb.util.URLs;
@@ -111,7 +112,6 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Comparator;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedList;
@@ -501,7 +501,7 @@ public class ConfigurationFactory implements OpenEjbConfigurationFactory
{
             final ResourceInfo resourceInfo = configureService(resource, ResourceInfo.class);
             resources.add(resourceInfo);
         }
-        Collections.sort(resources, new ResourceInfoComparator(resources));
+        sort(resources, null);
 
         sys.facilities.resources.addAll(resources);
 
@@ -1652,95 +1652,43 @@ public class ConfigurationFactory implements OpenEjbConfigurationFactory
{
         }
     }
 
-    public static class ResourceInfoComparator implements Comparator<ResourceInfo>
{
-
-        private final List<String> ids;
-        private static final int EQUAL = 0;
-        private static final int GREATER = 1;
-        private static final int LESS = -1;
-
-        public ResourceInfoComparator(final List<ResourceInfo> resources) {
-            ids = new ArrayList<String>();
-            for (final ResourceInfo info : resources) {
-                ids.add(info.id);
-            }
-        }
-
-        @Override
-        public int compare(final ResourceInfo a, final ResourceInfo b) {
-            final String refA = getReference(a);
-            final String refB = getReference(b);
-
-            // both null or the same id
-            if (refA == null && refB == null ||
-                refA != null && refA.equals(refB)) {
-                return EQUAL;
-            }
-
-            // b is referencing a
-            if (a.id.equals(refB)) {
-                return LESS;
-            }
-
-            // a is referencing b
-            if (b.id.equals(refA)) {
-                return GREATER;
-            }
-
-            // a has a ref and b doesn't
-            if (refA != null && refB == null) {
-                return GREATER;
-            }
-
-            // b has a ref and a doesn't
-            if (refA == null) {
-                return LESS;
-            }
-
-            return EQUAL;
-        }
-
-        public int hasReference(final ResourceInfo info) {
-            for (final Object value : info.properties.values()) {
-                if (String.class.isInstance(value)) {
-                    //noinspection SuspiciousMethodCalls
-                    if (ids.contains(value)) {
-                        return GREATER;
+    public static List<ResourceInfo> sort(final List<ResourceInfo> infos, final
String prefix) {
+        final Collection<String> ids = new HashSet<>();
+        return References.sort(infos, new References.Visitor<ResourceInfo>() {
+            @Override // called first so we can rely on it to ensure we have ids full before
any getReferences call
+            public String getName(final ResourceInfo resourceInfo) {
+                final String name = prefix != null && resourceInfo.id.startsWith(prefix)
? resourceInfo.id.substring(prefix.length()) : resourceInfo.id;
+                ids.add(name);
+                return name;
+            }
+
+            @Override
+            public Set<String> getReferences(final ResourceInfo resourceInfo) {
+                final Set<String> refs = new HashSet<>();
+                for (final Object value : resourceInfo.properties.values()) {
+                    if (!String.class.isInstance(value)) {
+                        continue;
                     }
-                }
-            }
-            return EQUAL;
-        }
-
-        public String getReference(final ResourceInfo info) {
-            for (final Object value : info.properties.values()) {
-                if (String.class.isInstance(value)) {
                     final String string = String.class.cast(value).trim();
                     if (string.isEmpty()) {
                         continue;
                     }
-
-                    if (ids.contains(string)) {
-                        return (String) value;
-                    }
-
                     if (string.contains(",")) { // multiple references
                         for (final String s : string.split(",")) {
-                            final String trimmed = s.trim();
-                            if (ids.contains(trimmed)) {
-                                return s;
+                            final String trim = s.trim();
+                            if (ids.contains(trim)) {
+                                refs.add(trim);
                             }
                         }
-                    }
-
-                    for (final String s : ids) {
-                        if (s.endsWith("/" + string)) { // submodule resources
-                            return s;
+                    } else {
+                        final String trim = String.valueOf(value).trim();
+                        if (ids.contains(trim)) {
+                            refs.add(trim);
                         }
                     }
                 }
+                return refs;
             }
-            return null;
-        }
+        });
     }
 }

http://git-wip-us.apache.org/repos/asf/tomee/blob/d16c3bed/container/openejb-core/src/main/java/org/apache/openejb/util/References.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/util/References.java
b/container/openejb-core/src/main/java/org/apache/openejb/util/References.java
index 52fe41e..6e6be22 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/util/References.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/util/References.java
@@ -67,7 +67,7 @@ public class References {
         }
         boolean circuitFounded = false;
         for (final Node node : nodes.values()) {
-            final Set<Node> visitedNodes = new HashSet<Node>();
+            final Set<Node> visitedNodes = new HashSet<>();
             if (!normalizeNodeReferences(node, node, visitedNodes)) {
                 circuitFounded = true;
                 break;

http://git-wip-us.apache.org/repos/asf/tomee/blob/d16c3bed/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/ResourceInfoComparatorTest.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/ResourceInfoComparatorTest.java
b/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/ResourceInfoComparatorTest.java
index 3c89fc2..0fa95b8 100644
--- a/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/ResourceInfoComparatorTest.java
+++ b/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/ResourceInfoComparatorTest.java
@@ -18,9 +18,10 @@ package org.apache.openejb.assembler.classic;
 
 import junit.framework.TestCase;
 import org.apache.openejb.config.ConfigurationFactory;
+import org.apache.openejb.util.Join;
+import org.junit.Test;
 
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
 import java.util.Properties;
 
@@ -51,12 +52,19 @@ public class ResourceInfoComparatorTest extends TestCase {
         resources.get(3).properties = new Properties();
         resources.get(3).properties.put("foo", "Green");
 
-        Collections.sort(resources, new ConfigurationFactory.ResourceInfoComparator(resources));
+        final List<ResourceInfo> sorted = ConfigurationFactory.sort(resources, null);
 
-        assertEquals("Green", resources.get(0).id);
-        assertEquals("Blue", resources.get(1).id);
-        assertEquals("Red", resources.get(2).id);
-        assertEquals("Yellow", resources.get(3).id);
+        final String error = Join.join(",", new Join.NameCallback<ResourceInfo>() {
+            @Override
+            public String getName(ResourceInfo object) {
+                return object.id;
+            }
+        }, resources);
+
+        assertEquals(error, "Green", sorted.get(0).id);
+        assertEquals(error, "Blue", sorted.get(1).id);
+        assertEquals(error, "Red", sorted.get(2).id);
+        assertEquals(error, "Yellow", sorted.get(3).id);
 
     }
 
@@ -81,12 +89,12 @@ public class ResourceInfoComparatorTest extends TestCase {
         resources.get(3).properties = new Properties();
         resources.get(3).properties.put("ResourceAdapter", "My JMS Resource Adapter");
 
-        Collections.sort(resources, new ConfigurationFactory.ResourceInfoComparator(resources));
+        final List<ResourceInfo> sorted = ConfigurationFactory.sort(resources, null);
 
-        assertEquals("My DataSource", resources.get(0).id);
-        assertEquals("My Unmanaged DataSource", resources.get(1).id);
-        assertEquals("My JMS Resource Adapter", resources.get(2).id);
-        assertEquals("My JMS Connection Factory", resources.get(3).id);
+        assertEquals("My DataSource", sorted.get(0).id);
+        assertEquals("My Unmanaged DataSource", sorted.get(1).id);
+        assertEquals("My JMS Resource Adapter", sorted.get(2).id);
+        assertEquals("My JMS Connection Factory", sorted.get(3).id);
 
     }
 
@@ -111,14 +119,55 @@ public class ResourceInfoComparatorTest extends TestCase {
         resources.get(3).id = "My DataSource";
         resources.get(3).properties = new Properties();
 
+        final List<ResourceInfo> sorted = ConfigurationFactory.sort(resources, null);
 
-        Collections.sort(resources, new ConfigurationFactory.ResourceInfoComparator(resources));
+        final String error = Join.join(",", new Join.NameCallback<ResourceInfo>() {
+            @Override
+            public String getName(ResourceInfo object) {
+                return object.id;
+            }
+        }, sorted);
 
         // since unmanaged is first in the "file" it should be first here
-        assertEquals("My Unmanaged DataSource", resources.get(0).id);
-        assertEquals("My DataSource", resources.get(1).id);
-        assertEquals("My JMS Resource Adapter", resources.get(2).id);
-        assertEquals("My JMS Connection Factory", resources.get(3).id);
+        assertEquals(error, "My Unmanaged DataSource", sorted.get(0).id);
+        assertEquals(error, "My JMS Resource Adapter", sorted.get(1).id);
+        assertEquals(error, "My JMS Connection Factory", sorted.get(2).id);
+        assertEquals(error, "My DataSource", sorted.get(3).id);
+    }
+
+    @Test
+    public void testRealWorld4() throws Exception {
+        final List<ResourceInfo> resources = new ArrayList<>();
+
+        resources.add(new ResourceInfo());
+        resources.get(0).id = "My JMS Connection Factory";
+        resources.get(0).properties = new Properties();
+        resources.get(0).properties.put("ResourceAdapter", "My JMS Resource Adapter");
+
+        resources.add(new ResourceInfo());
+        resources.get(1).id = "My Unmanaged DataSource";
+        resources.get(1).properties = new Properties();
+
+        resources.add(new ResourceInfo());
+        resources.get(2).id = "Test Resource";
+        resources.get(2).properties = new Properties();
+        resources.get(2).properties.put("ResourceAdapter", "My JMS Connection Factory");
+
+        resources.add(new ResourceInfo());
+        resources.get(3).id = "My DataSource";
+        resources.get(3).properties = new Properties();
+
+        resources.add(new ResourceInfo());
+        resources.get(4).id = "My JMS Resource Adapter";
+        resources.get(4).properties = new Properties();
+        resources.get(4).properties.put("DataSource", "My Unmanaged DataSource");
+
+        final List<ResourceInfo> sorted = ConfigurationFactory.sort(resources, null);
 
+        assertEquals("My Unmanaged DataSource", sorted.get(0).id);
+        assertEquals("My DataSource", sorted.get(1).id);
+        assertEquals("My JMS Resource Adapter", sorted.get(2).id);
+        assertEquals("My JMS Connection Factory", sorted.get(3).id);
+        assertEquals("Test Resource", sorted.get(4).id);
     }
 }

http://git-wip-us.apache.org/repos/asf/tomee/blob/d16c3bed/container/openejb-core/src/test/java/org/apache/openejb/resource/ResourceSortingTest.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/test/java/org/apache/openejb/resource/ResourceSortingTest.java
b/container/openejb-core/src/test/java/org/apache/openejb/resource/ResourceSortingTest.java
index 8da1009..02537b1 100644
--- a/container/openejb-core/src/test/java/org/apache/openejb/resource/ResourceSortingTest.java
+++ b/container/openejb-core/src/test/java/org/apache/openejb/resource/ResourceSortingTest.java
@@ -16,8 +16,6 @@
  */
 package org.apache.openejb.resource;
 
-import org.apache.openejb.assembler.classic.ResourceInfo;
-import org.apache.openejb.config.ConfigurationFactory;
 import org.apache.openejb.config.sys.JaxbOpenejb;
 import org.apache.openejb.config.sys.Resources;
 import org.apache.openejb.junit.ApplicationComposer;
@@ -29,10 +27,8 @@ import org.xml.sax.SAXException;
 
 import java.io.ByteArrayInputStream;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Properties;
 import javax.xml.bind.JAXBException;
 import javax.xml.parsers.ParserConfigurationException;
 
@@ -41,7 +37,7 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
-@SimpleLog
+@SimpleLog // close to org.apache.openejb.assembler.classic.ResourceInfoComparatorTest but
with a real case!
 @RunWith(ApplicationComposer.class)
 public class ResourceSortingTest {
 
@@ -72,42 +68,7 @@ public class ResourceSortingTest {
         assertFalse(letters.hasNext());
     }
 
-    @Test
-    public void testRealWorld4() throws Exception {
-        final List<ResourceInfo> resources = new ArrayList<>();
-
-        resources.add(new ResourceInfo());
-        resources.get(0).id = "My JMS Connection Factory";
-        resources.get(0).properties = new Properties();
-        resources.get(0).properties.put("ResourceAdapter", "My JMS Resource Adapter");
-
-        resources.add(new ResourceInfo());
-        resources.get(1).id = "My Unmanaged DataSource";
-        resources.get(1).properties = new Properties();
-
-        resources.add(new ResourceInfo());
-        resources.get(2).id = "Test Resource";
-        resources.get(2).properties = new Properties();
-        resources.get(2).properties.put("ResourceAdapter", "My JMS Connection Factory");
 
-        resources.add(new ResourceInfo());
-        resources.get(3).id = "My DataSource";
-        resources.get(3).properties = new Properties();
-
-        resources.add(new ResourceInfo());
-        resources.get(4).id = "My JMS Resource Adapter";
-        resources.get(4).properties = new Properties();
-        resources.get(4).properties.put("DataSource", "My Unmanaged DataSource");
-
-        Collections.sort(resources, new ConfigurationFactory.ResourceInfoComparator(resources));
-
-        for (ResourceInfo i : resources) System.out.println(i.id);
-        assertEquals("My Unmanaged DataSource", resources.get(0).id);
-        assertEquals("My DataSource", resources.get(1).id);
-        assertEquals("My JMS Resource Adapter", resources.get(2).id);
-        assertEquals("My JMS Connection Factory", resources.get(3).id);
-        assertEquals("Test Resource", resources.get(4).id);
-    }
 
     public static class Foo {
         private static final List<String> IDS = new ArrayList<>();


Mime
View raw message