KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > distribution > BroadcastingAC


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 Generaly 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;
19
20
21
22 import java.util.*;
23 import org.aopalliance.intercept.ConstructorInvocation;
24 import org.aopalliance.intercept.MethodInvocation;
25 import org.apache.log4j.Logger;
26 import org.objectweb.jac.core.*;
27 import org.objectweb.jac.core.dist.*;
28 import org.objectweb.jac.util.Log;
29
30 /**
31  * This aspect component implements a simple broadcasting aspect.
32  *
33  * <p>Principles: a broadcaster, located on a given host forwards some
34  * calls to a set of replicas located on remote hosts.
35  *
36  * @see BroadcastingConf
37  * @author Renaud Pawlak
38  */

39
40 public class BroadcastingAC
41     extends AspectComponent
42     implements BroadcastingConf
43 {
44     static Logger logger = Logger.getLogger("broadcasting");
45
46     public void addBroadcaster(
47         String JavaDoc wrappeeName,
48         String JavaDoc methods,
49         String JavaDoc broadcasterHost,
50         String JavaDoc replicasHost) {
51
52         pointcut(
53             wrappeeName,
54             ".*",
55             methods + " && !CONSTRUCTORS",
56             new BroadcastingWrapper(this, replicasHost),
57             broadcasterHost,
58             null);
59     }
60
61     /**
62      * This wrapper wraps the broadcaster with a wrapping method that
63      * broadcast all the calls to the remote replicas. */

64
65     public class BroadcastingWrapper extends Wrapper {
66
67         Vector replicas = null;
68         String JavaDoc hostExpr;
69         boolean doFill = true;
70
71         public BroadcastingWrapper(AspectComponent ac, String JavaDoc hostExpr) {
72             super(ac);
73             this.hostExpr = hostExpr;
74         }
75
76         public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
77             return broadcast((Interaction) invocation);
78         }
79
80         public Object JavaDoc construct(ConstructorInvocation invocation)
81             throws Throwable JavaDoc {
82             throw new Exception JavaDoc("This wrapper does not support constructor wrapping");
83         }
84
85         public void invalidate() {
86             doFill = true;
87         }
88
89         /**
90          * Performs a broadcasting. */

91
92         public Object JavaDoc broadcast(Interaction interaction) {
93             if (doFill) {
94                 replicas =
95                     Topology.getPartialTopology(hostExpr).getReplicas(
96                         interaction.wrappee);
97                 doFill = false;
98             }
99             if (replicas.size() == 0) {
100                 // none replicas where found, we perform a local call and
101
// will try to get them again on the next call
102
doFill = true;
103                 logger.warn("no replica found, local call performed");
104                 return proceed(interaction);
105             }
106             Object JavaDoc ret = null;
107             for (int i = 0; i < replicas.size(); i++) {
108                 ret =
109                     ((RemoteRef) replicas.get(i)).invoke(
110                         interaction.method.getName(),
111                         interaction.args);
112             }
113             return ret; //proceed(interaction);
114
}
115
116     }
117 }
118
Popular Tags