You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by jb...@apache.org on 2006/08/02 23:33:14 UTC

svn commit: r428158 - in /incubator/tuscany/java/sca: core/src/main/java/org/apache/tuscany/core/services/artifact/ core/src/test/java/org/apache/tuscany/core/services/artifact/ spi/src/main/java/org/apache/tuscany/spi/services/artifact/

Author: jboynes
Date: Wed Aug  2 14:33:13 2006
New Revision: 428158

URL: http://svn.apache.org/viewvc?rev=428158&view=rev
Log:
strawman API for using artifacts from a repo (as suggested on list)
basic impl against the local maven repo (very rudimentary)

Added:
    incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/services/artifact/
    incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/services/artifact/LocalMavenRepository.java   (with props)
    incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/services/artifact/
    incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/services/artifact/LocalMavenRepositoryTestCase.java   (with props)
    incubator/tuscany/java/sca/spi/src/main/java/org/apache/tuscany/spi/services/artifact/
    incubator/tuscany/java/sca/spi/src/main/java/org/apache/tuscany/spi/services/artifact/Artifact.java   (with props)
    incubator/tuscany/java/sca/spi/src/main/java/org/apache/tuscany/spi/services/artifact/ArtifactRepository.java   (with props)

Added: incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/services/artifact/LocalMavenRepository.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/services/artifact/LocalMavenRepository.java?rev=428158&view=auto
==============================================================================
--- incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/services/artifact/LocalMavenRepository.java (added)
+++ incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/services/artifact/LocalMavenRepository.java Wed Aug  2 14:33:13 2006
@@ -0,0 +1,97 @@
+/*
+ *
+ * Copyright 2006 The Apache Software Foundation or its licensors as applicable
+ *
+ *  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.tuscany.core.services.artifact;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.Collection;
+
+import org.apache.tuscany.spi.services.artifact.Artifact;
+import org.apache.tuscany.spi.services.artifact.ArtifactRepository;
+
+/**
+ * An implementation of ArtifactRepository that uses a local Maven2 repository.
+ *
+ * @version $Rev$ $Date$
+ */
+public class LocalMavenRepository implements ArtifactRepository {
+    private File localRepo;
+
+    /**
+     * Constructor specifying the location of the local repo.
+     * Relative paths are resolved against the user's home directory.
+     *
+     * @param repoPath the path to the local repo
+     */
+    public LocalMavenRepository(String repoPath) {
+        String home = AccessController.doPrivileged(new PrivilegedAction<String>() {
+            public String run() {
+                return System.getProperty("user.home");
+            }
+        });
+        this.localRepo = new File(home, repoPath);
+    }
+
+    public void resolve(Artifact artifact) {
+        if (artifact.getUrl() != null) {
+            return;
+        }
+        
+        String path = getPath(artifact);
+        File artifactFile = new File(localRepo, path);
+        if (artifactFile.exists()) {
+            try {
+                artifact.setUrl(artifactFile.toURI().toURL());
+            } catch (MalformedURLException e) {
+                // toURI should have escaped the filename to allow it to be converted to a URL
+                throw new AssertionError();
+            }
+        }
+    }
+
+    /**
+     * Return the path into the repo for an artifact.
+     * The path for an artifact is
+     * ${group.replace('.', '/')}/$[name}/${version}/${name}-${version}[-${classifier}].${type}
+     *
+     * @param artifact the artifact to resolve
+     * @return the path into the repo for the artifact
+     */
+    protected String getPath(Artifact artifact) {
+        StringBuilder builder = new StringBuilder();
+        if (artifact.getGroup() != null) {
+            builder.append(artifact.getGroup().replace('.', '/')).append('/');
+        }
+        builder.append(artifact.getName()).append('/');
+        builder.append(artifact.getVersion()).append('/');
+
+        builder.append(artifact.getName()).append('-').append(artifact.getVersion());
+        if (artifact.getClassifier() != null) {
+            builder.append('-').append(artifact.getClassifier());
+        }
+        builder.append('.').append(artifact.getType());
+        return builder.toString();
+    }
+
+    public void resolve(Collection<? extends Artifact> artifacts) {
+        for (Artifact artifact : artifacts) {
+            resolve(artifact);
+        }
+    }
+}

