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