1 16 package org.apache.cocoon.caching.impl; 17 18 import java.io.File ; 19 import java.io.FileInputStream ; 20 import java.io.FileNotFoundException ; 21 import java.io.FileOutputStream ; 22 import java.io.IOException ; 23 import java.io.ObjectInputStream ; 24 import java.io.ObjectOutputStream ; 25 26 import org.apache.avalon.framework.context.Context; 27 import org.apache.avalon.framework.context.ContextException; 28 import org.apache.avalon.framework.context.Contextualizable; 29 import org.apache.cocoon.Constants; 30 import org.apache.cocoon.caching.EventRegistry; 31 32 41 public class DefaultEventRegistryImpl extends AbstractDoubleMapEventRegistry 42 implements EventRegistry, Contextualizable { 43 44 private static final String PERSISTENT_FILE = "/WEB-INF/ev_cache.ser"; 45 46 private File m_persistentFile; 47 48 51 public void contextualize(Context context) throws ContextException { 52 org.apache.cocoon.environment.Context ctx = 53 (org.apache.cocoon.environment.Context) context.get( 54 Constants.CONTEXT_ENVIRONMENT_CONTEXT); 55 String path = ctx.getRealPath(PERSISTENT_FILE); 57 if (path == null) { 58 throw new ContextException("The cache event registry cannot be used inside an unexpanded WAR file. " + 59 "Real path for <" + PERSISTENT_FILE + "> is null."); 60 } 61 62 m_persistentFile = new File (path); 63 } 64 65 70 protected void persist(EventRegistryDataWrapper registryWrapper) { 71 ObjectOutputStream oos = null; 72 try { 73 oos = new ObjectOutputStream (new FileOutputStream (this.m_persistentFile)); 74 oos.writeObject(registryWrapper); 75 oos.flush(); 76 } catch (FileNotFoundException e) { 77 getLogger().error("Unable to persist EventRegistry", e); 78 } catch (IOException e) { 79 getLogger().error("Unable to persist EventRegistry", e); 80 } finally { 81 try { 82 if (oos != null) oos.close(); 83 } catch (IOException e) { } 84 } 85 } 86 87 92 protected boolean recover() { 93 if (this.m_persistentFile.exists()) { 94 ObjectInputStream ois = null; 95 EventRegistryDataWrapper ecdw = null; 96 try { 97 ois = new ObjectInputStream (new FileInputStream (this.m_persistentFile)); 98 ecdw = (EventRegistryDataWrapper) ois.readObject(); 99 } catch (FileNotFoundException e) { 100 getLogger().error("Unable to retrieve EventRegistry", e); 101 createBlankCache(); 102 return false; 103 } catch (IOException e) { 104 getLogger().error("Unable to retrieve EventRegistry", e); 105 createBlankCache(); 106 return false; 107 } catch (ClassNotFoundException e) { 108 getLogger().error("Unable to retrieve EventRegistry", e); 109 createBlankCache(); 110 return false; 111 } finally { 112 try { 113 if (ois != null) ois.close(); 114 } catch (IOException e) { 115 } 117 } 118 unwrapRegistry(ecdw); 119 } else { 120 getLogger().warn(this.m_persistentFile + " does not exist - Unable to " + 121 "retrieve EventRegistry."); 122 createBlankCache(); 123 return false; 124 } 125 return true; 126 } 127 128 } 129 | Popular Tags |