]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/ArtifactIndexer.java
Improve OSGi factory
[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
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(!support(fileNode.getPath()))
56 return;
57
58 if (!fileNode.isNodeType(NodeType.NT_FILE))
59 return;
60
61 String relativePath = getRelativePath(fileNode.getPath());
62 if (relativePath == null)
63 return;
64 artifact = AetherUtils.convertPathToArtifact(relativePath, null);
65 // support() guarantees that artifact won't be null, no NPE check
66 fileNode.addMixin(SlcTypes.SLC_ARTIFACT);
67 fileNode.setProperty(SlcNames.SLC_ARTIFACT_ID,
68 artifact.getArtifactId());
69 fileNode.setProperty(SlcNames.SLC_GROUP_ID, artifact.getGroupId());
70 fileNode.setProperty(SlcNames.SLC_ARTIFACT_VERSION,
71 artifact.getVersion());
72 fileNode.setProperty(SlcNames.SLC_ARTIFACT_EXTENSION,
73 artifact.getExtension());
74 // can be null but ok for JCR API
75 fileNode.setProperty(SlcNames.SLC_ARTIFACT_CLASSIFIER,
76 artifact.getClassifier());
77 JcrUtils.updateLastModified(fileNode);
78
79 // make sure there are checksums
80 String shaNodeName = fileNode.getName() + ".sha1";
81 if (!fileNode.getParent().hasNode(shaNodeName)) {
82 String sha = JcrUtils.checksumFile(fileNode, "SHA-1");
83 JcrUtils.copyBytesAsFile(fileNode.getParent(), shaNodeName,
84 sha.getBytes());
85 }
86 String md5NodeName = fileNode.getName() + ".md5";
87 if (!fileNode.getParent().hasNode(md5NodeName)) {
88 String md5 = JcrUtils.checksumFile(fileNode, "MD5");
89 JcrUtils.copyBytesAsFile(fileNode.getParent(), md5NodeName,
90 md5.getBytes());
91 }
92
93 // set higher levels
94 Node artifactVersionBase = fileNode.getParent();
95 if (!artifactVersionBase
96 .isNodeType(SlcTypes.SLC_ARTIFACT_VERSION_BASE)) {
97 artifactVersionBase
98 .addMixin(SlcTypes.SLC_ARTIFACT_VERSION_BASE);
99 artifactVersionBase.setProperty(SlcNames.SLC_ARTIFACT_VERSION,
100 artifact.getBaseVersion());
101 artifactVersionBase.setProperty(SlcNames.SLC_ARTIFACT_ID,
102 artifact.getArtifactId());
103 artifactVersionBase.setProperty(SlcNames.SLC_GROUP_ID,
104 artifact.getGroupId());
105 }
106 JcrUtils.updateLastModified(artifactVersionBase);
107
108 // pom
109 if (artifact.getExtension().equals("pom")) {
110 // TODO read to make it a distribution
111 }
112
113 Node artifactBase = artifactVersionBase.getParent();
114 if (!artifactBase.isNodeType(SlcTypes.SLC_ARTIFACT_BASE)) {
115 artifactBase.addMixin(SlcTypes.SLC_ARTIFACT_BASE);
116 artifactBase.setProperty(SlcNames.SLC_ARTIFACT_ID,
117 artifact.getArtifactId());
118 artifactBase.setProperty(SlcNames.SLC_GROUP_ID,
119 artifact.getGroupId());
120 }
121 JcrUtils.updateLastModified(artifactBase);
122
123 Node groupBase = artifactBase.getParent();
124 if (!groupBase.isNodeType(SlcTypes.SLC_GROUP_BASE)) {
125 // if (groupBase.isNodeType(SlcTypes.SLC_ARTIFACT_BASE)) {
126 // log.warn("Group base " + groupBase.getPath()
127 // + " is also artifact base");
128 // }
129 groupBase.addMixin(SlcTypes.SLC_GROUP_BASE);
130 groupBase.setProperty(SlcNames.SLC_GROUP_BASE_ID,
131 artifact.getGroupId());
132 }
133 JcrUtils.updateLastModifiedAndParents(groupBase,
134 RepoConstants.DEFAULT_ARTIFACTS_BASE_PATH);
135
136 if (log.isTraceEnabled())
137 log.trace("Indexed artifact " + artifact + " on " + fileNode);
138 } catch (Exception e) {
139 throw new SlcException("Cannot index artifact " + artifact
140 + " metadata on node " + fileNode, e);
141 }
142 }
143
144 private String getRelativePath(String nodePath) {
145 String basePath = RepoConstants.DEFAULT_ARTIFACTS_BASE_PATH;
146 if (!nodePath.startsWith(basePath))
147 return null;
148 String relativePath = nodePath.substring(basePath.length());
149 return relativePath;
150 }
151 }