support for the new CopyDir producer node
authorzapata <zapata>
Tue, 15 Oct 2002 23:07:33 +0000 (23:07 +0000)
committerzapata <zapata>
Tue, 15 Oct 2002 23:07:33 +0000 (23:07 +0000)
source/mir/producer/DirCopyingProducerNode.java [new file with mode: 0755]
source/mir/producer/ScriptCallingProducerNode.java
source/mir/producer/reader/DefaultProducerNodeBuilders.java
source/mir/util/FileCopier.java [new file with mode: 0755]
source/mircoders/localizer/basic/MirBasicProducerLocalizer.java

diff --git a/source/mir/producer/DirCopyingProducerNode.java b/source/mir/producer/DirCopyingProducerNode.java
new file mode 100755 (executable)
index 0000000..c943c98
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2001, 2002  The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with the com.oreilly.servlet library, any library
+ * licensed under the Apache Software License, The Sun (tm) Java Advanced
+ * Imaging library (JAI), The Sun JIMI library (or with modified versions of
+ * the above that use the same license as the above), and distribute linked
+ * combinations including the two.  You must obey the GNU General Public
+ * License in all respects for all of the code used other than the above
+ * mentioned libraries.  If you modify this file, you may extend this exception
+ * to your version of the file, but you are not obligated to do so.  If you do
+ * not wish to do so, delete this exception statement from your version.
+ */
+
+package mir.producer;
+
+import java.io.*;
+import java.util.*;
+import mir.util.*;
+
+public class DirCopyingProducerNode implements ProducerNode  {
+  private String sourceExpression;
+  private String destinationExpression;
+  private String sourceBasePath;
+  private String destinationBasePath;
+
+  public DirCopyingProducerNode(String aSourceBasePath, String aDestinationBasePath, String aSource, String aDestination) {
+    sourceBasePath = aSourceBasePath;
+    destinationBasePath = aDestinationBasePath;
+    sourceExpression = aSource;
+    destinationExpression = aDestination;
+  }
+
+  public void produce(Map aValueMap, String aVerb, PrintWriter aLogger) throws ProducerFailure {
+    String source = "";
+    String destination = "";
+
+    try {
+      source = ParameterExpander.expandExpression( aValueMap, sourceExpression );
+      destination = ParameterExpander.expandExpression( aValueMap, destinationExpression );
+      aLogger.println("Copying " + source + " into " + destination);
+      FileCopier.copyDirectory(
+        new File(sourceBasePath, source),
+        new File(destinationBasePath, destination));
+    }
+    catch (Throwable e) {
+      throw new ProducerFailure("Copying " + source + " into " + destination + " failed: " + e.getMessage(), e);
+    }
+  }
+
+  public Set buildVerbSet() {
+    return new HashSet();
+  }
+}
index 18ae9b4..6c7b873 100755 (executable)
@@ -35,8 +35,6 @@ import java.io.*;
 import java.util.*;
 import mir.util.*;
 
