]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/ArtifactIndexer.java
Working indexation (artifact, jar, osgi) in repo
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / ArtifactIndexer.java
1 package org.argeo.slc.repo;
2
3 import javax.jcr.Node;
4 import javax.jcr.nodetype.NodeType;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.argeo.slc.SlcException;
9 import org.argeo.slc.aether.AetherUtils;
10 import org.argeo.slc.jcr.SlcNames;
11 import org.argeo.slc.jcr.SlcTypes;
12 import org.sonatype.aether.artifact.Artifact;
13
14 /**
15 * Add {@link Artifact} properties to a {@link Node}. Does nothing if the node
16 * name doesn't start with the artifact id (in order to skip Maven metadata XML
17 * files and other non artifact files).
18 */
19 public class ArtifactIndexer implements NodeIndexer {
20 private Log log = LogFactory.getLog(ArtifactIndexer.class);
21
22 public Boolean support(String path) {
23 String relativePath = getRelativePath(path);
24 if (relativePath == null)
25 return false;
26 Artifact artifact = null;
27 try {
28 artifact = AetherUtils.convertPathToArtifact(relativePath, null);
29 } catch (Exception e) {
30 if (log.isTraceEnabled())
31 log.trace("Malformed path " + path + ", skipping silently", e);
32 }
33 return artifact != null;
34 }
35
36 public void index(Node fileNode) {
37 Artifact artifact = null;
38 try {
39 if (!fileNode.isNodeType(NodeType.NT_FILE))
40 return;
41
42 String relativePath = getRelativePath(fileNode.getPath());
43 if (relativePath == null)
44 return;
45 artifact = AetherUtils.convertPathToArtifact(relativePath, null);
46 // support() guarantees that artifact won't be null, no NPE check
47 fileNode.addMixin(SlcTypes.SLC_ARTIFACT);
48 fileNode.setProperty(SlcNames.SLC_ARTIFACT_ID,
49 artifact.getArtifactId());
50 fileNode.setProperty(SlcNames.SLC_GROUP_ID, artifact.getGroupId());
51 fileNode.setProperty(SlcNames.SLC_ARTIFACT_VERSION,
52 artifact.getVersion());
53 fileNode.setProperty(SlcNames.SLC_ARTIFACT_EXTENSION,
54 artifact.getExtension());
55 // can be null but ok for JCR API
56 fileNode.setProperty(SlcNames.SLC_ARTIFACT_CLASSIFIER,
57 artifact.getClassifier());
58
59 // set higher levels
60 Node artifactVersionBase = fileNode.getParent();
61 if (!artifactVersionBase
62 .isNodeType(SlcTypes.SLC_ARTIFACT_VERSION_BASE)) {
63 artifactVersionBase
64 .addMixin(SlcTypes.SLC_ARTIFACT_VERSION_BASE);
65 artifactVersionBase.setProperty(SlcNames.SLC_ARTIFACT_VERSION,
66 artifact.getBaseVersion());
67 artifactVersionBase.setProperty(SlcNames.SLC_ARTIFACT_ID,
68 artifact.getArtifactId());
69 artifactVersionBase.setProperty(SlcNames.SLC_GROUP_ID,
70 artifact.getGroupId());
71 }
72 Node artifactBase = artifactVersionBase.getParent();
73 if (!artifactBase.isNodeType(SlcTypes.SLC_ARTIFACT_BASE)) {
74 artifactBase.addMixin(SlcTypes.SLC_ARTIFACT_BASE);
75 artifactBase.setProperty(SlcNames.SLC_ARTIFACT_ID,
76 artifact.getArtifactId());
77 artifactBase.setProperty(SlcNames.SLC_GROUP_ID,
78 artifact.getGroupId());
79 }
80 Node groupBase = artifactBase.getParent();
81 if (!groupBase.isNodeType(SlcTypes.SLC_GROUP_BASE)) {
82 groupBase.addMixin(SlcTypes.SLC_GROUP_BASE);
83 groupBase.setProperty(SlcNames.SLC_GROUP_ID,
84 artifact.getGroupId());
85 }
86
87 if (log.isTraceEnabled())
88 log.trace("Indexed artifact " + artifact + " on " + fileNode);
89 } catch (Exception e) {
90 throw new SlcException("Cannot index artifact " + artifact
91 + " metadata on node " + fileNode, e);
92 }
93 }
94
95 private String getRelativePath(String nodePath) {
96 String basePath = RepoConstants.ARTIFACTS_BASE_PATH;
97 if (!nodePath.startsWith(basePath))
98 return null;
99 String relativePath = nodePath.substring(basePath.length());
100 return relativePath;
101 }
102 }