Author: [email protected]
Date: Mon Nov 21 16:06:06 2011
New Revision: 1742

Log:
AMDATU-464 Added snapshot timestamping support

Modified:
   
trunk/amdatu-maven-plugin/src/main/java/org/amdatu/maven/plugin/DeployBundleMojo.java

Modified: 
trunk/amdatu-maven-plugin/src/main/java/org/amdatu/maven/plugin/DeployBundleMojo.java
==============================================================================
--- 
trunk/amdatu-maven-plugin/src/main/java/org/amdatu/maven/plugin/DeployBundleMojo.java
       (original)
+++ 
trunk/amdatu-maven-plugin/src/main/java/org/amdatu/maven/plugin/DeployBundleMojo.java
       Mon Nov 21 16:06:06 2011
@@ -15,9 +15,22 @@
  */
 package org.amdatu.maven.plugin;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
 import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URL;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.jar.Attributes;
+import java.util.jar.JarEntry;
+import java.util.jar.JarInputStream;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
 
 import org.amdatu.ace.client.AceClient;
 import org.amdatu.ace.client.AceClientException;
@@ -26,13 +39,20 @@
 import org.amdatu.ace.client.model.ArtifactBuilder;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
+import org.osgi.framework.Constants;
 
 /**
- * Goal which deploys a bundle to Amdatu ACE
+ * Goal that deploys a bundle to Amdatu ACE from either a url or file. If 
+ * the file is a local file with a snapshot version it will apply timestamp 
+ * logic on the fly.
  * 
  * @goal deployBundle
  * @requiresProject false
  */
+/**
+ * @author bramk
+ * 
+ */
 public class DeployBundleMojo extends AbstractMojo {
 
     /**
@@ -68,9 +88,16 @@
     /**
      * Artifact URL
      * 
-     * @parameter expression="${artifactURL}"
+     * @parameter expression="${artifactFile}"
+     */
+    private String artifactFile;
+
+    /**
+     * Artifact URL
+     * 
+     * @parameter expression="${artifactUrl}"
      */
-    private String artifactURL;
+    private String artifactUrl;
 
     /**
      * Artifact URL
@@ -117,36 +144,82 @@
      */
     private Map<String, String> tags;
 
+    private File fartifactFile = null;
+    private URL uartifactUrl = null;
+
     public void execute() throws MojoExecutionException {
+        locateArtifactFileOrUrl();
+        applySnapshotTimestamping();
+        extractArtifactUrlFromFile();
+        deployArtifactToServer(uartifactUrl);
+        getLog().info("Deployed to Amdatu server: " + uartifactUrl.toString());
+    }
+
+    private void extractArtifactUrlFromFile() throws MojoExecutionException {
+        if (fartifactFile != null) {
+            try {
+                uartifactUrl = fartifactFile.toURI().toURL();
+            }
+            catch (MalformedURLException e) {
+                throw new MojoExecutionException("Unable to determine 
artifactUrl for file: "
+                    + fartifactFile.getAbsolutePath(), e);
+            }
+        }
+    }
+
+    private void applySnapshotTimestamping() throws MojoExecutionException {
+        // When we have a local file and the version is a Maven SNAPSHOT apply 
tstamp logic
+        // to allow repetitive uploads to the provisioning server during 
development
+        if (fartifactFile != null && bundleVersion.endsWith("-SNAPSHOT")) {
+            String tstamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new 
Date());
+            String newVersion = bundleVersion.replace("-SNAPSHOT", "." + 
tstamp);
+            String newAtrifactFile = 
fartifactFile.getAbsolutePath().replace("-SNAPSHOT", "." + tstamp);
+            File fnewArtifactFile = new File(newAtrifactFile);
+            createNewArtifactFile(fartifactFile, fnewArtifactFile, newVersion);
+            fartifactFile = fnewArtifactFile;
+        }
+    }
 
