Coverage Report - org.mule.util.queue.FilePersistenceStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
FilePersistenceStrategy
0%
0/56
0%
0/8
2.385
FilePersistenceStrategy$HolderImpl
0%
0/6
N/A
2.385
 
 1  
 /*
 2  
  * $Id: FilePersistenceStrategy.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.util.queue;
 12  
 
 13  
 import org.mule.MuleManager;
 14  
 import org.mule.config.MuleConfiguration;
 15  
 import org.mule.util.FileUtils;
 16  
 import org.mule.util.file.DeleteException;
 17  
 
 18  
 import java.io.File;
 19  
 import java.io.FileInputStream;
 20  
 import java.io.FileNotFoundException;
 21  
 import java.io.FileOutputStream;
 22  
 import java.io.IOException;
 23  
 import java.io.ObjectInputStream;
 24  
 import java.io.ObjectOutputStream;
 25  
 import java.util.ArrayList;
 26  
 import java.util.List;
 27  
 
 28  
 import org.apache.commons.logging.Log;
 29  
 import org.apache.commons.logging.LogFactory;
 30  
 import org.safehaus.uuid.UUIDGenerator;
 31  
 
 32  
 public class FilePersistenceStrategy implements QueuePersistenceStrategy
 33  
 {
 34  
 
 35  0
     private static final Log logger = LogFactory.getLog(FilePersistenceStrategy.class);
 36  
 
 37  
     public static final String EXTENSION = ".msg";
 38  
 
 39  
     private File store;
 40  
 
 41  0
     private UUIDGenerator gen = UUIDGenerator.getInstance();
 42  
 
 43  
     public FilePersistenceStrategy()
 44  
     {
 45  0
         super();
 46  0
     }
 47  
 
 48  
     protected String getId(Object obj)
 49  
     {
 50  0
         String id = gen.generateRandomBasedUUID().toString();
 51  0
         return id;
 52  
     }
 53  
 
 54  
     /*
 55  
      * (non-Javadoc)
 56  
      * 
 57  
      * @see org.mule.transaction.xa.queue.QueuePersistenceStrategy#store(java.lang.Object)
 58  
      */
 59  
     public Object store(String queue, Object obj) throws IOException
 60  
     {
 61  0
         String id = getId(obj);
 62  0
         File file = FileUtils.newFile(store, queue + File.separator + id + EXTENSION);
 63  0
         file.getParentFile().mkdirs();
 64  0
         ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
 65  0
         oos.writeObject(obj);
 66  0
         oos.close();
 67  0
         return id;
 68  
     }
 69  
 
 70  
     /*
 71  
      * (non-Javadoc)
 72  
      * 
 73  
      * @see org.mule.transaction.xa.queue.QueuePersistenceStrategy#remove(java.lang.Object)
 74  
      */
 75  
     public void remove(String queue, Object id) throws IOException
 76  
     {
 77  0
         File file = FileUtils.newFile(store, queue + File.separator + id + EXTENSION);
 78  0
         if (file.exists())
 79  
         {
 80  0
             if (!file.delete())
 81  
             {
 82  0
                 throw new DeleteException(file);
 83  
             }
 84  
         }
 85  
         else
 86  
         {
 87  0
             throw new FileNotFoundException(file.toString());
 88  
         }
 89  0
     }
 90  
 
 91  
     /*
 92  
      * (non-Javadoc)
 93  
      * 
 94  
      * @see org.mule.transaction.xa.queue.QueuePersistenceStrategy#load(java.lang.Object)
 95  
      */
 96  
     public Object load(String queue, Object id) throws IOException
 97  
     {
 98  0
         File file = FileUtils.newFile(store, queue + File.separator + id + EXTENSION);
 99  0
         ObjectInputStream ois = null;
 100  
         try
 101  
         {
 102  0
             ois = new ObjectInputStream(new FileInputStream(file));
 103  0
             Object obj = ois.readObject();
 104  0
             return obj;
 105  
         }
 106  0
         catch (ClassNotFoundException e)
 107  
         {
 108  0
             throw (IOException) new IOException("Error loading persistent object").initCause(e);
 109  
         }
 110  
         finally
 111  
         {
 112  0
             if (ois != null)
 113  
             {
 114  0
                 ois.close();
 115  
             }
 116  
         }
 117  
     }
 118  
 
 119  
     /*
 120  
      * (non-Javadoc)
 121  
      * 
 122  
      * @see org.mule.transaction.xa.queue.QueuePersistenceStrategy#restore()
 123  
      */
 124  
     public List restore() throws IOException
 125  
     {
 126  0
         List msgs = new ArrayList();
 127  0
         if (store == null)
 128  
         {
 129  0
             logger.warn("No store has be set on the File Persistence Strategy. Not restoring at this time");
 130  0
             return msgs;
 131  
         }
 132  
         try
 133  
         {
 134  0
             restoreFiles(store, msgs);
 135  0
             logger.debug("Restore retrieved " + msgs.size() + " objects");
 136  0
             return msgs;
 137  
         }
 138  0
         catch (ClassNotFoundException e)
 139  
         {
 140  0
             throw (IOException) new IOException("Could not restore").initCause(e);
 141  
         }
 142  
     }
 143  
 
 144  
     protected void restoreFiles(File dir, List msgs) throws IOException, ClassNotFoundException
 145  
     {
 146  0
         File[] files = dir.listFiles();
 147  0
         if (files == null)
 148  
         {
 149  0
             return;
 150  
         }
 151  
 
 152  0
         for (int i = 0; i < files.length; i++)
 153  
         {
 154  0
             if (files[i].isDirectory())
 155  
             {
 156  0
                 restoreFiles(files[i], msgs);
 157  
             }
 158  0
             else if (files[i].getName().endsWith(EXTENSION))
 159  
             {
 160  0
                 String id = files[i].getCanonicalPath();
 161  0
                 id = id.substring(store.getCanonicalPath().length() + 1, id.length() - EXTENSION.length());
 162  0
                 String queue = id.substring(0, id.indexOf(File.separator));
 163  0
                 id = id.substring(queue.length() + 1);
 164  0
                 msgs.add(new HolderImpl(queue, id));
 165  
             }
 166  
         }
 167  0
     }
 168  
 
 169  
     /*
 170  
      * (non-Javadoc)
 171  
      * 
 172  
      * @see org.mule.util.queue.QueuePersistenceStrategy#open()
 173  
      */
 174  
     public void open() throws IOException
 175  
     {
 176  0
         String path = MuleManager.getConfiguration().getWorkingDirectory() + File.separator
 177  
                       + MuleConfiguration.DEFAULT_QUEUE_STORE;
 178  0
         store = FileUtils.newFile(path).getCanonicalFile();
 179  0
         store.mkdirs();
 180  0
     }
 181  
 
 182  
     /*
 183  
      * (non-Javadoc)
 184  
      * 
 185  
      * @see org.mule.transaction.xa.queue.QueuePersistenceStrategy#close()
 186  
      */
 187  
     public void close() throws IOException
 188  
     {
 189  
         // Nothing to do
 190  0
     }
 191  
 
 192  
     protected static class HolderImpl implements Holder
 193  
     {
 194  
         private String queue;
 195  
         private Object id;
 196  
 
 197  
         public HolderImpl(String queue, Object id)
 198  0
         {
 199  0
             this.queue = queue;
 200  0
             this.id = id;
 201  0
         }
 202  
 
 203  
         public Object getId()
 204  
         {
 205  0
             return id;
 206  
         }
 207  
 
 208  
         public String getQueue()
 209  
         {
 210  0
             return queue;
 211  
         }
 212  
     }
 213  
 
 214  
     public boolean isTransient()
 215  
     {
 216  0
         return false;
 217  
     }
 218  
 }