activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chir...@apache.org
Subject svn commit: r1034561 - in /activemq/activemq-apollo/trunk: apollo-broker/src/test/scala/org/apache/activemq/apollo/broker/perf/ apollo-cli/src/main/scala/org/apache/activemq/apollo/cli/ apollo-cli/src/main/scala/org/apache/activemq/apollo/cli/commands/...
Date Fri, 12 Nov 2010 20:54:43 GMT
Author: chirino
Date: Fri Nov 12 20:54:42 2010
New Revision: 1034561

URL: http://svn.apache.org/viewvc?rev=1034561&view=rev
Log:
Removed the scala compiler dependency from the FileSupport class.  Made the file operations
helpers of the RichFile implicit wrapper.

Modified:
    activemq/activemq-apollo/trunk/apollo-broker/src/test/scala/org/apache/activemq/apollo/broker/perf/LargeInitialDB.scala
    activemq/activemq-apollo/trunk/apollo-cli/src/main/scala/org/apache/activemq/apollo/cli/Apollo.scala
    activemq/activemq-apollo/trunk/apollo-cli/src/main/scala/org/apache/activemq/apollo/cli/commands/Create.scala
    activemq/activemq-apollo/trunk/apollo-util/src/main/scala/org/apache/activemq/apollo/util/FileSupport.scala
    activemq/activemq-apollo/trunk/apollo-util/src/test/scala/org/apache/activemq/apollo/util/FileSupportTest.scala

Modified: activemq/activemq-apollo/trunk/apollo-broker/src/test/scala/org/apache/activemq/apollo/broker/perf/LargeInitialDB.scala
URL: http://svn.apache.org/viewvc/activemq/activemq-apollo/trunk/apollo-broker/src/test/scala/org/apache/activemq/apollo/broker/perf/LargeInitialDB.scala?rev=1034561&r1=1034560&r2=1034561&view=diff
==============================================================================
--- activemq/activemq-apollo/trunk/apollo-broker/src/test/scala/org/apache/activemq/apollo/broker/perf/LargeInitialDB.scala
(original)
+++ activemq/activemq-apollo/trunk/apollo-broker/src/test/scala/org/apache/activemq/apollo/broker/perf/LargeInitialDB.scala
Fri Nov 12 20:54:42 2010
@@ -18,16 +18,17 @@
 package org.apache.activemq.apollo.broker.perf
 
 import org.apache.activemq.apollo.broker.Destination
-import tools.nsc.io.Directory
 import org.apache.activemq.apollo.util.metric.MetricAggregator
 import org.apache.activemq.apollo.util.{ServiceControl, FileSupport}
