]> git.argeo.org Git - gpl/argeo-slc.git/blob - PdeSourcesIndexer.java
6b8aee95eee15fd3d2613c0dfddefc435fe0d229
[gpl/argeo-slc.git] / PdeSourcesIndexer.java
1 package org.argeo.slc.repo;
2
3 import javax.jcr.Binary;
4 import javax.jcr.Node;
5 import javax.jcr.Property;
6 import javax.jcr.RepositoryException;
7 import javax.jcr.Session;
8
9 import org.apache.commons.io.FilenameUtils;
10 import org.argeo.api.cms.CmsLog;
11 import org.argeo.jcr.JcrUtils;
12 import org.argeo.slc.NameVersion;
13 import org.argeo.slc.SlcException;
14 import org.argeo.slc.repo.maven.AetherUtils;
15 import org.argeo.slc.repo.maven.MavenConventionsUtils;
16 import org.eclipse.aether.artifact.Artifact;
17 import org.eclipse.aether.artifact.DefaultArtifact;
18
19 /**
20 * Creates pde sources from a source {@link Artifact} with name
21 * "...-sources.jar"
22 */
23 public class PdeSourcesIndexer implements NodeIndexer {
24 private CmsLog log = CmsLog.getLog(PdeSourcesIndexer.class);
25
26 private String artifactBasePath = RepoConstants.DEFAULT_ARTIFACTS_BASE_PATH;
27
28 // private ArtifactIndexer artifactIndexer;
29 // private JarFileIndexer jarFileIndexer;
30
31 // public PdeSourcesIndexer(){
32 // // ArtifactIndexer artifactIndexer,
33 // // JarFileIndexer jarFileIndexer) {
34 // // this.artifactIndexer = artifactIndexer;
35 // // this.jarFileIndexer = jarFileIndexer;
36 // }
37
38 public Boolean support(String path) {
39 // TODO implement clean management of same name siblings
40 String name = FilenameUtils.getBaseName(path);
41 // int lastInd = name.lastIndexOf("[");
42 // if (lastInd != -1)
43 // name = name.substring(0, lastInd);
44 return name.endsWith("-sources") && FilenameUtils.getExtension(path).equals("jar");
45 }
46
47 public void index(Node sourcesNode) {
48 try {
49 if (!support(sourcesNode.getPath()))
50 return;
51
52 packageSourcesAsPdeSource(sourcesNode);
53 } catch (Exception e) {
54 throw new SlcException("Cannot generate pde sources for node " + sourcesNode, e);
55 }
56 }
57
58 protected void packageSourcesAsPdeSource(Node sourcesNode) {
59 Binary origBinary = null;
60 Binary osgiBinary = null;
61 try {
62 Session session = sourcesNode.getSession();
63 Artifact sourcesArtifact = AetherUtils.convertPathToArtifact(sourcesNode.getPath(), null);
64
65 // read name version from manifest
66 Artifact osgiArtifact = new DefaultArtifact(sourcesArtifact.getGroupId(), sourcesArtifact.getArtifactId(),
67 sourcesArtifact.getExtension(), sourcesArtifact.getVersion());
68 String osgiPath = MavenConventionsUtils.artifactPath(artifactBasePath, osgiArtifact);
69 osgiBinary = session.getNode(osgiPath).getNode(Node.JCR_CONTENT).getProperty(Property.JCR_DATA).getBinary();
70
71 NameVersion nameVersion = RepoUtils.readNameVersion(osgiBinary.getStream());
72 if (nameVersion == null) {
73 log.warn("Cannot package PDE sources for " + osgiPath + " as it is probably not an OSGi bundle");
74 return;
75 }
76
77 // create PDe sources artifact
78 Artifact pdeSourceArtifact = new DefaultArtifact(sourcesArtifact.getGroupId(),
79 sourcesArtifact.getArtifactId() + ".source", sourcesArtifact.getExtension(),
80 sourcesArtifact.getVersion());
81 String targetSourceParentPath = MavenConventionsUtils.artifactParentPath(artifactBasePath,
82 pdeSourceArtifact);
83 String targetSourceFileName = MavenConventionsUtils.artifactFileName(pdeSourceArtifact);
84 // String targetSourceJarPath = targetSourceParentPath + '/'
85 // + targetSourceFileName;
86
87 Node targetSourceParentNode = JcrUtils.mkfolders(session, targetSourceParentPath);
88 origBinary = sourcesNode.getNode(Node.JCR_CONTENT).getProperty(Property.JCR_DATA).getBinary();
89 byte[] targetJarBytes = RepoUtils.packageAsPdeSource(origBinary.getStream(), nameVersion);
90 JcrUtils.copyBytesAsFile(targetSourceParentNode, targetSourceFileName, targetJarBytes);
91
92 // reindex
93 // Automagically done via the various listeners or manually
94 // triggered.
95 // Node targetSourceJarNode = session.getNode(targetSourceJarPath);
96 // artifactIndexer.index(targetSourceJarNode);
97 // jarFileIndexer.index(targetSourceJarNode);
98 if (log.isTraceEnabled())
99 log.trace("Created pde source artifact " + pdeSourceArtifact + " from " + sourcesNode);
100
101 } catch (RepositoryException e) {
102 throw new SlcException("Cannot add PDE sources for " + sourcesNode, e);
103 } finally {
104 JcrUtils.closeQuietly(origBinary);
105 JcrUtils.closeQuietly(osgiBinary);
106 }
107 }
108 }