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