~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/serialization/impl/java/JavaSerializationManager.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* JBoss, Home of Professional Open Source
 
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
 
4
* by the @authors tag. See the copyright.txt in the distribution for a
 
5
* full listing of individual contributors.
 
6
*
 
7
* This is free software; you can redistribute it and/or modify it
 
8
* under the terms of the GNU Lesser General Public License as
 
9
* published by the Free Software Foundation; either version 2.1 of
 
10
* the License, or (at your option) any later version.
 
11
*
 
12
* This software is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
15
* Lesser General Public License for more details.
 
16
*
 
17
* You should have received a copy of the GNU Lesser General Public
 
18
* License along with this software; if not, write to the Free
 
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 
21
*/
 
22
 
 
23
 
 
24
package org.jboss.remoting.serialization.impl.java;
 
25
 
 
26
import java.io.IOException;
 
27
import java.io.InputStream;
 
28
import java.io.ObjectInputStream;
 
29
import java.io.ObjectOutputStream;
 
30
import java.io.OptionalDataException;
 
31
import java.io.OutputStream;
 
32
import org.jboss.logging.Logger;
 
33
import org.jboss.remoting.Version;
 
34
import org.jboss.remoting.loading.ObjectInputStreamWithClassLoader;
 
35
import org.jboss.remoting.serialization.SerializationManager;
 
36
import org.jboss.remoting.serialization.SerializationStreamFactory;
 
37
import org.jboss.remoting.serialization.IMarshalledValue;
 
38
 
 
39
/**
 
40
 * $Id: JavaSerializationManager.java 4519 2008-08-30 06:20:07Z ron.sigal@jboss.com $
 
41
 *
 
42
 * @author <a href="mailto:clebert.suconic@jboss.com">Clebert Suconic</a>
 
43
 */
 
44
public class JavaSerializationManager extends SerializationManager
 
