KickJava   Java API By Example, From Geeks To Geeks.

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


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.fractal.api.Interface;
31
32 import org.objectweb.jonathan.apis.binding.NamingContext;
33 import org.objectweb.jonathan.apis.binding.Reference;
34
35 import java.io.IOException JavaDoc;
36 import java.io.ObjectOutputStream JavaDoc;
37 import java.io.OutputStream JavaDoc;
38
39 /**
40  * An {@link ObjectOutputStream} that replaces interface references with {@link
41  * Ref} objects.
42  */

43
44 public class RmiObjectOutputStream extends ObjectOutputStream JavaDoc {
45
46   /**
47    * The naming context used to export local interfaces, to make them
48    * remotely accessible.
49    */

50
51   protected NamingContext domain;
52
53   /**
54    * Constructs a new {@link RmiObjectOutputStream}.
55    *
56    * @param os the underlying output stream.
57    * @param domain the naming context to be used to export local interface
58    * references, to make them remotely accessible.
59    * @throws IOException if the super constructor throws an exception.
60    */

61
62   public RmiObjectOutputStream (
63     final OutputStream JavaDoc os,
64     final NamingContext domain) throws IOException JavaDoc
65   {
66     super(os);
67     enableReplaceObject(true);
68     this.domain = domain;
69     String JavaDoc codeBase = System.getProperty("java.rmi.server.codebase");
70     writeUTF(codeBase == null ? "" : codeBase);
71   }
72
73   /**
74    * Replaces component interfaces with {@link Ref} objects. If the given
75    * object is an {@link Interface}, two cases are possible. If the object is
76    * also a {@link Reference}, then it is replaced with a {@link Ref} object
77    * containing an encoded form of the identifier held by the {@link Reference}.
78    * If the object is not a {@link Reference}, then it is exported with {@link
79    * #domain domain}, and replaced with a {@link Ref} object containing an
80    * encoded form of the {@link org.objectweb.jonathan.apis.binding.Identifier}
81    * returned by {@link NamingContext#export export}.
82    *
83    * @param obj an object.
84    * @return a {@link Ref} object if <tt>obj</tt> is a component interface,
85    * or <tt>obj</tt> otherwise.
86    * @throws IOException if a component interface cannot be replaced with a
87    * {@link Ref} object.
88    */

89
90   protected Object JavaDoc replaceObject (final Object JavaDoc obj) throws IOException JavaDoc {
91     if (obj instanceof Interface) {
92       try {
93         Ref ref = new Ref();
94         ref.type = obj.getClass().getInterfaces()[0].getName();
95         if (obj instanceof Reference) {
96           ref.id = ((Reference)obj).getIdentifiers()[0].encode();
97         } else {
98           ref.id = domain.export(obj, null).encode();
99         }
100         return ref;
101       } catch (Exception JavaDoc e) {
102         throw new IOException JavaDoc("Cannot export object: " + e);
103       }
104     }
105     return obj;
106   }
107
108   /**
109    * Drain any buffered data in this stream.
110    *
111    * @throws IOException if an IO exception occurs.
112    */

113
114   protected void drain () throws IOException JavaDoc {
115     super.drain();
116   }
117 }
118
Popular Tags