geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject svn commit: r375484 - in /geronimo/branches/configid/modules: kernel/src/java/org/apache/geronimo/kernel/repository/ system/src/java/org/apache/geronimo/system/repository/
Date Tue, 07 Feb 2006 04:51:28 GMT
Author: dain
Date: Mon Feb  6 20:51:26 2006
New Revision: 375484

URL: http://svn.apache.org/viewcvs?rev=375484&view=rev
Log:
Initial maven 2 repository

Added:
    geronimo/branches/configid/modules/kernel/src/java/org/apache/geronimo/kernel/repository/Version.java
    geronimo/branches/configid/modules/system/src/java/org/apache/geronimo/system/repository/Maven2Repository.java
Modified:
    geronimo/branches/configid/modules/kernel/src/java/org/apache/geronimo/kernel/repository/Artifact.java

Modified: geronimo/branches/configid/modules/kernel/src/java/org/apache/geronimo/kernel/repository/Artifact.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/configid/modules/kernel/src/java/org/apache/geronimo/kernel/repository/Artifact.java?rev=375484&r1=375483&r2=375484&view=diff
==============================================================================
--- geronimo/branches/configid/modules/kernel/src/java/org/apache/geronimo/kernel/repository/Artifact.java
(original)
+++ geronimo/branches/configid/modules/kernel/src/java/org/apache/geronimo/kernel/repository/Artifact.java
Mon Feb  6 20:51:26 2006
@@ -23,11 +23,10 @@
 /**
  * @version $Rev:$ $Date:$
  */
