001    /*
002      Copyright (C) 2001 Lionel Seinturier
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser Generaly Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
017    
018    package org.objectweb.jac.core.dist.rmi;
019    
020    import java.io.Serializable;
021    import java.rmi.RemoteException;
022    import org.apache.log4j.Logger;
023    import org.objectweb.jac.core.dist.RemoteContainer;
024    import org.objectweb.jac.core.dist.RemoteRef;
025    
026    /**
027     * RMIRemoteContainerStub acts as a client stub to access a remote container.
028     *
029     * RMIRemoteContainerStub holds a RMIRemoteContainer_Stub instance.
030     * This is the client stub of the remote RMIRemoteContainer object that owns as
031     * a delegate the container that is to be accessed.
032     *
033     * Note: what we need is an instance of something that extends RemoteContainer.
034     * But we can't have an object that is both a client stub for a remote RMI
035     * object and a RemoteContainer (no multiple inheritance in Java).
036     * So we implemented this delegating scheme where:
037     * <ul>
038     * <li> RMIRemoteContainerStub (which is a RemoteContainer) delegates its job
039     *   to a RMIRemoteContainer client stub (generated by rmic) </li>
040     * <li> which itself transmits it to a remote RMIRemoteContainer object, </li>
041     * <li> which itself delegates it to a final RemoteContainer object. </li>
042     * </ul>
043     *
044     * @see org.objectweb.jac.core.dist.rmi.RMIRemoteContainer
045     *
046     * @author <a href="http://www-src.lip6.fr/homepages/Lionel.Seinturier/index-eng.html">Lionel Seinturier</a>
047     */
048     
049    public class RMIRemoteContainerStub
050        extends RemoteContainer implements Serializable 
051    {
052        static Logger logger = Logger.getLogger("dist.rmi");
053    
054        /**
055         * The RMI stub where the job is to be delegated.
056         */
057       
058        protected RMIRemoteContainerInterf delegate;
059       
060    
061        /**
062         * Create a new remote container stub.
063         *
064         * @param delegate  the stub where the job is to be delegated
065         * @param name      the name of the remote container
066         */
067       
068        public RMIRemoteContainerStub(RMIRemoteContainerInterf delegate,
069                                      String name)
070        {
071            this.delegate = delegate;
072            this.name = name;
073        }
074       
075    
076        /**
077         * This method instantiates a className object.
078         * Clients call it to remotely instantiate an object.
079         * instantiates creates an object and returns its index.
080         *
081         * @param className  the class name to instantiate
082         * @param args       initialization arguments for the instantiation
083         * @param fields     the object fields that are part of the state
084         * @param state      the state to copy
085         * @param collaboration the collaboration of the client
086         * @return           the index of the className object
087         */
088       
089        public int instantiates(String name, String className, Object[] args,
090                                String[] fields, byte[] state,
091                                byte[] collaboration) 
092        {
093            logger.debug(this.name+".instantiates("+name+")");
094            try {
095                return delegate.instantiates(name, className, args, fields, state, 
096                                             collaboration);
097            } catch (RemoteException e) { 
098                e.printStackTrace(); 
099            }
100          
101            return 0;
102        }
103    
104    
105        /**
106         * Copy a state into a base object.
107         *
108         * @param index   the callee index (see org.objectweb.jac.core.JacObject)
109         * @param fields  the object fields that are part of the state
110         * @param state   the state to copy
111         * @param collaboration the collaboration of the client
112         */
113        
114        public void copy(String name, int index, String[] fields, byte[] state,
115                         byte[] collaboration) {
116    
117            try {
118                delegate.copy( name, index, fields, state, collaboration );
119            } catch(RemoteException e) { 
120                e.printStackTrace(); 
121            }
122        }
123       
124       
125        /**
126         * Invoke a method on a base object.
127         *
128         * The base object is the remote counterpart of a local object
129         * that has been remotely instantiated by the org.objectweb.jac.dist.Distd daemon.
130         *
131         * @param index       the callee index (see org.objectweb.jac.core.JacObject)
132         * @param methodName  the callee method name
133         * @param methodArgs  the callee method arguments
134         * @return            the result
135         */
136       
137        public byte[] invoke(int index, String methodName, 
138                             byte[] methodArgs, byte[] collaboration) {
139          
140            try {
141                return delegate.invoke(index, methodName, methodArgs, collaboration);
142            } catch(RemoteException e) { 
143                e.printStackTrace(); 
144            }
145          
146            return null;
147        }
148    
149        public byte[] invokeRoleMethod(int index, 
150                                       String methodName, 
151                                       byte[] methodArgs, 
152                                       byte[] collaboration) 
153        {
154            try {
155                return delegate.invokeRoleMethod(index,methodName,
156                                                 methodArgs,collaboration);
157            } catch(RemoteException e) { 
158                e.printStackTrace(); 
159            }
160            return null;
161        }
162    
163        public byte[] getByteCodeFor(String className) {
164            try {
165                return delegate.getByteCodeFor(className);
166            } catch(RemoteException e) { 
167                e.printStackTrace(); 
168            }
169            return null;
170        }
171    
172        /**
173         * Returns a remote reference on the object corresponding to the
174         * given name. */
175    
176        public RemoteRef bindTo(String name) {
177            RemoteRef result=null;
178            try {
179                logger.debug("binding to "+name+" (delegate="+delegate+")");
180                result=delegate.bindTo( name );
181                logger.debug("result is "+result);
182            } catch(RemoteException e) { 
183                e.printStackTrace(); 
184            }
185            return result;
186        }
187    
188    
189        //    /**
190        //     * Get a client stub wrapping chain for a given object.
191        //     *
192        //     * This method is called whenever a daemon receives as a parameter
193        //     * a reference to a remote object, to get the wrapping chain
194        //     * (for instance an authentication wrapper, a verbose wrapper, ...)
195        //     * needed to create a client stub for this remote reference.
196        //     *
197        //     * @param index  the base object index (see org.objectweb.jac.core.JacObject)
198        //     * @return       the client stub wrapping chain as a serialized object
199        //     */
200        //    
201        //    public Vector getClientStubWrappingChain( int index ) {
202        //    
203        //       try {
204        //          return delegate.getClientStubWrappingChain( index );
205        //       }
206        //       catch( RemoteException e ) { e.printStackTrace(); }
207        //       
208        //       return null;
209        //    }
210    
211    }