1 17 package org.apache.geronimo.kernel.config; 18 19 import java.io.OutputStream ; 20 import java.io.IOException ; 21 import java.io.ObjectOutputStream ; 22 import java.io.InputStream ; 23 import java.io.ObjectInputStream ; 24 import java.io.ObjectStreamConstants ; 25 import java.io.PushbackInputStream ; 26 import java.util.Collection ; 27 28 31 public class SerializedConfigurationMarshaler implements ConfigurationMarshaler { 32 private static byte[] SERIALIZED_MAGIC = new byte[] { 33 (byte) ((ObjectStreamConstants.STREAM_MAGIC >>> 8) & 0xFF), 34 (byte) ((ObjectStreamConstants.STREAM_MAGIC >>> 0) & 0xFF) 35 }; 36 37 public ConfigurationData readConfigurationData(InputStream in) throws IOException , ClassNotFoundException { 38 PushbackInputStream pushbackInputStream = new PushbackInputStream (in, 2); 39 byte[] streamHeader = new byte[2]; 40 if (pushbackInputStream.read(streamHeader) != 2) throw new AssertionError ("Cound not read stream header"); 41 pushbackInputStream.unread(streamHeader); 42 43 if (SERIALIZED_MAGIC[0] != streamHeader[0] || SERIALIZED_MAGIC[1] != streamHeader[1]) { 45 ConfigurationMarshaler marshaler; 46 try { 47 marshaler = ConfigurationUtil.createConfigurationMarshaler("org.apache.geronimo.kernel.config.xstream.XStreamConfigurationMarshaler"); 48 } catch (Throwable ignored) { 49 throw new IOException ("Input does not contain a Java Object Serialization stream"); 50 } 51 return marshaler.readConfigurationData(pushbackInputStream); 52 53 } 54 55 ObjectInputStream oin = new ObjectInputStream (pushbackInputStream); 56 try { 57 return (ConfigurationData) oin.readObject(); 58 } finally { 59 oin.close(); 60 } 61 } 62 63 public void writeConfigurationData(ConfigurationData configurationData, OutputStream out) throws IOException { 64 ObjectOutputStream oout = new ObjectOutputStream (out); 65 try { 66 oout.writeObject(configurationData); 67 } finally { 68 if (oout != null) { 69 try { 70 oout.flush(); 71 } catch (IOException ignored) { 72 } 73 } 74 } 75 } 76 77 public GBeanState newGBeanState(Collection gbeans) { 78 return new SerializedGBeanState(gbeans); 79 } 80 } 81 | Popular Tags |