]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/MavenWrapper.java
Fix some glitches in the normalization
[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 private Boolean doNotModifySources = false;
35
36 public MavenWrapper() {
37 setFactory(this);
38 }
39
40 @Override
41 public String getVersion() {
42 String version = super.getVersion();
43 if (version != null)
44 return version;
45 return new DefaultArtifact(sourceCoords).getVersion();
46 }
47
48 public void run() {
49 Session distSession = null;
50 Session javaSession = null;
51 InputStream in = null;
52 ByteArrayOutputStream out = null;
53 try {
54 distSession = osgiFactory.openDistSession();
55 javaSession = osgiFactory.openJavaSession();
56 Node origArtifact;
57 try {
58 origArtifact = osgiFactory.getMaven(distSession, sourceCoords);
59 } catch (Exception e1) {
60 origArtifact = osgiFactory.getMaven(distSession, sourceCoords
61 + ":" + getVersion());
62 }
63
64 in = origArtifact.getNode(Node.JCR_CONTENT)
65 .getProperty(Property.JCR_DATA).getBinary().getStream();
66 out = new ByteArrayOutputStream();
67 wrapJar(in, out);
68 Node newJarNode = RepoUtils
69 .copyBytesAsArtifact(javaSession.getRootNode(),
70 getArtifact(), out.toByteArray());
71 osgiFactory.indexNode(newJarNode);
72 newJarNode.getSession().save();
73
74 if (log.isDebugEnabled())
75 log.debug("Wrapped Maven " + sourceCoords + " to "
76 + newJarNode.getPath());
77
78 // sources
79 Artifact sourcesArtifact = new SubArtifact(new DefaultArtifact(
80 sourceCoords), "sources", null);
81 Node sourcesArtifactNode;
82 try {
83
84 sourcesArtifactNode = osgiFactory.getMaven(distSession,
85 sourcesArtifact.toString());
86 } catch (SlcException e) {
87 // no sources available
88 return;
89 }
90
91 IOUtils.closeQuietly(in);
92 in = sourcesArtifactNode.getNode(Node.JCR_CONTENT)
93 .getProperty(Property.JCR_DATA).getBinary().getStream();
94 byte[] pdeSource;
95 if (doNotModifySources)
96 pdeSource = IOUtils.toByteArray(in);
97 else
98 pdeSource = RepoUtils.packageAsPdeSource(in,
99 new DefaultNameVersion(getName(), getVersion()));
100 Node pdeSourceNode = RepoUtils.copyBytesAsArtifact(javaSession
101 .getRootNode(), new DefaultArtifact(getCategory(),
102 getName() + ".source", "jar", getVersion()), pdeSource);
103 osgiFactory.indexNode(pdeSourceNode);
104 pdeSourceNode.getSession().save();
105
106 if (log.isDebugEnabled())
107 log.debug("Wrapped Maven " + sourcesArtifact
108 + " to PDE sources " + pdeSourceNode.getPath());
109 } catch (Exception e) {
110 throw new SlcException("Cannot wrap Maven " + sourceCoords, e);
111 } finally {
112 JcrUtils.logoutQuietly(distSession);
113 JcrUtils.logoutQuietly(javaSession);
114 IOUtils.closeQuietly(in);
115 IOUtils.closeQuietly(out);
116 }
117 }
118
119 public void setSourceCoords(String sourceCoords) {
120 this.sourceCoords = sourceCoords;
121 }
122
123 public void setOsgiFactory(OsgiFactory osgiFactory) {
124 this.osgiFactory = osgiFactory;
125 }
126
127 public void setDoNotModifySources(Boolean doNotModifySources) {
128 this.doNotModifySources = doNotModifySources;
129 }
130
131 }