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