1 /**
2 *
3 * Copyright 2005 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * 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 */
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 /**
29 * @version $Rev: 410741 $ $Date: 2006-05-31 21:35:48 -0700 (Wed, 31 May 2006) $
30 */
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
44 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 }