Deal with SNAPSHOTs
authorMathieu Baudier <mbaudier@argeo.org>
Sat, 6 Jan 2018 14:13:15 +0000 (15:13 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Sat, 6 Jan 2018 14:13:15 +0000 (15:13 +0100)
org.argeo.slc.repo/src/org/argeo/slc/repo/ModularDistributionFactory.java

index efd64aec5be6e0a8f5c01cecc3b4b94d150871dc..25b6c48c4f9f4d1d5408c4c0464e6d8586f0b5dc 100644 (file)
@@ -6,6 +6,9 @@ import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.Writer;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 import java.util.Iterator;
 import java.util.jar.Attributes;
 import java.util.jar.JarEntry;
@@ -30,10 +33,12 @@ import org.osgi.framework.Constants;
  * Creates a jar bundle from an ArgeoOsgiDistribution. This jar is then
  * persisted and indexed in a java repository using the OSGI Factory.
  * 
- * It does the following <list><li>Creates a Manifest</li><li>Creates files
- * indexes (csv, feature.xml ...)</li><li>Populate the corresponding jar</li><li>
- * Save it in the repository</li><li>Index the node and creates corresponding
- * sha1 and md5 files</li> </list>
+ * It does the following <list>
+ * <li>Creates a Manifest</li>
+ * <li>Creates files indexes (csv, feature.xml ...)</li>
+ * <li>Populate the corresponding jar</li>
+ * <li>Save it in the repository</li>
+ * <li>Index the node and creates corresponding sha1 and md5 files</li> </list>
  * 
  */
 public class ModularDistributionFactory implements Runnable {
@@ -47,13 +52,13 @@ public class ModularDistributionFactory implements Runnable {
 
        // Constants
        private final static String CSV_FILE_NAME = "modularDistribution.csv";
+       private final DateFormat snapshotTimestamp = new SimpleDateFormat("YYYYMMddhhmm");
 
        // private final static String FEATURE_FILE_NAME = "feature.xml";
        // private static int BUFFER_SIZE = 10240;
 
        /** Convenience constructor with minimal configuration */
-       public ModularDistributionFactory(OsgiFactory osgiFactory,
-                       ArgeoOsgiDistribution osgiDistribution) {
+       public ModularDistributionFactory(OsgiFactory osgiFactory, ArgeoOsgiDistribution osgiDistribution) {
                this.osgiFactory = osgiFactory;
                this.osgiDistribution = osgiDistribution;
        }
@@ -69,18 +74,14 @@ public class ModularDistributionFactory implements Runnable {
                        else if (artifactType == "pom")
                                distFile = generatePomFile();
                        else
-                               throw new SlcException(
-                                               "Unimplemented distribution artifact type: "
-                                                               + artifactType + " for "
-                                                               + osgiDistribution.toString());
+                               throw new SlcException("Unimplemented distribution artifact type: " + artifactType + " for "
+                                               + osgiDistribution.toString());
 
                        // Save in java repository
-                       Artifact osgiArtifact = new DefaultArtifact(
-                                       osgiDistribution.getCategory(), osgiDistribution.getName(),
+                       Artifact osgiArtifact = new DefaultArtifact(osgiDistribution.getCategory(), osgiDistribution.getName(),
                                        artifactType, osgiDistribution.getVersion());
 
-                       Node distNode = RepoUtils.copyBytesAsArtifact(
-                                       javaSession.getNode(artifactBasePath), osgiArtifact,
+                       Node distNode = RepoUtils.copyBytesAsArtifact(javaSession.getNode(artifactBasePath), osgiArtifact,
                                        distFile);
 
                        // index
@@ -90,8 +91,7 @@ public class ModularDistributionFactory implements Runnable {
                        javaSession.save();
                } catch (RepositoryException e) {
                        throw new SlcException(
-                                       "JCR error while persisting modular distribution in JCR "
-                                                       + osgiDistribution.toString(), e);
+                                       "JCR error while persisting modular distribution in JCR " + osgiDistribution.toString(), e);
                } finally {
                        JcrUtils.logoutQuietly(javaSession);
                }
@@ -108,9 +108,7 @@ public class ModularDistributionFactory implements Runnable {
                        jarOut.close();
                        return byteOut.toByteArray();
                } catch (IOException e) {
-                       throw new SlcException(
-                                       "IO error while generating modular distribution "
-                                                       + osgiDistribution.toString(), e);
+                       throw new SlcException("IO error while generating modular distribution " + osgiDistribution.toString(), e);
                } finally {
                        IOUtils.closeQuietly(byteOut);
                        IOUtils.closeQuietly(jarOut);
@@ -141,32 +139,29 @@ public class ModularDistributionFactory implements Runnable {
                Manifest manifest = new Manifest();
 
                // TODO make this configurable
-               manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION,
-                               "1.0");
-               addManifestAttribute(manifest,
-                               Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT, "JavaSE-1.6");
+               manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
+//             addManifestAttribute(manifest, Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT, "JavaSE-1.8");
                addManifestAttribute(manifest, Constants.BUNDLE_VENDOR, "Argeo");
                addManifestAttribute(manifest, Constants.BUNDLE_MANIFESTVERSION, "2");
-               addManifestAttribute(manifest, "Bundle-License",
-                               "http://www.apache.org/licenses/LICENSE-2.0.txt");
+//             addManifestAttribute(manifest, "Bundle-License", "http://www.apache.org/licenses/LICENSE-2.0.txt");
 
                // TODO define a user friendly name
-               addManifestAttribute(manifest, Constants.BUNDLE_NAME,
-                               osgiDistribution.getName());
+               addManifestAttribute(manifest, Constants.BUNDLE_NAME, osgiDistribution.getName());
 
                // Categorized name version
-               addManifestAttribute(manifest, RepoConstants.SLC_GROUP_ID,
-                               osgiDistribution.getCategory());
-               addManifestAttribute(manifest, Constants.BUNDLE_SYMBOLICNAME,
-                               osgiDistribution.getName());
-               addManifestAttribute(manifest, Constants.BUNDLE_VERSION,
-                               osgiDistribution.getVersion());
+               addManifestAttribute(manifest, RepoConstants.SLC_GROUP_ID, osgiDistribution.getCategory());
+               addManifestAttribute(manifest, Constants.BUNDLE_SYMBOLICNAME, osgiDistribution.getName());
+               String version = osgiDistribution.getVersion();
+               if (version.endsWith("-SNAPSHOT")) {
+                       version = version.substring(0, version.length() - "-SNAPSHOT".length());
+                       version = version + ".SNAPSHOT-r" + snapshotTimestamp.format(new Date());
+               }
+               addManifestAttribute(manifest, Constants.BUNDLE_VERSION, version);
 
                return manifest;
        }
 
-       private void addManifestAttribute(Manifest manifest, String name,
-                       String value) {
+       private void addManifestAttribute(Manifest manifest, String name, String value) {
                manifest.getMainAttributes().put(new Attributes.Name(name), value);
        }
 
@@ -178,15 +173,12 @@ public class ModularDistributionFactory implements Runnable {
                        tmpFile.deleteOnExit();
                        writer = new FileWriter(tmpFile);
                        // Populate the file
-                       for (Iterator<? extends NameVersion> it = osgiDistribution
-                                       .nameVersions(); it.hasNext();)
+                       for (Iterator<? extends NameVersion> it = osgiDistribution.nameVersions(); it.hasNext();)
                                writer.write(getCsvLine(it.next()));
                        writer.flush();
                        return FileUtils.readFileToByteArray(tmpFile);
                } catch (Exception e) {
-                       throw new SlcException(
-                                       "unable to create csv distribution file for "
-                                                       + osgiDistribution.toString(), e);
+                       throw new SlcException("unable to create csv distribution file for " + osgiDistribution.toString(), e);
                } finally {
                        IOUtils.closeQuietly(writer);
                }
@@ -404,28 +396,25 @@ public class ModularDistributionFactory implements Runnable {
                StringBuilder b = new StringBuilder();
                // XML header
                b.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
-               b.append("<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n");
+               b.append(
+                               "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n");
                b.append("<modelVersion>4.0.0</modelVersion>");
 
                // Artifact
-               b.append("<groupId>").append(osgiDistribution.getCategory())
-                               .append("</groupId>\n");
-               b.append("<artifactId>").append(osgiDistribution.getName())
-                               .append("</artifactId>\n");
-               b.append("<version>").append(osgiDistribution.getVersion())
-                               .append("</version>\n");
+               b.append("<groupId>").append(osgiDistribution.getCategory()).append("</groupId>\n");
+               b.append("<artifactId>").append(osgiDistribution.getName()).append("</artifactId>\n");
+               b.append("<version>").append(osgiDistribution.getVersion()).append("</version>\n");
                b.append("<packaging>pom</packaging>\n");
                // p.append("<name>").append("Bundle Name").append("</name>\n");
-               // p.append("<description>").append("Bundle Description").append("</description>\n");
+               // p.append("<description>").append("Bundle
+               // Description").append("</description>\n");
 
                // Dependencies
                b.append("<dependencies>\n");
-               for (Iterator<? extends NameVersion> it = osgiDistribution
-                               .nameVersions(); it.hasNext();) {
+               for (Iterator<? extends NameVersion> it = osgiDistribution.nameVersions(); it.hasNext();) {
                        NameVersion nameVersion = it.next();
                        if (!(nameVersion instanceof CategorizedNameVersion))
-                               throw new SlcException("Unsupported type "
-                                               + nameVersion.getClass());
+                               throw new SlcException("Unsupported type " + nameVersion.getClass());
                        CategorizedNameVersion nv = (CategorizedNameVersion) nameVersion;
                        b.append(getDependencySnippet(nv, false));
                }
@@ -435,10 +424,8 @@ public class ModularDistributionFactory implements Runnable {
                b.append("<dependencyManagement>\n");
                b.append("<dependencies>\n");
 
-               for (Iterator<? extends NameVersion> it = osgiDistribution
-                               .nameVersions(); it.hasNext();)
-                       b.append(getDependencySnippet((CategorizedNameVersion) it.next(),
-                                       true));
+               for (Iterator<? extends NameVersion> it = osgiDistribution.nameVersions(); it.hasNext();)
+                       b.append(getDependencySnippet((CategorizedNameVersion) it.next(), true));
                b.append("</dependencies>\n");
                b.append("</dependencyManagement>\n");
 
@@ -446,17 +433,14 @@ public class ModularDistributionFactory implements Runnable {
                return b.toString().getBytes();
        }
 
-       private String getDependencySnippet(CategorizedNameVersion cnv,
-                       boolean includeVersion) { // , String type, String scope
+       private String getDependencySnippet(CategorizedNameVersion cnv, boolean includeVersion) { // , String type, String
+                                                                                                                                                                                               // scope
                StringBuilder b = new StringBuilder();
                b.append("<dependency>\n");
-               b.append("\t<groupId>").append(cnv.getCategory())
-                               .append("</groupId>\n");
-               b.append("\t<artifactId>").append(cnv.getName())
-                               .append("</artifactId>\n");
+               b.append("\t<groupId>").append(cnv.getCategory()).append("</groupId>\n");
+               b.append("\t<artifactId>").append(cnv.getName()).append("</artifactId>\n");
                if (includeVersion)
-                       b.append("\t<version>").append(cnv.getVersion())
-                                       .append("</version>\n");
+                       b.append("\t<version>").append(cnv.getVersion()).append("</version>\n");
                // if (type!= null)
                // p.append("\t<type>").append(type).append("</type>\n");
                // if (type!= null)
@@ -466,8 +450,7 @@ public class ModularDistributionFactory implements Runnable {
        }
 
        // Helpers
-       private void addToJar(byte[] content, String name, JarOutputStream target)
-                       throws IOException {
+       private void addToJar(byte[] content, String name, JarOutputStream target) throws IOException {
                ByteArrayInputStream in = null;
                try {
                        target.putNextEntry(new JarEntry(name));
@@ -485,8 +468,7 @@ public class ModularDistributionFactory implements Runnable {
                }
        }
 
-       private String getCsvLine(NameVersion nameVersion)
-                       throws RepositoryException {
+       private String getCsvLine(NameVersion nameVersion) throws RepositoryException {
                if (!(nameVersion instanceof CategorizedNameVersion))
                        throw new SlcException("Unsupported type " + nameVersion.getClass());
                CategorizedNameVersion cnv = (CategorizedNameVersion) nameVersion;
@@ -523,8 +505,7 @@ public class ModularDistributionFactory implements Runnable {
                this.osgiDistribution = osgiDistribution;
        }
 
-       public void setModularDistributionSeparator(
-                       String modularDistributionSeparator) {
+       public void setModularDistributionSeparator(String modularDistributionSeparator) {
                this.modularDistributionSeparator = modularDistributionSeparator;
        }