-// ML: needs to be tested!
-
 public class ScriptCallingProducerNode implements ProducerNode  {
   String scriptExpression;
 
index ff726cb..86ce89a 100755 (executable)
@@ -41,20 +41,20 @@ public class DefaultProducerNodeBuilders {
 
   public static void registerBuilders(ProducerNodeBuilderLibrary aBuilderLibrary,
        EntityAdapterModel aModel, Generator.GeneratorLibrary aGeneratorLibrary,
-       WriterEngine aWriterEngine) {
+       WriterEngine aWriterEngine, String aSourceBasePath, String aDestinationBasePath) {
 
     aBuilderLibrary.registerBuilder("Set", EvaluatedAssignmentProducerNodeBuilder.class);
     aBuilderLibrary.registerBuilder("Define", ExpandedAssignmentProducerNodeBuilder.class);
     aBuilderLibrary.registerBuilder("Log", LoggingProducerNodeBuilder.class);
     aBuilderLibrary.registerBuilder("Execute", ScriptCallingProducerNodeBuilder.class);
     aBuilderLibrary.registerBuilder("Resource", ResourceBundleProducerNodeBuilder.class);
+    aBuilderLibrary.registerFactory("CopyDir", new DirCopyProducerNodeBuilder.factory( aSourceBasePath, aDestinationBasePath));
 
     aBuilderLibrary.registerBuilder("DeleteFile", FileDeletingProducerNodeBuilder.class);
     aBuilderLibrary.registerBuilder("SetFileDate", FileDateSettingProducerNodeBuilder.class);
     aBuilderLibrary.registerBuilder("If", ConditionalProducerNodeBuilder.class);
     aBuilderLibrary.registerBuilder("While", LoopProducerNodeBuilder.class);
 
-
     aBuilderLibrary.registerFactory("Enumerate", new EnumeratingProducerNodeBuilder.factory(aModel));
     aBuilderLibrary.registerFactory("List", new ListProducerNodeBuilder.factory(aModel));
     aBuilderLibrary.registerFactory("Batch", new BatchingProducerNodeBuilder.factory(aModel));
@@ -101,12 +101,13 @@ public class DefaultProducerNodeBuilders {
 
 ////////////////////////////////////////////////////////////////////////////////
 
+  private final static String   ASSIGNMENT_KEY_ATTRIBUTE = KEY_ATTRIBUTE;
+  private final static String   ASSIGNMENT_VALUE_ATTRIBUTE = "value";
+  private final static String[] ASSIGNMENT_REQUIRED_ATTRIBUTES = { ASSIGNMENT_KEY_ATTRIBUTE, ASSIGNMENT_VALUE_ATTRIBUTE };
+  private final static String[] ASSIGNMENT_OPTIONAL_ATTRIBUTES = {};
+  private final static String[] ASSIGNMENT_SUBNODES = {};
+
   public static class ExpandedAssignmentProducerNodeBuilder extends AbstractProducerNodeBuilder {
-    private final static String   ASSIGNMENT_KEY_ATTRIBUTE = KEY_ATTRIBUTE;
-    private final static String   ASSIGNMENT_VALUE_ATTRIBUTE = "value";
-    private final static String[] ASSIGNMENT_REQUIRED_ATTRIBUTES = { ASSIGNMENT_KEY_ATTRIBUTE, ASSIGNMENT_VALUE_ATTRIBUTE };
-    private final static String[] ASSIGNMENT_OPTIONAL_ATTRIBUTES = {};
-    private final static String[] ASSIGNMENT_SUBNODES = {};
 
     private String key;
     private String value;
@@ -130,11 +131,6 @@ public class DefaultProducerNodeBuilders {
 ////////////////////////////////////////////////////////////////////////////////
 
   public static class EvaluatedAssignmentProducerNodeBuilder extends AbstractProducerNodeBuilder {
-    private final static String   ASSIGNMENT_KEY_ATTRIBUTE = KEY_ATTRIBUTE;
-    private final static String   ASSIGNMENT_VALUE_ATTRIBUTE = "value";
-    private final static String[] ASSIGNMENT_REQUIRED_ATTRIBUTES = { ASSIGNMENT_KEY_ATTRIBUTE, ASSIGNMENT_VALUE_ATTRIBUTE };
-    private final static String[] ASSIGNMENT_OPTIONAL_ATTRIBUTES = {};
-    private final static String[] ASSIGNMENT_SUBNODES = {};
 
     private String key;
     private String value;
@@ -433,6 +429,53 @@ public class DefaultProducerNodeBuilders {
 
 ////////////////////////////////////////////////////////////////////////////////
 
+  private final static String   DIRCOPY_SOURCE_ATTRIBUTE = "source";
+  private final static String   DIRCOPY_DESTINATION_ATTRIBUTE = "destination";
+  private final static String[] DIRCOPY_REQUIRED_ATTRIBUTES = { DIRCOPY_SOURCE_ATTRIBUTE, DIRCOPY_DESTINATION_ATTRIBUTE };
+  private final static String[] DIRCOPY_OPTIONAL_ATTRIBUTES = {};
+  private final static String[] DIRCOPY_SUBNODES = {};
+
+  public static class DirCopyProducerNodeBuilder extends AbstractProducerNodeBuilder {
+    private String source;
+    private String destination;
+    private String sourceBasePath;
+    private String destinationBasePath;
+
+    public DirCopyProducerNodeBuilder(String aSourceBasePath, String aDestinationBasePath) {
+      super(DIRCOPY_SUBNODES);
+
+      sourceBasePath = aSourceBasePath;
+      destinationBasePath = aDestinationBasePath;
+    }
+
+    public void setAttributes(Map anAttributes) throws ProducerConfigExc {
+      ReaderTool.checkAttributes(anAttributes, DIRCOPY_REQUIRED_ATTRIBUTES, DIRCOPY_OPTIONAL_ATTRIBUTES);
+
+      source = (String) anAttributes.get(DIRCOPY_SOURCE_ATTRIBUTE);
+      destination = (String) anAttributes.get(DIRCOPY_DESTINATION_ATTRIBUTE);
+    };
+
+    public ProducerNode constructNode() {
+      return new DirCopyingProducerNode(sourceBasePath, destinationBasePath, source, destination);
+    };
+
+    public static class factory implements ProducerNodeBuilderFactory {
+      private String sourceBasePath;
+      private String destinationBasePath;
+
+      public factory(String aSourceBasePath, String aDestinationBasePath) {
+        sourceBasePath = aSourceBasePath;
+        destinationBasePath = aDestinationBasePath;
+      }
+
+      public ProducerNodeBuilder makeBuilder() {
+        return new DirCopyProducerNodeBuilder(sourceBasePath, destinationBasePath);
+      }
+    }
+  }
+
+////////////////////////////////////////////////////////////////////////////////
+
   public static class GeneratingProducerNodeBuilder extends AbstractProducerNodeBuilder {
     private final static String   GENERATION_GENERATOR_ATTRIBUTE = "generator";
     private final static String   GENERATION_DESTINATION_ATTRIBUTE = "destination";
diff --git a/source/mir/util/FileCopier.java b/source/mir/util/FileCopier.java
new file mode 100755 (executable)
index 0000000..e4442de
--- /dev/null
@@ -0,0 +1,61 @@
+package mir.util;
+
+import java.io.*;
+import java.util.*;
+
+public class FileCopier {
+  protected static final int FILE_COPY_BUFFER_SIZE = 65536;
+
+  protected FileCopier() {
+  }
+
+  public static void copyFile(File aSourceFile, File aDestinationFile) throws IOException {
+    FileInputStream inputStream;
+    FileOutputStream outputStream;
+    int nrBytesRead;
+    byte[] buffer = new byte[FILE_COPY_BUFFER_SIZE];
+
+    inputStream = new FileInputStream(aSourceFile);
+    try {
+      outputStream = new FileOutputStream(aDestinationFile);
+      try {
+        do {
+          nrBytesRead = inputStream.read(buffer);
+          if (nrBytesRead>0)
+            outputStream.write(buffer, 0, nrBytesRead);
+        }
+        while (nrBytesRead>=0);
+      }
+      finally {
+        outputStream.close();
+      }
+    }
+    finally {
+      inputStream.close();
+    }
+  }
+
+  public static void copyDirectory(File aSourceDirectory, File aDestinationDirectory) throws IOException {
+    int i;
+    File sourceFile;
+    File destinationFile;
+    File[] files = aSourceDirectory.listFiles();
+
+    if (!aDestinationDirectory.exists())
+      aDestinationDirectory.mkdirs();
+
+    for (i=0; i<files.length; i++) {
+      sourceFile = files[i];
+      destinationFile=new File(aDestinationDirectory, sourceFile.getName());
+      if (sourceFile.isDirectory()) {
+        if (!destinationFile.exists())
+          destinationFile.mkdir();
+        copyDirectory(sourceFile, destinationFile);
+      }
+      else {
+        copyFile(sourceFile, destinationFile);
+      }
+    }
+  }
+
+}
\ No newline at end of file
index fda014c..2b72ee7 100755 (executable)
@@ -108,7 +108,9 @@ public class MirBasicProducerLocalizer implements MirProducerLocalizer {
   }
 
   protected void setupProducerNodeBuilderLibrary(ProducerNodeBuilderLibrary aLibrary) {
-    DefaultProducerNodeBuilders.registerBuilders(aLibrary, model, generatorLibrary, writerEngine);
+    DefaultProducerNodeBuilders.registerBuilders(
+      aLibrary, model, generatorLibrary, writerEngine,
+      MirGlobal.getConfigProperty("Home"), MirGlobal.getConfigProperty("Producer.StorageRoot"));
     SupplementalProducerNodeBuilders.registerBuilders(aLibrary, model);
   }
 
@@ -120,7 +122,7 @@ public class MirBasicProducerLocalizer implements MirProducerLocalizer {
 
     aFileMonitor.clear();
     reader = new ProducerConfigReader();
-    reader.parseFile(MirGlobal.getConfigProperty("Home") + "/" + MirGlobal.getConfigProperty("Mir.Localizer.ProducerConfigFile"), library, aFactoriesMap, usedFiles);
+    reader.parseFile(MirGlobal.getConfigProperty("Home") + File.separatorChar + MirGlobal.getConfigProperty("Mir.Localizer.ProducerConfigFile"), library, aFactoriesMap, usedFiles);
 
     Iterator i = usedFiles.iterator();
     while (i.hasNext())