1 package org
.argeo
.slc
.repo
;
4 import javax
.jcr
.nodetype
.NodeType
;
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
;
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).
19 public class ArtifactIndexer
implements NodeIndexer
{
20 private Log log
= LogFactory
.getLog(ArtifactIndexer
.class);
22 public Boolean
support(String path
) {
23 String relativePath
= getRelativePath(path
);
24 if (relativePath
== null)
26 Artifact artifact
= null;
28 artifact
= AetherUtils
.convertPathToArtifact(relativePath
, null);
29 } catch (Exception e
) {
30 if (log
.isTraceEnabled())
31 log
.trace("Malformed path " + path
+ ", skipping silently", e
);
33 return artifact
!= null;
36 public void index(Node fileNode
) {
37 Artifact artifact
= null;
39 if (!fileNode
.isNodeType(NodeType
.NT_FILE
))
42 String relativePath
= getRelativePath(fileNode
.getPath());
43 if (relativePath
== null)
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());
60 Node artifactVersionBase
= fileNode
.getParent();
61 if (!artifactVersionBase
62 .isNodeType(SlcTypes
.SLC_ARTIFACT_VERSION_BASE
)) {
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());
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());
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());
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
);
95 private String
getRelativePath(String nodePath
) {
96 String basePath
= RepoConstants
.ARTIFACTS_BASE_PATH
;
97 if (!nodePath
.startsWith(basePath
))
99 String relativePath
= nodePath
.substring(basePath
.length());