1 package org
.argeo
.slc
.repo
;
3 import javax
.jcr
.Binary
;
5 import javax
.jcr
.Property
;
6 import javax
.jcr
.RepositoryException
;
7 import javax
.jcr
.Session
;
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
;
21 * Creates pde sources from a source {@link Artifact} with name
24 public class PdeSourcesIndexer
implements NodeIndexer
{
25 private Log log
= LogFactory
.getLog(PdeSourcesIndexer
.class);
27 private String artifactBasePath
= RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
;
29 // private ArtifactIndexer artifactIndexer;
30 // private JarFileIndexer jarFileIndexer;
32 // public PdeSourcesIndexer(){
33 // // ArtifactIndexer artifactIndexer,
34 // // JarFileIndexer jarFileIndexer) {
35 // // this.artifactIndexer = artifactIndexer;
36 // // this.jarFileIndexer = jarFileIndexer;
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("[");
44 // name = name.substring(0, lastInd);
45 return name
.endsWith("-sources") && FilenameUtils
.getExtension(path
).equals("jar");
48 public void index(Node sourcesNode
) {
50 if (!support(sourcesNode
.getPath()))
53 packageSourcesAsPdeSource(sourcesNode
);
54 } catch (Exception e
) {
55 throw new SlcException("Cannot generate pde sources for node " + sourcesNode
, e
);
59 protected void packageSourcesAsPdeSource(Node sourcesNode
) {
60 Binary origBinary
= null;
61 Binary osgiBinary
= null;
63 Session session
= sourcesNode
.getSession();
64 Artifact sourcesArtifact
= AetherUtils
.convertPathToArtifact(sourcesNode
.getPath(), null);
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();
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");
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
,
84 String targetSourceFileName
= MavenConventionsUtils
.artifactFileName(pdeSourceArtifact
);
85 // String targetSourceJarPath = targetSourceParentPath + '/'
86 // + targetSourceFileName;
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
);
94 // Automagically done via the various listeners or manually
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
);
102 } catch (RepositoryException e
) {
103 throw new SlcException("Cannot add PDE sources for " + sourcesNode
, e
);
105 JcrUtils
.closeQuietly(origBinary
);
106 JcrUtils
.closeQuietly(osgiBinary
);