001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  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;
018    
019    import java.io.OutputStream;
020    import java.io.IOException;
021    import java.io.ObjectOutputStream;
022    import java.io.InputStream;
023    import java.io.ObjectInputStream;
024    import java.io.ObjectStreamConstants;
025    import java.io.PushbackInputStream;
026    import java.util.Collection;
027    
028    /**
029     * @version $Rev: 549825 $ $Date: 2007-06-22 10:17:31 -0400 (Fri, 22 Jun 2007) $
030     */
031    public class SerializedConfigurationMarshaler implements ConfigurationMarshaler {
032        private static byte[] SERIALIZED_MAGIC = new byte[] {
033                (byte) ((ObjectStreamConstants.STREAM_MAGIC >>> 8) & 0xFF),
034                (byte) ((ObjectStreamConstants.STREAM_MAGIC >>> 0) & 0xFF)
035        };
036    
037        public ConfigurationData readConfigurationData(InputStream in) throws IOException, ClassNotFoundException {
038            PushbackInputStream pushbackInputStream = new PushbackInputStream(in, 2);
039            byte[] streamHeader = new byte[2];
040            if (pushbackInputStream.read(streamHeader) != 2) throw new AssertionError("Cound not read stream header");
041            pushbackInputStream.unread(streamHeader);
042    
043            // if this isn't a serialized config, fallback to the xstream marshaler
044            if (SERIALIZED_MAGIC[0] != streamHeader[0] || SERIALIZED_MAGIC[1] != streamHeader[1]) {
045                ConfigurationMarshaler marshaler;
046                try {
047                    marshaler = ConfigurationUtil.createConfigurationMarshaler("org.apache.geronimo.kernel.config.xstream.XStreamConfigurationMarshaler");
048                } catch (Throwable ignored) {
049                    throw (IOException)new IOException("Input does not contain a Java Object Serialization stream").initCause(ignored);
050                }
051                return marshaler.readConfigurationData(pushbackInputStream);
052    
053            }
054    
055            ObjectInputStream oin = new ObjectInputStream(pushbackInputStream);
056            try {
057                return (ConfigurationData) oin.readObject();
058            } finally {
059                oin.close();
060            }
061        }
062    
063        public void writeConfigurationData(ConfigurationData configurationData, OutputStream out) throws IOException {
064            ObjectOutputStream oout = new ObjectOutputStream(out);
065            try {
066                oout.writeObject(configurationData);
067            } finally {
068                if (oout != null) {
069                    try {
070                        oout.flush();
071                    } catch (IOException ignored) {
072                    }
073                }
074            }
075        }
076    
077        public GBeanState newGBeanState(Collection gbeans) {
078            return new SerializedGBeanState(gbeans);
079        }
080    }