KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > config > SerializedConfigurationMarshaler


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.ObjectOutputStream JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.ObjectInputStream JavaDoc;
24 import java.io.ObjectStreamConstants JavaDoc;
25 import java.io.PushbackInputStream JavaDoc;
26 import java.util.Collection JavaDoc;
27
28 /**
29  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 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 JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
38         PushbackInputStream JavaDoc pushbackInputStream = new PushbackInputStream JavaDoc(in, 2);
39         byte[] streamHeader = new byte[2];
40         if (pushbackInputStream.read(streamHeader) != 2) throw new AssertionError JavaDoc("Cound not read stream header");
41         pushbackInputStream.unread(streamHeader);
42
43         // if this isn't a serialized config, fallback to the xstream marshaler
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 JavaDoc ignored) {
49                 throw new IOException JavaDoc("Input does not contain a Java Object Serialization stream");
50             }
51             return marshaler.readConfigurationData(pushbackInputStream);
52
53         }
54
55         ObjectInputStream JavaDoc oin = new ObjectInputStream JavaDoc(pushbackInputStream);
56         try {
57             return (ConfigurationData) oin.readObject();
58         } finally {
59             oin.close();
60         }
61     }
62
63     public void writeConfigurationData(ConfigurationData configurationData, OutputStream JavaDoc out) throws IOException JavaDoc {
64         ObjectOutputStream JavaDoc oout = new ObjectOutputStream JavaDoc(out);
65         try {
66             oout.writeObject(configurationData);
67         } finally {
68             if (oout != null) {
69                 try {
70                     oout.flush();
71                 } catch (IOException JavaDoc ignored) {
72                 }
73             }
74         }
75     }
76
77     public GBeanState newGBeanState(Collection JavaDoc gbeans) {
78         return new SerializedGBeanState(gbeans);
79     }
80 }
81
Popular Tags