You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by da...@apache.org on 2006/03/20 20:01:10 UTC

svn commit: r387278 - in /geronimo/branches/1.1/modules/kernel/src: java/org/apache/geronimo/gbean/AbstractName.java java/org/apache/geronimo/kernel/repository/Artifact.java test/org/apache/geronimo/gbean/AbstractNameTest.java

Author: dain
Date: Mon Mar 20 11:01:08 2006
New Revision: 387278

URL: http://svn.apache.org/viewcvs?rev=387278&view=rev
Log:
Added toURI and a constructor that takes an URI to AbstractName.

Added:
    geronimo/branches/1.1/modules/kernel/src/test/org/apache/geronimo/gbean/AbstractNameTest.java
Modified:
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/gbean/AbstractName.java
    geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/repository/Artifact.java

Modified: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/gbean/AbstractName.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/gbean/AbstractName.java?rev=387278&r1=387277&r2=387278&view=diff
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/gbean/AbstractName.java (original)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/gbean/AbstractName.java Mon Mar 20 11:01:08 2006
@@ -17,14 +17,20 @@
 
 package org.apache.geronimo.gbean;
 
-import org.apache.geronimo.kernel.repository.Artifact;
-
-import javax.management.ObjectName;
 import java.io.Serializable;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Hashtable;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