45
{
 
46
   protected static final Logger log = Logger.getLogger(JavaSerializationManager.class);
 
47
 
 
48
   public ObjectInputStream createInput(InputStream input, ClassLoader loader) throws IOException
 
49
   {
 
50
      if(log.isTraceEnabled())
 
51
      {
 
52
         log.trace("Creating ObjectInputStreamWithClassLoader");
 
53
      }
 
54
      return new ObjectInputStreamWithClassLoader(input, loader);
 
55
   }
 
56
 
 
57
   public ObjectOutputStream createOutput(OutputStream output) throws IOException
 
58
   {
 
59
      if(log.isTraceEnabled())
 
60
      {
 
61
         log.trace("Creating ObjectOutputStream");
 
62
      }
 
63
      return new ClearableObjectOutputStream(output); 
 
64
   }
 
65
 
 
66
   /**
 
67
    * Creates a MarshalledValue that does lazy serialization.
 
68
    */
 
69
   public IMarshalledValue createdMarshalledValue(Object source) throws IOException
 
70
   {
 
71
      if(source instanceof IMarshalledValue)
 
72
      {
 
73
         return (IMarshalledValue) source;
 
74
      }
 
75
      else
 
76
      {
 
77
         return new JavaMarshalledValue(source);
 
78
      }
 
79
   }
 
80
 
 
81
    public IMarshalledValue createMarshalledValueForClone(Object original) throws IOException {
 
82
        return createdMarshalledValue(original);
 
83
    }
 
84
   
 
85
   public void sendObject(ObjectOutputStream oos, Object dataObject, int version) throws IOException
 
86
   {
 
87
      switch (version)
 
88
      {
 
89
         case Version.VERSION_1:
 
90
         case Version.VERSION_2:
 
91
            sendObjectVersion1_2(oos, dataObject);
 
92
            break;
 
93
            
 
94
         case Version.VERSION_2_2:
 
95
            sendObjectVersion2_2(oos, dataObject);
 
96
            break;
 
97
            
 
98
         default:
 
99
            throw new IOException("Can not process version " + version + ". " +
 
100
                  "Supported versions: " + Version.VERSION_1 + ", " + Version.VERSION_2 + ", " + Version.VERSION_2_2);
 
101
 
 
102
      }
 
103
   }
 
104
   
 
105
   protected void sendObjectVersion1_2(ObjectOutputStream oos, Object dataObject) throws IOException
 
106
   {
 
107
      oos.writeObject(dataObject);
 
108
      oos.reset();
 
109
      // to make sure stream gets reset
 
110
      // Stupid ObjectInputStream holds object graph
 
111
      // can only be set by the client/server sending a TC_RESET
 
112
      oos.writeObject(Boolean.TRUE);
 
113
      oos.flush();
 
114
      oos.reset();
 
115
   }
 
116
   
 
117
   protected void sendObjectVersion2_2(ObjectOutputStream oos, Object dataObject) throws IOException
 
118
   {
 
119
      oos.reset();
 
120
      oos.writeObject(dataObject);
 
121
      oos.flush();
 
122
      
 
123
      if (oos instanceof ClearableObjectOutputStream)
 
124
      {
 
125
         ((ClearableObjectOutputStream) oos).clear();
 
126
      }
 
127
   }
 
128
   
 
129
   public Object receiveObject(InputStream inputStream, ClassLoader customClassLoader, int version) throws IOException, ClassNotFoundException
 
130
   {
 
131
      switch (version)
 
132
      {
 
133
         case Version.VERSION_1:
 
134
         case Version.VERSION_2:
 
135
            return receiveObjectVersion1_2(inputStream, customClassLoader);
 
136
            
 
137
         case Version.VERSION_2_2:
 
138
            return receiveObjectVersion2_2(inputStream, customClassLoader);
 
139
            
 
140
         default:
 
141
            throw new IOException("Can not process version " + version + ". " +
 
142
                  "Supported versions: " + Version.VERSION_1 + ", " + Version.VERSION_2 + ", " + Version.VERSION_2_2);
 
143
 
 
144
      }
 
145
   }
 
146
   
 
147
   protected Object receiveObjectVersion1_2(InputStream inputStream, ClassLoader customClassLoader) throws IOException, ClassNotFoundException
 
148
   {
 
149
      ObjectInputStream objInputStream = null;
 
150
      Object obj = null;
 
151
      if(inputStream instanceof ObjectInputStreamWithClassLoader)
 
152
      {
 
153
         ((ObjectInputStreamWithClassLoader) inputStream).setClassLoader(customClassLoader);
 
154
         objInputStream = (ObjectInputStream) inputStream;
 
155
      }
 
156
      /*else if(inputStream instanceof JBossObjectInputStream)
 
157
 {
 
158
     if(((JBossObjectInputStream) inputStream).getClassLoader() == null)
 
159
     {
 
160
        ((JBossObjectInputStream) inputStream).setClassLoader(customClassLoader);
 
161
     }
 
162
     objInputStream = (ObjectInputStream) inputStream;
 
163
 } -- for future reference */
 
164
      else if(inputStream instanceof ObjectInputStream)
 
165
      {
 
166
         objInputStream = (ObjectInputStream) inputStream;
 
167
      }
 
168
      else
 
169
      {
 
170
         if(customClassLoader != null)
 
171
         {
 
172
            objInputStream = SerializationStreamFactory.getManagerInstance(SerializationStreamFactory.JAVA).createInput(inputStream, customClassLoader);
 
173
         }
 
174
         else
 
175
         {
 
176
            objInputStream = SerializationStreamFactory.getManagerInstance(SerializationStreamFactory.JAVA).createRegularInput(inputStream);
 
177
         }
 
178
      }
 
179
 
 
180
      try
 
181
      {
 
182
      obj = objInputStream.readObject();
 
183
      }
 
184
      catch (IOException e)
 
185
      {
 
186
         log.debug("", e);
 
187
         throw e;
 
188
      }
 
189
 
 
190
      try
 
191
      {
 
192
         objInputStream.readObject(); // for stupid ObjectInputStream reset
 
193
      }
 
194
      catch(Exception e)
 
195
      {
 
196
         /**
 
197
          * Putting try catch around this because if using servlet sever invoker, the previous
 
198
          * call is not needed, so will throw EOFException and can ignore.
 
199
          */
 
200
      }
 
201
      return obj;
 
202
   }
 
203
   
 
204
   protected Object receiveObjectVersion2_2(InputStream inputStream, ClassLoader customClassLoader) throws IOException, ClassNotFoundException
 
205
   {
 
206
      ObjectInputStream objInputStream = null;
 
207
      Object obj = null;
 
208
      if(inputStream instanceof ObjectInputStreamWithClassLoader)
 
209
      {
 
210
         ((ObjectInputStreamWithClassLoader) inputStream).setClassLoader(customClassLoader);
 
211
         objInputStream = (ObjectInputStream) inputStream;
 
212
      }
 
213
      /*else if(inputStream instanceof JBossObjectInputStream)
 
214
 {
 
215
     if(((JBossObjectInputStream) inputStream).getClassLoader() == null)
 
216
     {
 
217
        ((JBossObjectInputStream) inputStream).setClassLoader(customClassLoader);
 
218
     }
 
219
     objInputStream = (ObjectInputStream) inputStream;
 
220
 } -- for future reference */
 
221
      else if(inputStream instanceof ObjectInputStream)
 
222
      {
 
223
         objInputStream = (ObjectInputStream) inputStream;
 
224
      }
 
225
      else
 
226
      {
 
227
         if(customClassLoader != null)
 
228
         {
 
229
            objInputStream = SerializationStreamFactory.getManagerInstance(SerializationStreamFactory.JAVA).createInput(inputStream, customClassLoader);
 
230
         }
 
231
         else
 
232
         {
 
233
            objInputStream = SerializationStreamFactory.getManagerInstance(SerializationStreamFactory.JAVA).createRegularInput(inputStream);
 
234
         }
 
235
      }
 
236
 
 
237
 
 
238
      obj = objInputStream.readObject();
 
239
 
 
240
      if(inputStream instanceof ObjectInputStreamWithClassLoader)
 
241
      {
 
242
         ((ObjectInputStreamWithClassLoader) inputStream).clearCache();
 
243
      }
 
244
      
 
245
      return obj;
 
246
   }
 
247
}
 
 
b'\\ No newline at end of file'