Propchange: incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/services/artifact/LocalMavenRepository.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/services/artifact/LocalMavenRepository.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/services/artifact/LocalMavenRepositoryTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/services/artifact/LocalMavenRepositoryTestCase.java?rev=428158&view=auto
==============================================================================
--- incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/services/artifact/LocalMavenRepositoryTestCase.java (added)
+++ incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/services/artifact/LocalMavenRepositoryTestCase.java Wed Aug  2 14:33:13 2006
@@ -0,0 +1,75 @@
+/*
+ *
+ * Copyright 2006 The Apache Software Foundation or its licensors as applicable
+ *
+ *  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.tuscany.core.services.artifact;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.spi.services.artifact.Artifact;
+
+/**
+ * This testcase assumes that there is a maven repo in the default location.
+ * 
+ * @version $Rev$ $Date$
+ */
+public class LocalMavenRepositoryTestCase extends TestCase {
+    private LocalMavenRepository repo;
+    private Artifact artifact;
+
+    public void testPathWithNoClassifier() {
+        assertEquals("org/apache/tuscany/spi/1.0-SNAPSHOT/spi-1.0-SNAPSHOT.jar", repo.getPath(artifact));
+    }
+
+    public void testPathWithClassifier() {
+        artifact.setClassifier("x86");
+        assertEquals("org/apache/tuscany/spi/1.0-SNAPSHOT/spi-1.0-SNAPSHOT-x86.jar", repo.getPath(artifact));
+    }
+
+    public void testArtifactFoundInRepo() throws MalformedURLException {
+        String home = System.getProperty("user.home");
+        File file = new File(home, ".m2/repository/org/apache/tuscany/spi/1.0-SNAPSHOT/spi-1.0-SNAPSHOT.jar");
+        repo.resolve(artifact);
+        assertEquals(file.toURL(), artifact.getUrl());
+    }
+
+    public void testArtifactNotFoundInRepo() throws MalformedURLException {
+        artifact.setClassifier("x86");
+        repo.resolve(artifact);
+        assertNull(artifact.getUrl());
+    }
+
+    public void testNonNullURLIsUnmodified() throws MalformedURLException {
+        URL url = new URL("http://www.apache.org");
+        artifact.setUrl(url);
+        repo.resolve(artifact);
+        assertSame(url, artifact.getUrl());
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        repo = new LocalMavenRepository(".m2/repository");
+
+        artifact = new Artifact();
+        artifact.setGroup("org.apache.tuscany");
+        artifact.setName("spi");
+        artifact.setVersion("1.0-SNAPSHOT");
+        artifact.setType("jar");
+    }
+}

