commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Oliver Zeigermann" <oliver.zeigerm...@gmail.com>
Subject Re: svn commit: r519647 - in /jakarta/commons/proper/transaction/trunk/src: java/org/apache/commons/transaction/file/FileResourceManager.java test/org/apache/commons/transaction/file/FileResourceManagerVirtualAdminCommandsTest.java
Date Sun, 18 Mar 2007 17:56:02 GMT
This code is to allow calling certain manager methods simply by
accessing a certain path. It lacks a flexible configuration using a
mapping from suffix to method to be called, though.

2007/3/18, ozeigermann@apache.org <ozeigermann@apache.org>:
> Author: ozeigermann
> Date: Sun Mar 18 10:50:02 2007
> New Revision: 519647
>
> URL: http://svn.apache.org/viewvc?view=rev&rev=519647
> Log:
> Added sketch for administration interface
>
> Added:
>     jakarta/commons/proper/transaction/trunk/src/test/org/apache/commons/transaction/file/FileResourceManagerVirtualAdminCommandsTest.java
> Modified:
>     jakarta/commons/proper/transaction/trunk/src/java/org/apache/commons/transaction/file/FileResourceManager.java
>
> Modified: jakarta/commons/proper/transaction/trunk/src/java/org/apache/commons/transaction/file/FileResourceManager.java
> URL: http://svn.apache.org/viewvc/jakarta/commons/proper/transaction/trunk/src/java/org/apache/commons/transaction/file/FileResourceManager.java?view=diff&rev=519647&r1=519646&r2=519647
> ==============================================================================
> --- jakarta/commons/proper/transaction/trunk/src/java/org/apache/commons/transaction/file/FileResourceManager.java
(original)
> +++ jakarta/commons/proper/transaction/trunk/src/java/org/apache/commons/transaction/file/FileResourceManager.java
Sun Mar 18 10:50:02 2007
> @@ -18,6 +18,7 @@
>
>  import java.io.BufferedReader;
>  import java.io.BufferedWriter;
> +import java.io.ByteArrayInputStream;
>  import java.io.File;
>  import java.io.FileInputStream;
>  import java.io.FileNotFoundException;
> @@ -195,6 +196,8 @@
>      protected TransactionIdToPathMapper txIdMapper = null;
>
>      protected int idCnt = 0;
> +
> +    protected String virtualAdminPath = null;
>
>      /*
>       * --- ctor and general getter / setter methods ---
> @@ -202,6 +205,14 @@
>       *
>       */
>
> +    public String getVirtualAdminPath() {
> +        return virtualAdminPath;
> +    }
> +
> +    public void setVirtualAdminPath(String virutalAdminPath) {
> +        this.virtualAdminPath = virutalAdminPath;
> +    }
> +
>      /**
>       * Creates a new resource manager operation on the specified directories.
>       *
> @@ -717,16 +728,26 @@
>      }
>
>      public boolean resourceExists(Object txId, Object resourceId) throws ResourceManagerException
{
> +        if (isVirtualAdminId(resourceId)) {
> +            logger.logFine("Faking existence of virtual administration command");
> +            return true;
> +        }
> +
>          lockResource(resourceId, txId, true);
>          return (getPathForRead(txId, resourceId) != null);
>      }
>
>      public void deleteResource(Object txId, Object resourceId) throws ResourceManagerException
{
> +
> +        checkForVirtualAdminCommand(resourceId);
> +
>          deleteResource(txId, resourceId, true);
>      }
>
>      public void deleteResource(Object txId, Object resourceId, boolean assureOnly) throws
ResourceManagerException {
>
> +        checkForVirtualAdminCommand(resourceId);
> +
>          if (logger.isFineEnabled()) logger.logFine(txId + " deleting " + resourceId);
>
>          lockResource(resourceId, txId, false);
> @@ -760,11 +781,16 @@
>      }
>
>      public void createResource(Object txId, Object resourceId) throws ResourceManagerException
{
> +
> +        checkForVirtualAdminCommand(resourceId);
> +
>          createResource(txId, resourceId, true);
>      }
>
>      public void createResource(Object txId, Object resourceId, boolean assureOnly) throws
ResourceManagerException {
>
> +        checkForVirtualAdminCommand(resourceId);
> +
>          if (logger.isFineEnabled()) logger.logFine(txId + " creating " + resourceId);
>
>          lockResource(resourceId, txId, false);
> @@ -798,6 +824,10 @@
>      }
>
>      public void copyResource(Object txId, Object fromResourceId, Object toResourceId,
boolean overwrite) throws ResourceManagerException {
> +
> +        checkForVirtualAdminCommand(fromResourceId);
> +        checkForVirtualAdminCommand(toResourceId);
> +
>          if (logger.isFineEnabled()) logger.logFine(txId + " copying " + fromResourceId
+ " to " + toResourceId);
>
>          lockResource(fromResourceId, txId, true);
> @@ -825,6 +855,10 @@
>      }
>
>      public void moveResource(Object txId, Object fromResourceId, Object toResourceId,
boolean overwrite) throws ResourceManagerException {
> +
> +        checkForVirtualAdminCommand(fromResourceId);
> +        checkForVirtualAdminCommand(toResourceId);
> +
>          if (logger.isFineEnabled()) logger.logFine(txId + " moving " + fromResourceId
+ " to " + toResourceId);
>
>          lockResource(fromResourceId, txId, false);
> @@ -836,6 +870,12 @@
>      }
>
>      public InputStream readResource(Object resourceId) throws ResourceManagerException
{
> +
> +        if (isVirtualAdminId(resourceId)) {
> +            logger.logWarning("Issuing virtual admin command" + resourceId);
> +            return executeAdminCommand(resourceId);
> +        }
> +
>          // create temporary light weight tx
>          Object txId;
>          synchronized (globalTransactions) {
> @@ -855,6 +895,12 @@
>      }
>
>      public InputStream readResource(Object txId, Object resourceId) throws ResourceManagerException
{
> +
> +        if (isVirtualAdminId(resourceId)) {
> +            String message = "You must not call virtual admin commands (" + resourceId
+ ") from within transactions!";
> +            logger.logSevere(message);
> +            throw new ResourceManagerException(message);
> +        }
>
>          if (logger.isFineEnabled()) logger.logFine(txId + " reading " + resourceId);
>
> @@ -875,12 +921,73 @@
>          }
>      }
>
> +    protected void checkForVirtualAdminCommand(Object resourceId) throws ResourceManagerException
{
> +        if (isVirtualAdminId(resourceId)) {
> +            String message = "You must not make modification calls to virtual admin
commands ("
> +                    + resourceId + ")!";
> +            logger.logSevere(message);
> +            throw new ResourceManagerException(message);
> +        }
> +    }
> +
> +    protected boolean isVirtualAdminId(Object resourceId) {
> +        return (getVirtualAdminPath() != null && resourceId.toString().startsWith(
> +                getVirtualAdminPath()));
> +    }
> +
> +    protected InputStream executeAdminCommand(Object resourceId) {
> +        StringBuffer sb = new StringBuffer();
> +
> +        if (!isVirtualAdminId(resourceId)) {
> +            String message = "Internal error: " + resourceId.toString()
> +                    + " is no administration command, but is supposed to!";
> +            sb.append(message);
> +            logger.logSevere(message);
> +        } else {
> +            String command = resourceId.toString().substring(getVirtualAdminPath().length());
> +            logger.logInfo("Processing admin command " + command);
> +
> +            // XXX this really should be more flexible
> +            try {
> +                if (isAKnowCommand(command)) {
> +                    if (command.equals("recover")) {
> +                        recover();
> +                    }
> +
> +                    String message = "Command " + command + " terminated successfully";
> +                    sb.append(message);
> +                    logger.logInfo(message);
> +                } else {
> +                    String message = "Command " + command + " unknown";
> +                    sb.append(message);
> +                    logger.logWarning(message);
> +
> +                }
> +            } catch (ResourceManagerSystemException e) {
> +                String message = "Command " + command + " failed with the following
message: "
> +                        + e.getMessage();
> +                sb.append(message);
> +                logger.logSevere(message, e);
> +            }
> +
> +        }
> +        ByteArrayInputStream baIs = new ByteArrayInputStream(sb.toString().getBytes());
> +        return baIs;
> +
> +    }
> +
> +    protected boolean isAKnowCommand(String command) {
> +        return command.equals("recover");
> +    }
> +
>      public OutputStream writeResource(Object txId, Object resourceId) throws ResourceManagerException
{
>          return writeResource(txId, resourceId, false);
>      }
>
>      public OutputStream writeResource(Object txId, Object resourceId, boolean append)
throws ResourceManagerException {
>
> +        checkForVirtualAdminCommand(resourceId);
> +
>          if (logger.isFineEnabled()) logger.logFine(txId + " writing " + resourceId);
>
>          lockResource(resourceId, txId, false);
>
> Added: jakarta/commons/proper/transaction/trunk/src/test/org/apache/commons/transaction/file/FileResourceManagerVirtualAdminCommandsTest.java
> URL: http://svn.apache.org/viewvc/jakarta/commons/proper/transaction/trunk/src/test/org/apache/commons/transaction/file/FileResourceManagerVirtualAdminCommandsTest.java?view=auto&rev=519647
> ==============================================================================
> --- jakarta/commons/proper/transaction/trunk/src/test/org/apache/commons/transaction/file/FileResourceManagerVirtualAdminCommandsTest.java
(added)
> +++ jakarta/commons/proper/transaction/trunk/src/test/org/apache/commons/transaction/file/FileResourceManagerVirtualAdminCommandsTest.java
Sun Mar 18 10:50:02 2007
> @@ -0,0 +1,166 @@
> +/*
> + * Licensed to the Apache Software Foundation (ASF) under one or more
> + * contributor license agreements.  See the NOTICE file distributed with
> + * this work for additional information regarding copyright ownership.
> + * The ASF licenses this file to You 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.commons.transaction.file;
> +
> +import java.io.BufferedReader;
> +import java.io.File;
> +import java.io.FileOutputStream;
> +import java.io.IOException;
> +import java.io.InputStream;
> +import java.io.InputStreamReader;
> +
> +import junit.framework.Test;
> +import junit.framework.TestCase;
> +import junit.framework.TestSuite;
> +
> +import org.apache.commons.logging.Log;
> +import org.apache.commons.logging.LogFactory;
> +import org.apache.commons.transaction.util.CommonsLoggingLogger;
> +import org.apache.commons.transaction.util.FileHelper;
> +import org.apache.commons.transaction.util.LoggerFacade;
> +
> +/**
> + * Tests for FileResourceManager.
> + *
> + * @version $Id: FileResourceManagerTest.java 493628 2007-01-07 01:42:48Z joerg $
> + */
> +public class FileResourceManagerVirtualAdminCommandsTest extends TestCase {
> +
> +    private static final Log log = LogFactory
> +            .getLog(FileResourceManagerVirtualAdminCommandsTest.class.getName());
> +
> +    private static final LoggerFacade sLogger = new CommonsLoggingLogger(log);
> +
> +    private static final String ADMIN_COMMAND_PREFIX = "/extremelyUnlikelyPrefixForAdminCommands";
> +
> +    private static final String STORE = "tmp/store";
> +
> +    private static final String WORK = "tmp/work";
> +
> +    private static final String ENCODING = "ISO-8859-15";
> +
> +    private static final String[] INITIAL_FILES = new String[] { STORE + "/olli/Hubert6",
> +            STORE + "/olli/Hubert" };
> +
> +    protected static final long TIMEOUT = Long.MAX_VALUE;
> +
> +    private static void removeRec(String dirPath) {
> +        FileHelper.removeRec(new File(dirPath));
> +    }
> +
> +    private static final void createFiles(String[] filePaths) {
> +        createFiles(filePaths, null, null);
> +    }
> +
> +    private static final void createFiles(String[] filePaths, String[] contents, String
dirPath) {
> +        for (int i = 0; i < filePaths.length; i++) {
> +            String filePath = filePaths[i];
> +            File file;
> +            if (dirPath != null) {
> +                file = new File(new File(dirPath), filePath);
> +            } else {
> +                file = new File(filePath);
> +            }
> +            file.getParentFile().mkdirs();
> +            try {
> +                file.delete();
> +                file.createNewFile();
> +                String content = null;
> +                if (contents != null && contents.length > i) {
> +                    content = contents[i];
> +                }
> +                if (content != null) {
> +                    FileOutputStream stream = new FileOutputStream(file);
> +                    stream.write(contents[i].getBytes(ENCODING));
> +                    stream.close();
> +                }
> +            } catch (IOException e) {
> +            }
> +        }
> +    }
> +
> +    private static void reset() {
> +        removeRec(STORE);
> +        removeRec(WORK);
> +        new File(STORE).mkdirs();
> +        new File(WORK).mkdirs();
> +    }
> +
> +    private static void createInitialFiles() {
> +        createFiles(INITIAL_FILES);
> +    }
> +
> +    public static FileResourceManager createFRM() {
> +        return new FileResourceManager(STORE, WORK, false, sLogger, true) {
> +            public void setDirty(Object txId, Throwable t) {
> +                dirty = true;
> +            }
> +
> +        };
> +    }
> +
> +    public static Test suite() {
> +        TestSuite suite = new TestSuite(FileResourceManagerVirtualAdminCommandsTest.class);
> +        return suite;
> +    }
> +
> +    public static void main(java.lang.String[] args) {
> +        junit.textui.TestRunner.run(suite());
> +    }
> +
> +    public FileResourceManagerVirtualAdminCommandsTest(String testName) {
> +        super(testName);
> +    }
> +
> +    public void testRecover() throws Throwable {
> +        sLogger.logInfo("Checking recover() admin command");
> +        reset();
> +        createInitialFiles();
> +        FileResourceManager frm = createFRM();
> +        frm.setVirtualAdminPath(ADMIN_COMMAND_PREFIX);
> +        InputStream is = frm.readResource(ADMIN_COMMAND_PREFIX + "recover");
> +        BufferedReader bf = new BufferedReader(new InputStreamReader(is));
> +        String line = bf.readLine();
> +        assertEquals(
> +                line,
> +                "Command recover failed with the following message: Recovery is possible
in started or starting resource manager only: System error");
> +        frm.start();
> +        frm.setDirty(null, null);
> +        frm.startTransaction("newTx");
> +        boolean failed = false;
> +        try {
> +        frm.commitTransaction("newTx");
> +        } catch (ResourceManagerException rme) {
> +            failed = true;
> +        }
> +        assertTrue(failed);
> +        is = frm.readResource(ADMIN_COMMAND_PREFIX + "recover");
> +        bf = new BufferedReader(new InputStreamReader(is));
> +        line = bf.readLine();
> +        assertEquals(
> +                line,
> +                "Command recover terminated successfully");
> +        frm.startTransaction("newTx");
> +        failed = false;
> +        try {
> +            frm.commitTransaction("newTx");
> +            } catch (ResourceManagerException rme) {
> +                failed = true;
> +            }
> +            assertFalse(failed);
> +    }
> +}
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message