-import java.util.Set;
+import java.util.TreeMap;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+
+import org.apache.geronimo.kernel.repository.Artifact;
 
 /**
  * @version $Rev:$ $Date:$
@@ -35,14 +41,120 @@
     private final Artifact artifact;
     private final Map name;
     private final ObjectName objectName;
+    private final URI uri;
 
     public AbstractName(Artifact artifact, Map name, ObjectName objectName) {
-        assert artifact != null;
-        assert name != null;
-        assert objectName != null;
+        if (artifact == null) throw new NullPointerException("artifact is null");
+        if (name == null) throw new NullPointerException("name is null");
+        if (name.isEmpty()) throw new IllegalArgumentException("name is empty");
+        if (objectName == null) throw new NullPointerException("objectName is null");
+
         this.artifact = artifact;
         this.name = name;
         this.objectName = objectName;
+
+        this.uri = createURI(artifact, name);
+    }
+
+    public AbstractName(URI uri) {
+        if (uri == null) new NullPointerException("uri is null");
+
+        //
+        // Artifact
+        //
+        String artifactString = uri.getPath();
+        if (artifactString == null) throw new IllegalArgumentException("uri does not contain a path part used for the artifact");
+
+        List artifactParts = split(artifactString, '/');
+        if (artifactParts.size() != 4) {
+            throw new IllegalArgumentException("uri path must be in the form [?key=value[,key=value]*] : " + artifactString);
+        }
+
+        String groupId = (String) artifactParts.get(0);
+        if (groupId.length() == 0) groupId = null;
+
+        String artifactId = (String) artifactParts.get(1);
+        if (artifactId.length() == 0) artifactId = null;
+
+        String version = (String) artifactParts.get(2);
+        if (version.length() == 0) version = null;
+
+        String type = (String) artifactParts.get(3);
+        if (type.length() == 0) type = null;
+
+        artifact = new Artifact(groupId, artifactId, version, type);
+
+        //
+        // name map
+        //
+        name = new TreeMap();
+        String nameString = uri.getQuery();
+        if (nameString == null) throw new IllegalArgumentException("uri does not contain a query part used for the name map");
+        List nameParts = split(nameString, ',');
+        for (Iterator iterator = nameParts.iterator(); iterator.hasNext();) {
+            String namePart = (String) iterator.next();
+            List keyValue = split(namePart, '=');
+            if (keyValue.size() != 2) {
+                throw new IllegalArgumentException("uri query string must be in the form [vendorId]/artifactId/[version]/[type] : " + nameString);
+            }
+            String key = (String) keyValue.get(0);
+            String value = (String) keyValue.get(1);
+            if (name.containsKey(key)) {
+                throw new IllegalArgumentException("uri query string contains the key '"+ key + "' twice : " + nameString);
+            }
+            name.put(key, value);
+        }
+        if (name.isEmpty()) {
+            throw new IllegalArgumentException("name is empty: " + nameString);
+        }
+
+        //
+        // uri
+        //
+        this.uri = createURI(artifact, name);
+
+        //
+        // object name
+        //
+        try {
+            objectName = new ObjectName("geronimo", new Hashtable(name));
+        } catch (MalformedObjectNameException e) {
+            IllegalArgumentException illegalArgumentException = new IllegalArgumentException();
+            illegalArgumentException.initCause(e);
+            throw illegalArgumentException;
+        }
+    }
+
+    private static URI createURI(Artifact artifact, Map name) {
+        StringBuffer queryString = new StringBuffer();
+        TreeMap treeMap = new TreeMap(name);
+        for (Iterator iterator = treeMap.entrySet().iterator(); iterator.hasNext();) {
+            Map.Entry entry = (Map.Entry) iterator.next();
+            String key = (String) entry.getKey();
+            String value = (String) entry.getValue();
+            queryString.append(key).append('=').append(value);
+            if (iterator.hasNext()) {
+                queryString.append(',');
+            }
+        }
+        try {
+            return new URI(null, null, artifact.toString(), queryString.toString(), null);
+        } catch (URISyntaxException e) {
+            IllegalArgumentException illegalArgumentException = new IllegalArgumentException();
+            illegalArgumentException.initCause(e);
+            throw illegalArgumentException;
+        }
+    }
+
+    private static List split(String source, char delim) {
+        List parts = new ArrayList();
+        for (int index = source.indexOf(delim); index >= 0; index = source.indexOf(delim)) {
+            String part = source.substring(0, index);
+            source = source.substring(index +  1);
+            parts.add(part);
+        }
+        parts.add(source);
+        return parts;
     }
 
     public Artifact getArtifact() {
@@ -55,6 +167,10 @@
 
     public ObjectName getObjectName() {
         return objectName;
+    }
+
+    public URI toURI() {
+        return uri;
     }
 
     public String toString() {

Modified: geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/repository/Artifact.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/repository/Artifact.java?rev=387278&r1=387277&r2=387278&view=diff
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/repository/Artifact.java (original)
+++ geronimo/branches/1.1/modules/kernel/src/java/org/apache/geronimo/kernel/repository/Artifact.java Mon Mar 20 11:01:08 2006
@@ -143,7 +143,25 @@
     }
 
     public String toString() {
-        return groupId + "/" + artifactId + "/" + version + "/" + type;
+        StringBuffer buffer = new StringBuffer();
+
+        if (groupId != null) {
+            buffer.append(groupId);
+        }
+        buffer.append("/");
+
+        buffer.append(artifactId);
+        buffer.append("/");
+
+        if (version != null) {
+            buffer.append(version);
+        }
+        buffer.append("/");
+
+        if (type != null) {
+            buffer.append(type);
+        }
+        return buffer.toString();
     }
 
     /**

Added: geronimo/branches/1.1/modules/kernel/src/test/org/apache/geronimo/gbean/AbstractNameTest.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/kernel/src/test/org/apache/geronimo/gbean/AbstractNameTest.java?rev=387278&view=auto
==============================================================================
--- geronimo/branches/1.1/modules/kernel/src/test/org/apache/geronimo/gbean/AbstractNameTest.java (added)
+++ geronimo/branches/1.1/modules/kernel/src/test/org/apache/geronimo/gbean/AbstractNameTest.java Mon Mar 20 11:01:08 2006
@@ -0,0 +1,149 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed 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.geronimo.gbean;
+
+import java.util.Map;
+import java.util.LinkedHashMap;
+import java.util.Collections;
+import java.net.URI;
+
+import javax.management.ObjectName;
+
+import junit.framework.TestCase;
+import org.apache.geronimo.kernel.repository.Artifact;
+import org.apache.geronimo.kernel.repository.Version;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class AbstractNameTest extends TestCase {
+    public void testSimple() throws Exception {
+        Artifact artifact = new Artifact("groupId", "artifactId", "version", "type");
+
+        Map nameMap = new LinkedHashMap();
+        nameMap.put("a", "aaa");
+        nameMap.put("b", "bbb");
+        nameMap.put("c", "ccc");
+
+        AbstractName abstractName = new AbstractName(artifact, nameMap, new ObjectName("test:nothing=true"));
+        URI uri = abstractName.toURI();
+        assertEquals(abstractName, new AbstractName(uri));
+    }
+
+    public void testMinimalArtifact() throws Exception {
+        Artifact artifact = new Artifact(null, "artifactId", (Version)null, null);
+
+        Map nameMap = new LinkedHashMap();
+        nameMap.put("a", "aaa");
+        nameMap.put("b", "bbb");
+        nameMap.put("c", "ccc");
+
+        AbstractName abstractName = new AbstractName(artifact, nameMap, new ObjectName("test:nothing=true"));
+        URI uri = abstractName.toURI();
+        assertEquals(abstractName, new AbstractName(uri));
+    }
+
+    public void testCreateURI() throws Exception {
+        Artifact artifact = new Artifact("groupId", "artifactId", "version", "type");
+
+        URI uri = new URI(null, null, artifact.toString(), "a=aaa", null);
+
+        AbstractName abstractName = new AbstractName(artifact,
+                Collections.singletonMap("a", "aaa"),
+                new ObjectName("test:nothing=true"));
+        assertEquals(abstractName, new AbstractName(uri));
+    }
+
+    public void testEmptyName() throws Exception {
+        Artifact artifact = new Artifact("groupId", "artifactId", "version", "type");
+
+        URI uri = new URI(null, null, artifact.toString(), "", null);
+        try {
+            new AbstractName(uri);
+            fail("expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+    public void testNoName() throws Exception {
+        Artifact artifact = new Artifact("groupId", "artifactId", "version", "type");
+
+        URI uri = new URI(null, null, artifact.toString(), null, null);
+        try {
+            new AbstractName(uri);
+            fail("expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+    public void testEmptyArtifact() throws Exception {
+        URI uri = new URI(null, null, "", "a=aaa", null);
+        try {
+            new AbstractName(uri);
+            fail("expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+    public void testInvalidArtifact() throws Exception {
+        URI uri = new URI(null, null, "foo", "a=aaa", null);
+        try {
+            new AbstractName(uri);
+            fail("expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+
+        uri = new URI(null, null, "foo/", "a=aaa", null);
+        try {
+            new AbstractName(uri);
+            fail("expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+
+        uri = new URI(null, null, "/foo/", "a=aaa", null);
+        try {
+            new AbstractName(uri);
+            fail("expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+
+        uri = new URI(null, null, "foo////", "a=aaa", null);
+        try {
+            new AbstractName(uri);
+            fail("expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+    public void testNoArtifact() throws Exception {
+        URI uri = new URI(null, null, null, "a=aaa", null);
+        try {
+            new AbstractName(uri);
+            fail("expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+}
+