Author: reinhard
Date: Sun Oct 17 22:39:14 2004
New Revision: 54990

Modified:
   cocoon/whiteboard/block-deployer/.classpath
   
cocoon/whiteboard/block-deployer/src/impl/org/apache/cocoon/blockdeployer/repository/ApplicationServerLocator.java
   
cocoon/whiteboard/block-deployer/src/impl/org/apache/cocoon/blockdeployer/utils/LocatorUtils.java
   
cocoon/whiteboard/block-deployer/test/junit/org/apache/cocoon/blockdeployer/Constants.java
   
cocoon/whiteboard/block-deployer/test/junit/org/apache/cocoon/blockdeployer/utils/FilesystemUtilsTest.java
Log:
- new algorithm where blocks are extracted to (start with 000001, 000002, ...)
- provide testcases
- use jodd fileutils (recursive copies and delets)

Modified: cocoon/whiteboard/block-deployer/.classpath
==============================================================================
--- cocoon/whiteboard/block-deployer/.classpath (original)
+++ cocoon/whiteboard/block-deployer/.classpath Sun Oct 17 22:39:14 2004
@@ -16,5 +16,6 @@
        <classpathentry kind="lib" path="lib/log4j-1.2.8.jar"/>
        <classpathentry kind="lib" 
path="lib/commons-transaction-0.1pre-20041008.jar"/>
        <classpathentry kind="lib" path="lib/geronimo-spec-j2ee-1.0-M1.jar"/>
+       <classpathentry kind="lib" path="tools/lib/jodd-fileutils-0.29.jar"/>
        <classpathentry kind="output" path="build/eclipse"/>
 </classpath>

Modified: 
cocoon/whiteboard/block-deployer/src/impl/org/apache/cocoon/blockdeployer/repository/ApplicationServerLocator.java
==============================================================================
--- 
cocoon/whiteboard/block-deployer/src/impl/org/apache/cocoon/blockdeployer/repository/ApplicationServerLocator.java
  (original)
+++ 
cocoon/whiteboard/block-deployer/src/impl/org/apache/cocoon/blockdeployer/repository/ApplicationServerLocator.java
  Sun Oct 17 22:39:14 2004
@@ -100,10 +100,6 @@
      * Deploy an array of blocks. It makes sure that either any or all blocks
      * are deployed.
      */
-    /*
-     * FIXME throw appropriate exceptions if problem(s) occur(s) FIXME 
currently
-     * only hacked so that it works - needs some refactoring to clean up code
-     */
     public boolean deploy(RemoteBlock[] blocks) throws 
UnsupportedBlockException, IOException {        
         // create the workdir
         // FIXME workdir has to be configurable
@@ -124,6 +120,7 @@
                 .getAbsolutePath(), workDir, false, ctLogger, true);
 
         String txId = "deploy";
+        boolean success = false;
         long startDeployment = System.currentTimeMillis();
         try {
             // start resource manager
@@ -140,7 +137,8 @@
                     throw new UnsupportedBlockException("Deploying a block of 
this type is not supported!");
                 }
                 // create random string as base directory for the block to be 
deployer
-                String randDir = LocatorUtils.createRandomString();
+                String nextDir = LocatorUtils.getNextDirectory(this.basedir);
+                this.logger.info(this.getClass(), "Block to be deployed in " + 
nextDir);
                 // write block
                 ZipInputStream zis = new ZipInputStream(blocks[i].getStream());
                 ZipEntry document = null;
@@ -155,7 +153,7 @@
                             }
                             // open output stream with the file/directory-name
                             // saved in the ZIP file
-                            OutputStream out = frm.writeResource(txId, randDir 
+ "/" + document
+                            OutputStream out = frm.writeResource(txId, nextDir 
+ File.separator + document
                                     .getName());
                             ByteArrayOutputStream baos = new 
ByteArrayOutputStream();
                             // loop over ZIP entry stream
@@ -177,7 +175,7 @@
                 } finally {
                     zis.close();
                 }
