]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/ArtifactIndexer.java
Rename default artifact base path
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / ArtifactIndexer.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.slc.repo;
17
18 import javax.jcr.Node;
19 import javax.jcr.nodetype.NodeType;
20
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;
29
30 /**
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).
34 */
35 public class ArtifactIndexer implements NodeIndexer {
36 private Log log = LogFactory.getLog(ArtifactIndexer.class);
37
38 public Boolean support(String path) {
39 String relativePath = getRelativePath(path);
40 if (relativePath == null)
41 return false;
42 Artifact artifact = null;
43 try {
44 artifact = AetherUtils.convertPathToArtifact(relativePath, null);
45 } catch (Exception e) {
46 if (log.isTraceEnabled())
47 log.trace("Malformed path " + path + ", skipping silently", e);
48 }
49 return artifact != null;
50 }
51
52 public void index(Node fileNode) {
53 Artifact artifact = null;
54 try {
55 if (!fileNode.isNodeType(NodeType.NT_FILE))
56 return;
57
58 String relativePath = getRelativePath(fileNode.getPath());
59 if (relativePath == null)
60 return;
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);
75
76 // make sure there is a checksum
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,
81 sha.getBytes());
82 }
83
84 // set higher levels
85 Node artifactVersionBase = fileNode.getParent();
86 if (!artifactVersionBase
87 .isNodeType(SlcTypes.SLC_ARTIFACT_VERSION_BASE)) {
88 artifactVersionBase
89 .addMixin(SlcTypes.SLC_ARTIFACT_VERSION_BASE);
90 artifactVersionBase.setProperty(SlcNames.SLC_ARTIFACT_VERSION,
91 artifact.getBaseVersion());
92 artifactVersionBase.setProperty(SlcNames.SLC_ARTIFACT_ID,
93 artifact.getArtifactId());
94 artifactVersionBase.setProperty(SlcNames.SLC_GROUP_ID,
95 artifact.getGroupId());
96 }
97 JcrUtils.updateLastModified(artifactVersionBase);
98
99 // pom
100 if (artifact.getExtension().equals("pom")) {
101 // TODO read to make it a distribution
102 }
103
104 Node artifactBase = artifactVersionBase.getParent();
105 if (!artifactBase.isNodeType(SlcTypes.SLC_ARTIFACT_BASE)) {
106 artifactBase.addMixin(SlcTypes.SLC_ARTIFACT_BASE);
107 artifactBase.setProperty(SlcNames.SLC_ARTIFACT_ID,
108 artifact.getArtifactId());
109 artifactBase.setProperty(SlcNames.SLC_GROUP_ID,
110 artifact.getGroupId());
111 }
112 JcrUtils.updateLastModified(artifactBase);
113
114 Node groupBase = artifactBase.getParent();
115 if (!groupBase.isNodeType(SlcTypes.SLC_GROUP_BASE)) {
116 // if (groupBase.isNodeType(SlcTypes.SLC_ARTIFACT_BASE)) {
117 // log.warn("Group base " + groupBase.getPath()
118 // + " is also artifact base");
119 // }
120 groupBase.addMixin(SlcTypes.SLC_GROUP_BASE);
121 groupBase.setProperty(SlcNames.SLC_GROUP_BASE_ID,
122 artifact.getGroupId());
123 }
124 JcrUtils.updateLastModifiedAndParents(groupBase,
125 RepoConstants.DEFAULT_ARTIFACTS_BASE_PATH);
126
127 if (log.isTraceEnabled())
128 log.trace("Indexed artifact " + artifact + " on " + fileNode);
129 } catch (Exception e) {
130 throw new SlcException("Cannot index artifact " + artifact
131 + " metadata on node " + fileNode, e);
132 }
133 }
134
135 private String getRelativePath(String nodePath) {
136 String basePath = RepoConstants.DEFAULT_ARTIFACTS_BASE_PATH;
137 if (!nodePath.startsWith(basePath))
138 return null;
139 String relativePath = nodePath.substring(basePath.length());
140 return relativePath;
141 }
142 }