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
.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
;
20 * Creates pde sources from a source {@link Artifact} with name
23 public class PdeSourcesIndexer
implements NodeIndexer
{
24 private CmsLog log
= CmsLog
.getLog(PdeSourcesIndexer
.class);
26 private String artifactBasePath
= RepoConstants
.DEFAULT_ARTIFACTS_BASE_PATH
;
28 // private ArtifactIndexer artifactIndexer;
29 // private JarFileIndexer jarFileIndexer;
31 // public PdeSourcesIndexer(){
32 // // ArtifactIndexer artifactIndexer,
33 // // JarFileIndexer jarFileIndexer) {
34 // // this.artifactIndexer = artifactIndexer;
35 // // this.jarFileIndexer = jarFileIndexer;
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("[");
43 // name = name.substring(0, lastInd);
44 return name
.endsWith("-sources") && FilenameUtils
.getExtension(path
).equals("jar");
47 public void index(Node sourcesNode
) {
49 if (!support(sourcesNode
.getPath()))
52 packageSourcesAsPdeSource(sourcesNode
);
53 } catch (Exception e
) {
54 throw new SlcException("Cannot generate pde sources for node " + sourcesNode
, e
);
58 protected void packageSourcesAsPdeSource(Node sourcesNode
) {
59 Binary origBinary
= null;
60 Binary osgiBinary
= null;
62 Session session
= sourcesNode
.getSession();
63 Artifact sourcesArtifact
= AetherUtils
.convertPathToArtifact(sourcesNode
.getPath(), null);
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();
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");
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
,
83 String targetSourceFileName
= MavenConventionsUtils
.artifactFileName(pdeSourceArtifact
);
84 // String targetSourceJarPath = targetSourceParentPath + '/'
85 // + targetSourceFileName;
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
);
93 // Automagically done via the various listeners or manually
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
);
101 } catch (RepositoryException e
) {
102 throw new SlcException("Cannot add PDE sources for " + sourcesNode
, e
);
104 JcrUtils
.closeQuietly(origBinary
);
105 JcrUtils
.closeQuietly(osgiBinary
);