KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2001-2002 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 class implements the core protocol for a strong
29  * consistency that is based on a pull strategy.
30  *
31  * <p>On contrary to the <code>StrongPushConsistencyWrapper</code>,
32  * this protocol pulls the data from the other replicas. Indeed, each
33  * time a data is read and is not locally available, it is fetched
34  * from the known replicas.<p>
35  *
36  * @see StrongPushConsistencyWrapper
37  *
38  * @author <a HREF="http://cedric.cnam.fr/~pawlak/index-english.html">Renaud Pawlak</a>
39  */

40
41 public class StrongPullConsistencyWrapper extends ConsistencyWrapper {
42     static Logger logger = Logger.getLogger("consistency");
43
44     String JavaDoc hosts = null;
45
46     /**
47     * A friendly constructor for a pull consistency wrapper.
48     *
49     * @param hosts a regular expression that defines the host where
50     * the consistency protocol is installed */

51
52     public StrongPullConsistencyWrapper(AspectComponent ac, String JavaDoc hosts) {
53         super(ac);
54         this.hosts = hosts;
55         knownReplicas = null;
56     }
57
58     /** An empty constructor for the Consistency class. */
59     public StrongPullConsistencyWrapper(AspectComponent ac) {
60         super(ac);
61     }
62
63     /**
64     * This wrapping method first try to use the wrappee to get the
65     * result of the read method.<p>
66     *
67     * If this result is null or if an exception occurs, it considers
68     * that the read information was not locally present and tries to
69     * fetch it from the replicas it knows (recursivelly all the
70     * replicas are finally asked). If none of the replica returns a
71     * value, it returns null or throws an exception.
72     *
73     * @return the value returned by the wrapped read method */

74
75     public Object JavaDoc whenRead(Interaction interaction) {
76
77         Object JavaDoc ret = null;
78       
79         ret = proceed(interaction);
80
81         logger.debug("Pull knownReplicas = "+knownReplicas);
82
83         if (ret == null) {
84
85             if (knownReplicas != null) {
86                 Collaboration c = Collaboration.get();
87                 Vector asked_replicas = (Vector)c.getAttribute(visitedReplicas);
88                 if (asked_replicas == null) {
89                     asked_replicas = (Vector)c.addAttribute(
90                         visitedReplicas, new Vector());
91                 }
92
93                 //System.out.println ( "########### " + notified_replicas );
94
try {
95                     Vector new_ar = new Vector();
96                     RemoteRef cur_replica = RemoteRef.create(
97                         NameRepository.get().getName(interaction.wrappee),
98                         interaction.wrappee);
99                
100                     for (int i = 0; i < knownReplicas.size(); i++) {
101                         if ( (! asked_replicas.contains( knownReplicas.get(i) ) ) &&
102                              (! ((RemoteRef)knownReplicas.get(i)).getRemCont().isLocal()) ) {
103                      
104                             Vector kr = new Vector(knownReplicas);
105                             kr.remove(knownReplicas.get(i));
106                             new_ar.clear();
107                             new_ar.addAll(asked_replicas);
108                             new_ar.addAll(kr);
109                             new_ar.add(cur_replica);
110                             c.addAttribute(visitedReplicas, new_ar);
111                      
112                             logger.debug("(strong pull) read event on " +
113                                       interaction.wrappee + ":" +
114                                       interaction.method + ":" +
115                                       ((RemoteRef)knownReplicas.get(i)).getRemCont().getName());
116                             ret = ((RemoteRef)knownReplicas.get(i)).invokeRoleMethod(
117                                 "acceptRemoteRead",
118                                 new Object JavaDoc[] { null,
119                                                new Object JavaDoc[] { interaction.method,
120                                                               interaction.args } } );
121                             if (ret != null) {
122                                 break;
123                             }
124                         }
125                     }
126                 } finally {
127                     c.addAttribute(visitedReplicas, null);
128                 }
129             }
130         }
131         return ret;
132     }
133       
134     /**
135     * Try to read the method asked by <code>whenRead</code>.<p>
136     *
137     * The data is :<p>
138     * <ul><pre>
139     * data[0] = the read method name string
140     * data[1] = an array that contains the arguments of the read method
141     * </pre></ul>
142     *
143     * @param remoteReplica the replica that received the read event
144     * @param data the read event data
145     * @return the return value of the <code>data[0]</code> method*/

146
147     public Object JavaDoc acceptRemoteRead(Wrappee wrappee, RemoteRef remoteReplica,
148                                    Object JavaDoc[] data) {
149         logger.debug("(strong pull) remote read event on " +
150                      wrappee + ":" + (String JavaDoc)data[0]);
151         return ClassRepository.get().getClass(wrappee).getMethod((String JavaDoc)data[0]).invoke(
152             wrappee , (Object JavaDoc[])data[1] );
153     }
154    
155       
156 }
157
Popular Tags