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;
17:
18: import java.io.OutputStream;
19: import java.io.IOException;
20: import java.io.ObjectOutputStream;
21: import java.io.InputStream;
22: import java.io.ObjectInputStream;
23: import java.io.ObjectStreamConstants;
24: import java.io.PushbackInputStream;
25: import java.util.Collection;
26:
27: /**
28: * @version $Rev: 549825 $ $Date: 2007-06-22 07:17:31 -0700 (Fri, 22 Jun 2007) $
29: */
30: public class SerializedConfigurationMarshaler implements
31: 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: public ConfigurationData readConfigurationData(InputStream in)
37: throws IOException, ClassNotFoundException {
38: PushbackInputStream pushbackInputStream = new PushbackInputStream(
39: in, 2);
40: byte[] streamHeader = new byte[2];
41: if (pushbackInputStream.read(streamHeader) != 2)
42: throw new AssertionError("Cound not read stream header");
43: pushbackInputStream.unread(streamHeader);
44:
45: // if this isn't a serialized config, fallback to the xstream marshaler
46: if (SERIALIZED_MAGIC[0] != streamHeader[0]
47: || SERIALIZED_MAGIC[1] != streamHeader[1]) {
48: ConfigurationMarshaler marshaler;
49: try {
50: marshaler = ConfigurationUtil
51: .createConfigurationMarshaler("org.apache.geronimo.kernel.config.xstream.XStreamConfigurationMarshaler");
52: } catch (Throwable ignored) {
53: throw (IOException) new IOException(
54: "Input does not contain a Java Object Serialization stream")
55: .initCause(ignored);
56: }
57: return marshaler.readConfigurationData(pushbackInputStream);
58:
59: }
60:
61: ObjectInputStream oin = new ObjectInputStream(
62: pushbackInputStream);
63: try {
64: return (ConfigurationData) oin.readObject();
65: } finally {
66: oin.close();
67: }
68: }
69:
70: public void writeConfigurationData(
71: ConfigurationData configurationData, OutputStream out)
72: throws IOException {
73: ObjectOutputStream oout = new ObjectOutputStream(out);
74: try {
75: oout.writeObject(configurationData);
76: } finally {
77: if (oout != null) {
78: try {
79: oout.flush();
80: } catch (IOException ignored) {
81: }
82: }
83: }
84: }
85:
86: public GBeanState newGBeanState(Collection gbeans) {
87: return new SerializedGBeanState(gbeans);
88: }
89: }
|