KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > rmi > stub > Skeleton


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  * with some comments copied from Jonathan:
24  * org.objectweb.jonathan.apis.protocols.RequestSession (author: B. Dumant)
25  */

26
27 package org.objectweb.fractal.rmi.stub;
28
29 import org.objectweb.fractal.api.Component;
30 import org.objectweb.fractal.api.Interface;
31 import org.objectweb.fractal.api.Type;
32
33 import org.objectweb.jonathan.apis.kernel.JonathanException;
34 import org.objectweb.jonathan.apis.presentation.Marshaller;
35 import org.objectweb.jonathan.apis.presentation.UnMarshaller;
36 import org.objectweb.jonathan.apis.protocols.ReplySession;
37 import org.objectweb.jonathan.apis.protocols.RequestSession;
38
39 /**
40  * Super class of the skeleton classes generated by the {@link RmiStubFactory}.
41  * A skeleton gives access to a local interface of a Fractal
42  * component. It unmarshalls invocation messages and calls the corresponding
43  * methods on the local interface to which it gives access. Like stubs, a
44  * skeleton gives access to the "functional" interface provided by a server
45  * interface, and also gives access to the methods of the {@link Interface}
46  * interface implemented by this server interface.
47  */

48
49 public abstract class Skeleton implements RequestSession {
50
51   /**
52    * The local server interface to which this skeleton gives access.
53    */

54
55   protected Object JavaDoc target;
56
57   /**
58    * Constructs a new {@link Skeleton}.
59    */

60
61   public Skeleton () {
62   }
63
64   // --------------------------------------------------------------------------
65
// Implementation of the RequestSession interface
66
// --------------------------------------------------------------------------
67

68   /**
69    * Returns the target object represented by this request session.
70    *
71    * @return the target object represented by this request session.
72    */

73
74   public Object JavaDoc getTarget () {
75     return target;
76   }
77
78   /**
79    * Sends a message (a request) to its recipient. The unmarshaller representing
80    * the invocation is sent together with a reply session, to be used by the
81    * recipient to send the reply. The reply session may be null if no response
82    * is expected. It is the responsibility of the recipient to make sure that
83    * the unmarshaller will properly be {@link UnMarshaller#close() closed}.
84    *
85    * @param unmarshaller the unmarshaller representing the request.
86    * @param session the session to send the reply.
87    * @throws JonathanException if something goes wrong.
88    */

89
90   public abstract void send (UnMarshaller unmarshaller, ReplySession session)
91     throws JonathanException;
92
93   // --------------------------------------------------------------------------
94
// Utility methods
95
// --------------------------------------------------------------------------
96

97   /**
98    * Handles invocations messages corresponding to methods of the {@link
99    * Interface} interface.
100    *
101    * @param unmarshaller the unmarshaller representing the request.
102    * @param session the session to send the reply.
103    * @param methodIndex the index of the method to be called.
104    * @throws JonathanException if something goes wrong.
105    */

106
107   protected void handleInterfaceMethods (
108     final UnMarshaller unmarshaller,
109     final ReplySession session,
110     final int methodIndex) throws JonathanException
111   {
112     Interface itf = (Interface)target;
113     Marshaller marshaller;
114     try {
115       switch (methodIndex) {
116         case -1:
117           unmarshaller.close();
118           Component id = itf.getFcItfOwner();
119           marshaller = session.prepareReply();
120           marshaller.writeValue(id);
121           session.send(marshaller);
122           session.close();
123           return;
124         case -2:
125           unmarshaller.close();
126           String JavaDoc name = itf.getFcItfName();
127           marshaller = session.prepareReply();
128           marshaller.writeValue(name);
129           session.send(marshaller);
130           session.close();
131           return;
132         case -3:
133           unmarshaller.close();
134           Type type = itf.getFcItfType();
135           marshaller = session.prepareReply();
136           marshaller.writeValue(type);
137           session.send(marshaller);
138           session.close();
139           return;
140         case -4:
141           unmarshaller.close();
142           boolean internal = itf.isFcInternalItf();
143           marshaller = session.prepareReply();
144           marshaller.writeBoolean(internal);
145           session.send(marshaller);
146           session.close();
147           return;
148         default:
149           throw new Exception JavaDoc("No such method");
150       }
151     } catch (Exception JavaDoc e) {
152       handleException(e, session);
153     }
154   }
155
156   /**
157    * Handles an exception that happened in this skeleton. This method sends the
158    * given exception to the caller by using the given reply session.
159    *
160    * @param e the exception that happened.
161    * @param session the session to send the reply.
162    * @throws JonathanException if the given exception cannot be sent back to the
163    * caller.
164    */

165
166   protected void handleException (final Exception JavaDoc e, final ReplySession session)
167     throws JonathanException
168   {
169     try {
170       Marshaller marshaller = session.prepareExceptionReply();
171       marshaller.writeValue(e);
172       session.send(marshaller);
173     } catch (Exception JavaDoc f) {
174       f.printStackTrace();
175       throw new JonathanException(
176         "error during marshalling of exception by skeleton");
177     }
178     session.close();
179   }
180   
181   protected Object JavaDoc replaceClassName (Object JavaDoc o) throws ClassNotFoundException JavaDoc {
182     if (o instanceof String JavaDoc) {
183       return Class.forName((String JavaDoc)o);
184     } else if (o instanceof Object JavaDoc[]) {
185       Object JavaDoc[] desc = (Object JavaDoc[])o;
186       if (desc.length == 2 && desc[1] instanceof String JavaDoc) {
187         return new Object JavaDoc[] { desc[0], Class.forName((String JavaDoc)desc[1]) };
188       }
189     }
190     return o;
191   }
192   
193   protected Object JavaDoc replaceClassValue (Object JavaDoc o) {
194     if (o instanceof Class JavaDoc) {
195       return ((Class JavaDoc)o).getName();
196     } else if (o instanceof Object JavaDoc[]) {
197       Object JavaDoc[] desc = (Object JavaDoc[])o;
198       if (desc.length == 2 && desc[1] instanceof Class JavaDoc) {
199         return new Object JavaDoc[] { desc[0], ((Class JavaDoc)desc[1]).getName() };
200       }
201     }
202     return o;
203   }
204 }
205
Popular Tags