KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > hessian > io > HessianSerializerInput


1 /*
2  * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
3  *
4  * The Apache Software License, Version 1.1
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Caucho Technology (http://www.caucho.com/)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The names "Burlap", "Resin", and "Caucho" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * info@caucho.com.
29  *
30  * 5. Products derived from this software may not be called "Resin"
31  * nor may "Resin" appear in their names without prior written
32  * permission of Caucho Technology.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * @author Scott Ferguson
47  */

48
49 package com.caucho.hessian.io;
50
51 import java.io.IOException JavaDoc;
52 import java.io.InputStream JavaDoc;
53 import java.lang.reflect.Field JavaDoc;
54 import java.lang.reflect.Method JavaDoc;
55 import java.lang.reflect.Modifier JavaDoc;
56 import java.util.ArrayList JavaDoc;
57 import java.util.HashMap JavaDoc;
58
59 /**
60  * Input stream for Hessian requests, deserializing objects using the
61  * java.io.Serialization protocol.
62  *
63  * <p>HessianSerializerInput is unbuffered, so any client needs to provide
64  * its own buffering.
65  *
66  * <h3>Serialization</h3>
67  *
68  * <pre>
69  * InputStream is = new FileInputStream("test.xml");
70  * HessianOutput in = new HessianSerializerOutput(is);
71  *
72  * Object obj = in.readObject();
73  * is.close();
74  * </pre>
75  *
76  * <h3>Parsing a Hessian reply</h3>
77  *
78  * <pre>
79  * InputStream is = ...; // from http connection
80  * HessianInput in = new HessianSerializerInput(is);
81  * String value;
82  *
83  * in.startReply(); // read reply header
84  * value = in.readString(); // read string value
85  * in.completeReply(); // read reply footer
86  * </pre>
87  */

88 public class HessianSerializerInput extends HessianInput {
89   /**
90    * Creates a new Hessian input stream, initialized with an
91    * underlying input stream.
92    *
93    * @param is the underlying input stream.
94    */

95   public HessianSerializerInput(InputStream JavaDoc is)
96   {
97     super(is);
98   }
99
100   /**
101    * Creates an uninitialized Hessian input stream.
102    */

103   public HessianSerializerInput()
104   {
105   }
106
107   /**
108    * Reads an object from the input stream. cl is known not to be
109    * a Map.
110    */

111   protected Object JavaDoc readObjectImpl(Class JavaDoc cl)
112     throws IOException JavaDoc
113   {
114     try {
115       Object JavaDoc obj = cl.newInstance();
116
117       if (_refs == null)
118         _refs = new ArrayList JavaDoc();
119       _refs.add(obj);
120
121       HashMap JavaDoc fieldMap = getFieldMap(cl);
122
123       int code = read();
124       for (; code >= 0 && code != 'z'; code = read()) {
125         _peek = code;
126         
127         Object JavaDoc key = readObject();
128         
129         Field JavaDoc field = (Field JavaDoc) fieldMap.get(key);
130
131         if (field != null) {
132           Object JavaDoc value = readObject(field.getType());
133           field.set(obj, value);
134         }
135         else {
136           Object JavaDoc value = readObject();
137         }
138       }
139       
140       if (code != 'z')
141         throw expect("map", code);
142
143       // if there's a readResolve method, call it
144
try {
145         Method JavaDoc method = cl.getMethod("readResolve", new Class JavaDoc[0]);
146         return method.invoke(obj, new Object JavaDoc[0]);
147       } catch (Exception JavaDoc e) {
148       }
149
150       return obj;
151     } catch (IOException JavaDoc e) {
152       throw e;
153     } catch (Exception JavaDoc e) {
154       throw new IOExceptionWrapper(e);
155     }
156   }
157
158   /**
159    * Creates a map of the classes fields.
160    */

161   protected HashMap JavaDoc getFieldMap(Class JavaDoc cl)
162   {
163     HashMap JavaDoc fieldMap = new HashMap JavaDoc();
164     
165     for (; cl != null; cl = cl.getSuperclass()) {
166       Field JavaDoc []fields = cl.getDeclaredFields();
167       for (int i = 0; i < fields.length; i++) {
168         Field JavaDoc field = fields[i];
169
170         if (Modifier.isTransient(field.getModifiers()) ||
171             Modifier.isStatic(field.getModifiers()))
172           continue;
173
174         // XXX: could parameterize the handler to only deal with public
175
field.setAccessible(true);
176
177         fieldMap.put(field.getName(), field);
178       }
179     }
180
181     return fieldMap;
182   }
183 }
184
Popular Tags