View Javadoc

1   package org.apache.turbine.util;
2   
3   /* ====================================================================
4    * The Apache Software License, Version 1.1
5    *
6    * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
7    * reserved.
8    *
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   * 1. Redistributions of source code must retain the above copyright
14   *    notice, this list of conditions and the following disclaimer.
15   *
16   * 2. Redistributions in binary form must reproduce the above copyright
17   *    notice, this list of conditions and the following disclaimer in
18   *    the documentation and/or other materials provided with the
19   *    distribution.
20   *
21   * 3. The end-user documentation included with the redistribution,
22   *    if any, must include the following acknowledgment:
23   *       "This product includes software developed by the
24   *        Apache Software Foundation (http://www.apache.org/)."
25   *    Alternately, this acknowledgment may appear in the software itself,
26   *    if and wherever such third-party acknowledgments normally appear.
27   *
28   * 4. The names "Apache" and "Apache Software Foundation" and
29   *    "Apache Turbine" must not be used to endorse or promote products
30   *    derived from this software without prior written permission. For
31   *    written permission, please contact apache@apache.org.
32   *
33   * 5. Products derived from this software may not be called "Apache",
34   *    "Apache Turbine", nor may "Apache" appear in their name, without
35   *    prior written permission of the Apache Software Foundation.
36   *
37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48   * SUCH DAMAGE.
49   * ====================================================================
50   *
51   * This software consists of voluntary contributions made by many
52   * individuals on behalf of the Apache Software Foundation.  For more
53   * information on the Apache Software Foundation, please see
54   * <http://www.apache.org/>.
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             // These objects are closed in the finally.
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             // These streams are closed in finally.
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                 // If objectData has not been initialized, an
193                 // exception will occur.
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 }