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