]> git.argeo.org Git - gpl/argeo-slc.git/blob - MavenWrapper.java
a5b6ec9341501e08650a405a92dfc97853baf91e
[gpl/argeo-slc.git] / MavenWrapper.java
1 package org.argeo.slc.repo.osgi;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.InputStream;
5
6 import javax.jcr.Node;
7 import javax.jcr.Property;
8 import javax.jcr.Session;
9
10 import org.apache.commons.io.IOUtils;
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.argeo.jcr.JcrUtils;
14 import org.argeo.slc.DefaultNameVersion;
15 import org.argeo.slc.SlcException;
16 import org.argeo.slc.repo.OsgiFactory;
17 import org.argeo.slc.repo.RepoUtils;
18 import org.eclipse.aether.artifact.Artifact;
19 import org.eclipse.aether.artifact.DefaultArtifact;
20 import org.eclipse.aether.util.artifact.SubArtifact;
21
22 /**
23 * BND wrapper based on a Maven artifact available from one of the configured
24 * repositories.
25 */
26 public class MavenWrapper extends BndWrapper implements Runnable {
27 private final static Log log = LogFactory.getLog(MavenWrapper.class);
28
29 private String sourceCoords;
30
31 private OsgiFactory osgiFactory;
32
33 private Boolean doNotModifySources = false;
34
35 public MavenWrapper() {
36 setFactory(this);
37 }
38
39 @Override
40 public String getVersion() {
41 String version = super.getVersion();
42 if (version != null)
43 return version;
44 return new DefaultArtifact(sourceCoords).getVersion();
45 }
46
47 public void run() {
48 Session distSession = null;
49 Session javaSession = null;
50 InputStream in = null;
51 ByteArrayOutputStream out = null;
52 try {
53 distSession = osgiFactory.openDistSession();
54 javaSession = osgiFactory.openJavaSession();
55 Node origArtifact;
56 try {
57 origArtifact = osgiFactory.getMaven(distSession, sourceCoords);
58 } catch (Exception e1) {
59 origArtifact = osgiFactory.getMaven(distSession, sourceCoords
60 + ":" + getVersion());
61 }
62
63 in = origArtifact.getNode(Node.JCR_CONTENT)
64 .getProperty(Property.JCR_DATA).getBinary().getStream();
65 out = new ByteArrayOutputStream();
66 wrapJar(in, out);
67 Node newJarNode = RepoUtils
68 .copyBytesAsArtifact(javaSession.getRootNode(),
69 getArtifact(), out.toByteArray());
70 osgiFactory.indexNode(newJarNode);
71 newJarNode.getSession().save();
72
73 if (log.isDebugEnabled())
74 log.debug("Wrapped Maven " + sourceCoords + " to "
75 + newJarNode.getPath());
76
77 // sources
78 Artifact sourcesArtifact = new SubArtifact(new DefaultArtifact(
79 sourceCoords), "sources", null);
80 Node sourcesArtifactNode;
81 try {
82
83 sourcesArtifactNode = osgiFactory.getMaven(distSession,
84 sourcesArtifact.toString());
85 } catch (SlcException e) {
86 // no sources available
87 return;
88 }
89
90 IOUtils.closeQuietly(in);
91 in = sourcesArtifactNode.getNode(Node.JCR_CONTENT)
92 .getProperty(Property.JCR_DATA).getBinary().getStream();
93 byte[] pdeSource;
94 if (doNotModifySources)
95 pdeSource = IOUtils.toByteArray(in);
96 else
97 pdeSource = RepoUtils.packageAsPdeSource(in,
98 new DefaultNameVersion(getName(), getVersion()));
99 Node pdeSourceNode = RepoUtils.copyBytesAsArtifact(javaSession
100 .getRootNode(), new DefaultArtifact(getCategory(),
101 getName() + ".source", "jar", getVersion()), pdeSource);
102 osgiFactory.indexNode(pdeSourceNode);
103 pdeSourceNode.getSession().save();
104
105 if (log.isDebugEnabled())
106 log.debug("Wrapped Maven " + sourcesArtifact
107 + " to PDE sources " + pdeSourceNode.getPath());
108 } catch (Exception e) {
109 throw new SlcException("Cannot wrap Maven " + sourceCoords, e);
110 } finally {
111 JcrUtils.logoutQuietly(distSession);
112 JcrUtils.logoutQuietly(javaSession);
113 IOUtils.closeQuietly(in);
114 IOUtils.closeQuietly(out);
115 }
116 }
117
118 public void setSourceCoords(String sourceCoords) {
119 this.sourceCoords = sourceCoords;
120 }
121
122 public void setOsgiFactory(OsgiFactory osgiFactory) {
123 this.osgiFactory = osgiFactory;
124 }
125
126 public void setDoNotModifySources(Boolean doNotModifySources) {
127 this.doNotModifySources = doNotModifySources;
128 }
129
130 }