]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/MavenWrapper.java
Fix Maven sources download
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / osgi / MavenWrapper.java
index 2942e212bf1282a6271cd9ecf60475f69080bccf..3de52c53c9950510d9f87ceae677788277b95f7d 100644 (file)
@@ -8,12 +8,17 @@ import javax.jcr.Property;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.jcr.JcrUtils;
+import org.argeo.slc.DefaultNameVersion;
 import org.argeo.slc.SlcException;
 import org.argeo.slc.repo.OsgiFactory;
 import org.argeo.slc.repo.RepoUtils;
+import org.sonatype.aether.artifact.Artifact;
+import org.sonatype.aether.util.artifact.DefaultArtifact;
+import org.sonatype.aether.util.artifact.SubArtifact;
 
 public class MavenWrapper extends BndWrapper implements Runnable {
        private final static Log log = LogFactory.getLog(MavenWrapper.class);
@@ -34,10 +39,15 @@ public class MavenWrapper extends BndWrapper implements Runnable {
                try {
                        distSession = osgiFactory.openDistSession();
                        javaSession = osgiFactory.openJavaSession();
-                       Node sourceArtifact = osgiFactory.getMaven(distSession,
-                                       sourceCoords);
+                       Node origArtifact;
+                       try {
+                               origArtifact = osgiFactory.getMaven(distSession, sourceCoords);
+                       } catch (Exception e1) {
+                               origArtifact = osgiFactory.getMaven(distSession, sourceCoords
+                                               + ":" + getVersion());
+                       }
 
-                       in = sourceArtifact.getNode(Node.JCR_CONTENT)
+                       in = origArtifact.getNode(Node.JCR_CONTENT)
                                        .getProperty(Property.JCR_DATA).getBinary().getStream();
                        out = new ByteArrayOutputStream();
                        wrapJar(in, out);
@@ -46,9 +56,38 @@ public class MavenWrapper extends BndWrapper implements Runnable {
                                                        getArtifact(), out.toByteArray());
                        osgiFactory.indexNode(newJarNode);
                        newJarNode.getSession().save();
+
                        if (log.isDebugEnabled())
                                log.debug("Wrapped Maven " + sourceCoords + " to "
                                                + newJarNode.getPath());
+
+                       // sources
+                       Artifact sourcesArtifact = new SubArtifact(new DefaultArtifact(
+                                       sourceCoords), "sources", null);
+                       Node sourcesArtifactNode;
+                       try {
+
+                               sourcesArtifactNode = osgiFactory.getMaven(distSession,
+                                               sourcesArtifact.toString());
+                       } catch (SlcException e) {
+                               // no sources available
+                               return;
+                       }
+
+                       IOUtils.closeQuietly(in);
+                       in = sourcesArtifactNode.getNode(Node.JCR_CONTENT)
+                                       .getProperty(Property.JCR_DATA).getBinary().getStream();
+                       byte[] pdeSource = RepoUtils.packageAsPdeSource(in,
+                                       new DefaultNameVersion(getName(), getVersion()));
+                       Node pdeSourceNode = RepoUtils.copyBytesAsArtifact(javaSession
+                                       .getRootNode(), new DefaultArtifact(getCategory(),
+                                       getName() + ".source", "jar", getVersion()), pdeSource);
+                       osgiFactory.indexNode(pdeSourceNode);
+                       pdeSourceNode.getSession().save();
+
+                       if (log.isDebugEnabled())
+                               log.debug("Wrapped Maven " + sourcesArtifact
+                                               + " to PDE sources " + pdeSourceNode.getPath());
                } catch (RepositoryException e) {
                        throw new SlcException("Cannot wrap Maven " + sourceCoords, e);
                } finally {