]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.aether/src/main/java/org/argeo/slc/aether/AetherUtils.java
Working indexation (artifact, jar, osgi) in repo
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.aether / src / main / java / org / argeo / slc / aether / AetherUtils.java
1 package org.argeo.slc.aether;
2
3 import java.util.regex.Pattern;
4
5 import org.apache.commons.io.FilenameUtils;
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.argeo.slc.SlcException;
9 import org.sonatype.aether.artifact.Artifact;
10 import org.sonatype.aether.graph.DependencyNode;
11 import org.sonatype.aether.util.artifact.DefaultArtifact;
12
13 /** Utilities related to Aether */
14 public class AetherUtils {
15 public final static String SNAPSHOT = "SNAPSHOT";
16 // hacked from aether
17 public static final Pattern SNAPSHOT_TIMESTAMP = Pattern
18 .compile("^(.*-)?([0-9]{8}.[0-9]{6}-[0-9]+)$");
19
20 private final static Log log = LogFactory.getLog(AetherUtils.class);
21
22 /** Logs a dependency node and its transitive dependencies as a tree. */
23 public static void logDependencyNode(int depth,
24 DependencyNode dependencyNode) {
25 if (!log.isDebugEnabled())
26 return;
27
28 StringBuffer prefix = new StringBuffer(depth * 2 + 2);
29 // prefix.append("|-");
30 for (int i = 0; i < depth * 2; i++) {
31 prefix.append(' ');
32 }
33 Artifact artifact = dependencyNode.getDependency().getArtifact();
34 log.debug(prefix + "|-> " + artifact.getArtifactId() + " ["
35 + artifact.getVersion() + "]"
36 + (dependencyNode.getDependency().isOptional() ? " ?" : ""));
37 for (DependencyNode child : dependencyNode.getChildren()) {
38 logDependencyNode(depth + 1, child);
39 }
40 }
41
42 /**
43 * Converts a path (relative to a repository root) to an {@link Artifact}.
44 *
45 * @param path
46 * the relative path
47 * @param type
48 * the layout type, currently ignored because only the 'default'
49 * Maven 2 layout is currently supported:
50 * /my/group/id/artifactId/
51 * version/artifactId-version[-classifier].extension
52 * @return the related artifact or null if the file is not an artifact
53 * (Maven medata data XML files, check sums, etc.)
54 */
55 public static Artifact convertPathToArtifact(String path, String type) {
56 // TODO rewrite it with regexp (unit tests first!)
57
58 // normalize
59 if (path.startsWith("/"))
60 path = path.substring(1);
61
62 // parse group id
63 String[] tokensSlash = path.split("/");
64 if (tokensSlash.length < 4)
65 return null;
66 StringBuffer groupId = new StringBuffer(path.length());
67 for (int i = 0; i < tokensSlash.length - 3; i++) {
68 if (i != 0)
69 groupId.append('.');
70 groupId.append(tokensSlash[i]);
71 }
72 String artifactId = tokensSlash[tokensSlash.length - 3];
73 String baseVersion = tokensSlash[tokensSlash.length - 2];
74 String fileName = tokensSlash[tokensSlash.length - 1];
75
76 if (!fileName.startsWith(artifactId))
77 return null;
78 // FIXME make it configurable? (via an argument?)
79 if (FilenameUtils.isExtension(fileName, new String[] { "sha1", "md5" }))
80 return null;
81
82 String extension = FilenameUtils.getExtension(fileName);
83 String baseName = FilenameUtils.getBaseName(fileName);
84
85 // check since we assume hereafter
86 if (!baseName.startsWith(artifactId))
87 throw new SlcException("Base name '" + baseName
88 + " does not start with artifact id '" + artifactId
89 + "' in " + path);
90
91 boolean isSnapshot = baseVersion.endsWith("-" + SNAPSHOT);
92 String baseBaseVersion = isSnapshot ? baseVersion.substring(0,
93 baseVersion.length() - SNAPSHOT.length() - 1) : baseVersion;
94 int artifactAndBaseBaseVersionLength = artifactId.length() + 1
95 + baseBaseVersion.length() + 1;
96 String classifier = null;
97 if (baseName.length() > artifactAndBaseBaseVersionLength) {
98 String dashRest = baseName
99 .substring(artifactAndBaseBaseVersionLength);
100 String[] dashes = dashRest.split("-");
101
102 if (isSnapshot) {
103 if (dashes[0].equals(SNAPSHOT)) {
104 if (dashRest.length() > SNAPSHOT.length() + 1)
105 classifier = dashRest.substring(SNAPSHOT.length() + 1);
106
107 } else {
108 if (dashes.length > 2)// assume no '-' in classifier
109 classifier = dashes[2];
110 }
111 } else {
112 if (dashes.length > 0)
113 classifier = dashes[0];
114 }
115 }
116
117 // classifier
118 // String classifier = null;
119 // int firstDash = baseName.indexOf('-');
120 // int classifierDash = baseName.lastIndexOf('-');
121 // if (classifierDash > 0 && classifierDash != firstDash) {
122 // classifier = baseName.substring(classifierDash + 1);
123 // }
124 // if (isSnapshot && classifier != null) {
125 // if (classifier.equals(SNAPSHOT))
126 // classifier = null;
127 // else
128 // try {
129 // Long.parseLong(classifier); // build number
130 // // if not failed this is a timestamped version
131 // classifier = null;
132 // } catch (NumberFormatException e) {
133 // // silent
134 // }
135 // }
136
137 // version
138 String version = baseName.substring(artifactId.length() + 1);
139 if (classifier != null)
140 version = version.substring(0,
141 version.length() - classifier.length() - 1);
142
143 // consistency checks
144 if (!isSnapshot && !version.equals(baseVersion))
145 throw new SlcException("Base version '" + baseVersion
146 + "' and version '" + version + "' not in line in " + path);
147 if (!isSnapshot && isSnapshotVersion(version))
148 throw new SlcException("SNAPSHOT base version '" + baseVersion
149 + "' and version '" + version + "' not in line in " + path);
150
151 DefaultArtifact artifact = new DefaultArtifact(groupId.toString(),
152 artifactId, classifier, extension, version);
153 return artifact;
154 }
155
156 /** Hacked from aether */
157 public static boolean isSnapshotVersion(String version) {
158 return version.endsWith(SNAPSHOT)
159 || SNAPSHOT_TIMESTAMP.matcher(version).matches();
160 }
161
162 }