1 package org.apache.turbine.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 import java.io.BufferedInputStream;
58 import java.io.BufferedOutputStream;
59 import java.io.ByteArrayInputStream;
60 import java.io.ByteArrayOutputStream;
61 import java.io.IOException;
62 import java.io.ObjectInputStream;
63 import java.io.ObjectOutputStream;
64 import java.io.Serializable;
65 import java.util.Enumeration;
66 import java.util.Hashtable;
67 import java.util.List;
68
69 /***
70 * This is where common Object manipulation routines should go.
71 *
72 * @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
73 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
74 * @version $Id: ObjectUtils.java,v 1.7 2004/03/28 17:36:20 epugh Exp $
75 */
76 public abstract class ObjectUtils
77 {
78 /***
79 * Returns a default value if the object passed is null.
80 *
81 * @param o The object to test.
82 * @param dflt The default value to return.
83 * @return The object o if it is not null, dflt otherwise.
84 * @deprecated Use org.apache.commons.lang.ObjectUtils.defaultIfNull()
85 */
86 public static Object isNull(Object o, Object dflt)
87 {
88 return org.apache.commons.lang.ObjectUtils.defaultIfNull(o,dflt);
89 }
90
91 /***
92 * Adds an object to a vector, making sure the object is in the
93 * vector only once.
94 *
95 * @param v The vector.
96 * @param o The object.
97 * @deprecated No replacement
98 */
99 public static void addOnce(List l, Object o)
100 {
101 if (!l.contains(o))
102 {
103 l.add(o);
104 }
105 }
106
107 /***
108 * Converts a hashtable to a byte array for storage/serialization.
109 *
110 * @param hash The Hashtable to convert.
111 *
112 * @return A byte[] with the converted Hashtable.
113 *
114 * @exception Exception A generic exception.
115 */
116 public static byte[] serializeHashtable(Hashtable hash)
117 throws Exception
118 {
119 Hashtable saveData = new Hashtable(hash.size());
120 String key = null;
121 Object value = null;
122 byte[] byteArray = null;
123
124 Enumeration keys = hash.keys();
125
126 while (keys.hasMoreElements())
127 {
128 key = (String) keys.nextElement();
129 value = hash.get(key);
130 if (value instanceof Serializable)
131 {
132 saveData.put (key, value);
133 }
134 }
135
136 ByteArrayOutputStream baos = null;
137 BufferedOutputStream bos = null;
138 ObjectOutputStream out = null;
139 try
140 {
141
142 baos = new ByteArrayOutputStream();
143 bos = new BufferedOutputStream(baos);
144 out = new ObjectOutputStream(bos);
145
146 out.writeObject(saveData);
147 out.flush();
148 bos.flush();
149
150 byteArray = baos.toByteArray();
151 }
152 finally
153 {
154 if (out != null)
155 {
156 out.close();
157 }
158 if (bos != null)
159 {
160 bos.close();
161 }
162 if (baos != null)
163 {
164 baos.close();
165 }
166 }
167 return byteArray;
168 }
169
170 /***
171 * Deserializes a single object from an array of bytes.
172 *
173 * @param objectData The serialized object.
174 *
175 * @return The deserialized object, or <code>null</code> on failure.
176 */
177 public static Object deserialize(byte[] objectData)
178 {
179 Object object = null;
180
181 if (objectData != null)
182 {
183
184 ObjectInputStream in = null;
185 ByteArrayInputStream bin = new ByteArrayInputStream(objectData);
186 BufferedInputStream bufin = new BufferedInputStream(bin);
187
188 try
189 {
190 in = new ObjectInputStream(bufin);
191
192
193
194 object = in.readObject();
195 }
196 catch (Exception e)
197 {
198 }
199 finally
200 {
201 try
202 {
203 if (in != null)
204 {
205 in.close();
206 }
207 if (bufin != null)
208 {
209 bufin.close();
210 }
211 if (bin != null)
212 {
213 bin.close();
214 }
215 }
216 catch (IOException e)
217 {
218 }
219 }
220 }
221 return object;
222 }
223
224 /***
225 * Compares two Objects, returns true if their values are the
226 * same. It checks for null values prior to an o1.equals(o2)
227 * check
228 *
229 * @param o1 The first object.
230 * @param o2 The second object.
231 * @return True if the values of both xstrings are the same.
232 * @deprecated Use org.apache.commons.lang.ObjectUtils.equals()
233 */
234 public static boolean equals(Object o1, Object o2)
235 {
236 return org.apache.commons.lang.ObjectUtils.equals(o1,o2);
237 }
238
239 /***
240 * Nice method for adding data to a Hashtable in such a way
241 * as to not get NPE's. The point being that if the
242 * value is null, Hashtable.put() will throw an exception.
243 * That blows in the case of this class cause you may want to
244 * essentially treat put("Not Null", null ) == put("Not Null", "")
245 * We will still throw a NPE if the key is null cause that should
246 * never happen.
247 * @deprecated No replacement
248 */
249 public static final void safeAddToHashtable(Hashtable hash, Object key,
250 Object value)
251 throws NullPointerException
252 {
253 if (value == null)
254 {
255 hash.put(key, "");
256 }
257 else
258 {
259 hash.put(key, value);
260 }
261 }
262 }