KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2001 Renaud Pawlak
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.RemoteRef;
24 import org.objectweb.jac.core.rtti.MethodItem;
25 import org.objectweb.jac.util.Log;
26
27 /**
28  * This wrapper implements a consistency protocol that forwards all
29  * the writing calls to all the replicas that are known by the
30  * wrapper.
31  *
32  * <p>It is called "push" since the wrapper pushes the data to the
33  * other replicas. Despite this strategy is the most curently used,
34  * other strong or weak consistency strategies can be implemented by
35  * other consistency wrappers.
36  *
37  * @author <a HREF="http://cedric.cnam.fr/~pawlak/index-english.html">Renaud Pawlak</a>
38  *
39  * @see #whenWrite(Interaction)
40  * @see StrongPullConsistencyWrapper
41  * @see WeakConsistencyWrapper */

42
43 public class StrongPushConsistencyWrapper extends ConsistencyWrapper {
44     static Logger logger = Logger.getLogger("consistency");
45
46     /** a false that is true during notification. */
47     boolean inNotification = false;
48
49     /**
50      * A friendly constructor for a push consistency wrapper.
51      *
52      * @param hosts a regular expression that defines the host where
53      * the consistency protocol is installed */

54
55     public StrongPushConsistencyWrapper(AspectComponent ac, String JavaDoc hosts) {
56         super(ac);
57         knownReplicas = null;
58         this.hosts = hosts;
59     }
60
61     /** An empty constructor for the Consistency class. */
62     public StrongPushConsistencyWrapper(AspectComponent ac) {
63         super(ac);
64     }
65
66     /**
67      * Forwards the call to all the replicas and then call the
68      * replica.<p>
69      *
70      * The pushing mecanism is stopped by using the collaboration
71      * attribute value defined by <code>visitedReplicas</code>.
72      *
73      * @return the value returned by the wrapped method.
74      * @see ConsistencyWrapper#getVisitedReplicas() */

75
76     public Object JavaDoc whenWrite(Interaction interaction) {
77
78         Object JavaDoc ret = null;
79
80         if (knownReplicas == null) {
81             calculateKnownReplicas(interaction.wrappee);
82         }
83
84         attrdef("Persistence.disabled", "true");
85
86         if (knownReplicas != null) {
87             if (inNotification) return proceed(interaction);
88             inNotification = true;
89             Collaboration c = Collaboration.get();
90             Vector notified_replicas = (Vector)c.getAttribute(visitedReplicas);
91             if (notified_replicas == null) {
92                 notified_replicas = (Vector)c.addAttribute(
93                     visitedReplicas, new Vector());
94             }
95
96             try {
97                 Vector new_nr = new Vector();
98                 RemoteRef cur_replica = RemoteRef.create(
99                     NameRepository.get().getName(interaction.wrappee),
100                     interaction.wrappee);
101             
102                 for (int i = 0; i < knownReplicas.size(); i++) {
103                     if ( (! notified_replicas.contains(knownReplicas.get(i)) ) &&
104                          (! ((RemoteRef)knownReplicas.get(i)).getRemCont().isLocal()) ) {
105                   
106                         Vector kr = new Vector(knownReplicas);
107                         kr.remove(knownReplicas.get(i));
108                         new_nr.clear();
109                         new_nr.addAll(notified_replicas);
110                         new_nr.addAll(kr);
111                         new_nr.add (cur_replica);
112                         c.addAttribute(visitedReplicas, new_nr);
113                   
114                         logger.debug("(strong) write event on " +
115                                      NameRepository.get().getName(interaction.wrappee)+
116                                      ":" + interaction.method + ":" +
117                                      ((RemoteRef)knownReplicas.get(i)).getRemCont().getName());
118                         try {
119                             ((RemoteRef)knownReplicas.get(i)).invokeRoleMethod(
120                                 "acceptRemoteWrite",
121                                 new Object JavaDoc[] { null,
122                                                new Object JavaDoc[] { interaction.method,
123                                                               interaction.args } }
124                             );
125                         } catch ( Exception JavaDoc e ) {
126                             logger.error("strong consistency error: "+
127                                          "failed to remotely invoke "+
128                                          "acceptRemoteWrite for "+
129                                          interaction.wrappee+"."+interaction.method);
130                             e.printStackTrace();
131                             break;
132                         }
133                     }
134                 }
135             } finally {
136                 c.addAttribute(visitedReplicas,null);
137             }
138         } else {
139             logger.debug("none replicas are known for "+
140                          NameRepository.get().getName(interaction.wrappee));
141         }
142         ret = proceed(interaction);
143
144         inNotification = false;
145
146         return ret;
147     }
148
149     /**
150      * This method is called by <code>whenWrite</code> to push the
151      * needed data when a state is writen in a remote replica.
152      *
153      * The data is :<p>
154      * <ul><pre>
155      * data[0] = the write method name string
156      * data[1] = an array that contains the arguments of the write method
157      * </pre></ul>
158      *
159      * @param remoteReplica expected to be a reference on the remote
160      * replica that recieved the write event
161      * @param data the data transmittedd by <code>whenWrite</code>
162      * @return null by default
163      * @see #whenWrite(Interaction) */

164
165     public Object JavaDoc acceptRemoteWrite(Wrappee wrappee, RemoteRef remoteReplica,
166                                     Object JavaDoc[] data) {
167         logger.debug("(strong) remote write event on " +
168                      NameRepository.get().getName(wrappee) +
169                      ":" + data[0]);
170         Object JavaDoc ret=null;
171         try {
172             ret = ((MethodItem)data[0]).invoke(
173                 wrappee, (Object JavaDoc[])data[1]);
174         } catch (Exception JavaDoc e) {
175             e.printStackTrace();
176         }
177         return ret;
178     }
179    
180     /**
181      * Push the current replica state to the binding new replica.
182      *
183      * @param newReplica the replica that is currently binding
184      */

185
186     public void whenBindingNewReplica(Wrappee wrappee, RemoteRef newReplica) {
187         logger.debug("(strong) initialized " +
188                      newReplica + " with " +
189                      NameRepository.get().getName(wrappee));
190         newReplica.remoteCopy(wrappee);
191     }
192       
193 }
194
Popular Tags