|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
AttributeHolder.java | - | 19.2% | 55.6% | 28.6% |
|
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;
|
|
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 |
* 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 _attributes = new HashMap(); |
|
45 |
|
|
46 | 132 |
public Object getAttribute(String name)
|
47 |
{ |
|
48 | 132 |
return _attributes.get(name);
|
49 |
} |
|
50 |
|
|
51 | 106 |
public Enumeration getAttributeNames()
|
52 |
{ |
|
53 | 106 |
return getEnumeration(_attributes);
|
54 |
} |
|
55 |
|
|
56 | 128 |
protected Enumeration getEnumeration(Map map)
|
57 |
{ |
|
58 | 128 |
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 | 224 |
public void setAttribute(String name, Object value) |
72 |
{ |
|
73 | 224 |
_attributes.put(name, value); |
74 |
} |
|
75 |
|
|
76 | 48 |
public void removeAttribute(String name) |
77 |
{ |
|
78 | 48 |
_attributes.remove(name); |
79 |
} |
|
80 |
|
|
81 |
/**
|
|
82 |
* Serializes and then deserializes the {@link Map}
|
|
83 |
* containing all attributes.
|
|
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 |
} |
|
134 |
|
|