KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.*;
21 import org.objectweb.jac.core.*;
22 import org.objectweb.jac.core.dist.*;
23
24 /**
25  * This wrapper class implements a weak consistency protocol for a set
26  * of replicas.
27  *
28  * <p>The semantics of this protocol is that the readed data can be
29  * inconsistent with the other replicas. When a write event occurs,
30  * the currently written replicas asks for the consistent state to the
31  * valid replica (the owner) and becomes itself the valid
32  * replica. This protocol ensures that the set of modifications are
33  * finally done in at least one replica.<p>
34  *
35  * @author <a HREF="http://cedric.cnam.fr/~pawlak/index-english.html">Renaud Pawlak</a> */

36
37 public class WeakConsistencyWrapper extends ConsistencyWrapper {
38
39    /** A reference to the valid copy. */
40    RemoteRef owner = null;
41    /** True if we are the owner of the valid copy. */
42    boolean isOwner = false;
43
44    public WeakConsistencyWrapper(AspectComponent ac) {
45       super(ac);
46    }
47
48    /**
49     * Update the wrappee state with the owner replica state before
50     * proceeding the writing method.<p>
51     *
52     * The wrappee then becomes the owner.<p>
53     *
54      @return the value returned by the wrapped method */

55
56    public Object JavaDoc whenWrite(Interaction interaction) {
57       
58       if(!isOwner) {
59          /* Warn the owner and retrieves its state */
60          Object JavaDoc[] ownerState = (Object JavaDoc[]) owner.invokeRoleMethod(
61             "acceptRemoteWrite", new Object JavaDoc[] {}
62          );
63          
64          /* Set the new state */
65          Field[] fields = interaction.wrappee.getClass().getDeclaredFields();
66          for ( int i = 0; i < fields.length; i++ ) {
67             try {
68                fields[i].set( interaction.wrappee, ownerState[i] );
69             } catch (Exception JavaDoc e) {}
70          }
71
72          /* Warn the replicas that we are the new owner */
73          for (int i = 0; i < knownReplicas.size(); i++) {
74             ((RemoteRef)knownReplicas.get(i)).invokeRoleMethod(
75                "setOwner",
76                new Object JavaDoc[] { interaction.wrappee }
77             );
78          }
79       }
80       
81       /* Call the wrappee... */
82       return proceed(interaction);
83    }
84
85    /**
86     * The current object is not the owner anymore and returns the
87     * object state so that the new owner can be consistent
88     *
89     * @param remoteReplica the replica that is beeing written
90     * @param data this parameter is not used in this protocol (its
91     * value is null)
92     * @return the state of the object so that the written object can
93     * become a consistent owner */

94
95     public Object JavaDoc acceptRemoteWrite(Wrappee wrappee, RemoteRef remoteReplica,
96                                     Object JavaDoc[] data) {
97        Field[] fields = wrappee.getClass().getDeclaredFields();
98        Object JavaDoc[] state = new Object JavaDoc[fields.length];
99        for ( int i = 0; i < fields.length; i++ ) {
100           try {
101              state[i] = fields[i].get(wrappee);
102           } catch (Exception JavaDoc e) {}
103        }
104        isOwner = false;
105        return state;
106    }
107    
108    /**
109     * This role method sets a new owner.
110     *
111     * @param newOwner a remote reference on the new owner
112     * @return null */

113
114    public Object JavaDoc setOwner (RemoteRef newOwner) {
115       owner = newOwner;
116       isOwner = false;
117       return null;
118    }
119       
120 }
121
Popular Tags