Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags.
   * See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   * This copyrighted material is made available to anyone wishing to use,
   * modify, copy, or redistribute it subject to the terms and conditions
   * of the GNU Lesser General Public License, v. 2.1.
  * This program is distributed in the hope that it will be useful, but WITHOUT A
  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  * You should have received a copy of the GNU Lesser General Public License,
  * v.2.1 along with this distribution; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  * MA  02110-1301, USA.
  *
  * (C) 2005-2006,
  * @author JBoss Inc.
  */
 
 package com.arjuna.ats.arjuna.tools.log;
 
 
Commands: ls <type> - list logs for specified type. If no type specified then must be already attached. select [<type] - browse a specific type of transaction. Automatically detaches. attach <log> - cannot be attached to another log detach - must be attached to log forget <pid> - must be attached to log delete <pid> - must be attached to log types - lists supported transaction types quit - exit help - help

Author(s):
marklittle
 
 
 class LogConsole
 {
     public static final int MAX_COMMAND_LEN = 1024; // bytes
     public static final String DEFAULT_TYPE = "AtomicAction";
 
     private enum Command
     {
         invalid, ls, attach, detach, forget, delete, quit, select, types, help
     };
 
     public static final String ls = "ls";
     public static final String attach = "attach";
     public static final String detach = "detach";
     public static final String forget = "forget";
     public static final String delete = "delete";
     public static final String quit = "quit";
     public static final String select = "select";
     public static final String types = "types";
     public static final String help = "help";
     
     private static final String SPACE = " ";
     private static final String END = "\n";
     
     public void doWork ()
     {
         boolean attached = false;  // move these to member variables
         boolean exit = false;
         boolean selected = false;
 
         while (!exit)
         {
             byte[] command = new byte[];
             
             try
             {
                 ..print("\n"++" - "+" > ");
 
                 ..read(command);
 
                 String commandString = new String(command);
 
                 Command com = validCommand(commandString);
 
                 switch (com)
                 {
                 case :
                     return;
                 case :
                     help();
                    break;
                case :
                    if (!selected)
                        ..println("No transaction type selected.");
                    else
                    {
                        if ( == "")
                            listLogs();
                        else
                            dumpLog(new Uid());
                    }
                    ..println();
                    
                    break;
                case :
                    if (attached)
                    {
                        ..println("Detaching from existing log.");
                        
                        attached = false;
                    }
                    setTransactionType(commandString);
                    
                    if ("".equals())
                    {
                        ..println("Unsupported type.");
                        
                        selected = false;
                    }
                    else
                        selected = true;
                    
                    break;
                case :
                    if (attached)
                        ..println("Already attached.");
                    else
                    {
                        setLogId(commandString);
                        
                        if ("".equals())
                        {
                            ..println("Invalid log id.");
                        }
                        else
                            attached = true;
                    }
                    break;
                case :
                    if (!attached)
                        ..println("Not attached.");
                    
                     = "";
                    
                    attached = false;
                    break;
                case :
                    if (!attached)
                        ..println("Not attached.");
                    
                    Uid u = new Uid();
                    EditableTransaction act = TransactionTypeManager.getInstance().getTransaction(u);
                    
                    try
                    {
                        act.moveHeuristicToPrepared(getIndex(commandString));
                    }
                    catch (final IndexOutOfBoundsException ex)
                    {
                        ..println("Invalid index.");
                    }
                    dumpLog(u);
                    break;
                case :
                    if (!attached)
                        ..println("Not attached.");
                    
                    Uid uid = new Uid();
                    EditableTransaction ract = TransactionTypeManager.getInstance().getTransaction(uid);
                    
                    try
                    {
                        ract.deleteHeuristicParticipant(getIndex(commandString));
                    }
                    catch (final IndexOutOfBoundsException ex)
                    {
                        ..println("Invalid index.");
                    }
                    
                    dumpLog(uid);
                    
                    break;
                case :
                    printSupportedTypes();
                    break;
                default:
                    .
                            .println("Invalid command " + new String(command));
                    break;
                }
            }
            catch (final Exception ex)
            {
                ex.printStackTrace();
            }
        }
    }
    
    private void dumpLog (final Uid u)
    {
        EditableTransaction act = TransactionTypeManager.getInstance().getTransaction(u);
        
        if (act == null)
            ..println("Dump failed! Unknown type "+);
        else
            ..println(act.toString());
    }
    
    private void printSupportedTypes ()
    {
        ..println();
    }
    
    private final Command validCommand (String command)
    {
        if (command == null)
            return .;
        if (!command.startsWith())
        {
            if (!command.startsWith())
            {
                if (!command.startsWith())
                {
                    if (!command.startsWith())
                    {
                        if (!command.startsWith())
                        {
                            if (!command.startsWith())
                            {
                                if (!command.startsWith())
                                {
                                    if (!command.startsWith())
                                    {
                                        if (!command.startsWith())
                                            return .;
                                        else
                                            return .;
                                    }
                                    else
                                        return .;
                                }
                                else
                                    return .;
                            }
                            else
                                return .;
                        }
                        else
                            return .;
                    }
                    else
                        return .;
                }
                else
                    return .;
            }
            else
                return .;
        }
        else
            return .;
    }
    private final void setTransactionType (String command)
    {
        int index = command.indexOf();
        int end = command.indexOf();
        
        if (index != -1)
             = new String(command.substring(index + 1, end).trim());
        else
             = ;
        
        if (!TransactionTypeManager.getInstance().present())
        {
            ..println("Transaction log type "++" not supported.");            
             = "";
        }
    }
    
    private final void setLogId (String command)
    {
        int index = command.indexOf();
        int end = command.indexOf();
        
        if (index != -1)
        {
             = new String(command.substring(index + 1, end).trim());
            if (!supportedLog())
                 = "";
        }
        else
             = "";
    }
    
    private final int getIndex (String command)
    {
        int index = command.indexOf();
        int end = command.indexOf();
        
        if (index != -1)
        {
            try
            {
                return Integer.parseInt(command.substring(index+1, end).trim());
            }
            catch (final Exception ex)
            {
                return -1;
            }
        }
        else
            return -1;
    }
    /*
     * Go through the log and print out all of the instances.
     */
    
    private final void listLogs (String typethrows IOException
    {
        InputObjectState buff = new InputObjectState();
        
        try
        {
            if (StoreManager.getRecoveryStore().allObjUids(typebuff))
            {
                Uid u = null;
                do
                {
                    u = UidHelper.unpackFrom(buff);
                    if (Uid.nullUid().notEquals(u))
                    {
                        ..println("Log: " + u);
                    }
                }
                while (Uid.nullUid().notEquals(u));
            }
        }
        catch (final ObjectStoreException ex)
        {
            throw new IOException();
        }
    }
    
    /*
     * Is this a type/instance in the log that we support?
     */
    
    private final boolean supportedLog (String logID)
    {
        Uid id = new Uid(logID);
        
        if (id.equals(Uid.nullUid()))
            return false;
        ObjectStoreIterator iter = new ObjectStoreIterator(StoreManager.getRecoveryStore(), );
        Uid u;
        do
        {
            u = iter.iterate();
            if (u.equals(id))
                return true;
        }
        while (Uid.nullUid().notEquals(u));
        return false;
    }
    
    private final void help ()
    {
        ..println("\nls <type> - list logs for specified type. If no type specified then must be already attached.");
        ..println("select [<type] - browse a specific type of transaction. Automatically detaches.");
        ..println("attach <log> - cannot be attached to another log.");
        ..println("detach - must be attached to log.");
        ..println("forget <pid> - must be attached to log.");
        ..println("delete <pid> - must be attached to log.");
        ..println("types - lists supported transaction types.");
        ..println("quit - exit.");
        ..println("help - print out commands.\n");
    }
    
    private String _currentLog = "";
    private String _transactionType = "";
public class LogBrowser
    public static final void main (String[] args)
    {
        LogConsole console = new LogConsole();
        console.doWork();
    }
New to GrepCode? Check out our FAQ X