-public class Artifact {
-
+public class Artifact implements Comparable {
     private String groupId;
     private String artifactId;
-    private String version;
+    private Version version;
     private String type;
     private boolean resolved;
 
@@ -37,7 +36,7 @@
     public Artifact(String groupId, String artifactId, String version, String type, boolean
resolved) {
         this.groupId = groupId;
         this.artifactId = artifactId;
-        this.version = version;
+        this.version = new Version(version);
         this.type = type;
         this.resolved = resolved;
     }
@@ -58,11 +57,11 @@
         this.artifactId = artifactId;
     }
 
-    public String getVersion() {
+    public Version getVersion() {
         return version;
     }
 
-    public void setVersion(String version) {
+    public void setVersion(Version version) {
         this.version = version;
     }
 
@@ -82,6 +81,33 @@
         this.resolved = resolved;
     }
 
+    public int compareTo(Object object) {
+        Artifact artifact = (Artifact) object;
+
+        int i = safeCompare(groupId, artifact.groupId);
+        if (i != 0) return i;
+
+        i = safeCompare(artifactId, artifact.artifactId);
+        if (i != 0) return i;
+
+        i = safeCompare(version, artifact.version);
+        if (i != 0) return i;
+
+        i = safeCompare(type, artifact.type);
+        return i;
+    }
+
+    private static int GREATER = 1;
+    private static int LESS = -1;
+    private static int safeCompare(Comparable left, Comparable right) {
+        if (left == null) {
+            if (right != null) return LESS;
+            return 0;
+        }
+        if (right == null) return GREATER;
+        return left.compareTo(right);
+    }
+
     /**
      * @deprecated for use during conversion only!
      * @return
@@ -90,5 +116,10 @@
     public URI toURI() throws URISyntaxException {
         String id = groupId + "/" + artifactId + "/" + version + "/" + type;
         return new URI(id);
+    }
+
+    public String toString() {
+        String id = groupId + "/" + artifactId + "/" + version + "/" + type;
+        return id;
     }
 }

Added: geronimo/branches/configid/modules/kernel/src/java/org/apache/geronimo/kernel/repository/Version.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/configid/modules/kernel/src/java/org/apache/geronimo/kernel/repository/Version.java?rev=375484&view=auto
==============================================================================
--- geronimo/branches/configid/modules/kernel/src/java/org/apache/geronimo/kernel/repository/Version.java
(added)
+++ geronimo/branches/configid/modules/kernel/src/java/org/apache/geronimo/kernel/repository/Version.java
Mon Feb  6 20:51:26 2006
@@ -0,0 +1,209 @@
+/*
+ * Copyright 2001-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.kernel.repository;
+
+import java.util.StringTokenizer;
+
+/**
+ * Default implementation of artifact versioning.
+ *
+ * @author <a href="mailto:brett@apache.org">Brett Porter</a>
+ * @version $Id$
+ */
+// This code was lifted from Apache Maven
+public class Version implements Comparable {
+    private Integer majorVersion;
+
+    private Integer minorVersion;
+
+    private Integer incrementalVersion;
+
+    private Integer buildNumber;
+
+    private String qualifier;
+
+    public Version(String version) {
+        parseVersion(version);
+    }
+
+    public int compareTo(Object o) {
+        Version otherVersion = (Version) o;
+
+        int result = compareIntegers(majorVersion, otherVersion.majorVersion);
+        if (result == 0) {
+            result = compareIntegers(minorVersion, otherVersion.minorVersion);
+        }
+        if (result == 0) {
+            result = compareIntegers(incrementalVersion, otherVersion.incrementalVersion);
+        }
+        if (result == 0) {
+            if (buildNumber != null || otherVersion.buildNumber != null) {
+                result = compareIntegers(buildNumber, otherVersion.buildNumber);
+            } else if (qualifier != null) {
+                if (otherVersion.qualifier != null) {
+                    if (qualifier.length() > otherVersion.qualifier.length() &&
+                            qualifier.startsWith(otherVersion.qualifier)) {
+                        // here, the longer one that otherwise match is considered older
+                        result = -1;
+                    } else if (qualifier.length() < otherVersion.qualifier.length() &&
+                            otherVersion.qualifier.startsWith(qualifier)) {
+                        // here, the longer one that otherwise match is considered older
+                        result = 1;
+                    } else {
+                        result = qualifier.compareTo(otherVersion.qualifier);
+                    }
+                } else {
+                    // otherVersion has no qualifier but we do - that's newer
+                    result = -1;
+                }
+            } else if (otherVersion.qualifier != null) {
+                // otherVersion has a qualifier but we don't, we're newer
+                result = 1;
+            }
+        }
+        return result;
+    }
+
+    private int compareIntegers(Integer i1, Integer i2) {
+        // treat null as 0 in comparison
+        if (i1 == null ? i2 == null : i1.equals(i2)) {
+            return 0;
+        } else if (i1 == null) {
+            return -i2.intValue();
+        } else if (i2 == null) {
+            return i1.intValue();
+        } else {
+            return i1.intValue() - i2.intValue();
+        }
+    }
+
+    public int getMajorVersion() {
+        return majorVersion != null ? majorVersion.intValue() : 0;
+    }
+
+    public int getMinorVersion() {
+        return minorVersion != null ? minorVersion.intValue() : 0;
+    }
+
+    public int getIncrementalVersion() {
+        return incrementalVersion != null ? incrementalVersion.intValue() : 0;
+    }
+
+    public int getBuildNumber() {
+        return buildNumber != null ? buildNumber.intValue() : 0;
+    }
+
+    public String getQualifier() {
+        return qualifier;
+    }
+
+    public final void parseVersion(String version) {
+        int index = version.indexOf("-");
+
+        String part1;
+        String part2 = null;
+
+        if (index < 0) {
+            part1 = version;
+        } else {
+            part1 = version.substring(0, index);
+            part2 = version.substring(index + 1);
+        }
+
+        if (part2 != null) {
+            try {
+                if (part2.length() == 1 || !part2.startsWith("0")) {
+                    buildNumber = Integer.valueOf(part2);
+                } else {
+                    qualifier = part2;
+                }
+            }
+            catch (NumberFormatException e) {
+                qualifier = part2;
+            }
+        }
+
+        if (part1.indexOf(".") < 0 && !part1.startsWith("0")) {
+            try {
+                majorVersion = Integer.valueOf(part1);
+            }
+            catch (NumberFormatException e) {
+                // qualifier is the whole version, including "-"
+                qualifier = version;
+                buildNumber = null;
+            }
+        } else {
+            boolean fallback = false;
+            StringTokenizer tok = new StringTokenizer(part1, ".");
+            try {
+                majorVersion = getNextIntegerToken(tok);
+                if (tok.hasMoreTokens()) {
+                    minorVersion = getNextIntegerToken(tok);
+                }
+                if (tok.hasMoreTokens()) {
+                    incrementalVersion = getNextIntegerToken(tok);
+                }
+                if (tok.hasMoreTokens()) {
+                    fallback = true;
+                }
+            }
+            catch (NumberFormatException e) {
+                fallback = true;
+            }
+
+            if (fallback) {
+                // qualifier is the whole version, including "-"
+                qualifier = version;
+                majorVersion = null;
+                minorVersion = null;
+                incrementalVersion = null;
+            }
+        }
+    }
+
+    private static Integer getNextIntegerToken(StringTokenizer tok) {
+        String s = tok.nextToken();
+        if (s.length() > 1 && s.startsWith("0")) {
+            throw new NumberFormatException("Number part has a leading 0: '" + s + "'");
+        }
+        return Integer.valueOf(s);
+    }
+
+    public String toString() {
+        StringBuffer buf = new StringBuffer();
+        if (majorVersion != null) {
+            buf.append(majorVersion);
+        }
+        if (minorVersion != null) {
+            buf.append(".");
+            buf.append(minorVersion);
+        }
+        if (incrementalVersion != null) {
+            buf.append(".");
+            buf.append(incrementalVersion);
+        }
+        if (buildNumber != null) {
+            buf.append("-");
+            buf.append(buildNumber);
+        } else if (qualifier != null) {
+            if (buf.length() > 0) {
+                buf.append("-");
+            }
+            buf.append(qualifier);
+        }
+        return buf.toString();
+    }
+}

Added: geronimo/branches/configid/modules/system/src/java/org/apache/geronimo/system/repository/Maven2Repository.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/configid/modules/system/src/java/org/apache/geronimo/system/repository/Maven2Repository.java?rev=375484&view=auto
==============================================================================
--- geronimo/branches/configid/modules/system/src/java/org/apache/geronimo/system/repository/Maven2Repository.java
(added)
+++ geronimo/branches/configid/modules/system/src/java/org/apache/geronimo/system/repository/Maven2Repository.java
Mon Feb  6 20:51:26 2006
@@ -0,0 +1,237 @@
+/**
+ *
+ * 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.system.repository;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.geronimo.gbean.GBeanInfo;
+import org.apache.geronimo.gbean.GBeanInfoBuilder;
+import org.apache.geronimo.kernel.repository.Artifact;
+import org.apache.geronimo.kernel.repository.FileWriteMonitor;
+import org.apache.geronimo.system.serverinfo.ServerInfo;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class Maven2Repository {
+    private static final Log log = LogFactory.getLog(FileSystemRepository.class);
+    private final static int TRANSFER_NOTIFICATION_SIZE = 10240;  // announce every this
many bytes
+    private final static int TRANSFER_BUF_SIZE = 10240;  // try this many bytes at a time
+    private File rootFile;
+
+    public Maven2Repository(URI root, ServerInfo serverInfo) {
+        this(resolveRoot(root, serverInfo));
+    }
+
+    private static File resolveRoot(URI root, ServerInfo serverInfo) {
+        if (root == null) throw new NullPointerException("root is null");
+        if (serverInfo == null) throw new NullPointerException("serverInfo is null");
+
+        if (!root.toString().endsWith("/")) {
+            try {
+                root = new URI(root.toString() + "/");
+            } catch (URISyntaxException e) {
+                throw new RuntimeException("Invalid repository root (does not end with /
) and can't add myself", e);
+            }
+        }
+
+        URI resolvedUri = serverInfo.resolve(root);
+
+        if (!resolvedUri.getScheme().equals("file")) {
+            throw new IllegalStateException("FileSystemRepository must have a root that's
a local directory (not " + resolvedUri + ")");
+        }
+
+        File rootFile = new File(resolvedUri);
+        return rootFile;
+    }
+
+    public Maven2Repository(File rootFile) {
+        if (rootFile == null) throw new NullPointerException("root is null");
+
+        if (!rootFile.exists() || !rootFile.isDirectory() || !rootFile.canRead()) {
+            throw new IllegalStateException("Maven2Repository must have a root that's a valid
readable directory (not " + rootFile.getAbsolutePath() + ")");
+        }
+
+        this.rootFile = rootFile;
+        log.debug("Repository root is " + rootFile.getAbsolutePath());
+    }
+
+    public boolean contains(Artifact artifact) {
+        File location = getLocation(artifact);
+        return location.isFile() && location.canRead();
+    }
+
+    public File getLocation(Artifact artifact) {
+        File path = new File(rootFile, artifact.getGroupId().replace('.', File.separatorChar));
+        path = new File(path, artifact.getArtifactId());
+        path = new File(path, artifact.getVersion().toString());
+        path = new File(path, artifact.getArtifactId() + "-" + artifact.getVersion() + "."
+ artifact.getType());
+
+        return path;
+    }
+
+    public List list() {
+        List artifacts = new ArrayList();
+        File[] groupIds = rootFile.listFiles();
+        for (int i = 0; i < groupIds.length; i++) {
+            File groupId = groupIds[i];
+            if (groupId.canRead() && groupId.isDirectory()) {
+                File[] versionDirs = groupId.listFiles();
+                for (int j = 0; j < versionDirs.length; j++) {
+                    File versionDir = versionDirs[j];
+                    if (versionDir.canRead() && versionDir.isDirectory()) {
+                        artifacts.addAll(getArtifacts(null, versionDir));
+                    }
+                }
+            }
+        }
+        return artifacts;
+    }
+
+    private List getArtifacts(String groupId, File versionDir) {
+        // org/apache/xbean/xbean-classpath/2.2-SNAPSHOT/xbean-classpath-2.2-SNAPSHOT.jar
+        List artifacts = new ArrayList();
+        String artifactId = versionDir.getParentFile().getName();
+
+        File[] files = versionDir.listFiles();
+        for (int i = 0; i < files.length; i++) {
+            File file = files[i];
+            if (file.canRead()) {
+                if (file.isDirectory()) {
+
+                    String nextGroupId;
+                    if (groupId == null) {
+                        nextGroupId = artifactId;
+                    } else {
+                        nextGroupId = groupId + "." + artifactId;
+                    }
+
+                    artifacts.addAll(getArtifacts(nextGroupId, file));
+                } else if (groupId != null) {
+                    String version = versionDir.getName();
+                    String fileHeader = artifactId + "-" + version + ".";
+
+                    String fileName = file.getName();
+                    if (fileName.startsWith(fileHeader)) {
+                        // type is everything after the file header
+                        String type = fileName.substring(fileHeader.length());
+
+                        if (!type.endsWith(".sha1") && !type.endsWith(".md5")) {
+                            artifacts.add(new Artifact(groupId,
+                                    artifactId,
+                                    version,
+                                    type,
+                                    true));
+                        }
+                    }
+                }
+            }
+        }
+        return artifacts;
+    }
+
+
+    public void install(File source, Artifact destination, FileWriteMonitor monitor) throws
IOException {
+        if (!source.exists() || !source.canRead() || source.isDirectory()) {
+            throw new IllegalArgumentException("Cannot read source file at " + source.getAbsolutePath());
+        }
+        copyToRepository(new FileInputStream(source), destination, monitor);
+    }
+
+    public void copyToRepository(InputStream source, Artifact destination, FileWriteMonitor
monitor) throws IOException {
+        // is this a wrtiable repository
+        if (!rootFile.canWrite()) {
+            throw new IllegalStateException("This repository is not writable: " + rootFile.getAbsolutePath()
+ ")");
+        }
+
+        // where are we going to install the file
+        File location = getLocation(destination);
+
+        // assure that there isn't already a file installed at the specified location
+        if (location.exists()) {
+            throw new IllegalArgumentException("Destination " + location.getAbsolutePath()
+ " already exists!");
+        }
+
+        // assure that the target directory exists
+        File parent = location.getParentFile();
+        if (!parent.exists() && !parent.mkdirs()) {
+            throw new RuntimeException("Unable to create directories from " + rootFile.getAbsolutePath()
+ " to " + parent.getAbsolutePath());
+        }
+
+        // copy it
+        if (monitor != null) {
+            monitor.writeStarted(destination.toString());
+        }
+        int total = 0;
+        try {
+            int threshold = TRANSFER_NOTIFICATION_SIZE;
+            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(location));
+            BufferedInputStream in = new BufferedInputStream(source);
+            byte[] buf = new byte[TRANSFER_BUF_SIZE];
+            int count;
+            while ((count = in.read(buf)) > -1) {
+                out.write(buf, 0, count);
+                if (monitor != null) {
+                    total += count;
+                    if (total > threshold) {
+                        threshold += TRANSFER_NOTIFICATION_SIZE;
+                        monitor.writeProgress(total);
+                    }
+                }
+            }
+            out.flush();
+            out.close();
+            in.close();
+        } finally {
+            if (monitor != null) {
+                monitor.writeComplete(total);
+            }
+        }
+    }
+
+    public static final GBeanInfo GBEAN_INFO;
+
+    static {
+        GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(FileSystemRepository.class);
+
+        infoFactory.addAttribute("root", URI.class, true);
+
+        infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean");
+
+        infoFactory.addInterface(Maven2Repository.class);
+
+        infoFactory.setConstructor(new String[]{"root", "ServerInfo"});
+
+        GBEAN_INFO = infoFactory.getBeanInfo();
+    }
+
+    public static GBeanInfo getGBeanInfo() {
+        return GBEAN_INFO;
+    }
+}



Mime
View raw message