-                // add new blocks to wiring object
+                // add new block to wiring object
 
             }
 
@@ -185,6 +183,7 @@
 
             // commit transaction
             frm.commitTransaction(txId);
+            success = true;
             this.logger.debug(this.getClass(), "Deployment took " + 
(System.currentTimeMillis() - startDeployment) + "ms"); 
             this.logger.info(this.getClass(), "Transaction \"" + txId + "\" 
has been committed");
         } catch (Exception rme) {
@@ -195,15 +194,16 @@
             } catch (ResourceManagerException rme1) {
                 this.logger.error(this.getClass(), "While rolling back 
transaction \"" + txId + "\" an error occurred: " + rme1.getStackTrace());
             }
-            // FIXME provide exception explanation
             throw new IOException(this.getClass().getName() + ": While 
deploying the passed blocks an error occurred. Check the logs for details. " + 
rme.getClass());
         } finally {
             try {
                 frm.stop(FileResourceManager.SHUTDOWN_MODE_NORMAL);
             } catch (ResourceManagerSystemException e) {
-                // FIXME provide explanation (at this point the blocks have 
been
-                // deployed successfully but FileResourceManager couldn't have 
been stopped.
-                throw new IOException("");
+                String msg = "the filesystem transaction that extracts all 
blocks was NOT successful.";
+                if(success) {
+                       msg = "the filesystem transaction that extracts all 
blocks WAS successful.";
+                }
+                throw new RuntimeException("FileResourceManager wasn't shut 
down normally - " + msg);
             }
         }
         return true;

Modified: 
cocoon/whiteboard/block-deployer/src/impl/org/apache/cocoon/blockdeployer/utils/LocatorUtils.java
==============================================================================
--- 
cocoon/whiteboard/block-deployer/src/impl/org/apache/cocoon/blockdeployer/utils/LocatorUtils.java
   (original)
+++ 
cocoon/whiteboard/block-deployer/src/impl/org/apache/cocoon/blockdeployer/utils/LocatorUtils.java
   Sun Oct 17 22:39:14 2004
@@ -21,7 +21,9 @@
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.Random;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 
@@ -130,18 +132,47 @@
                 absoluteBlockBasedir + " for the first available COB file.");
     }
 
-    // FIXME hack (synchronization, already created directories, alghoritmus)
-    // FIXME replace with syncronized counter starting with 0001, 0002, ...
-    public static String createRandomString() {
-        String rand = null;
-        do {
-            Random random = new Random();
-            long l = random.nextLong();
-            if(l < 0) {
-                l = l * -1;
+       /**
+     * Create a directory following the naming scheme: 000001, 000002, 000003, 
000004, ...
+     * This method is synchronized.
+     * 
+        * @param file - the base directory where the new directory should be 
created
+        * @return the new directory as @link File.
+        */
+       public static synchronized String getNextDirectory(File dir) {
+        File[] dirContent = dir.listFiles();
+        List allDirectories = new ArrayList();
+        // filter all directories
+        for(int i = 0; i < dirContent.length; i++ ) {
+            if(dirContent[i].isDirectory()) {
+               allDirectories.add(dirContent[i]);
             }
-            rand = Long.toString(l);
-        } while(rand.length() < 4);
-        return rand.substring(0,4);
-    }
+        }
+        int highestDirNumber = 0;
+        // iterate over all directories and get highest number
+        Iterator fileIterator = allDirectories.iterator();
+        while(fileIterator.hasNext()) {
+            String fileName = ((File) fileIterator.next()).getName();
+            int fileNameAsInteger;
+            try {
+                fileNameAsInteger = (new Integer(fileName)).intValue();
+                if(fileNameAsInteger > highestDirNumber) {
+                       highestDirNumber = fileNameAsInteger;
+                }
+            } catch(NumberFormatException nfe) {
+               // ignore it because directories with alpha characters are no 
problem
+            }
+        }
+        
+        // maximum length of direcotries
+        int maxDirLength = 6;
+        
+        // create a string representation of the next available directory
+        String nextDir = Integer.toString(highestDirNumber + 1);
+        StringBuffer nextDirSB = new StringBuffer();
+        for(int i = nextDir.length(); i < maxDirLength; i++) {
+               nextDirSB.append("0");
+        }
+        return nextDirSB.append(nextDir).toString();
+       }
 }

