KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

50    
51    public StubWrapper(AspectComponent ac, RemoteRef remoteRef) {
52       super(ac);
53       this.remoteRef = remoteRef;
54    }
55
56    /**
57     * A more user-friendly constructor.
58     *
59     * @param serverContainer the name of the container where the
60     * server is deployed (can be a regular expression) */

61
62    public StubWrapper(AspectComponent ac, String JavaDoc serverContainer) {
63       super(ac);
64       this.serverContainer = serverContainer;
65       Topology t = Topology.getPartialTopology( serverContainer );
66       if( t!=null && t.countContainers()>0 ) {
67          this.serverContainer=t.getContainer(0).getName();
68       } else {
69          this.serverContainer = serverContainer;
70       }
71    }
72    
73    String JavaDoc serverContainer = null;
74    
75    /** The remote reference attached to this stub */
76
77    protected RemoteRef remoteRef;
78    
79    
80    /**
81     * The getter method for the remoteRef field.
82     *
83     * @return the remoteRef field
84     */

85     
86    public RemoteRef getRemoteRef() { return remoteRef; }
87    
88    
89    /**
90     * Forward a call to the remote reference.
91     */

92    
93    public Object JavaDoc _invoke(Interaction interaction) {
94
95       if( remoteRef == null ) {
96          if( serverContainer == null ) {
97             logger.warn("local call (1) for stub "+interaction.wrappee);
98             return proceed(interaction);
99          }
100          RemoteContainer rc = Topology.get().getFirstContainer(serverContainer);
101          if( rc == null ) {
102             logger.warn("local call (2) for stub "+interaction.wrappee);
103             return proceed(interaction);
104          }
105          remoteRef = rc.bindTo(NameRepository.get().getName(interaction.wrappee));
106          if( remoteRef == null ) {
107             logger.warn("local call (3) for stub "+interaction.wrappee+
108                         " ("+rc+","+serverContainer+")");
109             return proceed(interaction);
110          }
111       }
112
113       logger.debug(interaction.wrappee + " forwards to the server");
114    
115       /** Invoke the remote reference */
116
117       return remoteRef.invoke(interaction.method.getName(), interaction.args);
118    }
119
120    public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
121        return _invoke((Interaction) invocation);
122    }
123
124    public Object JavaDoc construct(ConstructorInvocation invocation)
125        throws Throwable JavaDoc {
126        throw new Exception JavaDoc("Wrapper "+this+" does not support construction interception.");
127    }
128
129 }
130
131
132
133
134
135
136
137
Popular Tags