KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > queue > FilePersistenceStrategy


1 /*
2  * $Id: FilePersistenceStrategy.java 3937 2006-11-20 16:04:25Z lajos $
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 MuleSource MPL
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.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.mule.MuleManager;
16 import org.mule.config.MuleConfiguration;
17 import org.mule.util.file.DeleteException;
18 import org.mule.util.FileUtils;
19 import org.safehaus.uuid.UUIDGenerator;
20
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30
31 public class FilePersistenceStrategy implements QueuePersistenceStrategy
32 {
33
34     private static final Log logger = LogFactory.getLog(FilePersistenceStrategy.class);
35
36     public static final String JavaDoc EXTENSION = ".msg";
37
38     private File JavaDoc store;
39
40     private UUIDGenerator gen = UUIDGenerator.getInstance();
41
42     public FilePersistenceStrategy()
43     {
44         super();
45     }
46
47     protected String JavaDoc getId(Object JavaDoc obj)
48     {
49         String JavaDoc id = gen.generateRandomBasedUUID().toString();
50         return id;
51     }
52
53     /*
54      * (non-Javadoc)
55      *
56      * @see org.mule.transaction.xa.queue.QueuePersistenceStrategy#store(java.lang.Object)
57      */

58     public Object JavaDoc store(String JavaDoc queue, Object JavaDoc obj) throws IOException JavaDoc
59     {
60         String JavaDoc id = getId(obj);
61         File JavaDoc file = new File JavaDoc(store, queue + File.separator + id + EXTENSION);
62         file.getParentFile().mkdirs();
63         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(new FileOutputStream JavaDoc(file));
64         oos.writeObject(obj);
65         oos.close();
66         return id;
67     }
68
69     /*
70      * (non-Javadoc)
71      *
72      * @see org.mule.transaction.xa.queue.QueuePersistenceStrategy#remove(java.lang.Object)
73      */

74     public void remove(String JavaDoc queue, Object JavaDoc id) throws IOException JavaDoc
75     {
76         File JavaDoc file = new File JavaDoc(store, queue + File.separator + id + EXTENSION);
77         if (file.exists())
78         {
79             if (!file.delete())
80             {
81                 throw new DeleteException(file);
82             }
83         }
84         else
85         {
86             throw new FileNotFoundException JavaDoc(file.toString());
87         }
88     }
89
90     /*
91      * (non-Javadoc)
92      *
93      * @see org.mule.transaction.xa.queue.QueuePersistenceStrategy#load(java.lang.Object)
94      */

95     public Object JavaDoc load(String JavaDoc queue, Object JavaDoc id) throws IOException JavaDoc
96     {
97         File JavaDoc file = new File JavaDoc(store, queue + File.separator + id + EXTENSION);
98         ObjectInputStream JavaDoc ois = null;
99         try
100         {
101             ois = new ObjectInputStream JavaDoc(new FileInputStream JavaDoc(file));
102             Object JavaDoc obj = ois.readObject();
103             return obj;
104         }
105         catch (ClassNotFoundException JavaDoc e)
106         {
107             throw (IOException JavaDoc)new IOException JavaDoc("Error loading persistent object").initCause(e);
108         }
109         finally
110         {
111             if (ois != null)
112             {
113                 ois.close();
114             }
115         }
116     }
117
118     /*
119      * (non-Javadoc)
120      *
121      * @see org.mule.transaction.xa.queue.QueuePersistenceStrategy#restore()
122      */

123     public List JavaDoc restore() throws IOException JavaDoc
124     {
125         List JavaDoc msgs = new ArrayList JavaDoc();
126         if (store == null)
127         {
128             logger.warn("No store has be set on the File Persistence Strategy. Not restoring at this time");
129             return msgs;
130         }
131         try
132         {
133             restoreFiles(store, msgs);
134             logger.debug("Restore retrieved " + msgs.size() + " objects");
135             return msgs;
136         }
137         catch (ClassNotFoundException JavaDoc e)
138         {
139             throw (IOException JavaDoc)new IOException JavaDoc("Could not restore").initCause(e);
140         }
141     }
142
143     protected void restoreFiles(File JavaDoc dir, List JavaDoc msgs) throws IOException JavaDoc, ClassNotFoundException JavaDoc
144     {
145         File JavaDoc[] files = dir.listFiles();
146         if (files == null)
147         {
148             return;
149         }
150
151         for (int i = 0; i < files.length; i++)
152         {
153             if (files[i].isDirectory())
154             {
155                 restoreFiles(files[i], msgs);
156             }
157             else if (files[i].getName().endsWith(EXTENSION))
158             {
159                 String JavaDoc id = files[i].getCanonicalPath();
160                 id = id.substring(store.getCanonicalPath().length() + 1, id.length() - EXTENSION.length());
161                 String JavaDoc queue = id.substring(0, id.indexOf(File.separator));
162                 id = id.substring(queue.length() + 1);
163                 msgs.add(new HolderImpl(queue, id));
164             }
165         }
166     }
167
168     /*
169      * (non-Javadoc)
170      *
171      * @see org.mule.util.queue.QueuePersistenceStrategy#open()
172      */

173     public void open() throws IOException JavaDoc
174     {
175         String JavaDoc path = MuleManager.getConfiguration().getWorkingDirectory() + File.separator
176                       + MuleConfiguration.DEFAULT_QUEUE_STORE;
177         store = FileUtils.newFile(path).getCanonicalFile();
178         store.mkdirs();
179     }
180
181     /*
182      * (non-Javadoc)
183      *
184      * @see org.mule.transaction.xa.queue.QueuePersistenceStrategy#close()
185      */

186     public void close() throws IOException JavaDoc
187     {
188         // Nothing to do
189
}
190
191     protected static class HolderImpl implements Holder
192     {
193         private String JavaDoc queue;
194         private Object JavaDoc id;
195
196         public HolderImpl(String JavaDoc queue, Object JavaDoc id)
197         {
198             this.queue = queue;
199             this.id = id;
200         }
201
202         public Object JavaDoc getId()
203         {
204             return id;
205         }
206
207         public String JavaDoc getQueue()
208         {
209             return queue;
210         }
211     }
212
213     public boolean isTransient()
214     {
215         return false;
216     }
217 }
218
Popular Tags