001    /**
002     *
003     * Copyright 2005 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.kernel.config.xstream;
018    
019    import java.io.IOException;
020    import java.io.InputStream;
021    import java.io.InputStreamReader;
022    import java.io.OutputStream;
023    import java.io.Reader;
024    import java.io.PushbackInputStream;
025    import java.io.ObjectStreamConstants;
026    import java.util.Collection;
027    
028    import com.thoughtworks.xstream.XStream;
029    import org.apache.geronimo.kernel.config.ConfigurationData;
030    import org.apache.geronimo.kernel.config.ConfigurationMarshaler;
031    import org.apache.geronimo.kernel.config.GBeanState;
032    import org.apache.geronimo.kernel.config.SerializedConfigurationMarshaler;
033    
034    /**
035     * @version $Rev: 410741 $ $Date: 2006-05-31 21:35:48 -0700 (Wed, 31 May 2006) $
036     */
037    public class XStreamConfigurationMarshaler implements ConfigurationMarshaler {
038        private static byte[] SERIALIZED_MAGIC = new byte[] {
039                (byte) ((ObjectStreamConstants.STREAM_MAGIC >>> 8) & 0xFF),
040                (byte) ((ObjectStreamConstants.STREAM_MAGIC >>> 0) & 0xFF)
041        };
042    
043        public XStreamConfigurationMarshaler() {
044            // create an xstream just to assuer all the required libraries are present
045            XStreamUtil.createXStream();
046        }
047    
048        public ConfigurationData readConfigurationData(InputStream in) throws IOException, ClassNotFoundException {
049            PushbackInputStream pushbackInputStream = new PushbackInputStream(in, 2);
050            byte[] streamHeader = new byte[2];
051            if (pushbackInputStream.read(streamHeader) != 2) throw new AssertionError("Cound not read stream header");
052            pushbackInputStream.unread(streamHeader);
053    
054            // if this is a serialized config, fallback to the serialization marshaler
055            if (SERIALIZED_MAGIC[0] == streamHeader[0] && SERIALIZED_MAGIC[1] == streamHeader[1]) {
056                return new SerializedConfigurationMarshaler().readConfigurationData(pushbackInputStream);
057            }
058    
059            XStream xstream = XStreamUtil.createXStream();
060            Reader reader = new InputStreamReader(pushbackInputStream);
061            ConfigurationData configurationData = (ConfigurationData)xstream.fromXML(reader);
062            return configurationData;
063        }
064    
065        public void writeConfigurationData(ConfigurationData configurationData, OutputStream out) throws IOException {
066            XStream xstream = XStreamUtil.createXStream();
067            String xml = xstream.toXML(configurationData);
068    
069            out.write(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
070                    "\n" +
071                    "<!-- ======================================================== -->\n" +
072                    "<!-- Warning - Modification of this file may cause server     -->\n" +
073                    "<!-- instability.  Also, the format of this XML file is       -->\n" +
074                    "<!-- undocumented and subject to change without notice.       -->\n" +
075                    "<!-- ======================================================== -->\n" +
076                    "\n").getBytes());
077    
078            out.write(xml.getBytes());
079            out.flush();
080        }
081    
082        public GBeanState newGBeanState(Collection gbeans) {
083            return new XStreamGBeanState(gbeans);
084        }
085    }