Propchange: incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/services/artifact/LocalMavenRepositoryTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/services/artifact/LocalMavenRepositoryTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/tuscany/java/sca/spi/src/main/java/org/apache/tuscany/spi/services/artifact/Artifact.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/spi/src/main/java/org/apache/tuscany/spi/services/artifact/Artifact.java?rev=428158&view=auto
==============================================================================
--- incubator/tuscany/java/sca/spi/src/main/java/org/apache/tuscany/spi/services/artifact/Artifact.java (added)
+++ incubator/tuscany/java/sca/spi/src/main/java/org/apache/tuscany/spi/services/artifact/Artifact.java Wed Aug  2 14:33:13 2006
@@ -0,0 +1,144 @@
+/*
+ *
+ * Copyright 2006 The Apache Software Foundation or its licensors as applicable
+ *
+ *  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.tuscany.spi.services.artifact;
+
+import java.net.URL;
+
+/**
+ * Description of some packaged artifact such as a JAR file or a Composite.
+ *
+ * @version $Rev$ $Date$
+ */
+public class Artifact {
+    private String group;
+    private String name;
+    private String version;
+    private String classifier;
+    private String type;
+    private URL url;
+
+    /**
+     * Returns the name of a logical grouping to which this artifact belongs.
+     * For example, this might represent the original publisher of the artifact.
+     *
+     * @return the name of a logical grouping to which this artifact belongs
+     */
+    public String getGroup() {
+        return group;
+    }
+
+    /**
+     * Sets the name of a logical grouping to which this artifact belongs.
+     *
+     * @param group the name of a logical grouping to which this artifact belongs
+     */
+    public void setGroup(String group) {
+        this.group = group;
+    }
+
+    /**
+     * Returns the name of an artifact.
+     *
+     * @return the name of an artifact
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * Sets the name of an artifact.
+     *
+     * @param name the name of an artifact
+     */
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    /**
+     * Returns the version of an artifact.
+     *
+     * @return the version of an artifact
+     */
+    public String getVersion() {
+        return version;
+    }
+
+    /**
+     * Sets the version of an artifact.
+     *
+     * @param version the version of an artifact
+     */
+    public void setVersion(String version) {
+        this.version = version;
+    }
+
+    /**
+     * Returns a way of classifying an artifact.
+     * This can be used to distinguish variants of an artifact that provide the same function
+     * but which may have platform specific requirements. For example, it may contain the name
+     * of a hardware platform for artifacts that contain native code.
+     *
+     * @return a way of classifying an artifact
+     */
+    public String getClassifier() {
+        return classifier;
+    }
+
+    /**
+     * Sets a way of classifying an artifact
+     *
+     * @param classifier a way of classifying an artifact
+     */
+    public void setClassifier(String classifier) {
+        this.classifier = classifier;
+    }
+
+    /**
+     * Returns the type of artifact.
+     *
+     * @return the type of artifact
+     */
+    public String getType() {
+        return type;
+    }
+
+    /**
+     * Sets the type of artifact.
+     * @param type the type of artifact
+     */
+    public void setType(String type) {
+        this.type = type;
+    }
+
+    /**
+     * Returns a URL from which the artifact can be obtained.
+     *
+     * @return a URL from which the artifact can be obtained
+     */
+    public URL getUrl() {
+        return url;
+    }
+
+    /**
+     * Sets a URL from which the artifact can be obtained.
+     *
+     * @param url a URL from which the artifact can be obtained
+     */
+    public void setUrl(URL url) {
+        this.url = url;
+    }
+}

Propchange: incubator/tuscany/java/sca/spi/src/main/java/org/apache/tuscany/spi/services/artifact/Artifact.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/spi/src/main/java/org/apache/tuscany/spi/services/artifact/Artifact.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/tuscany/java/sca/spi/src/main/java/org/apache/tuscany/spi/services/artifact/ArtifactRepository.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/spi/src/main/java/org/apache/tuscany/spi/services/artifact/ArtifactRepository.java?rev=428158&view=auto
==============================================================================
--- incubator/tuscany/java/sca/spi/src/main/java/org/apache/tuscany/spi/services/artifact/ArtifactRepository.java (added)
+++ incubator/tuscany/java/sca/spi/src/main/java/org/apache/tuscany/spi/services/artifact/ArtifactRepository.java Wed Aug  2 14:33:13 2006
@@ -0,0 +1,43 @@
+/*
+ *
+ * Copyright 2006 The Apache Software Foundation or its licensors as applicable
+ *
+ *  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.tuscany.spi.services.artifact;
+
+import java.util.Collection;
+
+/**
+ * Abstraction for a repository of SCA and other artifacts.
+ *
+ * @version $Rev$ $Date$
+ */
+public interface ArtifactRepository {
+    /**
+     * Resolve an artifact.
+     * This ensures that the information associated with an artifact is fully populated;
+     * Specifically, after this operation the URL should contain a location where the artifact can be obtained.
+     *
+     * @param artifact the artifact to be resolved
+     */
+    void resolve(Artifact artifact);
+
+    /**
+     * Resolve a collection of Artifacts.
+     *
+     * @param artifacts a collection of artifacts to be resolved
+     * @see #resolve(Artifact)
+     */
+    void resolve(Collection<? extends Artifact> artifacts);
+}

Propchange: incubator/tuscany/java/sca/spi/src/main/java/org/apache/tuscany/spi/services/artifact/ArtifactRepository.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/spi/src/main/java/org/apache/tuscany/spi/services/artifact/ArtifactRepository.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org