2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.repo
;
18 import javax
.jcr
.Node
;
19 import javax
.jcr
.nodetype
.NodeType
;
21 import org
.apache
.commons
.logging
.Log
;
22 import org
.apache
.commons
.logging
.LogFactory
;
23 import org
.argeo
.jcr
.JcrUtils
;
24 import org
.argeo
.slc
.SlcException
;
25 import org
.argeo
.slc
.aether
.AetherUtils
;
26 import org
.argeo
.slc
.jcr
.SlcNames
;
27 import org
.argeo
.slc
.jcr
.SlcTypes
;
28 import org
.sonatype
.aether
.artifact
.Artifact
;
31 * Add {@link Artifact} properties to a {@link Node}. Does nothing if the node
32 * name doesn't start with the artifact id (in order to skip Maven metadata XML
33 * files and other non artifact files).
35 public class ArtifactIndexer
implements NodeIndexer
{
36 private Log log
= LogFactory
.getLog(ArtifactIndexer
.class);
38 public Boolean
support(String path
) {
39 String relativePath
= getRelativePath(path
);
40 if (relativePath
== null)
42 Artifact artifact
= null;
44 artifact
= AetherUtils
.convertPathToArtifact(relativePath
, null);
45 } catch (Exception e
) {
46 if (log
.isTraceEnabled())
47 log
.trace("Malformed path " + path
+ ", skipping silently", e
);
49 return artifact
!= null;
52 public void index(Node fileNode
) {
53 Artifact artifact
= null;
55 if (!fileNode
.isNodeType(NodeType
.NT_FILE
))
58 String relativePath
= getRelativePath(fileNode
.getPath());
59 if (relativePath
== null)
61 artifact
= AetherUtils
.convertPathToArtifact(relativePath
, null);
62 // support() guarantees that artifact won't be null, no NPE check
63 fileNode
.addMixin(SlcTypes
.SLC_ARTIFACT
);
64 fileNode
.setProperty(SlcNames
.SLC_ARTIFACT_ID
,
65 artifact
.getArtifactId());
66 fileNode
.setProperty(SlcNames
.SLC_GROUP_ID
, artifact
.getGroupId());
67 fileNode
.setProperty(SlcNames
.SLC_ARTIFACT_VERSION
,
68 artifact
.getVersion());
69 fileNode
.setProperty(SlcNames
.SLC_ARTIFACT_EXTENSION
,
70 artifact
.getExtension());
71 // can be null but ok for JCR API
72 fileNode
.setProperty(SlcNames
.SLC_ARTIFACT_CLASSIFIER
,
73 artifact
.getClassifier());
74 JcrUtils
.updateLastModified(fileNode
);
76 // make sure there are checksums
77 String shaNodeName
= fileNode
.getName() + ".sha1";
78 if (!fileNode
.getParent().hasNode(shaNodeName
)) {
79 String sha
= JcrUtils
.checksumFile(fileNode
, "SHA-1");
80 JcrUtils
.copyBytesAsFile(fileNode
.getParent(), shaNodeName
,
83 String md5NodeName
= fileNode
.getName() + ".md5";
84 if (!fileNode
.getParent().hasNode(md5NodeName
)) {
85 String md5
= JcrUtils
.checksumFile(fileNode
, "MD5");
86 JcrUtils
.copyBytesAsFile(fileNode
.getParent(), md5NodeName
,
91 Node artifactVersionBase
= fileNode
.getParent();
92 if (!artifactVersionBase
93 .isNodeType(SlcTypes
.SLC_ARTIFACT_VERSION_BASE
)) {
95 .addMixin(SlcTypes
.SLC_ARTIFACT_VERSION_BASE
);
96 artifactVersionBase
.setProperty(SlcNames
.SLC_ARTIFACT_VERSION
,
97 artifact
.getBaseVersion());
98 artifactVersionBase
.setProperty(SlcNames
.SLC_ARTIFACT_ID
,
99 artifact
.getArtifactId());
100 artifactVersionBase
.setProperty(SlcNames
.SLC_GROUP_ID
,
101 artifact
.getGroupId());
103 JcrUtils
.updateLastModified(artifactVersionBase
);
106 if (artifact
.getExtension().equals("pom")) {
107 // TODO read to make it a distribution
110 Node artifactBase
= artifactVersionBase
.getParent();
111 if (!artifactBase
.isNodeType(SlcTypes
.SLC_ARTIFACT_BASE
)) {
112 artifactBase
.addMixin(SlcTypes
.SLC_ARTIFACT_BASE
);
113 artifactBase
.setProperty(SlcNames
.SLC_ARTIFACT_ID
,
114 artifact
.getArtifactId());
115 artifactBase
.setProperty(SlcNames
.SLC_GROUP_ID
,
116 artifact
.getGroupId());
118 JcrUtils
.updateLastModified(artifactBase
);
120 Node groupBase
= artifactBase
.getParent();
121 if (!groupBase
.isNodeType(SlcTypes
.SLC_GROUP_BASE
)) {
122 // if (groupBase.isNodeType(SlcTypes.SLC_ARTIFACT_BASE)) {
123 // log.warn("Group base " + groupBase.getPath()
124 // + " is also artifact base");
126 groupBase
.addMixin(SlcTypes
.SLC_GROUP_BASE
);
127 groupBase
.setProperty(SlcNames
.SLC_GROUP_BASE_ID
,
128 artifact
.getGroupId());
130 JcrUtils
.updateLastModifiedAndParents(groupBase
,
131 RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
);
133 if (log
.isTraceEnabled())
134 log
.trace("Indexed artifact " + artifact
+ " on " + fileNode
);
135 } catch (Exception e
) {
136 throw new SlcException("Cannot index artifact " + artifact
137 + " metadata on node " + fileNode
, e
);
141 private String
getRelativePath(String nodePath
) {
142 String basePath
= RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
;
143 if (!nodePath
.startsWith(basePath
))
145 String relativePath
= nodePath
.substring(basePath
.length());