Modified: 
cocoon/whiteboard/block-deployer/test/junit/org/apache/cocoon/blockdeployer/Constants.java
==============================================================================
--- 
cocoon/whiteboard/block-deployer/test/junit/org/apache/cocoon/blockdeployer/Constants.java
  (original)
+++ 
cocoon/whiteboard/block-deployer/test/junit/org/apache/cocoon/blockdeployer/Constants.java
  Sun Oct 17 22:39:14 2004
@@ -19,10 +19,14 @@
  * @since 0.1
  */
 public interface Constants {
+    
+    // test base dir
+    public static final String TMP_JUNIT = "build/junit/tmp";
 
     // basedirs for locators
     public static final String VALID_LOCATION_1 = 
"test/sample-repositories/rep1";
     public static final String VALID_ASL_1 = 
"test/sample-repositories/application-server/WEB-INF/blocks";
+    public static final String TMP_VALID_ASL_1 = TMP_JUNIT + "/asf1";
     public static final String INVALID_LOCATION = "bla";
     
     // available and valid block
@@ -40,5 +44,6 @@
     
     // sample wiring
     public static final String WIRING_FILE = 
"test/sample-repositories/application-server/WEB-INF/blocks/wiring.xml";
+
 
 }

Modified: 
cocoon/whiteboard/block-deployer/test/junit/org/apache/cocoon/blockdeployer/utils/FilesystemUtilsTest.java
==============================================================================
--- 
cocoon/whiteboard/block-deployer/test/junit/org/apache/cocoon/blockdeployer/utils/FilesystemUtilsTest.java
  (original)
+++ 
cocoon/whiteboard/block-deployer/test/junit/org/apache/cocoon/blockdeployer/utils/FilesystemUtilsTest.java
  Sun Oct 17 22:39:14 2004
@@ -21,6 +21,8 @@
 import java.io.InputStream;
 import java.util.zip.ZipInputStream;
 
+import jodd.file.FileUtil;
+
 import org.apache.cocoon.blockdeployer.Constants;
 import org.apache.cocoon.blockdeployer.LogEnabledTestCase;
 import org.apache.cocoon.blockdeployer.repository.Locator;
@@ -29,6 +31,36 @@
  * @since 17.08.2004
  */
 public class FilesystemUtilsTest extends LogEnabledTestCase {  
+    
+    public final void testCreateNextDirectory() {
+       // create directory structure
+        String tmpDir = Constants.TMP_JUNIT + "/locatorUtil";
+        FileUtil.deleteDir(tmpDir);
+        new File(tmpDir).mkdirs();
+        new File(tmpDir + "/000010").mkdirs();
+        new File(tmpDir + "/000022").mkdirs();
+        
+        // call utitilty method
+        String newDir = LocatorUtils.getNextDirectory(new File(tmpDir));
+        assertEquals(newDir, "000023");
+        
+        // clean up
+        FileUtil.deleteDir(tmpDir);
+    }
+
+    public final void testCreateNextDirectoryWhenEmpty() {
+        // create directory structure
+        String tmpDir = Constants.TMP_JUNIT + "/locatorUtil";
+        FileUtil.deleteDir(tmpDir);
+        new File(tmpDir).mkdirs();
+        
+        // call utitilty method
+        String newDir = LocatorUtils.getNextDirectory(new File(tmpDir));
+        assertEquals(newDir, "000001");
+        
+        // clean up
+        FileUtil.deleteDir(tmpDir);
+    }
     
     public final void testReadingDescriptor() {   
         File cobFile = new File(Constants.VALID_LOCATION_1 + 

Reply via email to