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