KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > test > mock > AttributeHolder


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.test.mock;
16
17 import java.io.ByteArrayInputStream JavaDoc;
18 import java.io.ByteArrayOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.ObjectOutputStream JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.apache.hivemind.ApplicationRuntimeException;
29
30 /**
31  *
32  * Base class for holders of named attributes such as
33  * {@link javax.servlet.http.HttpSession},
34  * {@link javax.servlet.http.HttpServletRequest}
35  * and {@link javax.servlet.ServletContext}.
36  *
37  *
38  * @author Howard Lewis Ship
39  * @since 4.0
40  */

41
42 public class AttributeHolder
43 {
44     private Map JavaDoc _attributes = new HashMap JavaDoc();
45
46     public Object JavaDoc getAttribute(String JavaDoc name)
47     {
48         return _attributes.get(name);
49     }
50
51     public Enumeration JavaDoc getAttributeNames()
52     {
53         return getEnumeration(_attributes);
54     }
55
56     protected Enumeration JavaDoc getEnumeration(Map JavaDoc map)
57     {
58         return Collections.enumeration(map.keySet());
59     }
60
61     public String JavaDoc[] getAttributeNamesArray()
62     {
63         Set JavaDoc keys = _attributes.keySet();
64         int count = keys.size();
65
66         String JavaDoc[] array = new String JavaDoc[count];
67
68         return (String JavaDoc[]) keys.toArray(array);
69     }
70
71     public void setAttribute(String JavaDoc name, Object JavaDoc value)
72     {
73         _attributes.put(name, value);
74     }
75
76     public void removeAttribute(String JavaDoc name)
77     {
78         _attributes.remove(name);
79     }
80
81     /**
82      * Serializes and then deserializes the {@link Map}
83      * containing all attributes.
84      *
85      **/

86
87     public void simulateFailover()
88     {
89         byte[] serialized = serializeAttributes();
90
91         _attributes = null;
92
93         Map JavaDoc restoredAttributes = deserializeAttributes(serialized);
94
95         _attributes = restoredAttributes;
96     }
97
98     private byte[] serializeAttributes()
99     {
100         try
101         {
102             ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
103             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(bos);
104
105             oos.writeObject(_attributes);
106
107             oos.close();
108
109             return bos.toByteArray();
110         }
111         catch (IOException JavaDoc ex)
112         {
113             throw new ApplicationRuntimeException("Unable to serialize attributes.", ex);
114         }
115     }
116
117     private Map JavaDoc deserializeAttributes(byte[] serialized)
118     {
119         try
120         {
121             ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(serialized);
122             ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bis);
123
124             Map JavaDoc result = (Map JavaDoc) ois.readObject();
125
126             return result;
127         }
128         catch (Exception JavaDoc ex)
129         {
130             throw new ApplicationRuntimeException("Unable to deserialize attributes.", ex);
131         }
132     }
133 }
134
Popular Tags