KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > config > xstream > XStreamConfigurationMarshaler


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.xstream;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.InputStreamReader JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.Reader JavaDoc;
24 import java.io.PushbackInputStream JavaDoc;
25 import java.io.ObjectStreamConstants JavaDoc;
26 import java.util.Collection JavaDoc;
27
28 import com.thoughtworks.xstream.XStream;
29 import org.apache.geronimo.kernel.config.ConfigurationData;
30 import org.apache.geronimo.kernel.config.ConfigurationMarshaler;
31 import org.apache.geronimo.kernel.config.GBeanState;
32 import org.apache.geronimo.kernel.config.SerializedConfigurationMarshaler;
33
34 /**
35  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
36  */

37 public class XStreamConfigurationMarshaler implements ConfigurationMarshaler {
38     private static byte[] SERIALIZED_MAGIC = new byte[] {
39             (byte) ((ObjectStreamConstants.STREAM_MAGIC >>> 8) & 0xFF),
40             (byte) ((ObjectStreamConstants.STREAM_MAGIC >>> 0) & 0xFF)
41     };
42
43     public XStreamConfigurationMarshaler() {
44         // create an xstream just to assuer all the required libraries are present
45
XStreamUtil.createXStream();
46     }
47
48     public ConfigurationData readConfigurationData(InputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
49         PushbackInputStream JavaDoc pushbackInputStream = new PushbackInputStream JavaDoc(in, 2);
50         byte[] streamHeader = new byte[2];
51         if (pushbackInputStream.read(streamHeader) != 2) throw new AssertionError JavaDoc("Cound not read stream header");
52         pushbackInputStream.unread(streamHeader);
53
54         // if this is a serialized config, fallback to the serialization marshaler
55
if (SERIALIZED_MAGIC[0] == streamHeader[0] && SERIALIZED_MAGIC[1] == streamHeader[1]) {
56             return new SerializedConfigurationMarshaler().readConfigurationData(pushbackInputStream);
57         }
58
59         XStream xstream = XStreamUtil.createXStream();
60         Reader JavaDoc reader = new InputStreamReader JavaDoc(pushbackInputStream);
61         ConfigurationData configurationData = (ConfigurationData)xstream.fromXML(reader);
62         return configurationData;
63     }
64
65     public void writeConfigurationData(ConfigurationData configurationData, OutputStream JavaDoc out) throws IOException JavaDoc {
66         XStream xstream = XStreamUtil.createXStream();
67         String JavaDoc xml = xstream.toXML(configurationData);
68
69         out.write(("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
70                 "\n" +
71                 "<!-- ======================================================== -->\n" +
72                 "<!-- Warning - Modification of this file may cause server -->\n" +
73                 "<!-- instability. Also, the format of this XML file is -->\n" +
74                 "<!-- undocumented and subject to change without notice. -->\n" +
75                 "<!-- ======================================================== -->\n" +
76                 "\n").getBytes());
77
78         out.write(xml.getBytes());
79         out.flush();
80     }
81
82     public GBeanState newGBeanState(Collection JavaDoc gbeans) {
83         return new XStreamGBeanState(gbeans);
84     }
85 }
86
Popular Tags