View Javadoc

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.xstream;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.InputStreamReader;
22  import java.io.OutputStream;
23  import java.io.Reader;
24  import java.io.PushbackInputStream;
25  import java.io.ObjectStreamConstants;
26  import java.util.Collection;
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: 410741 $ $Date: 2006-05-31 21:35:48 -0700 (Wed, 31 May 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 in) throws IOException, ClassNotFoundException {
49          PushbackInputStream pushbackInputStream = new PushbackInputStream(in, 2);
50          byte[] streamHeader = new byte[2];
51          if (pushbackInputStream.read(streamHeader) != 2) throw new AssertionError("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 reader = new InputStreamReader(pushbackInputStream);
61          ConfigurationData configurationData = (ConfigurationData)xstream.fromXML(reader);
62          return configurationData;
63      }
64  
65      public void writeConfigurationData(ConfigurationData configurationData, OutputStream out) throws IOException {
66          XStream xstream = XStreamUtil.createXStream();
67          String 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 gbeans) {
83          return new XStreamGBeanState(gbeans);
84      }
85  }