+import FileSupport._
+import java.io.File
 
 trait LargeInitialDB extends PersistentScenario {
   PURGE_STORE = false
   MULTI_BROKER = false
 
-  var original: Directory = null
-  var backup: Directory = null;
+  var original: File = null
+  var backup: File = null;
 
   // Keep it simple.. we are only creating 1 queue with a large number of entries.
   override def partitionedLoad = List(1)
@@ -48,12 +49,12 @@ trait LargeInitialDB extends PersistentS
 
     initBrokers
 
-    original = new Directory(storeDirectory)
+    original = storeDirectory
     if (original.exists) {
-      original.deleteRecursively
-      original.createDirectory(true)
+      original.recursive_delete
+      original.mkdirs
     }
-    backup = FileSupport.toDirectory(storeDirectory.getParent)./(FileSupport.toDirectory("backup"))
+    backup = storeDirectory.getParentFile / "backup"
     cleanBackup
 
     println("Using store at " + original + " and backup at " + backup)
@@ -90,29 +91,29 @@ trait LargeInitialDB extends PersistentS
   def saveDB {
     println("Copying contents of " + original + " to " + backup)
     cleanBackup
-    FileSupport.recursiveCopy(original, backup)
+    original.recursive_copy_to(backup)
     printStores
   }
 
   def printStores {
     println("\nOriginal store")
-    original.deepList().foreach(println)
+    original.recursive_list.foreach(println)
     println("\n\nBackup store")
-    backup.deepList().foreach(println)
+    backup.recursive_list.foreach(println)
   }
 
   def restoreDB {
-    original.deleteRecursively
+    original.recursive_delete
     println("Copying contents of " + backup + " to " + original)
-    FileSupport.recursiveCopy(backup, original)
+    backup.recursive_copy_to(original)
     printStores
   }
 
   def cleanBackup {
     if (backup.exists) {
-      backup.deleteRecursively
+      backup.recursive_delete
     }
-    backup.createDirectory(true)
+    backup.mkdirs
     printStores
   }
 

Modified: activemq/activemq-apollo/trunk/apollo-cli/src/main/scala/org/apache/activemq/apollo/cli/Apollo.scala
URL: http://svn.apache.org/viewvc/activemq/activemq-apollo/trunk/apollo-cli/src/main/scala/org/apache/activemq/apollo/cli/Apollo.scala?rev=1034561&r1=1034560&r2=1034561&view=diff
==============================================================================
--- activemq/activemq-apollo/trunk/apollo-cli/src/main/scala/org/apache/activemq/apollo/cli/Apollo.scala
(original)
+++ activemq/activemq-apollo/trunk/apollo-cli/src/main/scala/org/apache/activemq/apollo/cli/Apollo.scala
Fri Nov 12 20:54:42 2010
@@ -63,8 +63,9 @@ class Apollo extends Main with Action {
       protected override def getPrompt = BOLD+"apollo> "+RESET
       protected override def isPrintStackTraces = debug
       protected override def welcome = {
-        val source = getClass().getResourceAsStream("banner.txt")
-        copy(source, session.getConsole())
+        using(getClass().getResourceAsStream("banner.txt")) {source=>
+          copy(source, session.getConsole())
+        }
       }
 
       protected override def setSessionProperties = {}

Modified: activemq/activemq-apollo/trunk/apollo-cli/src/main/scala/org/apache/activemq/apollo/cli/commands/Create.scala
URL: http://svn.apache.org/viewvc/activemq/activemq-apollo/trunk/apollo-cli/src/main/scala/org/apache/activemq/apollo/cli/commands/Create.scala?rev=1034561&r1=1034560&r2=1034561&view=diff
==============================================================================
--- activemq/activemq-apollo/trunk/apollo-cli/src/main/scala/org/apache/activemq/apollo/cli/commands/Create.scala
(original)
+++ activemq/activemq-apollo/trunk/apollo-cli/src/main/scala/org/apache/activemq/apollo/cli/commands/Create.scala
Fri Nov 12 20:54:42 2010
@@ -91,10 +91,11 @@ class Create extends Action {
     if( target.exists && !force ) {
       error("The file '%s' already exists.  Use --force to overwrite.".format(target))
     }
-    val out = new FileOutputStream(target)
-    val in = getClass.getResourceAsStream(source)
-    copy(in, out)
-    close(out)
+    using(new FileOutputStream(target)) { out=>
+      using(getClass.getResourceAsStream(source)) { in=>
+        copy(in, out)
+      }
+    }
   }
 
 

Modified: activemq/activemq-apollo/trunk/apollo-util/src/main/scala/org/apache/activemq/apollo/util/FileSupport.scala
URL: http://svn.apache.org/viewvc/activemq/activemq-apollo/trunk/apollo-util/src/main/scala/org/apache/activemq/apollo/util/FileSupport.scala?rev=1034561&r1=1034560&r2=1034561&view=diff
==============================================================================
--- activemq/activemq-apollo/trunk/apollo-util/src/main/scala/org/apache/activemq/apollo/util/FileSupport.scala
(original)
+++ activemq/activemq-apollo/trunk/apollo-util/src/main/scala/org/apache/activemq/apollo/util/FileSupport.scala
Fri Nov 12 20:54:42 2010
@@ -16,35 +16,11 @@
  */
 package org.apache.activemq.apollo.util
 
-import tools.nsc.io.Path
-import java.io.{OutputStream, InputStream, File}
+import java.io._
 
 object FileSupport {
 
-  def recursiveCopy(source: Path, target: Path) : Unit = {
-    require(source.isDirectory, source.path + " must be a directory.")
-    if ( !target.exists ) {
-      target.toDirectory.createDirectory()
-    }
-
-    def createOrCopy(file: Path) : Unit = {
-      val newTarget = target / FileSupport.toDirectory(file.name)
-      if (file.isDirectory) {
-        recursiveCopy(file.toDirectory, newTarget)
-      } else {
-        file.toFile.copyTo(newTarget)
-      }
-    }
-    source.toDirectory.list.foreach(createOrCopy)
-  }
-
-  def toDirectory(name: String) = {
-    new tools.nsc.io.Directory(new File(name))
-  }
-
-  def toFile(name: String) = {
-    new tools.nsc.io.File(new File(name))
-  }
+  implicit def to_rich_file(file:File):RichFile = new RichFile(file)
 
 
   def system_dir(name:String) = {
@@ -59,31 +35,68 @@ object FileSupport {
     file
   }
 
+  case class RichFile(self:File) {
 
-  class RichFile(file:File) {
-    def / (path:String) = new File(file, path)
-  }
-  implicit def toRichFile(file:File):RichFile = new RichFile(file)
+    def / (path:String) = new File(self, path)
 
+    def copy_to(target:File) = {
+      using(new FileOutputStream(target)){ os=>
+        using(new FileInputStream(self)){ is=>
+          FileSupport.copy(is, os)
+        }
+      }
+    }
 
+    def recursive_list:List[File] = {
+      if( self.isDirectory ) {
+        self :: self.listFiles.toList.flatten( _.recursive_list )
+      } else {
+        self :: Nil
+      }
+    }
+
+    def recursive_delete: Unit = {
+      if( self.exists ) {
+        if( self.isDirectory ) {
+          self.listFiles.foreach(_.recursive_delete)
+        }
+        self.delete
+      }
+    }
+
+    def recursive_copy_to(target: File) : Unit = {
+      if (self.isDirectory) {
+        target.mkdirs
+        self.listFiles.foreach( file=> file.recursive_copy_to( target / file.getName)
)
+      } else {
+        self.copy_to(target)
+      }
+    }
+
+  }
+
+  /**
+   * Returns the number of bytes copied.
+   */
   def copy(in: InputStream, out: OutputStream): Long = {
+    var bytesCopied: Long = 0
+    val buffer = new Array[Byte](8192)
+    var bytes = in.read(buffer)
+    while (bytes >= 0) {
+      out.write(buffer, 0, bytes)
+      bytesCopied += bytes
+      bytes = in.read(buffer)
+    }
+    bytesCopied
+  }
+
+  def using[R,C <: Closeable](closable: C)(proc: C=>R) = {
     try {
-      var bytesCopied: Long = 0
-      val buffer = new Array[Byte](8192)
-      var bytes = in.read(buffer)
-      while (bytes >= 0) {
-        out.write(buffer, 0, bytes)
-        bytesCopied += bytes
-        bytes = in.read(buffer)
-      }
-      bytesCopied
+      proc(closable)
     } finally {
-      try { in.close  }  catch { case ignore =>  }
+      try { closable.close  }  catch { case ignore =>  }
     }
   }
 
-  def close(out: OutputStream) = {
-    try { out.close  }  catch { case ignore =>  }
-  }
 
 }
\ No newline at end of file

Modified: activemq/activemq-apollo/trunk/apollo-util/src/test/scala/org/apache/activemq/apollo/util/FileSupportTest.scala
URL: http://svn.apache.org/viewvc/activemq/activemq-apollo/trunk/apollo-util/src/test/scala/org/apache/activemq/apollo/util/FileSupportTest.scala?rev=1034561&r1=1034560&r2=1034561&view=diff
==============================================================================
--- activemq/activemq-apollo/trunk/apollo-util/src/test/scala/org/apache/activemq/apollo/util/FileSupportTest.scala
(original)
+++ activemq/activemq-apollo/trunk/apollo-util/src/test/scala/org/apache/activemq/apollo/util/FileSupportTest.scala
Fri Nov 12 20:54:42 2010
@@ -18,47 +18,26 @@
 package org.apache.activemq.apollo.util
 
 import org.scalatest.matchers.ShouldMatchers
-import tools.nsc.io.{Path, File, Directory}
 import collection.mutable.ArrayBuffer
+import java.io.File
+import FileSupport._
 
 class FileSupportTest extends FunSuiteSupport with ShouldMatchers {
 
   test("recursive file copy test") {
 
-    val base = new Directory(basedir)
-    var target = base / FileSupport.toDirectory("target")
+    val source_dir = basedir / "target" / "source-dir"
+    val target_dir = basedir / "target" / "target-dir"
+    List(source_dir, target_dir).foreach(_.recursive_delete)
 
-    val sourceDir: Directory = target / FileSupport.toDirectory("sourceDir")
-    if ( sourceDir.exists ) {
-      sourceDir.deleteRecursively
-    }
-    sourceDir.createDirectory(false)
+    (source_dir / "sub-dir" / "some-file").mkdirs
 
-    val subDir: Directory = sourceDir / FileSupport.toDirectory("subDir")
-    subDir.createDirectory(false)
+    source_dir.recursive_copy_to(target_dir)
 
-    val someFile: File = subDir / FileSupport.toFile("someFile")
-    someFile.createFile(false)
+    val listing = target_dir.recursive_list.map(file => file.getCanonicalPath)
 
-    val targetDir: Directory = target / FileSupport.toDirectory("targetDir")
-    if ( targetDir.exists ) {
-      targetDir.deleteRecursively
-    }
-
-    FileSupport.recursiveCopy(sourceDir, targetDir)
-
-    val listing = new ArrayBuffer[String]
-
-    targetDir.deepList().foreach(file => listing.append(file.toString))
-
-
-
-
-    
-    var expected = (new File(basedir)/"target"/"targetDir"/"subDir").normalize.toString
-    listing should contain( expected )
-    expected = (new File(basedir)/"target"/"targetDir"/"subDir"/"someFile").normalize.toString
-    listing should contain(expected)
+    listing should contain( (basedir/"target"/"target-dir"/"sub-dir").getCanonicalPath  )
+    listing should contain( (basedir/"target"/"target-dir"/"sub-dir"/"some-file").getCanonicalPath
)
 
   }
 



Mime
View raw message