KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > rmi > io > RmiObjectInputStream


1 /***
2  * Fractal RMI: a binder for remote method calls between Fractal components.
3  * Copyright (C) 2003 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  *
23  * adapted from Jonathan:
24  * org.objectweb.jeremie.libs.presentation.StdMarshallerFactory
25  * (authors: B. Dumant, K. Milsted)
26  */

27
28 package org.objectweb.fractal.rmi.io;
29
30 import org.objectweb.jonathan.apis.binding.Identifier;
31 import org.objectweb.jonathan.apis.binding.NamingContext;
32 import org.objectweb.jonathan.apis.kernel.Context;
33 import org.objectweb.jonathan.apis.kernel.ContextFactory;
34
35 import java.io.IOException JavaDoc;
36 import java.io.InputStream JavaDoc;
37 import java.io.ObjectInputStream JavaDoc;
38 import java.io.ObjectStreamClass JavaDoc;
39 import java.rmi.server.RMIClassLoader JavaDoc;
40
41 /**
42  * An {@link ObjectInputStream} that replaces {@link Ref} objects with stubs.
43  */

44
45 public class RmiObjectInputStream extends ObjectInputStream JavaDoc {
46
47   /**
48    * The naming context used to decode the identifiers contained in {@link Ref}
49    * objects.
50    */

51
52   protected NamingContext domain;
53
54   /**
55    * The context factory used to create hints for the {@link Identifier#bind
56    * bind} method.
57    */

58
59   protected ContextFactory contextFactory;
60
61   /**
62    * The code base to be used to load classes.
63    */

64   
65   protected String JavaDoc codeBase;
66   
67   /**
68    * Constructs a new {@link RmiObjectInputStream}.
69    *
70    * @param is the underlying input stream.
71    * @param domain the naming context to be used to decode the identifiers
72    * contained in {@link Ref} objects.
73    * @param contextFactory the context factory to be used to create hints for
74    * the {@link Identifier#bind bind} method.
75    * @throws IOException if the super constructor throws an exception.
76    */

77   
78   public RmiObjectInputStream (
79     final InputStream JavaDoc is,
80     final NamingContext domain,
81     final ContextFactory contextFactory) throws IOException JavaDoc
82   {
83     super(is);
84     enableResolveObject(true);
85     this.domain = domain;
86     this.contextFactory = contextFactory;
87     this.codeBase = readUTF();
88     
89     ClassLoader JavaDoc l;
90     if (codeBase.length() == 0) {
91       l = getClass().getClassLoader();
92     } else {
93       Thread JavaDoc t = Thread.currentThread();
94       ClassLoader JavaDoc current = t.getContextClassLoader();
95       try {
96         t.setContextClassLoader(getClass().getClassLoader());
97         l = RMIClassLoader.getClassLoader(codeBase);
98       } finally {
99         t.setContextClassLoader(current);
100       }
101     }
102     if (l != null) {
103       Thread.currentThread().setContextClassLoader(l);
104     }
105   }
106
107   /**
108    * Replaces {@link Ref} objects with corresponding stubs. This method uses
109    * the {@link #domain domain} to decode the identifier encoded in a {@link
110    * Ref} object, and then uses the {@link Identifier#bind bind} method of the
111    * decoded identifier to get a stub corresponding to this identifier. The
112    * type stored in the {@link Ref} object is passed as an hint to the
113    * {@link Identifier#bind bind} method.
114    *
115    * @param obj an object.
116    * @return a stub if obj is a {@link Ref} object, or <tt>obj</tt>
117    * otherwise.
118    * @throws IOException if a {@link Ref} object cannot be replaced with a
119    * stub.
120    */

121
122   protected Object JavaDoc resolveObject (final Object JavaDoc obj) throws IOException JavaDoc {
123     if (obj instanceof Ref) {
124       try {
125         Ref ref = (Ref)obj;
126         Identifier id = domain.decode(ref.id, 0, ref.id.length);
127         Context hints = contextFactory.newContext();
128         hints.addElement("interface_type", String JavaDoc.class, ref.type, (char)0);
129         Object JavaDoc newObj = id.bind(new Identifier[] {id}, hints);
130         hints.release();
131         return newObj;
132       } catch (Exception JavaDoc e) {
133         throw new IOException JavaDoc("cannot bind to object: " + e);
134       }
135     } else {
136       return obj;
137     }
138   }
139
140   protected Class JavaDoc resolveClass (ObjectStreamClass JavaDoc desc)
141     throws IOException JavaDoc, ClassNotFoundException JavaDoc
142   {
143     try {
144       try {
145         return RMIClassLoader.loadClass((String JavaDoc)codeBase, desc.getName());
146       } catch (ClassNotFoundException JavaDoc e) {
147         return getClass().getClassLoader().loadClass(desc.getName());
148       }
149     } catch (ClassNotFoundException JavaDoc e) {
150       System.err.println("WARNING: " + e.toString());
151       throw e;
152     } catch (IOException JavaDoc e) {
153       System.err.println("WARNING: " + e.toString());
154       throw e;
155     }
156   }
157 }
158
Popular Tags