KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > distribution > consistency > ClientServerConsistencyWrapper


1 /*
2   Copyright (C) 2001 Renaud Pawlak renaud@aopsys.com
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.distribution.consistency;
19
20 import java.util.*;
21 import org.apache.log4j.Logger;
22 import org.objectweb.jac.core.*;
23 import org.objectweb.jac.core.dist.*;
24 import org.objectweb.jac.core.rtti.ClassRepository;
25 import org.objectweb.jac.util.*;
26
27 /**
28  * This wrapper implements a client-server consistency protocol.
29  *
30  * <p>It is a special consistency protocol since the wrappee acts as a
31  * pure client (a stub) for its known replicas.<p>
32  *
33  * @author <a HREF="http://cedric.cnam.fr/~pawlak/index-english.html">Renaud Pawlak</a> */

34
35 public class ClientServerConsistencyWrapper extends ConsistencyWrapper {
36     static Logger logger = Logger.getLogger("consistency");
37
38     String JavaDoc hosts = null;
39
40     /**
41     * A friendly constructor for a client-server consistency wrapper.
42     *
43     * @param hosts a regular expression that defines the host where
44     * the server is located
45     */

46     public ClientServerConsistencyWrapper(AspectComponent ac, String JavaDoc hosts) {
47         super(ac);
48         knownReplicas = null;
49         this.hosts = hosts;
50     }
51
52     /** An empty constructor for the Consistency class. */
53     public ClientServerConsistencyWrapper(AspectComponent ac) {
54         super(ac);
55     }
56
57     /**
58     * Forwards the call to the server(s).<p>
59     *
60     * Do not call the replica except if we do not know any server (in
61     * this case, we are a server).<p>
62     *
63     * @return the value returned by the server */

64
65     public Object JavaDoc whenCall(Interaction interaction) {
66
67         if( knownReplicas == null ) {
68             knownReplicas = Topology.getPartialTopology(hosts).getReplicas(interaction.wrappee);
69         }
70
71         if(knownReplicas != null && knownReplicas.size() > 0 &&
72            (! ((RemoteRef)knownReplicas.get(0)).getRemCont().isLocal()) ) {
73             logger.debug("(client-server) call event on " +
74                       NameRepository.get().getName(interaction.wrappee) +
75                       ":" + interaction.method + ":" +
76                       ((RemoteRef)knownReplicas.get(0)).getRemCont().getName());
77             Object JavaDoc ret = ((RemoteRef)knownReplicas.get(0)).invokeRoleMethod(
78                 "acceptRemoteCall",
79                 new Object JavaDoc[] { null, new Object JavaDoc[] { interaction.method, interaction.args } }
80             );
81             return ret;
82         } else {
83             /* we do not know any replica, so we are the server... */
84             return proceed(interaction);
85         }
86     }
87
88     /**
89     * Calls the method on the server.
90     *
91     * @param remoteReplica the client remote reference
92     * @param data the name and the parameters of the server method
93     */

94
95     public Object JavaDoc acceptRemoteCall(Wrappee wrappee, RemoteRef remoteReplica,
96                                    Object JavaDoc[] data) {
97         logger.debug("(client-server) remote call event on " +
98                      NameRepository.get().getName(wrappee) +
99                      ":" + (String JavaDoc)data[0] + ":" +
100                      Arrays.asList((Object JavaDoc[])data[1]));
101         return ClassRepository.get().getClass(wrappee).getMethod((String JavaDoc)data[0]).invoke(
102             wrappee , (Object JavaDoc[])data[1] );
103     }
104 }
105
Popular Tags