001: package org.apache.turbine.util;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.BufferedInputStream;
020: import java.io.BufferedOutputStream;
021: import java.io.ByteArrayInputStream;
022: import java.io.ByteArrayOutputStream;
023: import java.io.IOException;
024: import java.io.ObjectInputStream;
025: import java.io.ObjectOutputStream;
026: import java.io.Serializable;
027: import java.util.Enumeration;
028: import java.util.Hashtable;
029: import java.util.List;
030:
031: /**
032: * This is where common Object manipulation routines should go.
033: *
034: * @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
035: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
036: * @version $Id: ObjectUtils.java 264148 2005-08-29 14:21:04Z henning $
037: */
038: public abstract class ObjectUtils {
039: /**
040: * Returns a default value if the object passed is null.
041: *
042: * @param o The object to test.
043: * @param dflt The default value to return.
044: * @return The object o if it is not null, dflt otherwise.
045: * @deprecated Use org.apache.commons.lang.ObjectUtils.defaultIfNull()
046: */
047: public static Object isNull(Object o, Object dflt) {
048: return org.apache.commons.lang.ObjectUtils.defaultIfNull(o,
049: dflt);
050: }
051:
052: /**
053: * Adds an object to a vector, making sure the object is in the
054: * vector only once.
055: *
056: * @param v The vector.
057: * @param o The object.
058: * @deprecated No replacement
059: */
060: public static void addOnce(List l, Object o) {
061: if (!l.contains(o)) {
062: l.add(o);
063: }
064: }
065:
066: /**
067: * Converts a hashtable to a byte array for storage/serialization.
068: *
069: * @param hash The Hashtable to convert.
070: *
071: * @return A byte[] with the converted Hashtable.
072: *
073: * @exception Exception A generic exception.
074: */
075: public static byte[] serializeHashtable(Hashtable hash)
076: throws Exception {
077: Hashtable saveData = new Hashtable(hash.size());
078: String key = null;
079: Object value = null;
080: byte[] byteArray = null;
081:
082: Enumeration keys = hash.keys();
083:
084: while (keys.hasMoreElements()) {
085: key = (String) keys.nextElement();
086: value = hash.get(key);
087: if (value instanceof Serializable) {
088: saveData.put(key, value);
089: }
090: }
091:
092: ByteArrayOutputStream baos = null;
093: BufferedOutputStream bos = null;
094: ObjectOutputStream out = null;
095: try {
096: // These objects are closed in the finally.
097: baos = new ByteArrayOutputStream();
098: bos = new BufferedOutputStream(baos);
099: out = new ObjectOutputStream(bos);
100:
101: out.writeObject(saveData);
102: out.flush();
103: bos.flush();
104:
105: byteArray = baos.toByteArray();
106: } finally {
107: if (out != null) {
108: out.close();
109: }
110: if (bos != null) {
111: bos.close();
112: }
113: if (baos != null) {
114: baos.close();
115: }
116: }
117: return byteArray;
118: }
119:
120: /**
121: * Deserializes a single object from an array of bytes.
122: *
123: * @param objectData The serialized object.
124: *
125: * @return The deserialized object, or <code>null</code> on failure.
126: */
127: public static Object deserialize(byte[] objectData) {
128: Object object = null;
129:
130: if (objectData != null) {
131: // These streams are closed in finally.
132: ObjectInputStream in = null;
133: ByteArrayInputStream bin = new ByteArrayInputStream(
134: objectData);
135: BufferedInputStream bufin = new BufferedInputStream(bin);
136:
137: try {
138: in = new ObjectInputStream(bufin);
139:
140: // If objectData has not been initialized, an
141: // exception will occur.
142: object = in.readObject();
143: } catch (Exception e) {
144: } finally {
145: try {
146: if (in != null) {
147: in.close();
148: }
149:
150: bufin.close();
151: bin.close();
152: } catch (IOException e) {
153: }
154: }
155: }
156: return object;
157: }
158:
159: /**
160: * Compares two Objects, returns true if their values are the
161: * same. It checks for null values prior to an o1.equals(o2)
162: * check
163: *
164: * @param o1 The first object.
165: * @param o2 The second object.
166: * @return True if the values of both xstrings are the same.
167: * @deprecated Use org.apache.commons.lang.ObjectUtils.equals()
168: */
169: public static boolean equals(Object o1, Object o2) {
170: return org.apache.commons.lang.ObjectUtils.equals(o1, o2);
171: }
172:
173: /**
174: * Nice method for adding data to a Hashtable in such a way
175: * as to not get NPE's. The point being that if the
176: * value is null, Hashtable.put() will throw an exception.
177: * That blows in the case of this class cause you may want to
178: * essentially treat put("Not Null", null ) == put("Not Null", "")
179: * We will still throw a NPE if the key is null cause that should
180: * never happen.
181: * @deprecated No replacement
182: */
183: public static final void safeAddToHashtable(Hashtable hash,
184: Object key, Object value) throws NullPointerException {
185: if (value == null) {
186: hash.put(key, "");
187: } else {
188: hash.put(key, value);
189: }
190: }
191: }
|