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