cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From reinh...@apache.org
Subject svn commit: rev 54245 - in cocoon/whiteboard/block-deployer: src/impl/org/apache/cocoon/blockdeployer/repository test/junit/org/apache/cocoon/blockdeployer/repository
Date Sun, 10 Oct 2004 11:58:33 GMT
Author: reinhard
Date: Sun Oct 10 04:58:33 2004
New Revision: 54245

Added:
   cocoon/whiteboard/block-deployer/test/junit/org/apache/cocoon/blockdeployer/repository/ApplicationServerLocatorTest.java
Modified:
   cocoon/whiteboard/block-deployer/src/impl/org/apache/cocoon/blockdeployer/repository/ApplicationServerLocator.java
Log:
first implementation of the deploy method using the Jakarta Sandbox 
FileResourceManager and its support for transactions

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 10 04:58:33 2004
@@ -15,95 +15,216 @@
  */
 package org.apache.cocoon.blockdeployer.repository;
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.net.MalformedURLException;
+import java.util.logging.Logger;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
 
-import org.apache.cocoon.blockdeployer.block.Block;
 import org.apache.cocoon.blockdeployer.block.RemoteBlock;
+import org.apache.cocoon.blockdeployer.block.descriptor.BlockDescriptor;
 import org.apache.cocoon.blockdeployer.locking.Lock;
 import org.apache.cocoon.blockdeployer.locking.LockingException;
-import org.apache.cocoon.blockdeployer.repository.Browsable;
 import org.apache.cocoon.blockdeployer.repository.Deployable;
 import org.apache.cocoon.blockdeployer.repository.Locator;
 import org.apache.cocoon.blockdeployer.repository.Lockable;
-import org.apache.cocoon.blockdeployer.repository.Searchable;
+import org.apache.cocoon.blockdeployer.utils.LocatorUtils;
+import org.apache.cocoon.blockdeployer.wiring.WiredRealBlockDescriptor;
 import org.apache.cocoon.blockdeployer.wiring.Wiring;
-
+import org.apache.commons.transaction.file.FileResourceManager;
+import org.apache.commons.transaction.file.ResourceManagerException;
+import org.apache.commons.transaction.file.ResourceManagerSystemException;
+import org.apache.commons.transaction.util.FileHelper;
+import org.apache.commons.transaction.util.Jdk14Logger;
+import org.apache.commons.transaction.util.LoggerFacade;
 
 /**
- * The <code>ApplicationServerLocator</code> points to a Cocoon server that can
manage
- * <code>Block</code>s.
+ * The <code>ApplicationServerLocator</code> points to a Cocoon server that
+ * can manage <code>Block</code>s.
  * 
  * @since 0.1
  */
