]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/PdeSourcesIndexer.java
Clean, update comments
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / PdeSourcesIndexer.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.Binary;
19 import javax.jcr.Node;
20 import javax.jcr.Property;
21 import javax.jcr.RepositoryException;
22 import javax.jcr.Session;
23
24 import org.apache.commons.io.FilenameUtils;
25 import org.argeo.jcr.JcrUtils;
26 import org.argeo.slc.NameVersion;
27 import org.argeo.slc.SlcException;
28 import org.argeo.slc.aether.AetherUtils;
29 import org.argeo.slc.repo.maven.MavenConventionsUtils;
30 import org.sonatype.aether.artifact.Artifact;
31 import org.sonatype.aether.util.artifact.DefaultArtifact;
32
33 /**
34 * Creates pde sources from a source {@link Artifact} with name
35 * "...-sources.jar"
36 */
37 public class PdeSourcesIndexer implements NodeIndexer {
38 // private Log log = LogFactory.getLog(PdeSourcesIndexer.class);
39
40 private String artifactBasePath = RepoConstants.DEFAULT_ARTIFACTS_BASE_PATH;
41
42 private ArtifactIndexer artifactIndexer;
43 private JarFileIndexer jarFileIndexer;
44
45 public PdeSourcesIndexer(ArtifactIndexer artifactIndexer,
46 JarFileIndexer jarFileIndexer) {
47 this.artifactIndexer = artifactIndexer;
48 this.jarFileIndexer = jarFileIndexer;
49 }
50
51 public Boolean support(String path) {
52 // TODO implement clean management of same name siblings
53 String name = FilenameUtils.getBaseName(path);
54 // int lastInd = name.lastIndexOf("[");
55 // if (lastInd != -1)
56 // name = name.substring(0, lastInd);
57 return name.endsWith("-sources")
58 && FilenameUtils.getExtension(path).equals("jar");
59 }
60
61 public void index(Node sourcesNode) {
62 try {
63 packageSourcesAsPdeSource(sourcesNode);
64 } catch (Exception e) {
65 throw new SlcException("Cannot generate pde sources for node "
66 + sourcesNode, e);
67 }
68 }
69
70 protected void packageSourcesAsPdeSource(Node sourcesNode) {
71 Binary origBinary = null;
72 Binary osgiBinary = null;
73 try {
74 Session session = sourcesNode.getSession();
75 Artifact sourcesArtifact = AetherUtils.convertPathToArtifact(
76 sourcesNode.getPath(), null);
77
78 // read name version from manifest
79 Artifact osgiArtifact = new DefaultArtifact(
80 sourcesArtifact.getGroupId(),
81 sourcesArtifact.getArtifactId(),
82 sourcesArtifact.getExtension(),
83 sourcesArtifact.getVersion());
84 String osgiPath = MavenConventionsUtils.artifactPath(
85 artifactBasePath, osgiArtifact);
86 osgiBinary = session.getNode(osgiPath).getNode(Node.JCR_CONTENT)
87 .getProperty(Property.JCR_DATA).getBinary();
88
89 NameVersion nameVersion = RepoUtils.readNameVersion(osgiBinary
90 .getStream());
91
92 // create PDe sources artifact
93 Artifact pdeSourceArtifact = new DefaultArtifact(
94 sourcesArtifact.getGroupId(),
95 sourcesArtifact.getArtifactId() + ".source",
96 sourcesArtifact.getExtension(),
97 sourcesArtifact.getVersion());
98 String targetSourceParentPath = MavenConventionsUtils
99 .artifactParentPath(artifactBasePath, pdeSourceArtifact);
100 String targetSourceFileName = MavenConventionsUtils
101 .artifactFileName(pdeSourceArtifact);
102 String targetSourceJarPath = targetSourceParentPath + '/'
103 + targetSourceFileName;
104
105 Node targetSourceParentNode = JcrUtils.mkfolders(session,
106 targetSourceParentPath);
107 origBinary = sourcesNode.getNode(Node.JCR_CONTENT)
108 .getProperty(Property.JCR_DATA).getBinary();
109 byte[] targetJarBytes = RepoUtils.packageAsPdeSource(
110 origBinary.getStream(), nameVersion);
111 JcrUtils.copyBytesAsFile(targetSourceParentNode,
112 targetSourceFileName, targetJarBytes);
113
114 // reindex
115 Node targetSourceJarNode = session.getNode(targetSourceJarPath);
116 artifactIndexer.index(targetSourceJarNode);
117 jarFileIndexer.index(targetSourceJarNode);
118 } catch (RepositoryException e) {
119 throw new SlcException("Cannot add PDE sources for " + sourcesNode,
120 e);
121 } finally {
122 JcrUtils.closeQuietly(origBinary);
123 JcrUtils.closeQuietly(osgiBinary);
124 }
125 }
126 }