KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > invocation > MarshalledValueOutputStream


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 package org.jboss.invocation;
23
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.ObjectOutputStream JavaDoc;
27 import java.rmi.Remote JavaDoc;
28 import java.rmi.server.RemoteObject JavaDoc;
29 import java.rmi.server.RemoteStub JavaDoc;
30 import java.security.PrivilegedAction JavaDoc;
31 import java.security.AccessController JavaDoc;
32
33 /**
34  * An ObjectOutputStream subclass used by the MarshalledValue class to
35  * ensure the classes and proxies are loaded using the thread context
36  * class loader. Currently this does not do anything as neither class or
37  * proxy annotations are used.
38  *
39  * @author Scott.Stark@jboss.org
40  * @version $Revision: 37459 $
41  */

42 public class MarshalledValueOutputStream
43    extends ObjectOutputStream JavaDoc
44 {
45    /** Creates a new instance of MarshalledValueOutputStream
46     If there is a security manager installed, this method requires a
47     SerializablePermission("enableSubstitution") permission to ensure it's
48     ok to enable the stream to do replacement of objects in the stream.
49     */

50    public MarshalledValueOutputStream(OutputStream JavaDoc os) throws IOException JavaDoc
51    {
52       super(os);
53       EnableReplaceObjectAction.enableReplaceObject(this);
54    }
55
56    /**
57     * @throws IOException Any exception thrown by the underlying OutputStream.
58     */

59    protected void annotateClass(Class JavaDoc cl) throws IOException JavaDoc
60    {
61       super.annotateClass(cl);
62    }
63    
64    /**
65     * @throws IOException Any exception thrown by the underlying OutputStream.
66     */

67    protected void annotateProxyClass(Class JavaDoc cl) throws IOException JavaDoc
68    {
69       super.annotateProxyClass(cl);
70    }
71
72    /** Override replaceObject to check for Remote objects that are
73     not RemoteStubs.
74    */

75    protected Object JavaDoc replaceObject(Object JavaDoc obj) throws IOException JavaDoc
76    {
77       if( (obj instanceof Remote JavaDoc) && !(obj instanceof RemoteStub JavaDoc) )
78       {
79          Remote JavaDoc remote = (Remote JavaDoc) obj;
80          try
81          {
82             obj = RemoteObject.toStub(remote);
83          }
84          catch(IOException JavaDoc ignore)
85          {
86             // Let the Serialization layer try with the orignal obj
87
}
88       }
89       return obj;
90    }
91
92    private static class EnableReplaceObjectAction implements PrivilegedAction JavaDoc
93    {
94       MarshalledValueOutputStream os;
95       EnableReplaceObjectAction(MarshalledValueOutputStream os)
96       {
97          this.os = os;
98       }
99       public Object JavaDoc run()
100       {
101          os.enableReplaceObject(true);
102          return null;
103       }
104       static void enableReplaceObject(MarshalledValueOutputStream os)
105       {
106          EnableReplaceObjectAction action = new EnableReplaceObjectAction(os);
107          AccessController.doPrivileged(action);
108       }
109    }
110 }
111
Popular Tags