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