Source Code Cross Referenced for ObjectUtils.java in  » Web-Framework » TURBINE » org » apache » turbine » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » TURBINE » org.apache.turbine.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.