-        String artifactUrlToUse = null;
-        if (artifactURL != null) {
-            artifactUrlToUse = artifactURL;
+    private void locateArtifactFileOrUrl() throws MojoExecutionException {
+        if (artifactFile != null && !"".equals(artifactFile)) {
+            fartifactFile = new File(artifactFile);
+            if (fartifactFile.exists() || fartifactFile.canRead()) {
+                throw new MojoExecutionException("Configured artifactFile can 
not be accessed: " + artifactFile);
+            }
+            return;
         }
-        else if (artifact != null) {
+        if (artifactUrl != null && !"".equals(artifactUrl)) {
             try {
-                artifactUrlToUse = 
artifact.getFile().toURI().toURL().toString();
+                uartifactUrl = URI.create(artifactUrl).toURL();
             }
             catch (MalformedURLException e) {
-                throw new MojoExecutionException(e.getMessage(), e);
+                throw new MojoExecutionException("Configured artifactUrl is 
invalid: " + artifactUrl, e);
             }
+            return;
         }
-        else {
-            throw new MojoExecutionException("Could not determine 
artifactUrl");
+        if (artifact != null) {
+            fartifactFile = artifact.getFile();
+            if (!fartifactFile.exists() || !fartifactFile.canRead()) {
+                throw new MojoExecutionException("Project artifactFile can not 
be accessed: " + artifactFile);
+            }
+            return;
         }
+        throw new MojoExecutionException("Unable to determing 
artifactFile/URL... check your config");
+    }
 
+    private void deployArtifactToServer(URL uartifactUrl) throws 
MojoExecutionException {
         AceClientWorkspace workspace = null;
         try {
             AceClient client = new AceClient(amdatuServer);
             ArtifactBuilder builder = new ArtifactBuilder()
-                            .isBundle()
-                            .setName(artifactName)
-                            .setDescription(artifactDescription)
-                            .setUrl(artifactUrlToUse)
-                            .setProcessorPid(artifactProcessor)
-                            .setBundleName(bundleName)
-                            .setBundleSymbolicName(bundleSymbolicName)
-                            
.setBundleVersion(bundleVersion.replace("-SNAPSHOT", "." + 
System.currentTimeMillis()));
+                .isBundle()
+                .setName(artifactName)
+                .setDescription(artifactDescription)
+                .setUrl(uartifactUrl.toString())
+                .setProcessorPid(artifactProcessor)
+                .setBundleName(bundleName)
+                .setBundleSymbolicName(bundleSymbolicName)
+                .setBundleVersion(bundleVersion.replace("-SNAPSHOT", "." + 
System.currentTimeMillis()));
 
             if (attributes != null) {
                 for (Entry<String, String> entry : attributes.entrySet()) {
@@ -179,4 +252,52 @@
             }
         }
     }
+
+    private void createNewArtifactFile(File fartifactFile, File 
fnewArtifactFile, String newVersion)
+        throws MojoExecutionException {
+        JarInputStream jis = null;
+        JarOutputStream jos = null;
+
+        try {
+            jis = new JarInputStream(new FileInputStream(fartifactFile));
+
+            Manifest manifest = jis.getManifest();
+            Attributes attributes = manifest.getMainAttributes();
+            attributes.putValue(Constants.BUNDLE_VERSION, newVersion);
+
+            jos = new JarOutputStream(new FileOutputStream(fnewArtifactFile), 
manifest);
+
+            byte[] buffer = new byte[4096];
+            JarEntry entry = jis.getNextJarEntry();
+            while (entry != null) {
+                jos.putNextEntry(new JarEntry(entry.getName()));
+                int bytes = jis.read(buffer);
+                while (bytes != -1) {
+                    jos.write(buffer, 0, bytes);
+                    bytes = jis.read(buffer);
+                }
+                jos.closeEntry();
+                jis.closeEntry();
+                entry = jis.getNextJarEntry();
+            }
+            jos.flush();
+            jos.close();
+            jis.close();
+        }
+        catch (IOException e) {
+            if (jis != null) {
+                try {
+                    jis.close();
+                }
+                catch (IOException e1) {}
+            }
+            if (jos != null) {
+                try {
+                    jos.close();
+                }
+                catch (IOException e1) {}
+            }
+            throw new MojoExecutionException("File to create timstamped 
bundle", e);
+        }
+    }
 }
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to