1 9 package org.jboss.portal.core.impl.tree.loader; 10 11 import org.jboss.cache.loader.CacheLoader; 12 import org.jboss.cache.TreeCache; 13 import org.jboss.cache.Fqn; 14 import org.jboss.cache.Modification; 15 import org.apache.log4j.Logger; 16 17 import java.util.Properties ; 18 import java.util.Set ; 19 import java.util.Map ; 20 import java.util.List ; 21 import java.util.Iterator ; 22 import java.util.HashMap ; 23 import java.util.ArrayList ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.ObjectOutputStream ; 26 import java.io.IOException ; 27 import java.io.ByteArrayInputStream ; 28 import java.io.ObjectInputStream ; 29 import java.io.ObjectStreamException ; 30 31 37 public class DelegatingCacheLoader implements CacheLoader 38 { 39 40 41 private Logger log = Logger.getLogger(DelegatingCacheLoader.class); 42 43 44 private CacheLoader delegate; 45 46 public void setConfig(Properties props) 47 { 48 try 49 { 50 String className = props.getProperty("delegate.classname"); 51 52 log.debug("Loading delegate class " + className); 53 Class clazz = Thread.currentThread().getContextClassLoader().loadClass(className); 54 55 log.debug("Instantiating delegate"); 56 delegate = (CacheLoader)clazz.newInstance(); 57 58 log.debug("Adapting delegate properties"); 59 Properties delegateProps = new Properties (); 60 for (Iterator i = props.entrySet().iterator(); i.hasNext();) 61 { 62 Map.Entry entry = (Map.Entry )i.next(); 63 String propName = (String )entry.getKey(); 64 if (propName.startsWith("delegate.prop.")) 65 { 66 propName = propName.substring("delegate.prop.".length()); 67 String propValue = (String )entry.getValue(); 68 log.debug("Adding delegate property " + propName + " = " + propValue); 69 delegateProps.put(propName, propValue); 70 } 71 } 72 delegate.setConfig(delegateProps); 73 } 74 catch (Exception e) 75 { 76 throw new Error ("Cannot create delegate cache loader", e); 77 } 78 } 79 80 public void setCache(TreeCache cache) 81 { 82 delegate.setCache(cache); 83 } 84 85 public Set getChildrenNames(Fqn fqn) throws Exception 86 { 87 return delegate.getChildrenNames(fqn); 88 } 89 90 public Object get(Fqn fqn, Object key) throws Exception 91 { 92 byte[] bytes = (byte[])delegate.get(fqn, key); 93 if (bytes == null) 94 { 95 return null; 96 } 97 Object value = unmarshall(bytes); 98 return value; 99 } 100 101 public Map get(Fqn fqn) throws Exception 102 { 103 Map data = delegate.get(fqn); 104 if (data == null) 105 { 106 return null; 107 } 108 data = unmarshallMap(data); 109 return data; 110 } 111 112 public boolean exists(Fqn fqn) throws Exception 113 { 114 return delegate.exists(fqn); 115 } 116 117 public Object put(Fqn fqn, Object key, Object value) throws Exception 118 { 119 byte[] bytes = marshall(value); 120 bytes = (byte[])delegate.put(fqn, key, bytes); 121 if (bytes == null) 122 { 123 return null; 124 } 125 return unmarshall(bytes); 126 } 127 128 public void put(Fqn fqn, Map data) throws Exception 129 { 130 data = marshallMap(data); 131 delegate.put(fqn, data); 132 } 133 134 public void put(List modifications) throws Exception 135 { 136 modifications = marshallModifications(modifications); 137 delegate.put(modifications); 138 } 139 140 public Object remove(Fqn fqn, Object key) throws Exception 141 { 142 byte[] bytes = (byte[])delegate.remove(fqn, key); 143 if (bytes == null) 144 { 145 return null; 146 } 147 Object value = unmarshall(bytes); 148 return value; 149 } 150 151 public void remove(Fqn fqn) throws Exception 152 { 153 delegate.remove(fqn); 154 } 155 156 public void removeData(Fqn fqn) throws Exception 157 { 158 delegate.removeData(fqn); 159 } 160 161 public void prepare(Object tx, List modifications, boolean onePhase) throws Exception 162 { 163 modifications = marshallModifications(modifications); 164 delegate.prepare(tx, modifications, onePhase); 165 } 166 167 public void commit(Object tx) throws Exception 168 { 169 delegate.commit(tx); 170 } 171 172 public void rollback(Object tx) 173 { 174 delegate.rollback(tx); 175 } 176 177 public byte[] loadEntireState() throws Exception 178 { 179 return delegate.loadEntireState(); 180 } 181 182 public void storeEntireState(byte[] bytes) throws Exception 183 { 184 delegate.storeEntireState(bytes); 185 } 186 187 public void create() throws Exception 188 { 189 delegate.create(); 190 } 191 192 public void start() throws Exception 193 { 194 delegate.start(); 195 } 196 197 public void stop() 198 { 199 delegate.stop(); 200 } 201 202 public void destroy() 203 { 204 delegate.destroy(); 205 } 206 207 protected List marshallModifications(List modifications) throws ObjectStreamException 208 { 209 List copy = new ArrayList (); 210 for (Iterator i = modifications.iterator(); i.hasNext();) 211 { 212 Modification a = (Modification)i.next(); 213 Object value = a.getValue(); 214 Object oldValue = a.getOldValue(); 215 Map data = a.getData(); 216 Map oldData = a.getOldData(); 217 if (value != null) 218 { 219 value = marshall(value); 220 } 221 if (oldValue != null) 222 { 223 oldValue = marshall(oldValue); 224 } 225 if (data != null) 226 { 227 data = marshallMap(data); 228 } 229 if (oldData != null) 230 { 231 oldData = marshallMap(oldData); 232 } 233 Modification b = new Modification( 234 a.getType(), 235 a.getFqn(), 236 a.getKey(), 237 value, 238 oldValue, 239 data, 240 oldData); 241 copy.add(b); 242 } 243 return copy; 244 } 245 246 protected Map marshallMap(Map data) throws ObjectStreamException 247 { 248 data = new HashMap (data); 249 for (Iterator i = data.entrySet().iterator(); i.hasNext();) 250 { 251 Map.Entry entry = (Map.Entry )i.next(); 252 Object value = entry.getValue(); 253 byte[] bytes = (byte[])marshall(value); 254 entry.setValue(bytes); 255 } 256 return data; 257 } 258 259 protected Map unmarshallMap(Map data) throws ClassNotFoundException , ObjectStreamException 260 { 261 data = new HashMap (data); 262 for (Iterator i = data.entrySet().iterator(); i.hasNext();) 263 { 264 Map.Entry entry = (Map.Entry )i.next(); 265 byte[] bytes = (byte[])entry.getValue(); 266 Object value = unmarshall(bytes); 267 entry.setValue(value); 268 } 269 return data; 270 } 271 272 protected byte[] marshall(Object o) throws ObjectStreamException 273 { 274 ByteArrayOutputStream baos = null; 275 try 276 { 277 baos = new ByteArrayOutputStream (); 278 ObjectOutputStream oos = new ObjectOutputStream (baos); 279 oos.writeObject(o); 280 oos.close(); 281 return baos.toByteArray(); 282 } 283 catch (IOException e) 284 { 285 if (e instanceof ObjectStreamException ) 286 { 287 throw (ObjectStreamException )e; 288 } 289 throw new Error ("Impossible", e); 290 } 291 } 292 293 protected Object unmarshall(byte[] bytes) throws ClassNotFoundException , ObjectStreamException 294 { 295 try 296 { 297 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 298 ObjectInputStream ois = new ObjectInputStream (bais); 299 return ois.readObject(); 300 } 301 catch (IOException e) 302 { 303 if (e instanceof ObjectStreamException ) 304 { 305 throw (ObjectStreamException )e; 306 } 307 throw new Error ("Impossible", e); 308 } 309 } 310 } 311 | Popular Tags |