Improve URI wrapper
authorMathieu Baudier <mbaudier@argeo.org>
Fri, 2 Jan 2015 12:29:38 +0000 (12:29 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Fri, 2 Jan 2015 12:29:38 +0000 (12:29 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@7610 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/OsgiFactoryImpl.java
runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/UriWrapper.java

index dd90a26861343afce89b7efb337d124df3b064ad..bd4f4311ce94595120145ce67dfafb8ed58d1984 100644 (file)
@@ -1,10 +1,12 @@
 package org.argeo.slc.repo.osgi;
 
+import java.io.BufferedInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.net.URLConnection;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -15,6 +17,7 @@ import javax.jcr.Repository;
 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;
@@ -186,11 +189,17 @@ public class OsgiFactoryImpl implements OsgiFactory, SlcNames {
                        log.debug("Downloading " + url + "...");
 
                InputStream in = null;
+               URLConnection conn = null;
                Node folderNode = JcrUtils.mkfolders(distSession,
                                JcrUtils.parentPath(path));
                try {
                        URL u = new URL(url);
-                       in = u.openStream();
+                       conn = u.openConnection();
+                       conn.connect();
+                       in = new BufferedInputStream(conn.getInputStream());
+                       // byte[] arr = IOUtils.toByteArray(in);
+                       // Node fileNode = JcrUtils.copyBytesAsFile(folderNode,
+                       // JcrUtils.nodeNameFromPath(path), arr);
                        Node fileNode = JcrUtils.copyStreamAsFile(folderNode,
                                        JcrUtils.nodeNameFromPath(path), in);
                        fileNode.addMixin(SlcTypes.SLC_KNOWN_ORIGIN);
@@ -204,6 +213,8 @@ public class OsgiFactoryImpl implements OsgiFactory, SlcNames {
                        throw e;
                } catch (IOException e) {
                        throw new SlcException("Cannot load " + url + " to " + path, e);
+               } finally {
+                       IOUtils.closeQuietly(in);
                }
 
        }
index 7d717381da7df253dcd9a94d94c434db724becb1..c07fde99e0b7e5cc001dd320d8b4a1098238b3e8 100644 (file)
@@ -1,18 +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.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);
@@ -24,7 +33,7 @@ public class UriWrapper extends BndWrapper implements Runnable {
 
        private OsgiFactory osgiFactory;
 
-       // private SourcesProvider sourcesProvider;
+       private SourcesProvider sourcesProvider;
 
        public UriWrapper() {
                setFactory(this);
@@ -35,7 +44,7 @@ public class UriWrapper extends BndWrapper implements Runnable {
                Session javaSession = null;
                InputStream in;
                ByteArrayOutputStream out;
-               // Jar jar = null;
+               Jar jar = null;
                try {
                        distSession = osgiFactory.openDistSession();
                        javaSession = osgiFactory.openJavaSession();
@@ -48,6 +57,8 @@ public class UriWrapper extends BndWrapper implements Runnable {
                        // TODO factorize with Maven
                        in = sourceArtifact.getNode(Node.JCR_CONTENT)
                                        .getProperty(Property.JCR_DATA).getBinary().getStream();
+                       out=null;
+                       
                        out = new ByteArrayOutputStream();
                        wrapJar(in, out);
                        Node newJarNode = RepoUtils
@@ -59,39 +70,39 @@ public class UriWrapper extends BndWrapper implements Runnable {
                                log.debug("Wrapped " + uri + " to " + newJarNode.getPath());
 
                        // 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());
-                       // }
+                       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 {
                        JcrUtils.logoutQuietly(distSession);
                        JcrUtils.logoutQuietly(javaSession);
-                       // if (jar != null)
-                       // jar.close();
+                       if (jar != null)
+                               jar.close();
                }
        }
 
@@ -110,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;
+       }
 }