1 17 18 19 package org.apache.catalina.session; 20 21 22 import java.io.BufferedInputStream ; 23 import java.io.BufferedOutputStream ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileNotFoundException ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.ObjectInputStream ; 30 import java.io.ObjectOutputStream ; 31 import java.util.ArrayList ; 32 33 import javax.servlet.ServletContext ; 34 35 import org.apache.catalina.Container; 36 import org.apache.catalina.Context; 37 import org.apache.catalina.Globals; 38 import org.apache.catalina.Loader; 39 import org.apache.catalina.Session; 40 import org.apache.catalina.Store; 41 import org.apache.catalina.util.CustomObjectInputStream; 42 43 44 52 53 public final class FileStore 54 extends StoreBase implements Store { 55 56 57 59 60 63 private static final String FILE_EXT = ".session"; 64 65 66 68 69 74 private String directory = "."; 75 76 77 80 private File directoryFile = null; 81 82 83 86 private static final String info = "FileStore/1.0"; 87 88 91 private static final String storeName = "fileStore"; 92 93 96 private static final String threadName = "FileStore"; 97 98 99 101 102 105 public String getDirectory() { 106 107 return (directory); 108 109 } 110 111 112 117 public void setDirectory(String path) { 118 119 String oldDirectory = this.directory; 120 this.directory = path; 121 this.directoryFile = null; 122 support.firePropertyChange("directory", oldDirectory, 123 this.directory); 124 125 } 126 127 128 133 public String getInfo() { 134 135 return (info); 136 137 } 138 139 142 public String getThreadName() { 143 return(threadName); 144 } 145 146 149 public String getStoreName() { 150 return(storeName); 151 } 152 153 154 159 public int getSize() throws IOException { 160 161 File file = directory(); 163 if (file == null) { 164 return (0); 165 } 166 String files[] = file.list(); 167 168 int keycount = 0; 170 for (int i = 0; i < files.length; i++) { 171 if (files[i].endsWith(FILE_EXT)) { 172 keycount++; 173 } 174 } 175 return (keycount); 176 177 } 178 179 180 182 183 188 public void clear() 189 throws IOException { 190 191 String [] keys = keys(); 192 for (int i = 0; i < keys.length; i++) { 193 remove(keys[i]); 194 } 195 196 } 197 198 199 206 public String [] keys() throws IOException { 207 208 File file = directory(); 210 if (file == null) { 211 return (new String [0]); 212 } 213 214 String files[] = file.list(); 215 216 if((files == null) || (files.length < 1)) { 218 return (new String [0]); 219 } 220 221 ArrayList list = new ArrayList (); 223 int n = FILE_EXT.length(); 224 for (int i = 0; i < files.length; i++) { 225 if (files[i].endsWith(FILE_EXT)) { 226 list.add(files[i].substring(0, files[i].length() - n)); 227 } 228 } 229 return ((String []) list.toArray(new String [list.size()])); 230 231 } 232 233 234 244 public Session load(String id) 245 throws ClassNotFoundException , IOException { 246 247 File file = file(id); 249 if (file == null) { 250 return (null); 251 } 252 253 if (! file.exists()) { 254 return (null); 255 } 256 if (manager.getContainer().getLogger().isDebugEnabled()) { 257 manager.getContainer().getLogger().debug(sm.getString(getStoreName()+".loading", 258 id, file.getAbsolutePath())); 259 } 260 261 FileInputStream fis = null; 262 ObjectInputStream ois = null; 263 Loader loader = null; 264 ClassLoader classLoader = null; 265 try { 266 fis = new FileInputStream (file.getAbsolutePath()); 267 BufferedInputStream bis = new BufferedInputStream (fis); 268 Container container = manager.getContainer(); 269 if (container != null) 270 loader = container.getLoader(); 271 if (loader != null) 272 classLoader = loader.getClassLoader(); 273 if (classLoader != null) 274 ois = new CustomObjectInputStream(bis, classLoader); 275 else 276 ois = new ObjectInputStream (bis); 277 } catch (FileNotFoundException e) { 278 if (manager.getContainer().getLogger().isDebugEnabled()) 279 manager.getContainer().getLogger().debug("No persisted data file found"); 280 return (null); 281 } catch (IOException e) { 282 if (ois != null) { 283 try { 284 ois.close(); 285 } catch (IOException f) { 286 ; 287 } 288 ois = null; 289 } 290 throw e; 291 } 292 293 try { 294 StandardSession session = 295 (StandardSession) manager.createEmptySession(); 296 session.readObjectData(ois); 297 session.setManager(manager); 298 return (session); 299 } finally { 300 if (ois != null) { 302 try { 303 ois.close(); 304 } catch (IOException f) { 305 ; 306 } 307 } 308 } 309 } 310 311 312 321 public void remove(String id) throws IOException { 322 323 File file = file(id); 324 if (file == null) { 325 return; 326 } 327 if (manager.getContainer().getLogger().isDebugEnabled()) { 328 manager.getContainer().getLogger().debug(sm.getString(getStoreName()+".removing", 329 id, file.getAbsolutePath())); 330 } 331 file.delete(); 332 333 } 334 335 336 344 public void save(Session session) throws IOException { 345 346 File file = file(session.getIdInternal()); 348 if (file == null) { 349 return; 350 } 351 if (manager.getContainer().getLogger().isDebugEnabled()) { 352 manager.getContainer().getLogger().debug(sm.getString(getStoreName()+".saving", 353 session.getIdInternal(), file.getAbsolutePath())); 354 } 355 FileOutputStream fos = null; 356 ObjectOutputStream oos = null; 357 try { 358 fos = new FileOutputStream (file.getAbsolutePath()); 359 oos = new ObjectOutputStream (new BufferedOutputStream (fos)); 360 } catch (IOException e) { 361 if (oos != null) { 362 try { 363 oos.close(); 364 } catch (IOException f) { 365 ; 366 } 367 } 368 throw e; 369 } 370 371 try { 372 ((StandardSession)session).writeObjectData(oos); 373 } finally { 374 oos.close(); 375 } 376 377 } 378 379 380 382 383 388 private File directory() { 389 390 if (this.directory == null) { 391 return (null); 392 } 393 if (this.directoryFile != null) { 394 return (this.directoryFile); 396 } 397 File file = new File (this.directory); 398 if (!file.isAbsolute()) { 399 Container container = manager.getContainer(); 400 if (container instanceof Context ) { 401 ServletContext servletContext = 402 ((Context ) container).getServletContext(); 403 File work = (File ) 404 servletContext.getAttribute(Globals.WORK_DIR_ATTR); 405 file = new File (work, this.directory); 406 } else { 407 throw new IllegalArgumentException 408 ("Parent Container is not a Context"); 409 } 410 } 411 if (!file.exists() || !file.isDirectory()) { 412 file.delete(); 413 file.mkdirs(); 414 } 415 this.directoryFile = file; 416 return (file); 417 418 } 419 420 421 428 private File file(String id) { 429 430 if (this.directory == null) { 431 return (null); 432 } 433 String filename = id + FILE_EXT; 434 File file = new File (directory(), filename); 435 return (file); 436 437 } 438 439 440 } 441 | Popular Tags |