]> git.argeo.org Git - gpl/argeo-slc.git/commitdiff
Improve 'do not modify' mode
authorMathieu Baudier <mbaudier@argeo.org>
Fri, 2 Jan 2015 14:15:51 +0000 (14:15 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Fri, 2 Jan 2015 14:15:51 +0000 (14:15 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@7611 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

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

index 6601db0b93bc708d6e6de51b98a1ac9512e1c345..7b75b903d4609df31054fc876042ad05c8c4a767 100644 (file)
@@ -1,10 +1,12 @@
 package org.argeo.slc.repo.osgi;
 
+import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.Properties;
 import java.util.jar.Manifest;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.slc.CategorizedNameVersion;
@@ -38,9 +40,11 @@ public class BndWrapper implements Constants, CategorizedNameVersion,
 
        public void wrapJar(InputStream in, OutputStream out) {
                Builder b = new Builder();
+               Jar jar = null;
                try {
-                       Jar jar = new Jar(null, in);
+                       byte[] jarBytes = IOUtils.toByteArray(in);
 
+                       jar = new Jar(null, new ByteArrayInputStream(jarBytes));
                        Manifest sourceManifest = jar.getManifest();
 
                        Version versionToUse;
@@ -58,15 +62,15 @@ public class BndWrapper implements Constants, CategorizedNameVersion,
                                // Version
                                String sourceVersion = sourceManifest.getMainAttributes()
                                                .getValue(BUNDLE_VERSION);
-                               if (version == null && sourceVersion == null) {
+                               if (getVersion() == null && sourceVersion == null) {
                                        throw new SlcException("A bundle version must be defined.");
-                               } else if (version == null && sourceVersion != null) {
+                               } else if (getVersion() == null && sourceVersion != null) {
                                        versionToUse = new Version(sourceVersion);
                                        version = sourceVersion; // set wrapper version
-                               } else if (version != null && sourceVersion == null) {
-                                       versionToUse = new Version(version);
+                               } else if (getVersion() != null && sourceVersion == null) {
+                                       versionToUse = new Version(getVersion());
                                } else {// both set
-                                       versionToUse = new Version(version);
+                                       versionToUse = new Version(getVersion());
                                        Version sv = new Version(sourceVersion);
                                        if (versionToUse.getMajor() != sv.getMajor()
                                                        || versionToUse.getMinor() != sv.getMinor()
@@ -78,11 +82,12 @@ public class BndWrapper implements Constants, CategorizedNameVersion,
                                        }
                                }
                        } else {
-                               versionToUse = new Version(version);
+                               versionToUse = new Version(getVersion());
                        }
 
                        if (doNotModify) {
-                               jar.write(out);
+                               IOUtils.write(jarBytes, out);
+                               // jar.write(out);
                        } else {
 
                                Properties properties = new Properties();
@@ -114,11 +119,14 @@ public class BndWrapper implements Constants, CategorizedNameVersion,
 
                                Jar newJar = b.build();
                                newJar.write(out);
+                               newJar.close();
                        }
                } catch (Exception e) {
                        throw new SlcException("Cannot wrap jar", e);
                } finally {
                        b.close();
+                       if (jar != null)
+                               jar.close();
                }
 
        }
@@ -196,7 +204,7 @@ public class BndWrapper implements Constants, CategorizedNameVersion,
        }
 
        public Artifact getArtifact() {
-               return new DefaultArtifact(groupId, name, "jar", version);
+               return new DefaultArtifact(groupId, name, "jar", getVersion());
        }
 
        @Override
index 3de52c53c9950510d9f87ceae677788277b95f7d..085e1ddfd73e944f6b4e94c8994a99ae53ee7135 100644 (file)
@@ -20,6 +20,10 @@ import org.sonatype.aether.artifact.Artifact;
 import org.sonatype.aether.util.artifact.DefaultArtifact;
 import org.sonatype.aether.util.artifact.SubArtifact;
 
+/**
+ * BND wrapper based on a Maven artifact available from one of the configured
+ * repositories.
+ */
 public class MavenWrapper extends BndWrapper implements Runnable {
        private final static Log log = LogFactory.getLog(MavenWrapper.class);
 
@@ -31,6 +35,14 @@ public class MavenWrapper extends BndWrapper implements Runnable {
                setFactory(this);
        }
 
+       @Override
+       public String getVersion() {
+               String version = super.getVersion();
+               if (version != null)
+                       return version;
+               return new DefaultArtifact(sourceCoords).getVersion();
+       }
+
        public void run() {
                Session distSession = null;
                Session javaSession = null;
index c07fde99e0b7e5cc001dd320d8b4a1098238b3e8..cf0422e4309e66dcaed5a124498e3e0bd5d6e247 100644 (file)
@@ -42,8 +42,8 @@ public class UriWrapper extends BndWrapper implements Runnable {
        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();
@@ -57,8 +57,6 @@ 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
@@ -99,6 +97,8 @@ public class UriWrapper extends BndWrapper implements Runnable {
                } 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)