]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/ArtifactIndexer.java
f62142254bb6a27e7d9cc429804d03fe6ba2ad44
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / ArtifactIndexer.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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 private Boolean force = false;
38
39 public Boolean support(String path) {
40 String relativePath = getRelativePath(path);
41 if (relativePath == null)
42 return false;
43 Artifact artifact = null;
44 try {
45 artifact = AetherUtils.convertPathToArtifact(relativePath, null);
46 } catch (Exception e) {
47 if (log.isTraceEnabled())
48 log.trace("Malformed path " + path + ", skipping silently", e);
49 }
50 return artifact != null;
51 }
52
53 public void index(Node fileNode) {
54 Artifact artifact = null;
55 try {
56 if (!support(fileNode.getPath()))
57 return;
58
59 // Already indexed
60 if (!force && fileNode.isNodeType(SlcTypes.SLC_ARTIFACT))
61 return;
62
63 if (!fileNode.isNodeType(NodeType.NT_FILE))
64 return;
65
66 String relativePath = getRelativePath(fileNode.getPath());
67 if (relativePath == null)
68 return;
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);
83
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,
89 sha.getBytes());
90 }
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,
95 md5.getBytes());
96 }
97
98 // set higher levels
99 Node artifactVersionBase = fileNode.getParent();
100 if (!artifactVersionBase
101 .isNodeType(SlcTypes.SLC_ARTIFACT_VERSION_BASE)) {
102 artifactVersionBase
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());
110 }
111 JcrUtils.updateLastModified(artifactVersionBase);
112
113 // pom
114 if (artifact.getExtension().equals("pom")) {
115 // TODO read to make it a distribution
116 }
117
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());
125 }
126 JcrUtils.updateLastModified(artifactBase);
127
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");
133 // }
134 groupBase.addMixin(SlcTypes.SLC_GROUP_BASE);
135 groupBase.setProperty(SlcNames.SLC_GROUP_BASE_ID,
136 artifact.getGroupId());
137 }
138 JcrUtils.updateLastModifiedAndParents(groupBase,
139 RepoConstants.DEFAULT_ARTIFACTS_BASE_PATH);
140
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);
146 }
147 }
148
149 private String getRelativePath(String nodePath) {
150 String basePath = RepoConstants.DEFAULT_ARTIFACTS_BASE_PATH;
151 if (!nodePath.startsWith(basePath))
152 return null;
153 String relativePath = nodePath.substring(basePath.length());
154 return relativePath;
155 }
156
157 public void setForce(Boolean force) {
158 this.force = force;
159 }
160
161 }