1 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 ; 22 import java.io.FileInputStream ; 23 import java.io.FileNotFoundException ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectOutputStream ; 28 import java.util.ArrayList ; 29 import java.util.List ; 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 EXTENSION = ".msg"; 37 38 private File store; 39 40 private UUIDGenerator gen = UUIDGenerator.getInstance(); 41 42 public FilePersistenceStrategy() 43 { 44 super(); 45 } 46 47 protected String getId(Object obj) 48 { 49 String id = gen.generateRandomBasedUUID().toString(); 50 return id; 51 } 52 53 58 public Object store(String queue, Object obj) throws IOException 59 { 60 String id = getId(obj); 61 File file = new File (store, queue + File.separator + id + EXTENSION); 62 file.getParentFile().mkdirs(); 63 ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream (file)); 64 oos.writeObject(obj); 65 oos.close(); 66 return id; 67 } 68 69 74 public void remove(String queue, Object id) throws IOException 75 { 76 File file = new File (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 (file.toString()); 87 } 88 } 89 90 95 public Object load(String queue, Object id) throws IOException 96 { 97 File file = new File (store, queue + File.separator + id + EXTENSION); 98 ObjectInputStream ois = null; 99 try 100 { 101 ois = new ObjectInputStream (new FileInputStream (file)); 102 Object obj = ois.readObject(); 103 return obj; 104 } 105 catch (ClassNotFoundException e) 106 { 107 throw (IOException )new IOException ("Error loading persistent object").initCause(e); 108 } 109 finally 110 { 111 if (ois != null) 112 { 113 ois.close(); 114 } 115 } 116 } 117 118 123 public List restore() throws IOException 124 { 125 List msgs = new ArrayList (); 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 e) 138 { 139 throw (IOException )new IOException ("Could not restore").initCause(e); 140 } 141 } 142 143 protected void restoreFiles(File dir, List msgs) throws IOException , ClassNotFoundException 144 { 145 File [] 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 id = files[i].getCanonicalPath(); 160 id = id.substring(store.getCanonicalPath().length() + 1, id.length() - EXTENSION.length()); 161 String 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 173 public void open() throws IOException 174 { 175 String path = MuleManager.getConfiguration().getWorkingDirectory() + File.separator 176 + MuleConfiguration.DEFAULT_QUEUE_STORE; 177 store = FileUtils.newFile(path).getCanonicalFile(); 178 store.mkdirs(); 179 } 180 181 186 public void close() throws IOException 187 { 188 } 190 191 protected static class HolderImpl implements Holder 192 { 193 private String queue; 194 private Object id; 195 196 public HolderImpl(String queue, Object id) 197 { 198 this.queue = queue; 199 this.id = id; 200 } 201 202 public Object getId() 203 { 204 return id; 205 } 206 207 public String getQueue() 208 { 209 return queue; 210 } 211 } 212 213 public boolean isTransient() 214 { 215 return false; 216 } 217 } 218 | Popular Tags |