JNDIUtils.java

00001 
00028 package org.objectweb.jonas.common;
00029 
00030 import java.io.ByteArrayInputStream;
00031 import java.io.ByteArrayOutputStream;
00032 import java.io.IOException;
00033 import java.io.ObjectInputStream;
00034 import java.io.ObjectOutputStream;
00035 import java.io.OptionalDataException;
00036 import org.objectweb.util.monolog.api.BasicLevel;
00037 import org.objectweb.util.monolog.api.Logger;
00038 
00043 public class JNDIUtils {
00044 
00048     private JNDIUtils() { }
00049 
00055     public static byte[] getBytesFromObject(Object obj) {
00056         return getBytesFromObject(obj, null);
00057     }
00058     
00065     public static byte[] getBytesFromObject(Object obj, Logger logger) {
00066 
00067         if (obj == null) {
00068             return null;
00069         }
00070 
00071         ByteArrayOutputStream baos = new ByteArrayOutputStream();
00072 
00073         ObjectOutputStream oos = null;
00074         byte[] bytes = null;
00075 
00076         try {
00077             oos = new ObjectOutputStream(baos);
00078             oos.writeObject(obj);
00079             bytes = baos.toByteArray();
00080         } catch (Exception e) {
00081             //Can't tranform
00082             return null;
00083         } finally {
00084             try {
00085                 oos.close();
00086                 baos.close();
00087             } catch (Exception e) {
00088                 if (logger != null) {
00089                     logger.log(BasicLevel.DEBUG, "Cannot close output streams : '" + e.getMessage() + "'");
00090                 }
00091             }
00092         }
00093         return bytes;
00094     }
00095 
00102     public static Object getObjectFromBytes(byte[] bytes) {
00103         return getObjectFromBytes(bytes, null);    
00104     }
00105     
00113     public static Object getObjectFromBytes(byte[] bytes, Logger logger) {
00114         //Declaration
00115         ByteArrayInputStream bis = null;
00116         ObjectInputStream ois = null;
00117         Object obj = null;
00118 
00119         if (bytes == null) {
00120             return null;
00121         }
00122 
00123         bis = new ByteArrayInputStream(bytes);
00124         try {
00125             ois = new ObjectInputStream(bis);
00126             obj = ois.readObject();
00127 
00128         } catch (ClassNotFoundException cfe) {
00129             if (logger != null) {
00130                 logger.log(BasicLevel.DEBUG, "Cannot get object from bytes : " + cfe.getMessage());
00131             }
00132         } catch (OptionalDataException ode) {
00133             if (logger != null) {
00134                 logger.log(BasicLevel.DEBUG, "Cannot get object from bytes : " + ode.getMessage());
00135             }
00136         } catch (IOException ioe) {
00137             if (logger != null) {
00138                 logger.log(BasicLevel.DEBUG, "Cannot get object from bytes : " + ioe.getMessage());
00139             }
00140         } finally {
00141             try {
00142                 bis.close();
00143                 ois.close();
00144             } catch (Exception e) {
00145                 if (logger != null) {
00146                     logger.log(BasicLevel.DEBUG, "Cannot close input stream : " + e.getMessage());
00147                 }
00148             }
00149         }
00150         return obj;
00151     }
00152 
00153 }

Generated on Tue Feb 15 15:05:14 2005 for JOnAS by  doxygen 1.3.9.1