1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.argeo.jcr.JcrUtils;
28 import org.argeo.slc.NameVersion;
29 import org.argeo.slc.SlcException;
30 import org.argeo.slc.repo.maven.AetherUtils;
31 import org.argeo.slc.repo.maven.MavenConventionsUtils;
32 import org.eclipse.aether.artifact.Artifact;
33 import org.eclipse.aether.artifact.DefaultArtifact;
34
35
36
37
38
39 public class PdeSourcesIndexer implements NodeIndexer {
40 private Log log = LogFactory.getLog(PdeSourcesIndexer.class);
41
42 private String artifactBasePath = RepoConstants.DEFAULT_ARTIFACTS_BASE_PATH;
43
44
45
46
47
48
49
50
51
52
53
54 public Boolean support(String path) {
55
56 String name = FilenameUtils.getBaseName(path);
57
58
59
60 return name.endsWith("-sources") && FilenameUtils.getExtension(path).equals("jar");
61 }
62
63 public void index(Node sourcesNode) {
64 try {
65 if (!support(sourcesNode.getPath()))
66 return;
67
68 packageSourcesAsPdeSource(sourcesNode);
69 } catch (Exception e) {
70 throw new SlcException("Cannot generate pde sources for node " + sourcesNode, e);
71 }
72 }
73
74 protected void packageSourcesAsPdeSource(Node sourcesNode) {
75 Binary origBinary = null;
76 Binary osgiBinary = null;
77 try {
78 Session session = sourcesNode.getSession();
79 Artifact sourcesArtifact = AetherUtils.convertPathToArtifact(sourcesNode.getPath(), null);
80
81
82 Artifact osgiArtifact = new DefaultArtifact(sourcesArtifact.getGroupId(), sourcesArtifact.getArtifactId(),
83 sourcesArtifact.getExtension(), sourcesArtifact.getVersion());
84 String osgiPath = MavenConventionsUtils.artifactPath(artifactBasePath, osgiArtifact);
85 osgiBinary = session.getNode(osgiPath).getNode(Node.JCR_CONTENT).getProperty(Property.JCR_DATA).getBinary();
86
87 NameVersion nameVersion = RepoUtils.readNameVersion(osgiBinary.getStream());
88 if (nameVersion == null) {
89 log.warn("Cannot package PDE sources for " + osgiPath + " as it is probably not an OSGi bundle");
90 return;
91 }
92
93
94 Artifact pdeSourceArtifact = new DefaultArtifact(sourcesArtifact.getGroupId(),
95 sourcesArtifact.getArtifactId() + ".source", sourcesArtifact.getExtension(),
96 sourcesArtifact.getVersion());
97 String targetSourceParentPath = MavenConventionsUtils.artifactParentPath(artifactBasePath,
98 pdeSourceArtifact);
99 String targetSourceFileName = MavenConventionsUtils.artifactFileName(pdeSourceArtifact);
100
101
102
103 Node targetSourceParentNode = JcrUtils.mkfolders(session, targetSourceParentPath);
104 origBinary = sourcesNode.getNode(Node.JCR_CONTENT).getProperty(Property.JCR_DATA).getBinary();
105 byte[] targetJarBytes = RepoUtils.packageAsPdeSource(origBinary.getStream(), nameVersion);
106 JcrUtils.copyBytesAsFile(targetSourceParentNode, targetSourceFileName, targetJarBytes);
107
108
109
110
111
112
113
114 if (log.isTraceEnabled())
115 log.trace("Created pde source artifact " + pdeSourceArtifact + " from " + sourcesNode);
116
117 } catch (RepositoryException e) {
118 throw new SlcException("Cannot add PDE sources for " + sourcesNode, e);
119 } finally {
120 JcrUtils.closeQuietly(origBinary);
121 JcrUtils.closeQuietly(osgiBinary);
122 }
123 }
124 }