KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > core > dist > NonBlockingStubWrapper


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

18
19 package org.objectweb.jac.core.dist;
20
21 import org.apache.log4j.Logger;
22 import org.objectweb.jac.core.*;
23 import org.objectweb.jac.util.*;
24
25 /**
26  * NonBlockingStubWrapper is a dynamic client stub for org.objectweb.jac.
27  * Every method called on an object wrapped by such a wrapper
28  * is forwarded to a remote reference.
29  *
30  * The call is non blocking.
31  * For blocking calls see StubWrapper.
32  *
33  * This a wrapper class.
34  * The invoke method wraps all the methods of a wrappee.
35  *
36  * @see org.objectweb.jac.core.dist.StubWrapper
37  *
38  * @author <a HREF="http://www-src.lip6.fr/homepages/Lionel.Seinturier/index-eng.html">Lionel Seinturier</a>
39  */

40  
41 public class NonBlockingStubWrapper extends StubWrapper {
42     static Logger logger = Logger.getLogger("stub");
43
44    /**
45     * Construct a new dynamic stub.
46     *
47     * @param remoteRef the remote reference associated to the stub
48     */

49    
50     public NonBlockingStubWrapper(AspectComponent ac, RemoteRef remoteRef) {
51         super(ac,remoteRef);
52     }
53
54     /**
55     * A more user-friendly constructor.
56     *
57     * @param serverContainer the name of the container where the
58     * server is deployed */

59
60     public NonBlockingStubWrapper(AspectComponent ac, String JavaDoc serverContainer) {
61         super(ac,serverContainer);
62     }
63
64
65     /**
66     * Forward a call to the remote reference.
67     */

68    
69     public Object JavaDoc invoke(Interaction interaction) {
70
71         if (remoteRef == null) {
72             if (serverContainer == null) {
73                 logger.warn("local call (1) for stub "+interaction.wrappee);
74                 return proceed(interaction);
75             }
76             RemoteContainer rc = Topology.get().getFirstContainer(serverContainer);
77             if (rc == null) {
78                 logger.warn("local call (2) for stub "+interaction.wrappee);
79                 return proceed(interaction);
80             }
81             remoteRef = rc.bindTo(NameRepository.get().getName(interaction.wrappee));
82             if (remoteRef == null) {
83                 logger.warn("local call (3) for stub "+interaction.wrappee+
84                             " ("+rc+","+serverContainer+")");
85                 return proceed(interaction);
86             }
87         }
88
89         logger.debug(interaction.wrappee + " forwards to the server");
90       
91         /**
92          * These final local variables are required
93          * for the enclosed local class defined below.
94          */

95       
96         final String JavaDoc finalMethodName = interaction.method.getName();
97         final Object JavaDoc[] finalMethodArgs = interaction.args;
98    
99         // I disabled the results...
100
new Thread JavaDoc() {
101                 public void run() {
102                     //results[firstFreeCell] =
103
remoteRef.invoke( finalMethodName, finalMethodArgs );
104                 }
105             } . start();
106       
107         return null;
108         //new Integer( firstFreeCell );
109
}
110    
111     public void setFirstFreeCell() {
112     }
113
114     /**
115      * Maximum number of results stored.
116      *
117      * The idea is that if nbMaxOfResults consecutive calls have been made,
118      * the probability that the initial result is to be required is low.
119      */

120     final static protected int nbMaxOfResults = 16;
121
122     /**
123      * Mailbox for results received from asynchonous calls.
124      * The array is managed as a circular list.
125      */

126     protected Object JavaDoc[] results = new Object JavaDoc[nbMaxOfResults];
127
128     /** Index of the 1st free cell in results. */
129     protected int firstFreeCell = 0;
130
131     /**
132      * Return the requested result.
133      *
134      * @param index the result index
135      * @return the requested result
136      */

137     public Object JavaDoc getResult( Integer JavaDoc index ) {
138         int ind = index.intValue();
139         Object JavaDoc result = results[ind];
140       
141         // Deference the result in the array.
142
// Once the result have been requested, if we do not set the cell to null,
143
// we may prevent the result object from being garbage collected.
144
results[ind] = null;
145       
146         return result;
147     }
148
149 }
150
Popular Tags