]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/UriWrapper.java
Add BSD License.
[gpl/argeo-slc.git] / runtime / org.argeo.slc.repo / src / main / java / org / argeo / slc / repo / osgi / UriWrapper.java
index 45456ad31abb4d525a9b34f1c53636628682913a..cf0422e4309e66dcaed5a124498e3e0bd5d6e247 100644 (file)
@@ -1,19 +1,27 @@
 package org.argeo.slc.repo.osgi;
 
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
+import java.util.List;
+import java.util.zip.ZipOutputStream;
 
 import javax.jcr.Node;
 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 aQute.lib.osgi.Jar;
 
 public class UriWrapper extends BndWrapper implements Runnable {
        private final static Log log = LogFactory.getLog(UriWrapper.class);
@@ -25,11 +33,18 @@ public class UriWrapper extends BndWrapper implements Runnable {
 
        private OsgiFactory osgiFactory;
 
+       private SourcesProvider sourcesProvider;
+
+       public UriWrapper() {
+               setFactory(this);
+       }
+
        public void run() {
                Session distSession = null;
                Session javaSession = null;
-               InputStream in;
-               ByteArrayOutputStream out;
+               InputStream in = null;
+               ByteArrayOutputStream out = null;
+               Jar jar = null;
                try {
                        distSession = osgiFactory.openDistSession();
                        javaSession = osgiFactory.openJavaSession();
@@ -51,11 +66,43 @@ public class UriWrapper extends BndWrapper implements Runnable {
                        newJarNode.getSession().save();
                        if (log.isDebugEnabled())
                                log.debug("Wrapped " + uri + " to " + newJarNode.getPath());
-               } catch (RepositoryException e) {
-                       throw new SlcException("Cannot wrap Maven " + uri, e);
+
+                       // sources
+                       if (sourcesProvider != null) {
+                               IOUtils.closeQuietly(in);
+                               in = new ByteArrayInputStream(out.toByteArray());
+                               jar = new Jar(null, in);
+                               List<String> packages = jar.getPackages();
+
+                               IOUtils.closeQuietly(out);
+                               out = new ByteArrayOutputStream();
+                               sourcesProvider
+                                               .writeSources(packages, new ZipOutputStream(out));
+
+                               IOUtils.closeQuietly(in);
+                               in = new ByteArrayInputStream(out.toByteArray());
+                               byte[] sourcesJar = RepoUtils.packageAsPdeSource(in,
+                                               new DefaultNameVersion(this));
+                               Artifact sourcesArtifact = new DefaultArtifact(getArtifact()
+                                               .getGroupId(), getArtifact().getArtifactId()
+                                               + ".source", "jar", getArtifact().getVersion());
+                               Node sourcesJarNode = RepoUtils.copyBytesAsArtifact(
+                                               javaSession.getRootNode(), sourcesArtifact, sourcesJar);
+                               sourcesJarNode.getSession().save();
+
+                               if (log.isDebugEnabled())
+                                       log.debug("Added sources " + sourcesArtifact
+                                                       + " for bundle " + getArtifact());
+                       }
+               } catch (Exception e) {
+                       throw new SlcException("Cannot wrap URI " + uri, e);
                } finally {
+                       IOUtils.closeQuietly(in);
+                       IOUtils.closeQuietly(out);
                        JcrUtils.logoutQuietly(distSession);
                        JcrUtils.logoutQuietly(javaSession);
+                       if (jar != null)
+                               jar.close();
                }
        }
 
@@ -74,4 +121,8 @@ public class UriWrapper extends BndWrapper implements Runnable {
        public void setVersionSeparator(String versionSeparator) {
                this.versionSeparator = versionSeparator;
        }
+
+       public void setSourcesProvider(SourcesProvider sourcesProvider) {
+               this.sourcesProvider = sourcesProvider;
+       }
 }