1 15 package org.apache.tapestry.test.mock; 16 17 import java.io.ByteArrayInputStream ; 18 import java.io.ByteArrayOutputStream ; 19 import java.io.IOException ; 20 import java.io.ObjectInputStream ; 21 import java.io.ObjectOutputStream ; 22 import java.util.Collections ; 23 import java.util.Enumeration ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 import java.util.Set ; 27 28 import org.apache.hivemind.ApplicationRuntimeException; 29 30 41 42 public class AttributeHolder 43 { 44 private Map _attributes = new HashMap (); 45 46 public Object getAttribute(String name) 47 { 48 return _attributes.get(name); 49 } 50 51 public Enumeration getAttributeNames() 52 { 53 return getEnumeration(_attributes); 54 } 55 56 protected Enumeration getEnumeration(Map map) 57 { 58 return Collections.enumeration(map.keySet()); 59 } 60 61 public String [] getAttributeNamesArray() 62 { 63 Set keys = _attributes.keySet(); 64 int count = keys.size(); 65 66 String [] array = new String [count]; 67 68 return (String []) keys.toArray(array); 69 } 70 71 public void setAttribute(String name, Object value) 72 { 73 _attributes.put(name, value); 74 } 75 76 public void removeAttribute(String name) 77 { 78 _attributes.remove(name); 79 } 80 81 86 87 public void simulateFailover() 88 { 89 byte[] serialized = serializeAttributes(); 90 91 _attributes = null; 92 93 Map restoredAttributes = deserializeAttributes(serialized); 94 95 _attributes = restoredAttributes; 96 } 97 98 private byte[] serializeAttributes() 99 { 100 try 101 { 102 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 103 ObjectOutputStream oos = new ObjectOutputStream (bos); 104 105 oos.writeObject(_attributes); 106 107 oos.close(); 108 109 return bos.toByteArray(); 110 } 111 catch (IOException ex) 112 { 113 throw new ApplicationRuntimeException("Unable to serialize attributes.", ex); 114 } 115 } 116 117 private Map deserializeAttributes(byte[] serialized) 118 { 119 try 120 { 121 ByteArrayInputStream bis = new ByteArrayInputStream (serialized); 122 ObjectInputStream ois = new ObjectInputStream (bis); 123 124 Map result = (Map ) ois.readObject(); 125 126 return result; 127 } 128 catch (Exception ex) 129 { 130 throw new ApplicationRuntimeException("Unable to deserialize attributes.", ex); 131 } 132 } 133 } 134 | Popular Tags |