KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > rmi > multi > IrmiPRODelegate


1 /**
2  * Copyright (C) 2002,2005 - INRIA (www.inria.fr)
3  *
4  * CAROL: Common Architecture for RMI ObjectWeb Layer
5  *
6  * This library is developed inside the ObjectWeb Consortium,
7  * http://www.objectweb.org
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  *
24  * --------------------------------------------------------------------------
25  * $Id: IrmiPRODelegate.java,v 1.1 2005/05/14 00:03:58 rhs Exp $
26  * --------------------------------------------------------------------------
27  */

28 package org.objectweb.carol.rmi.multi;
29
30 import java.io.IOException JavaDoc;
31 import java.io.ObjectInput JavaDoc;
32 import java.io.ObjectOutput JavaDoc;
33
34 import java.util.Collection JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Properties JavaDoc;
37
38 import org.objectweb.carol.irmi.PRO;
39 import org.objectweb.carol.irmi.Interceptor;
40 import org.objectweb.carol.irmi.ClientInterceptor;
41 import org.objectweb.carol.irmi.Server;
42 import org.objectweb.carol.rmi.jrmp.interceptor.JClientRequestInfo;
43 import org.objectweb.carol.rmi.jrmp.interceptor.JClientRequestInterceptor;
44 import org.objectweb.carol.rmi.jrmp.interceptor.JInterceptorStore;
45 import org.objectweb.carol.rmi.jrmp.interceptor.JRMPClientRequestInfoImpl;
46 import org.objectweb.carol.rmi.jrmp.interceptor.JRMPServerRequestInfoImpl;
47 import org.objectweb.carol.rmi.jrmp.interceptor.JServerRequestInfo;
48 import org.objectweb.carol.rmi.jrmp.interceptor.JServerRequestInterceptor;
49 import org.objectweb.carol.rmi.jrmp.interceptor.JServiceContext;
50 import org.objectweb.carol.rmi.jrmp.server.JUnicastRemoteObject;
51 import org.objectweb.carol.rmi.util.PortNumber;
52 import org.objectweb.carol.util.configuration.CarolDefaultValues;
53 import org.objectweb.carol.util.configuration.ConfigurationRepository;
54
55 /**
56  * IrmiPRODelegate
57  *
58  * @author Rafael H. Schloming <rhs@mit.edu>
59  **/

60
61 public class IrmiPRODelegate extends PRO {
62
63     private static class ServerInterceptorImpl implements Interceptor {
64
65         private JServerRequestInterceptor[] sis;
66
67         public ServerInterceptorImpl(JServerRequestInterceptor[] sis) {
68             this.sis = sis;
69         }
70
71         public void receive(byte code, ObjectInput JavaDoc in)
72             throws IOException JavaDoc, ClassNotFoundException JavaDoc {
73             JServerRequestInfo info = new JRMPServerRequestInfoImpl();
74             int len = in.readShort();
75             for (int i = 0; i < len; i++) {
76                 info.add_reply_service_context((JServiceContext) in.readObject());
77             }
78             for (int i = 0; i < sis.length; i++) {
79                 sis[i].receive_request(info);
80             }
81         }
82
83         public void send(byte code, ObjectOutput JavaDoc out) throws IOException JavaDoc {
84             JServerRequestInfo info = new JRMPServerRequestInfoImpl();
85             for (int i = 0; i < sis.length; i++) {
86                 switch (code) {
87                 case METHOD_RESULT:
88                     sis[i].send_reply(info);
89                     break;
90                 case METHOD_ERROR:
91                 case SYSTEM_ERROR:
92                     sis[i].send_exception(info);
93                     break;
94                 }
95             }
96             Collection JavaDoc c = info.get_all_reply_service_context();
97             out.writeShort(c.size());
98             for (Iterator JavaDoc it = c.iterator(); it.hasNext(); ) {
99                 out.writeObject(it.next());
100             }
101         }
102
103     }
104
105     private static class ClientInterceptorImpl implements ClientInterceptor {
106
107         private JClientRequestInterceptor[] cis;
108
109         public ClientInterceptorImpl(JClientRequestInterceptor[] cis) {
110             this.cis = cis;
111         }
112
113         public void send(byte code, ObjectOutput JavaDoc out) throws IOException JavaDoc {
114             JClientRequestInfo info = new JRMPClientRequestInfoImpl();
115             for (int i = 0; i < cis.length; i++) {
116                 cis[i].send_request(info);
117             }
118             Collection JavaDoc c = info.get_all_request_service_context();
119             out.writeShort(c.size());
120             for (Iterator JavaDoc it = c.iterator(); it.hasNext(); ) {
121                 out.writeObject(it.next());
122             }
123         }
124
125         public void receive(byte code, ObjectInput JavaDoc in)
126             throws IOException JavaDoc, ClassNotFoundException JavaDoc {
127             JClientRequestInfo info = new JRMPClientRequestInfoImpl();
128             int len = in.readShort();
129             for (int i = 0; i < len; i++) {
130                 info.add_request_service_context((JServiceContext) in.readObject());
131             }
132             for (int i = 0; i < cis.length; i++) {
133                 switch (code) {
134                 case METHOD_RESULT:
135                     cis[i].receive_reply(info);
136                     break;
137                 case METHOD_ERROR:
138                 case SYSTEM_ERROR:
139                     cis[i].receive_exception(info);
140                     break;
141                 }
142             }
143         }
144
145     }
146
147     private static Server getServer(boolean usingCmi) {
148         int port = 0;
149         Properties JavaDoc prop = ConfigurationRepository.getProperties();
150         if (!usingCmi && prop != null) {
151             String JavaDoc propertyName = CarolDefaultValues.SERVER_JRMP_PORT;
152             port = PortNumber.strToint(prop.getProperty(propertyName, "0"), propertyName);
153         }
154         return new Server(port, new ClientInterceptorImpl(JInterceptorStore.getLocalClientInterceptors()),
155                           new ServerInterceptorImpl(JInterceptorStore.getLocalServerInterceptors()));
156     }
157
158     public IrmiPRODelegate(boolean usingCmi) {
159         super(getServer(usingCmi));
160     }
161
162     public IrmiPRODelegate() {
163         this(false);
164     }
165
166 }
167
Popular Tags