Improve distribution
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 26 Jan 2014 12:24:20 +0000 (12:24 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 26 Jan 2014 12:24:20 +0000 (12:24 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@6767 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ArgeoOsgiDistribution.java
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/ProcessDistribution.java [new file with mode: 0644]
runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/UriWrapper.java [new file with mode: 0644]

index 5c9ffccdeb9f29145d52af98e4254c5969abaeb9..f0ab4e2e487d5e6a87f17d5d7dd66ef4ab9d19c8 100644 (file)
@@ -6,12 +6,15 @@ import java.util.List;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.argeo.slc.CategorizedNameVersion;
 import org.argeo.slc.ModuleSet;
 import org.argeo.slc.NameVersion;
 import org.argeo.slc.build.Distribution;
 import org.argeo.slc.build.ModularDistribution;
 import org.argeo.slc.execution.ExecutionFlow;
 import org.argeo.slc.repo.ArtifactDistribution;
+import org.sonatype.aether.artifact.Artifact;
+import org.sonatype.aether.util.artifact.DefaultArtifact;
 
 /** A consistent and versioned OSGi distribution, which can be built and tested. */
 public class ArgeoOsgiDistribution extends ArtifactDistribution implements
index 55ad84dfdcfc2cfc7de1d367b8c075b245722a7e..f1acf49e4d8c54fb2e10c3d953ab73db1455beba 100644 (file)
@@ -29,6 +29,8 @@ public class BndWrapper implements Constants, CategorizedNameVersion,
        private String version;
        private Properties bndProperties = new Properties();
 
+       private Boolean doNotModify = false;
+
        public void wrapJar(InputStream in, OutputStream out) {
                Builder b = new Builder();
                try {
@@ -72,19 +74,25 @@ public class BndWrapper implements Constants, CategorizedNameVersion,
                                versionToUse = new Version(version);
                        }
 
-                       Properties properties = new Properties();
-                       properties.putAll(bndProperties);
-                       properties.setProperty(BUNDLE_SYMBOLICNAME, name);
-                       properties.setProperty(BUNDLE_VERSION, versionToUse.toString());
+                       if (doNotModify) {
+                               jar.write(out);
+                       } else {
+
+                               Properties properties = new Properties();
+                               properties.putAll(bndProperties);
+                               properties.setProperty(BUNDLE_SYMBOLICNAME, name);
+                               properties.setProperty(BUNDLE_VERSION, versionToUse.toString());
 
-                       // b.addIncluded(jarFile);
-                       b.addClasspath(jar);
+                               // b.addIncluded(jarFile);
+                               b.addClasspath(jar);
 
-                       log.debug(properties);
-                       b.setProperties(properties);
+                               if (log.isDebugEnabled())
+                                       log.debug(properties);
+                               b.setProperties(properties);
 
-                       Jar newJar = b.build();
-                       newJar.write(out);
+                               Jar newJar = b.build();
+                               newJar.write(out);
+                       }
                } catch (Exception e) {
                        throw new SlcException("Cannot wrap jar", e);
                } finally {
@@ -162,4 +170,8 @@ public class BndWrapper implements Constants, CategorizedNameVersion,
                        return false;
        }
 
+       public void setDoNotModify(Boolean doNotModify) {
+               this.doNotModify = doNotModify;
+       }
+
 }
diff --git a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ProcessDistribution.java b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/ProcessDistribution.java
new file mode 100644 (file)
index 0000000..26f0639
--- /dev/null
@@ -0,0 +1,67 @@
+package org.argeo.slc.repo.osgi;
+
+import java.util.Iterator;
+
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.jcr.JcrUtils;
+import org.argeo.slc.CategorizedNameVersion;
+import org.argeo.slc.NameVersion;
+import org.argeo.slc.SlcException;
+import org.argeo.slc.repo.OsgiFactory;
+import org.argeo.slc.repo.maven.MavenConventionsUtils;
+import org.sonatype.aether.artifact.Artifact;
+import org.sonatype.aether.util.artifact.DefaultArtifact;
+
+public class ProcessDistribution implements Runnable {
+       private final static Log log = LogFactory.getLog(ProcessDistribution.class);
+
+       private ArgeoOsgiDistribution osgiDistribution;
+       private OsgiFactory osgiFactory;
+
+       public void run() {
+               Session javaSession = null;
+               try {
+                       javaSession = osgiFactory.openJavaSession();
+
+                       Iterator<NameVersion> it = osgiDistribution.nameVersions();
+                       while (it.hasNext()) {
+                               NameVersion t = it.next();
+                               if (log.isDebugEnabled())
+                                       log.debug("Check " + t + "...");
+                               if (!(t instanceof CategorizedNameVersion))
+                                       throw new SlcException("Unsupported type " + t.getClass());
+                               CategorizedNameVersion nv = (CategorizedNameVersion) t;
+                               Artifact artifact = new DefaultArtifact(nv.getCategory(),
+                                               nv.getName(), "jar", nv.getVersion());
+                               String path = MavenConventionsUtils.artifactPath("/", artifact);
+                               if (!javaSession.itemExists(path)) {
+                                       if (nv instanceof Runnable) {
+                                               if (log.isDebugEnabled())
+                                                       log.debug("Run " + nv + "...");
+                                               ((Runnable) nv).run();
+                                       } else {
+                                               log.warn("Skipped unsupported " + nv);
+                                       }
+                               }
+                       }
+               } catch (RepositoryException e) {
+                       throw new SlcException("Cannot process distribution "
+                                       + osgiDistribution, e);
+               } finally {
+                       JcrUtils.logoutQuietly(javaSession);
+               }
+       }
+
+       public void setOsgiDistribution(ArgeoOsgiDistribution osgiDistribution) {
+               this.osgiDistribution = osgiDistribution;
+       }
+
+       public void setOsgiFactory(OsgiFactory osgiFactory) {
+               this.osgiFactory = osgiFactory;
+       }
+
+}
diff --git a/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/UriWrapper.java b/runtime/org.argeo.slc.repo/src/main/java/org/argeo/slc/repo/osgi/UriWrapper.java
new file mode 100644 (file)
index 0000000..45456ad
--- /dev/null
@@ -0,0 +1,77 @@
+package org.argeo.slc.repo.osgi;
+
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.jcr.JcrUtils;
+import org.argeo.slc.SlcException;
+import org.argeo.slc.repo.OsgiFactory;
+import org.argeo.slc.repo.RepoUtils;
+
+public class UriWrapper extends BndWrapper implements Runnable {
+       private final static Log log = LogFactory.getLog(UriWrapper.class);
+
+       private String uri;
+       private String baseUri;
+       private String versionSeparator = "-";
+       private String extension = "jar";
+
+       private OsgiFactory osgiFactory;
+
+       public void run() {
+               Session distSession = null;
+               Session javaSession = null;
+               InputStream in;
+               ByteArrayOutputStream out;
+               try {
+                       distSession = osgiFactory.openDistSession();
+                       javaSession = osgiFactory.openJavaSession();
+                       if (uri == null) {
+                               uri = baseUri + '/' + getName() + versionSeparator
+                                               + getVersion() + "." + extension;
+                       }
+                       Node sourceArtifact = osgiFactory.getDist(distSession, uri);
+
+                       // TODO factorize with Maven
+                       in = sourceArtifact.getNode(Node.JCR_CONTENT)
+                                       .getProperty(Property.JCR_DATA).getBinary().getStream();
+                       out = new ByteArrayOutputStream();
+                       wrapJar(in, out);
+                       Node newJarNode = RepoUtils
+                                       .copyBytesAsArtifact(javaSession.getRootNode(),
+                                                       getArtifact(), out.toByteArray());
+                       osgiFactory.indexNode(newJarNode);
+                       newJarNode.getSession().save();
+                       if (log.isDebugEnabled())
+                               log.debug("Wrapped " + uri + " to " + newJarNode.getPath());
+               } catch (RepositoryException e) {
+                       throw new SlcException("Cannot wrap Maven " + uri, e);
+               } finally {
+                       JcrUtils.logoutQuietly(distSession);
+                       JcrUtils.logoutQuietly(javaSession);
+               }
+       }
+
+       public void setUri(String sourceCoords) {
+               this.uri = sourceCoords;
+       }
+
+       public void setOsgiFactory(OsgiFactory osgiFactory) {
+               this.osgiFactory = osgiFactory;
+       }
+
+       public void setBaseUri(String baseUri) {
+               this.baseUri = baseUri;
+       }
+
+       public void setVersionSeparator(String versionSeparator) {
+               this.versionSeparator = versionSeparator;
+       }
+}