X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=runtime%2Forg.argeo.slc.support.aether%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Faether%2FAetherUtils.java;h=0749e8ab2306506567553f156e1853a74ccd6328;hb=912cf5554a61dc35fb61183da39673656b668467;hp=c088afe1f08a234b6c250f2f3906b08d107087d3;hpb=6a0f4cf1c0a1b49004b8c3dbc075f7202f7536f1;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.support.aether/src/main/java/org/argeo/slc/aether/AetherUtils.java b/runtime/org.argeo.slc.support.aether/src/main/java/org/argeo/slc/aether/AetherUtils.java index c088afe1f..0749e8ab2 100644 --- a/runtime/org.argeo.slc.support.aether/src/main/java/org/argeo/slc/aether/AetherUtils.java +++ b/runtime/org.argeo.slc.support.aether/src/main/java/org/argeo/slc/aether/AetherUtils.java @@ -1,12 +1,22 @@ package org.argeo.slc.aether; +import java.util.regex.Pattern; + +import org.apache.commons.io.FilenameUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.argeo.slc.SlcException; import org.sonatype.aether.artifact.Artifact; import org.sonatype.aether.graph.DependencyNode; +import org.sonatype.aether.util.artifact.DefaultArtifact; /** Utilities related to Aether */ public class AetherUtils { + public final static String SNAPSHOT = "SNAPSHOT"; + // hacked from aether + public static final Pattern SNAPSHOT_TIMESTAMP = Pattern + .compile("^(.*-)?([0-9]{8}.[0-9]{6}-[0-9]+)$"); + private final static Log log = LogFactory.getLog(AetherUtils.class); /** Logs a dependency node and its transitive dependencies as a tree. */ @@ -29,4 +39,124 @@ public class AetherUtils { } } + /** + * Converts a path (relative to a repository root) to an {@link Artifact}. + * + * @param path + * the relative path + * @param type + * the layout type, currently ignored because only the 'default' + * Maven 2 layout is currently supported: + * /my/group/id/artifactId/ + * version/artifactId-version[-classifier].extension + * @return the related artifact or null if the file is not an artifact + * (Maven medata data XML files, check sums, etc.) + */ + public static Artifact convertPathToArtifact(String path, String type) { + // TODO rewrite it with regexp (unit tests first!) + + // normalize + if (path.startsWith("/")) + path = path.substring(1); + + // parse group id + String[] tokensSlash = path.split("/"); + if (tokensSlash.length < 4) + return null; + StringBuffer groupId = new StringBuffer(path.length()); + for (int i = 0; i < tokensSlash.length - 3; i++) { + if (i != 0) + groupId.append('.'); + groupId.append(tokensSlash[i]); + } + String artifactId = tokensSlash[tokensSlash.length - 3]; + String baseVersion = tokensSlash[tokensSlash.length - 2]; + String fileName = tokensSlash[tokensSlash.length - 1]; + + if (!fileName.startsWith(artifactId)) + return null; + // FIXME make it configurable? (via an argument?) + if (FilenameUtils.isExtension(fileName, new String[] { "sha1", "md5" })) + return null; + + String extension = FilenameUtils.getExtension(fileName); + String baseName = FilenameUtils.getBaseName(fileName); + + // check since we assume hereafter + if (!baseName.startsWith(artifactId)) + throw new SlcException("Base name '" + baseName + + " does not start with artifact id '" + artifactId + + "' in " + path); + + boolean isSnapshot = baseVersion.endsWith("-" + SNAPSHOT); + String baseBaseVersion = isSnapshot ? baseVersion.substring(0, + baseVersion.length() - SNAPSHOT.length() - 1) : baseVersion; + int artifactAndBaseBaseVersionLength = artifactId.length() + 1 + + baseBaseVersion.length() + 1; + String classifier = null; + if (baseName.length() > artifactAndBaseBaseVersionLength) { + String dashRest = baseName + .substring(artifactAndBaseBaseVersionLength); + String[] dashes = dashRest.split("-"); + + if (isSnapshot) { + if (dashes[0].equals(SNAPSHOT)) { + if (dashRest.length() > SNAPSHOT.length() + 1) + classifier = dashRest.substring(SNAPSHOT.length() + 1); + + } else { + if (dashes.length > 2)// assume no '-' in classifier + classifier = dashes[2]; + } + } else { + if (dashes.length > 0) + classifier = dashes[0]; + } + } + + // classifier + // String classifier = null; + // int firstDash = baseName.indexOf('-'); + // int classifierDash = baseName.lastIndexOf('-'); + // if (classifierDash > 0 && classifierDash != firstDash) { + // classifier = baseName.substring(classifierDash + 1); + // } + // if (isSnapshot && classifier != null) { + // if (classifier.equals(SNAPSHOT)) + // classifier = null; + // else + // try { + // Long.parseLong(classifier); // build number + // // if not failed this is a timestamped version + // classifier = null; + // } catch (NumberFormatException e) { + // // silent + // } + // } + + // version + String version = baseName.substring(artifactId.length() + 1); + if (classifier != null) + version = version.substring(0, + version.length() - classifier.length() - 1); + + // consistency checks + if (!isSnapshot && !version.equals(baseVersion)) + throw new SlcException("Base version '" + baseVersion + + "' and version '" + version + "' not in line in " + path); + if (!isSnapshot && isSnapshotVersion(version)) + throw new SlcException("SNAPSHOT base version '" + baseVersion + + "' and version '" + version + "' not in line in " + path); + + DefaultArtifact artifact = new DefaultArtifact(groupId.toString(), + artifactId, classifier, extension, version); + return artifact; + } + + /** Hacked from aether */ + public static boolean isSnapshotVersion(String version) { + return version.endsWith(SNAPSHOT) + || SNAPSHOT_TIMESTAMP.matcher(version).matches(); + } + }