1 7 package org.jboss.remoting; 8 9 import org.jboss.logging.Logger; 10 11 import java.io.File ; 12 import java.io.FileInputStream ; 13 import java.io.FileOutputStream ; 14 import java.io.FilenameFilter ; 15 import java.io.IOException ; 16 import java.io.ObjectInputStream ; 17 import java.io.ObjectOutputStream ; 18 import java.io.Serializable ; 19 import java.util.Map ; 20 21 39 public class CallbackStore implements CallbackStoreMBean 40 { 41 private String filePath = null; 42 private String fileSuffix = "ser"; 43 44 private boolean isStarted = false; 45 private boolean purgeOnShutdown = false; 46 47 public static final String FILE_PATH_KEY = "StoreFilePath"; 48 public static final String FILE_SUFFIX_KEY = "StoreFileSuffix"; 49 50 private static final Logger log = Logger.getLogger(CallbackStore.class); 51 52 55 public CallbackStore() 56 { 57 58 } 59 60 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 103 public void setPurgeOnShutdown(boolean purgeOnShutdown) 104 { 105 this.purgeOnShutdown = purgeOnShutdown; 106 } 107 108 112 public boolean getPurgeOnShutdown() 113 { 114 return purgeOnShutdown; 115 } 116 117 122 public void create() throws Exception 123 { 124 } 125 126 131 public void stop() 132 { 133 isStarted = false; 134 } 135 136 139 public void destroy() 140 { 141 if(purgeOnShutdown) 142 { 143 purgeFiles(); 144 } 145 } 146 147 public void purgeFiles() 148 { 149 String [] fileList = getObjectFileList(); 150 String fileToDelete = null; 151 for(int x = 0; x < fileList.length; x++) 152 { 153 try 154 { 155 fileToDelete = filePath + System.getProperty("file.separator") + fileList[x]; 156 File currentFile = new File (fileToDelete); 157 boolean deleted = currentFile.delete(); 158 if(!deleted) 159 { 160 log.warn("Error purging file " + fileToDelete); 161 } 162 } 163 catch(Exception e) 164 { 165 log.warn("Error purging file " + fileToDelete); 166 } 167 } 168 } 169 170 177 public void setConfig(Map config) 178 { 179 if(config != null) 180 { 181 String newFilePath = (String ) config.get(FILE_PATH_KEY); 182 if(newFilePath != null) 183 { 184 filePath = newFilePath; 185 } 186 String newFileSuffix = (String ) config.get(FILE_SUFFIX_KEY); 187 if(newFileSuffix != null) 188 { 189 fileSuffix = newFileSuffix; 190 } 191 } 192 } 193 194 199 public String getStoreFilePath() 200 { 201 return filePath; 202 } 203 204 209 public void setStoreFilePath(String filePath) 210 { 211 this.filePath = filePath; 212 } 213 214 219 public String getStoreFileSuffix() 220 { 221 return fileSuffix; 222 } 223 224 229 public void setStoreFileSuffix(String fileSuffix) 230 { 231 this.fileSuffix = fileSuffix; 232 } 233 234 235 240 public int size() 241 { 242 verifyStarted(); 243 String [] objectFileList = getObjectFileList(); 244 if(objectFileList != null) 245 { 246 return objectFileList.length; 247 } 248 else 249 { 250 return 0; 251 } 252 } 253 254 private void verifyStarted() 255 { 256 if(!isStarted) 257 { 258 throw new RuntimeException ("Can not call upon this store method before it has been started."); 259 } 260 } 261 262 271 public Object getNext() throws IOException 272 { 273 verifyStarted(); 274 275 Object obj = null; 276 String objectFilePath = null; 277 278 synchronized(filePath) 279 { 280 String [] objectFileList = getObjectFileList(); 281 FileInputStream inFile = null; 282 ObjectInputStream in = null; 283 284 if(objectFileList != null && objectFileList.length > 0) 285 { 286 try 287 { 288 objectFilePath = filePath + System.getProperty("file.separator") + objectFileList[0]; 291 inFile = new FileInputStream (objectFilePath); 292 in = new ObjectInputStream (inFile); 293 try 294 { 295 obj = in.readObject(); 296 } 297 catch(ClassNotFoundException e) 298 { 299 throw new IOException ("Error loading persisted object. Could not load class (" + e.getMessage() + ")."); 300 } 301 } 302 finally 303 { 304 if(inFile != null) 305 { 306 try 307 { 308 inFile.close(); 309 } 310 catch(IOException ioe) 311 { 312 log.debug("Error closing FileInputStream.", ioe); 313 } 314 } 315 if(in != null) 316 { 317 try 318 { 319 in.close(); 320 } 321 catch(IOException ioe) 322 { 323 log.debug("Error closing ObjectInputStream.", ioe); 324 } 325 } 326 } 327 } 328 } 329 330 if(objectFilePath != null) 331 { 332 File objectFile = new File (objectFilePath); 334 boolean isDeleted = objectFile.delete(); 335 if(log.isTraceEnabled()) 336 { 337 log.trace("object file (" + objectFilePath + ") has been deleted - " + isDeleted); 338 } 339 } 340 341 return obj; 342 } 343 344 private String [] getObjectFileList() 345 { 346 File storePath = new File (filePath); 347 String [] objectFileList = storePath.list(new StoreFileFilter()); 348 return objectFileList; 349 } 350 351 359 public void add(Serializable object) throws IOException 360 { 361 verifyStarted(); 362 363 synchronized(filePath) 364 { 365 long currentTimestamp = System.currentTimeMillis(); 366 File storeFile = new File (filePath + System.getProperty("file.separator") + String.valueOf(currentTimestamp) + "." + fileSuffix); 367 FileOutputStream outFile = null; 368 ObjectOutputStream out = null; 369 370 try 371 { 372 outFile = new FileOutputStream (storeFile, false); 373 out = new ObjectOutputStream (outFile); 374 out.writeObject(object); 375 out.flush(); 376 } 377 finally 378 { 379 if(outFile != null) 380 { 381 try 382 { 383 outFile.close(); 384 } 385 catch(IOException ioe) 386 { 387 log.debug("Error closing FileInputStream.", ioe); 388 } 389 } 390 if(out != null) 391 { 392 try 393 { 394 out.close(); 395 } 396 catch(IOException ioe) 397 { 398 log.debug("Error closing ObjectInputStream.", ioe); 399 } 400 } 401 402 } 403 } 404 } 405 406 public class StoreFileFilter implements FilenameFilter 407 { 408 416 public boolean accept(File dir, String name) 417 { 418 if(name.endsWith(fileSuffix)) 419 { 420 return true; 421 } 422 else 423 { 424 return false; 425 } 426 } 427 } 428 429 } 430 | Popular Tags |