1 17 package org.apache.geronimo.kernel.config.xstream; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.InputStreamReader ; 22 import java.io.OutputStream ; 23 import java.io.Reader ; 24 import java.io.PushbackInputStream ; 25 import java.io.ObjectStreamConstants ; 26 import java.util.Collection ; 27 28 import com.thoughtworks.xstream.XStream; 29 import org.apache.geronimo.kernel.config.ConfigurationData; 30 import org.apache.geronimo.kernel.config.ConfigurationMarshaler; 31 import org.apache.geronimo.kernel.config.GBeanState; 32 import org.apache.geronimo.kernel.config.SerializedConfigurationMarshaler; 33 34 37 public class XStreamConfigurationMarshaler implements ConfigurationMarshaler { 38 private static byte[] SERIALIZED_MAGIC = new byte[] { 39 (byte) ((ObjectStreamConstants.STREAM_MAGIC >>> 8) & 0xFF), 40 (byte) ((ObjectStreamConstants.STREAM_MAGIC >>> 0) & 0xFF) 41 }; 42 43 public XStreamConfigurationMarshaler() { 44 XStreamUtil.createXStream(); 46 } 47 48 public ConfigurationData readConfigurationData(InputStream in) throws IOException , ClassNotFoundException { 49 PushbackInputStream pushbackInputStream = new PushbackInputStream (in, 2); 50 byte[] streamHeader = new byte[2]; 51 if (pushbackInputStream.read(streamHeader) != 2) throw new AssertionError ("Cound not read stream header"); 52 pushbackInputStream.unread(streamHeader); 53 54 if (SERIALIZED_MAGIC[0] == streamHeader[0] && SERIALIZED_MAGIC[1] == streamHeader[1]) { 56 return new SerializedConfigurationMarshaler().readConfigurationData(pushbackInputStream); 57 } 58 59 XStream xstream = XStreamUtil.createXStream(); 60 Reader reader = new InputStreamReader (pushbackInputStream); 61 ConfigurationData configurationData = (ConfigurationData)xstream.fromXML(reader); 62 return configurationData; 63 } 64 65 public void writeConfigurationData(ConfigurationData configurationData, OutputStream out) throws IOException { 66 XStream xstream = XStreamUtil.createXStream(); 67 String xml = xstream.toXML(configurationData); 68 69 out.write(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 70 "\n" + 71 "<!-- ======================================================== -->\n" + 72 "<!-- Warning - Modification of this file may cause server -->\n" + 73 "<!-- instability. Also, the format of this XML file is -->\n" + 74 "<!-- undocumented and subject to change without notice. -->\n" + 75 "<!-- ======================================================== -->\n" + 76 "\n").getBytes()); 77 78 out.write(xml.getBytes()); 79 out.flush(); 80 } 81 82 public GBeanState newGBeanState(Collection gbeans) { 83 return new XStreamGBeanState(gbeans); 84 } 85 } 86 | Popular Tags |