1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
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 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| public class AttributeHolder |
43 |
| { |
44 |
| private Map _attributes = new HashMap(); |
45 |
| |
46 |
113
| public Object getAttribute(String name)
|
47 |
| { |
48 |
113
| return _attributes.get(name);
|
49 |
| } |
50 |
| |
51 |
92
| public Enumeration getAttributeNames()
|
52 |
| { |
53 |
92
| return getEnumeration(_attributes);
|
54 |
| } |
55 |
| |
56 |
113
| protected Enumeration getEnumeration(Map map)
|
57 |
| { |
58 |
113
| return Collections.enumeration(map.keySet());
|
59 |
| } |
60 |
| |
61 |
0
| public String[] getAttributeNamesArray()
|
62 |
| { |
63 |
0
| Set keys = _attributes.keySet();
|
64 |
0
| int count = keys.size();
|
65 |
| |
66 |
0
| String[] array = new String[count];
|
67 |
| |
68 |
0
| return (String[]) keys.toArray(array);
|
69 |
| } |
70 |
| |
71 |
191
| public void setAttribute(String name, Object value)
|
72 |
| { |
73 |
191
| _attributes.put(name, value);
|
74 |
| } |
75 |
| |
76 |
42
| public void removeAttribute(String name)
|
77 |
| { |
78 |
42
| _attributes.remove(name);
|
79 |
| } |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
0
| public void simulateFailover()
|
88 |
| { |
89 |
0
| byte[] serialized = serializeAttributes();
|
90 |
| |
91 |
0
| _attributes = null;
|
92 |
| |
93 |
0
| Map restoredAttributes = deserializeAttributes(serialized);
|
94 |
| |
95 |
0
| _attributes = restoredAttributes;
|
96 |
| } |
97 |
| |
98 |
0
| private byte[] serializeAttributes()
|
99 |
| { |
100 |
0
| try
|
101 |
| { |
102 |
0
| ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
103 |
0
| ObjectOutputStream oos = new ObjectOutputStream(bos);
|
104 |
| |
105 |
0
| oos.writeObject(_attributes);
|
106 |
| |
107 |
0
| oos.close();
|
108 |
| |
109 |
0
| return bos.toByteArray();
|
110 |
| } |
111 |
| catch (IOException ex) |
112 |
| { |
113 |
0
| throw new ApplicationRuntimeException("Unable to serialize attributes.", ex);
|
114 |
| } |
115 |
| } |
116 |
| |
117 |
0
| private Map deserializeAttributes(byte[] serialized)
|
118 |
| { |
119 |
0
| try
|
120 |
| { |
121 |
0
| ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
|
122 |
0
| ObjectInputStream ois = new ObjectInputStream(bis);
|
123 |
| |
124 |
0
| Map result = (Map) ois.readObject();
|
125 |
| |
126 |
0
| return result;
|
127 |
| } |
128 |
| catch (Exception ex) |
129 |
| { |
130 |
0
| throw new ApplicationRuntimeException("Unable to deserialize attributes.", ex);
|
131 |
| } |
132 |
| } |
133 |
| } |