You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by db...@apache.org on 2005/11/23 01:04:13 UTC

svn commit: r348307 - in /geronimo/gbuild/trunk/gbuild-agent/src: main/java/org/apache/geronimo/gbuild/agent/WriteIncludeFileExtention.java test/java/org/apache/geronimo/gbuild/agent/WriteIncludeFileExtentionTest.java

Author: dblevins
Date: Tue Nov 22 16:04:05 2005
New Revision: 348307

URL: http://svn.apache.org/viewcvs?rev=348307&view=rev
Log:
Rewrote the WriteIncludeExtention to use a file name template for creating the actual file name.  The template can include any number of the values from the map (the JMS results message).  If the file name includes directories, those will be created as well.

Added:
    geronimo/gbuild/trunk/gbuild-agent/src/test/java/org/apache/geronimo/gbuild/agent/WriteIncludeFileExtentionTest.java
Modified:
    geronimo/gbuild/trunk/gbuild-agent/src/main/java/org/apache/geronimo/gbuild/agent/WriteIncludeFileExtention.java

Modified: geronimo/gbuild/trunk/gbuild-agent/src/main/java/org/apache/geronimo/gbuild/agent/WriteIncludeFileExtention.java
URL: http://svn.apache.org/viewcvs/geronimo/gbuild/trunk/gbuild-agent/src/main/java/org/apache/geronimo/gbuild/agent/WriteIncludeFileExtention.java?rev=348307&r1=348306&r2=348307&view=diff
==============================================================================
--- geronimo/gbuild/trunk/gbuild-agent/src/main/java/org/apache/geronimo/gbuild/agent/WriteIncludeFileExtention.java (original)
+++ geronimo/gbuild/trunk/gbuild-agent/src/main/java/org/apache/geronimo/gbuild/agent/WriteIncludeFileExtention.java Tue Nov 22 16:04:05 2005
@@ -23,6 +23,7 @@
 import java.util.Map;
 import java.util.Iterator;
 import java.util.Date;
+import java.util.HashMap;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
@@ -41,17 +42,14 @@
     /**
      * @plexus.configuration
      */
-    private String fileExtention;
+    private String fileNameTemplate;
 
-    /**
-     * @plexus.configuration
-     */
-    private String resultsDirectory;
+    private StringTemplate template;
 
     /**
      * @plexus.configuration
      */
-    private String useHeader;
+    private String resultsDirectory;
 
     /**
      * @plexus.configuration
@@ -62,7 +60,15 @@
     private File directory;
     private SimpleDateFormat dateFormatter;
 
+    public WriteIncludeFileExtention(String includePattern, String fileNameTemplate, String resultsDirectory, String dateFormat) {
+        this.includePattern = includePattern;
+        this.fileNameTemplate = fileNameTemplate;
+        this.resultsDirectory = resultsDirectory;
+        this.dateFormat = dateFormat;
+    }
+
     public void start() throws StartingException {
+        template = new StringTemplate(fileNameTemplate);
         directory = new File(resultsDirectory);
         directory.mkdirs();
         assert directory.exists(): "File specified does not exist. " + directory.getAbsolutePath();
@@ -78,41 +84,40 @@
 
     public void execute(Map context) throws Exception {
         getLogger().debug("Pattern "+includePattern);
-        try {
-            String header = (String) context.get(useHeader);
-
-            for (Iterator iterator = context.entrySet().iterator(); iterator.hasNext();) {
-                Map.Entry entry = (Map.Entry) iterator.next();
-                String key = (String) entry.getKey();
-                Object value = entry.getValue();
-
-                if (key.matches(includePattern)){
-                    getLogger().info("Found include pattern "+key);
-                    String fileName = header;
-                    fileName += key.replaceFirst(includePattern, "");
-                    fileName += "-"+ dateFormatter.format(new Date());
-                    fileName += fileExtention;
-
-                    write(fileName, (String)value);
-                } else {
-                    getLogger().debug("No Match "+key);
+        Map map = new HashMap();
+        map.putAll(context);
+        map.put("date", dateFormatter.format(new Date()));
+
+        for (Iterator iterator = context.entrySet().iterator(); iterator.hasNext();) {
+            Map.Entry entry = (Map.Entry) iterator.next();
+            String key = (String) entry.getKey();
+            Object value = entry.getValue();
+
+            if (key.matches(includePattern)){
+                try {
+                    getLogger().debug("Found include pattern "+key);
+                    String fileName = template.apply(map);
+                    File file = new File(directory, fileName);
+                    File parent = file.getParentFile();
+                    parent.mkdirs();
+                    write(file, (String)value);
+                } catch (Exception e) {
+                    getLogger().warn("Abnormal failure on header "+key, e);
                 }
             }
-        } catch (Exception e) {
-            e.printStackTrace();
         }
+
     }
 
-    private void write(String fileName, String content) {
-        File outputFile = new File(directory, fileName);
+    private void write(File file, String content) {
         try {
-            getLogger().info("Writing "+content.length()+" characters to "+outputFile.getAbsolutePath());
-            FileOutputStream file = new FileOutputStream(outputFile);
-            file.write(content.getBytes());
-            file.flush();
-            file.close();
+            getLogger().info("Writing "+content.length()+" characters to "+file.getAbsolutePath());
+            FileOutputStream out = new FileOutputStream(file);
+            out.write(content.getBytes());
+            out.flush();
+            out.close();
         } catch (IOException e) {
-            getLogger().error("Could not write to file "+outputFile.getAbsolutePath(), e);
+            getLogger().error("Could not write to file "+file.getAbsolutePath(), e);
         }
     }
 }

Added: geronimo/gbuild/trunk/gbuild-agent/src/test/java/org/apache/geronimo/gbuild/agent/WriteIncludeFileExtentionTest.java
URL: http://svn.apache.org/viewcvs/geronimo/gbuild/trunk/gbuild-agent/src/test/java/org/apache/geronimo/gbuild/agent/WriteIncludeFileExtentionTest.java?rev=348307&view=auto
==============================================================================
--- geronimo/gbuild/trunk/gbuild-agent/src/test/java/org/apache/geronimo/gbuild/agent/WriteIncludeFileExtentionTest.java (added)
+++ geronimo/gbuild/trunk/gbuild-agent/src/test/java/org/apache/geronimo/gbuild/agent/WriteIncludeFileExtentionTest.java Tue Nov 22 16:04:05 2005
@@ -0,0 +1,40 @@
+package org.apache.geronimo.gbuild.agent;
+/**
+ * @version $Rev$ $Date$
+ */
+
+import junit.framework.TestCase;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.util.HashMap;
+import java.util.Properties;
+
+public class WriteIncludeFileExtentionTest extends TestCase {
+
+    public void testExecute() throws Exception {
+        WriteIncludeFileExtention extention = new WriteIncludeFileExtention("^include.*", "{foo}/{name}.properties", "target", "yyyy");
+        extention.enableLogging(new TestLogger("include-writer"));
+        extention.start();
+
+        HashMap map = new HashMap();
+        map.put("include.hello", "id=abc");
+        map.put("foo", "include-test");
+        map.put("name", "one");
+        extention.execute(map);
+
+        File file = new File("target/include-test/one.properties");
+        assertTrue("file.exists()", file.exists());
+        assertEquals("file.size", 6, file.length());
+
+        FileInputStream in = new FileInputStream(file);
+        Properties properties = new Properties();
+        properties.load(in);
+        in.close();
+        String property = properties.getProperty("id");
+        assertNotNull("property", property);
+        assertEquals("property", "abc", property);
+
+        file.delete();
+    }
+}
\ No newline at end of file