KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > common > JNDIUtils


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Guillaume Sauthier
22  * Contributor(s):
23  *
24  * --------------------------------------------------------------------------
25  * $Id: JNDIUtils.java,v 1.2 2004/09/17 22:33:33 ehardesty Exp $
26  * --------------------------------------------------------------------------
27  */

28 package org.objectweb.jonas.common;
29
30 import java.io.ByteArrayInputStream JavaDoc;
31 import java.io.ByteArrayOutputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.ObjectInputStream JavaDoc;
34 import java.io.ObjectOutputStream JavaDoc;
35 import java.io.OptionalDataException JavaDoc;
36 import org.objectweb.util.monolog.api.BasicLevel;
37 import org.objectweb.util.monolog.api.Logger;
38
39 /**
40  * JNDIUtils groups all commonly used methods for JNDI.
41  * @author Guillaume Sauthier
42  */

43 public class JNDIUtils {
44
45     /**
46      * Private empty Constructor for Utility classes
47      */

48     private JNDIUtils() { }
49
50     /**
51      * Return an array of byte from a given object
52      * @param obj the object from which we must extract the bytes.
53      * @return the byte[] from an object
54      */

55     public static byte[] getBytesFromObject(Object JavaDoc obj) {
56         return getBytesFromObject(obj, null);
57     }
58     
59     /**
60      * Return an array of byte from a given object
61      * @param obj the object from which we must extract the bytes.
62      * @param logger logger to log exceptions
63      * @return the byte[] from an object
64      */

65     public static byte[] getBytesFromObject(Object JavaDoc obj, Logger logger) {
66
67         if (obj == null) {
68             return null;
69         }
70
71         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
72
73         ObjectOutputStream JavaDoc oos = null;
74         byte[] bytes = null;
75
76         try {
77             oos = new ObjectOutputStream JavaDoc(baos);
78             oos.writeObject(obj);
79             bytes = baos.toByteArray();
80         } catch (Exception JavaDoc e) {
81             //Can't tranform
82
return null;
83         } finally {
84             try {
85                 oos.close();
86                 baos.close();
87             } catch (Exception JavaDoc e) {
88                 if (logger != null) {
89                     logger.log(BasicLevel.DEBUG, "Cannot close output streams : '" + e.getMessage() + "'");
90                 }
91             }
92         }
93         return bytes;
94     }
95
96     /**
97      * Return an object from an array of bytes. Useful for BinaryRefAddr
98      * Useful for BinaryRefAddr
99      * @param bytes an array of bytes
100      * @return an object or null if there is an error of if it's empty
101      */

102     public static Object JavaDoc getObjectFromBytes(byte[] bytes) {
103         return getObjectFromBytes(bytes, null);
104     }
105     
106     /**
107      * Return an object from an array of bytes. Useful for BinaryRefAddr
108      * Useful for BinaryRefAddr
109      * @param bytes an array of bytes
110      * @param logger logger to log exceptions
111      * @return an object or null if there is an error of if it's empty
112      */

113     public static Object JavaDoc getObjectFromBytes(byte[] bytes, Logger logger) {
114         //Declaration
115
ByteArrayInputStream JavaDoc bis = null;
116         ObjectInputStream JavaDoc ois = null;
117         Object JavaDoc obj = null;
118
119         if (bytes == null) {
120             return null;
121         }
122
123         bis = new ByteArrayInputStream JavaDoc(bytes);
124         try {
125             ois = new ObjectInputStream JavaDoc(bis);
126             obj = ois.readObject();
127
128         } catch (ClassNotFoundException JavaDoc cfe) {
129             if (logger != null) {
130                 logger.log(BasicLevel.DEBUG, "Cannot get object from bytes : " + cfe.getMessage());
131             }
132         } catch (OptionalDataException JavaDoc ode) {
133             if (logger != null) {
134                 logger.log(BasicLevel.DEBUG, "Cannot get object from bytes : " + ode.getMessage());
135             }
136         } catch (IOException JavaDoc ioe) {
137             if (logger != null) {
138                 logger.log(BasicLevel.DEBUG, "Cannot get object from bytes : " + ioe.getMessage());
139             }
140         } finally {
141             try {
142                 bis.close();
143                 ois.close();
144             } catch (Exception JavaDoc e) {
145                 if (logger != null) {
146                     logger.log(BasicLevel.DEBUG, "Cannot close input stream : " + e.getMessage());
147                 }
148             }
149         }
150         return obj;
151     }
152
153 }
154
Popular Tags