01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.geronimo.kernel.config.xstream;
17:
18: import java.io.IOException;
19: import java.io.InputStream;
20: import java.io.InputStreamReader;
21: import java.io.OutputStream;
22: import java.io.Reader;
23: import java.io.PushbackInputStream;
24: import java.io.ObjectStreamConstants;
25: import java.util.Collection;
26:
27: import com.thoughtworks.xstream.XStream;
28: import org.apache.geronimo.kernel.config.ConfigurationData;
29: import org.apache.geronimo.kernel.config.ConfigurationMarshaler;
30: import org.apache.geronimo.kernel.config.GBeanState;
31: import org.apache.geronimo.kernel.config.SerializedConfigurationMarshaler;
32:
33: /**
34: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
35: */
36: public class XStreamConfigurationMarshaler implements
37: 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: public XStreamConfigurationMarshaler() {
43: // create an xstream just to assuer all the required libraries are present
44: XStreamUtil.createXStream();
45: }
46:
47: public ConfigurationData readConfigurationData(InputStream in)
48: throws IOException, ClassNotFoundException {
49: PushbackInputStream pushbackInputStream = new PushbackInputStream(
50: in, 2);
51: byte[] streamHeader = new byte[2];
52: if (pushbackInputStream.read(streamHeader) != 2)
53: throw new AssertionError("Cound not read stream header");
54: pushbackInputStream.unread(streamHeader);
55:
56: // if this is a serialized config, fallback to the serialization marshaler
57: if (SERIALIZED_MAGIC[0] == streamHeader[0]
58: && SERIALIZED_MAGIC[1] == streamHeader[1]) {
59: return new SerializedConfigurationMarshaler()
60: .readConfigurationData(pushbackInputStream);
61: }
62:
63: XStream xstream = XStreamUtil.createXStream();
64: Reader reader = new InputStreamReader(pushbackInputStream);
65: ConfigurationData configurationData = (ConfigurationData) xstream
66: .fromXML(reader);
67: return configurationData;
68: }
69:
70: public void writeConfigurationData(
71: ConfigurationData configurationData, OutputStream out)
72: throws IOException {
73: XStream xstream = XStreamUtil.createXStream();
74: String xml = xstream.toXML(configurationData);
75:
76: out
77: .write(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
78: + "\n"
79: + "<!-- ======================================================== -->\n"
80: + "<!-- Warning - Modification of this file may cause server -->\n"
81: + "<!-- instability. Also, the format of this XML file is -->\n"
82: + "<!-- undocumented and subject to change without notice. -->\n"
83: + "<!-- ======================================================== -->\n"
84: + "\n").getBytes());
85:
86: out.write(xml.getBytes());
87: out.flush();
88: }
89:
90: public GBeanState newGBeanState(Collection gbeans) {
91: return new XStreamGBeanState(gbeans);
92: }
93: }
|