1 16 package org.apache.cocoon.transformation.helpers; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.ObjectInputStream ; 21 import java.io.ObjectOutputStream ; 22 import java.io.OutputStream ; 23 import java.io.Serializable ; 24 25 import org.apache.avalon.framework.logger.Logger; 26 import org.apache.cocoon.CascadingIOException; 27 import org.apache.cocoon.util.HashUtil; 28 import org.apache.excalibur.source.ModifiableSource; 29 import org.apache.excalibur.source.Source; 30 import org.apache.excalibur.source.SourceResolver; 31 32 41 public final class ModifiableSourceIncludeCacheStorageProxy 42 implements IncludeCacheStorageProxy { 43 44 private SourceResolver resolver; 45 private String parentURI; 46 private Logger logger; 47 48 54 public ModifiableSourceIncludeCacheStorageProxy(SourceResolver resolver, 55 String parentURI, 56 Logger logger) { 57 this.resolver = resolver; 58 this.parentURI= parentURI; 59 this.logger = logger; 60 } 61 62 67 private String getURI(String uri) { 68 final long hash = HashUtil.hash(uri); 69 final StringBuffer buffer = new StringBuffer (this.parentURI); 70 buffer.append('/'); 71 if (hash < 0) { 72 buffer.append('M').append(hash * -1); 73 } else { 74 buffer.append(hash); 75 } 76 buffer.append(".cxml"); 77 return buffer.toString(); 78 } 79 80 83 public Serializable get(String uri) { 84 if (logger.isDebugEnabled()) { 85 logger.debug("WSCProxy: Getting content for " + uri); 86 } 87 88 Source child = null; 89 Serializable result = null; 90 try { 91 child = this.resolver.resolveURI(this.getURI(uri)); 92 93 if (logger.isDebugEnabled()) { 94 logger.debug("WSCProxy: Resolved to " + child.getURI()); 95 } 96 97 if (child.exists()) { 98 InputStream is = child.getInputStream(); 99 ObjectInputStream ois = new ObjectInputStream (is); 100 result = (Serializable )ois.readObject(); 101 ois.close(); 102 } 103 } catch (Exception ignore) { 104 } finally { 105 this.resolver.release( child ); 106 } 107 108 if (logger.isDebugEnabled()) { 109 logger.debug("WSCProxy: Result for " + uri + " : " + (result == null ? "Not in cache" : "Found")); 110 } 111 return result; 112 } 113 114 117 public void put(String uri, Serializable object) 118 throws IOException { 119 if (logger.isDebugEnabled()) { 120 logger.debug("WSCProxy: Storing content for " + uri); 121 } 122 Source child = null; 123 try { 124 child = this.resolver.resolveURI(this.getURI(uri)); 125 126 if (logger.isDebugEnabled()) { 127 logger.debug("WSCProxy: Resolved to " + child.getURI()); 128 } 129 130 OutputStream os; 131 if (child instanceof ModifiableSource) { 132 os = ((ModifiableSource)child).getOutputStream(); 133 } else { 134 throw new IOException ("Source " + uri + " is not writeable."); 135 } 136 ObjectOutputStream oos = new ObjectOutputStream (os); 137 oos.writeObject(object); 138 oos.flush(); 139 oos.close(); 140 } catch (IOException io) { 141 throw io; 142 } catch (Exception ignore) { 143 throw new CascadingIOException("Exception.", ignore); 144 } finally { 145 this.resolver.release( child ); 146 } 147 } 148 149 152 public void remove(String uri) { 153 if (logger.isDebugEnabled()) { 154 logger.debug("WSCProxy: Removing content for " + uri); 155 } 156 Source child = null; 157 try { 158 child = this.resolver.resolveURI(this.getURI(uri)); 159 160 if (logger.isDebugEnabled()) { 161 logger.debug("WSCProxy: Resolved to " + child.getURI()); 162 } 163 164 if (child instanceof ModifiableSource) { 165 ((ModifiableSource)child).delete(); 166 } else { 167 throw new IOException ("Source " + uri + " is not writeable."); 168 } 169 } catch (Exception ignore) { 170 } finally { 171 this.resolver.release( child ); 172 } 173 } 174 175 178 public boolean equals(Object object) { 179 if (object instanceof ModifiableSourceIncludeCacheStorageProxy) { 180 return this.parentURI.equals(((ModifiableSourceIncludeCacheStorageProxy)object).parentURI); 181 } 182 return false; 183 } 184 185 188 public int hashCode() { 189 return this.parentURI.hashCode(); 190 } 191 192 } 193 | Popular Tags |