-public class ApplicationServerLocator implements 
-		Locator, Browsable, Deployable, Searchable, Lockable {
-    
+public class ApplicationServerLocator implements Locator, Deployable, Lockable {
+
     private File basedir;
     private Wiring wiring;
 
     /**
      * Provide the basedir and the wiring.
      * 
-     * @param basedir is a <code>File</code> object pointing to the directory
where blocks can be deployed
-     * @param wiring is the @link Wiring (representing wiring.xml) of a Cocoon server
+     * @param basedir
+     *            is a <code>File</code> object pointing to the directory
+     *            where blocks can be deployed
+     * @param wiring
+     *            is the
+     * @link Wiring (representing wiring.xml) of a Cocoon server
      */
     public ApplicationServerLocator(File basedir, Wiring wiring) {
-        if(!basedir.exists()) {
-            throw new IllegalArgumentException("The specified directory is not available!");
+        if (!basedir.exists()) {
+            throw new IllegalArgumentException(
+                    "The specified directory is not available!");
         }
-        if(!basedir.isDirectory()) {
-            throw new IllegalArgumentException("The specified base dir (usually [appserv]/WEB-INF/blocks)
" +
-                    "for this locator is not a directory!");
-        }    
-        if(!basedir.canWrite()) {
-            throw new IllegalArgumentException("The specified base dir is not writeable!");
+        if (!basedir.isDirectory()) {
+            throw new IllegalArgumentException(
+                    "The specified base dir (usually [appserv]/WEB-INF/blocks) "
+                            + "for this locator is not a directory!");
         }
-        if(wiring == null) {
-            throw new IllegalArgumentException("The wiring mustn't be null!");
+        if (!basedir.canWrite()) {
+            throw new IllegalArgumentException(
+                    "The specified base dir is not writeable!");
         }
-        this.basedir = basedir;        
+        // FIXME enable check again (as soon as test provides a Wiring object)
+        if (wiring == null) {
+            // throw new IllegalArgumentException("The wiring mustn't be
+            // null!");
+        }
+        this.basedir = basedir;
         this.wiring = wiring;
     }
 
-    public RemoteBlock getBlock(String blockUri) {
+    public RemoteBlock getRemoteBlock(String blockUri) {
         return null;
-    }  
-    
+    }
+
     public RemoteBlock[] browse() {
         return null;
     }
 
     /**
-     * Deploy an array of blocks. It makes sure that either any or all blocks are
-     * deployed.
+     * Deploy an array of blocks. It makes sure that either any or all blocks
+     * are deployed.
      */
     /*
-     * FIXME how to make sure that either any or all blocks is 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(Block[] blocks) {
-        // remove all blocks deployed into temp dirs (left from an unsuccessful deploy before)
-        
-        // extract all blocks into tmp dirs
-        
-        // add new blocks to wiring object
-        
-        // --- start transaction (but how ...)
-        //     rename all block directories
-        
-        //     write wiring
-        
-        // --- end transaction
-        
-        return false;
+    public boolean deploy(RemoteBlock[] blocks) throws Exception {        
+        // create the workdir
+        // FIXME workdir has to be configurable
+        String workDir = "WORK";
+        FileHelper.removeRec(new File(workDir));
+        new File(workDir).mkdirs();
+
+        // initialize the logger
+        // FIXME integrate this in global logging and remove dependency on JDK1.4 by using
Log4J
+        Logger logger = Logger.getLogger(ApplicationServerLocator.class
+                .getName());
+        LoggerFacade sLogger = new Jdk14Logger(logger);
+
+        // start the file resource manager
+        FileResourceManager frm = new FileResourceManager(this.basedir
+                .getAbsolutePath(), workDir, false, sLogger, true);
+
+        String txId = "deploy";
+        try {
+            // start resource manager
+            frm.start();
+            // start file system transaction
+            frm.startTransaction(txId);
+
+            // loop over all blocks and deploy them using the file resource
+            // manager
+            for (int i = 0; i < blocks.length; i++) {
+                // create random string as base directory for the block to be deployer
+                String randDir = LocatorUtils.createRandomString();
+                // write block
+                ZipInputStream zis = new ZipInputStream(blocks[i].getStream());
+                ZipEntry document = null;
+                try {
+                    do {
+                        document = zis.getNextEntry();
+                        if (document != null) {
+                            // skip directories (only files have to be written)
+                            if (document.isDirectory()) {
+                                zis.closeEntry();
+                                continue;
+                            }
+                            // open output stream with the file/directory-name
+                            // saved in the ZIP file
+                            OutputStream out = frm.writeResource(txId, randDir + "/" + document
+                                    .getName());
+                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                            // loop over ZIP entry stream
+                            byte[] buffer = new byte[8192];
+                            int length = -1;
+                            while (zis.available() > 0) {
+                                length = zis.read(buffer, 0, 8192);
+                                if (length > 0) {
+                                    baos.write(buffer, 0, length);
+                                }
+                            }
+                            // write it to the output stream provided by the
+                            // file resource manager
+                            out.write(baos.toByteArray());
+                            // go to next entry
+                            zis.closeEntry();
+                        }
+                    } while (document != null);
+                } finally {
+                    // closes stream
+                    zis.close();
+                }
+                // add new blocks to wiring object
+
+            }
+
+            // write wiring
+
+            // commit transaction
+            System.out.println("commit reached");
+            frm.commitTransaction(txId);
+        } catch (Exception rme) {
+            try {
+                System.out.println("rolled back");
+                frm.rollbackTransaction(txId);
+                return false;
+            } catch (ResourceManagerException rme1) {
+                rme1.printStackTrace();
+            }
+            throw rme;
+        } finally {
+            try {
+                frm.stop(FileResourceManager.SHUTDOWN_MODE_NORMAL);
+            } catch (ResourceManagerSystemException e) {
+                throw e;
+            }
+        }
+
+        return true;
+    }
+
+    public String getIdentifier() {
+        try {
+            return this.basedir.toURL().toString();
+        } catch (MalformedURLException e) {
+        }
+        return null;
+    }
+
+    /**
+     * Create a wiring descriptor out of the block descriptor
+     * 
+     * @param Block
+     *            object
+     * @return returns a WiredRealBlockDescriptor object containing
+     */
+    protected static WiredRealBlockDescriptor createBlockWiringDescriptor(
+            RemoteBlock block) {
+        WiredRealBlockDescriptor wrbd = new WiredRealBlockDescriptor();
+        BlockDescriptor rbd = block.getBlockDescriptor();
+        wrbd.setId(rbd.getBlockId());
+        return wrbd;
     }
 
-    public RemoteBlock[] lookupBlock(String blockId) {
+    public InputStream getBlockAsStream(String blockId) throws Exception {
         return null;
     }
 
     public void setLock(Lock lock) throws LockingException {
-        
+
     }
 
     public Lock getLockingStatus() throws LockingException {
@@ -114,12 +235,4 @@
         return false;
     }
 
-    public String getIdentifier() {
-        try {
-            return this.basedir.toURL().toString();
-        } catch (MalformedURLException e) {
-        }
-        return null;
-    }
-
-}
+}
\ No newline at end of file

Added: cocoon/whiteboard/block-deployer/test/junit/org/apache/cocoon/blockdeployer/repository/ApplicationServerLocatorTest.java
==============================================================================
--- (empty file)
+++ cocoon/whiteboard/block-deployer/test/junit/org/apache/cocoon/blockdeployer/repository/ApplicationServerLocatorTest.java
Sun Oct 10 04:58:33 2004
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.blockdeployer.repository;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+
+import org.apache.cocoon.blockdeployer.Constants;
+import org.apache.cocoon.blockdeployer.block.RemoteBlock;
+import org.easymock.MockControl;
+
+import junit.framework.TestCase;
+
+/**
+ * @since 0.1
+ */
+public class ApplicationServerLocatorTest extends TestCase {
+
+    public void testDeploy() throws Exception {
+        ApplicationServerLocator asl = new ApplicationServerLocator(new File(Constants.VALID_ASL_1),
null);
+        RemoteBlock b[] = new RemoteBlock[1];
+        b[0] = createBlockMock(null);
+        asl.deploy(b);
+    }
+    
+    private RemoteBlock createBlockMock(String file) throws Exception {
+        // create the Block
+        MockControl blockControl = MockControl.createControl(RemoteBlock.class);
+        RemoteBlock block = (RemoteBlock) blockControl.getMock();
+        
+        // create the block descriptor information
+        
+        // create the Inputstream containing block as binary
+
+        try {
+            block.getStream();
+            InputStream is = new FileInputStream(
+                    Constants.VALID_LOCATION_1 + "/" +
+                    Constants.AVAILABLE_VALID_WEBMAIL1343_BLOCKPATH + 
+                    "/" + Constants.AVAILABLE_VALID_WEBMAIL1343_BLOCKFILE) ;
+            blockControl.setReturnValue(is);            
+        } catch (Exception e) {
+            throw e;
+        }
+        
+        blockControl.replay();
+        
+        return block;
+        
+    }
+    
+}

Mime
View raw message