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);
37 private Boolean force
= false;
39 public Boolean
support(String path
) {
40 String relativePath
= getRelativePath(path
);
41 if (relativePath
== null)
43 Artifact artifact
= null;
45 artifact
= AetherUtils
.convertPathToArtifact(relativePath
, null);
46 } catch (Exception e
) {
47 if (log
.isTraceEnabled())
48 log
.trace("Malformed path " + path
+ ", skipping silently", e
);
50 return artifact
!= null;
53 public void index(Node fileNode
) {
54 Artifact artifact
= null;
56 if (!support(fileNode
.getPath()))
60 if (!force
&& fileNode
.isNodeType(SlcTypes
.SLC_ARTIFACT
))
63 if (!fileNode
.isNodeType(NodeType
.NT_FILE
))
66 String relativePath
= getRelativePath(fileNode
.getPath());
67 if (relativePath
== null)
69 artifact
= AetherUtils
.convertPathToArtifact(relativePath
, null);
70 // support() guarantees that artifact won't be null, no NPE check
71 fileNode
.addMixin(SlcTypes
.SLC_ARTIFACT
);
72 fileNode
.setProperty(SlcNames
.SLC_ARTIFACT_ID
,
73 artifact
.getArtifactId());
74 fileNode
.setProperty(SlcNames
.SLC_GROUP_ID
, artifact
.getGroupId());
75 fileNode
.setProperty(SlcNames
.SLC_ARTIFACT_VERSION
,
76 artifact
.getVersion());
77 fileNode
.setProperty(SlcNames
.SLC_ARTIFACT_EXTENSION
,
78 artifact
.getExtension());
79 // can be null but ok for JCR API
80 fileNode
.setProperty(SlcNames
.SLC_ARTIFACT_CLASSIFIER
,
81 artifact
.getClassifier());
82 JcrUtils
.updateLastModified(fileNode
);
84 // make sure there are checksums
85 String shaNodeName
= fileNode
.getName() + ".sha1";
86 if (!fileNode
.getParent().hasNode(shaNodeName
)) {
87 String sha
= JcrUtils
.checksumFile(fileNode
, "SHA-1");
88 JcrUtils
.copyBytesAsFile(fileNode
.getParent(), shaNodeName
,
91 String md5NodeName
= fileNode
.getName() + ".md5";
92 if (!fileNode
.getParent().hasNode(md5NodeName
)) {
93 String md5
= JcrUtils
.checksumFile(fileNode
, "MD5");
94 JcrUtils
.copyBytesAsFile(fileNode
.getParent(), md5NodeName
,
99 Node artifactVersionBase
= fileNode
.getParent();
100 if (!artifactVersionBase
101 .isNodeType(SlcTypes
.SLC_ARTIFACT_VERSION_BASE
)) {
103 .addMixin(SlcTypes
.SLC_ARTIFACT_VERSION_BASE
);
104 artifactVersionBase
.setProperty(SlcNames
.SLC_ARTIFACT_VERSION
,
105 artifact
.getBaseVersion());
106 artifactVersionBase
.setProperty(SlcNames
.SLC_ARTIFACT_ID
,
107 artifact
.getArtifactId());
108 artifactVersionBase
.setProperty(SlcNames
.SLC_GROUP_ID
,
109 artifact
.getGroupId());
111 JcrUtils
.updateLastModified(artifactVersionBase
);
114 if (artifact
.getExtension().equals("pom")) {
115 // TODO read to make it a distribution
118 Node artifactBase
= artifactVersionBase
.getParent();
119 if (!artifactBase
.isNodeType(SlcTypes
.SLC_ARTIFACT_BASE
)) {
120 artifactBase
.addMixin(SlcTypes
.SLC_ARTIFACT_BASE
);
121 artifactBase
.setProperty(SlcNames
.SLC_ARTIFACT_ID
,
122 artifact
.getArtifactId());
123 artifactBase
.setProperty(SlcNames
.SLC_GROUP_ID
,
124 artifact
.getGroupId());
126 JcrUtils
.updateLastModified(artifactBase
);
128 Node groupBase
= artifactBase
.getParent();
129 if (!groupBase
.isNodeType(SlcTypes
.SLC_GROUP_BASE
)) {
130 // if (groupBase.isNodeType(SlcTypes.SLC_ARTIFACT_BASE)) {
131 // log.warn("Group base " + groupBase.getPath()
132 // + " is also artifact base");
134 groupBase
.addMixin(SlcTypes
.SLC_GROUP_BASE
);
135 groupBase
.setProperty(SlcNames
.SLC_GROUP_BASE_ID
,
136 artifact
.getGroupId());
138 JcrUtils
.updateLastModifiedAndParents(groupBase
,
139 RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
);
141 if (log
.isTraceEnabled())
142 log
.trace("Indexed artifact " + artifact
+ " on " + fileNode
);
143 } catch (Exception e
) {
144 throw new SlcException("Cannot index artifact " + artifact
145 + " metadata on node " + fileNode
, e
);
149 private String
getRelativePath(String nodePath
) {
150 String basePath
= RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
;
151 if (!nodePath
.startsWith(basePath
))
153 String relativePath
= nodePath
.substring(basePath
.length());
157 public void setForce(Boolean force
) {