1 7 package org.jboss.remoting.callback; 8 9 import java.io.File ; 10 import java.io.FileInputStream ; 11 import java.io.FileOutputStream ; 12 import java.io.FilenameFilter ; 13 import java.io.IOException ; 14 import java.io.ObjectInputStream ; 15 import java.io.ObjectOutputStream ; 16 import java.io.Serializable ; 17 import java.util.Map ; 18 import org.jboss.logging.Logger; 19 20 38 public class CallbackStore implements CallbackStoreMBean 39 { 40 private String filePath = null; 41 private String fileSuffix = "ser"; 42 43 private boolean isStarted = false; 44 private boolean purgeOnShutdown = false; 45 46 public static final String FILE_PATH_KEY = "StoreFilePath"; 47 public static final String FILE_SUFFIX_KEY = "StoreFileSuffix"; 48 49 private static final Logger log = Logger.getLogger(CallbackStore.class); 50 51 54 public CallbackStore() 55 { 56 57 } 58 59 65 public CallbackStore(boolean purgeOnDestroy) 66 { 67 this.purgeOnShutdown = purgeOnDestroy; 68 } 69 70 77 public void start() throws Exception 78 { 79 if(!isStarted) 80 { 81 if(filePath == null) 83 { 84 filePath = System.getProperty("jboss.server.data.dir", "data"); 85 } 86 File storeFile = new File (filePath); 87 if(!storeFile.exists()) 88 { 89 boolean madeDir = storeFile.mkdirs(); 90 if(!madeDir) 91 { 92 throw new IOException ("Can not create directory for store. Path given: " + filePath); 93 } 94 } 95 isStarted = true; 96 } 97 } 98 99 104 public void setPurgeOnShutdown(boolean purgeOnShutdown) 105 { 106 this.purgeOnShutdown = purgeOnShutdown; 107 } 108 109 114 public boolean getPurgeOnShutdown() 115 { 116 return purgeOnShutdown; 117 } 118 119 124 public void create() throws Exception 125 { 126 } 127 128 133 public void stop() 134 { 135 isStarted = false; 136 } 137 138 141 public void destroy() 142 { 143 if(purgeOnShutdown) 144 { 145 purgeFiles(); 146 } 147 } 148 149 public void purgeFiles() 150 { 151 String [] fileList = getObjectFileList(); 152 String fileToDelete = null; 153 for(int x = 0; x < fileList.length; x++) 154 { 155 try 156 { 157 fileToDelete = filePath + System.getProperty("file.separator") + fileList[x]; 158 File currentFile = new File (fileToDelete); 159 boolean deleted = currentFile.delete(); 160 if(!deleted) 161 { 162 log.warn("Error purging file " + fileToDelete); 163 } 164 } 165 catch(Exception e) 166 { 167 log.warn("Error purging file " + fileToDelete); 168 } 169 } 170 } 171 172 179 public void setConfig(Map config) 180 { 181 if(config != null) 182 { 183 String newFilePath = (String ) config.get(FILE_PATH_KEY); 184 if(newFilePath != null) 185 { 186 filePath = newFilePath; 187 } 188 String newFileSuffix = (String ) config.get(FILE_SUFFIX_KEY); 189 if(newFileSuffix != null) 190 { 191 fileSuffix = newFileSuffix; 192 } 193 } 194 } 195 196 201 public String getStoreFilePath() 202 { 203 return filePath; 204 } 205 206 211 public void setStoreFilePath(String filePath) 212 { 213 this.filePath = filePath; 214 } 215 216 221 public String getStoreFileSuffix() 222 { 223 return fileSuffix; 224 } 225 226 231 public void setStoreFileSuffix(String fileSuffix) 232 { 233 this.fileSuffix = fileSuffix; 234 } 235 236 237 242 public int size() 243 { 244 verifyStarted(); 245 String [] objectFileList = getObjectFileList(); 246 if(objectFileList != null) 247 { 248 return objectFileList.length; 249 } 250 else 251 { 252 return 0; 253 } 254 } 255 256 private void verifyStarted() 257 { 258 if(!isStarted) 259 { 260 throw new RuntimeException ("Can not call upon this store method before it has been started."); 261 } 262 } 263 264 273 public Object getNext() throws IOException 274 { 275 verifyStarted(); 276 277 Object obj = null; 278 String objectFilePath = null; 279 280 synchronized(filePath) 281 { 282 String [] objectFileList = getObjectFileList(); 283 FileInputStream inFile = null; 284 ObjectInputStream in = null; 285 286 if(objectFileList != null && objectFileList.length > 0) 287 { 288 try 289 { 290 objectFilePath = filePath + System.getProperty("file.separator") + objectFileList[0]; 293 inFile = new FileInputStream (objectFilePath); 294 in = new ObjectInputStream (inFile); 295 try 296 { 297 obj = in.readObject(); 298 } 299 catch(ClassNotFoundException e) 300 { 301 throw new IOException ("Error loading persisted object. Could not load class (" + e.getMessage() + ")."); 302 } 303 } 304 finally 305 { 306 if(inFile != null) 307 { 308 try 309 { 310 inFile.close(); 311 } 312 catch(IOException ioe) 313 { 314 log.debug("Error closing FileInputStream.", ioe); 315 } 316 } 317 if(in != null) 318 { 319 try 320 { 321 in.close(); 322 } 323 catch(IOException ioe) 324 { 325 log.debug("Error closing ObjectInputStream.", ioe); 326 } 327 } 328 } 329 } 330 } 331 332 if(objectFilePath != null) 333 { 334 File objectFile = new File (objectFilePath); 336 boolean isDeleted = objectFile.delete(); 337 if(log.isTraceEnabled()) 338 { 339 log.trace("object file (" + objectFilePath + ") has been deleted - " + isDeleted); 340 } 341 } 342 343 return obj; 344 } 345 346 private String [] getObjectFileList() 347 { 348 File storePath = new File (filePath); 349 String [] objectFileList = storePath.list(new StoreFileFilter()); 350 return objectFileList; 351 } 352 353 361 public void add(Serializable object) throws IOException 362 { 363 verifyStarted(); 364 365 synchronized(filePath) 366 { 367 long currentTimestamp = System.currentTimeMillis(); 368 File storeFile = new File (filePath + System.getProperty("file.separator") + String.valueOf(currentTimestamp) + "." + fileSuffix); 369 FileOutputStream outFile = null; 370 ObjectOutputStream out = null; 371 372 try 373 { 374 outFile = new FileOutputStream (storeFile, false); 375 out = new ObjectOutputStream (outFile); 376 out.writeObject(object); 377 out.flush(); 378 } 379 finally 380 { 381 if(outFile != null) 382 { 383 try 384 { 385 outFile.close(); 386 } 387 catch(IOException ioe) 388 { 389 log.debug("Error closing FileInputStream.", ioe); 390 } 391 } 392 if(out != null) 393 { 394 try 395 { 396 out.close(); 397 } 398 catch(IOException ioe) 399 { 400 log.debug("Error closing ObjectInputStream.", ioe); 401 } 402 } 403 404 } 405 } 406 } 407 408 public class StoreFileFilter implements FilenameFilter 409 { 410 418 public boolean accept(File dir, String name) 419 { 420 if(name.endsWith(fileSuffix)) 421 { 422 return true; 423 } 424 else 425 { 426 return false; 427 } 428 } 429 } 430 431 } 432 | Popular Tags |