001    /*
002      Copyright (C) 2001 Renaud Pawlak
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 General 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.aspects.distribution;
019    
020    
021    import java.util.*;
022    import org.apache.log4j.Logger;
023    import org.objectweb.jac.core.*;
024    import org.objectweb.jac.core.dist.*;
025    import org.objectweb.jac.util.*;
026    
027    /**
028     * This aspect component implements a remote access aspect.
029     *
030     * <p>On contrary to the deployement AC, JAC do not have to be lauched
031     * in a distributed mode and the server host do not have to be
032     * registered in the topology. Hence, this aspect allows a JAC
033     * container to bind to a different namespace/aspect-space in a
034     * client/server mode.
035     * 
036     * @author <a href="http://cedric.cnam.fr/~pawlak/index-english.html">Renaud Pawlak</a>
037     *
038     * @see RemoteAccessConf
039     * @see DeploymentRule */
040    
041    public class RemoteAccessAC extends AspectComponent implements RemoteAccessConf {
042        static Logger logger = Logger.getLogger("remoteaccess");
043        static Logger loggerSerial = Logger.getLogger("serialization");
044    
045        public void whenSerialized ( SerializedJacObject finalObject ) {
046            
047            loggerSerial.debug("DeploymentAC.whenSerialize");
048            NameRepository nr = (NameRepository) NameRepository.get();
049            Wrappee orgObject = (Wrappee)attr( "orgObject" );
050            String name = nr.getName( orgObject );
051            //if ( ! isMatchingARule( orgObject ) ) {
052            loggerSerial.debug(name + " is not matching any rule");
053            finalObject.disableForwarding();
054            Object[] state = ObjectState.getState(orgObject);
055            String[] fieldsName = (String[]) state[0];
056            Object[] fieldsValue = (Object[]) state[1];
057          
058            for ( int i = 0; i < fieldsName.length; i++ ) {
059                loggerSerial.debug(" serializing field "+fieldsName[i]+
060                                   " - "+fieldsValue[i]); 
061                finalObject.addField( fieldsName[i], fieldsValue[i] );
062            }
063             
064            /*} else {
065              DeploymentRule r = getMatchingRule( orgObject );
066              if( r != null && r.getType().equals( "dynamic client-server" ) ) {
067              finalObject.disableForwarding();
068              finalObject.setACInfos( 
069              ACManager.get().getName( this ),
070              RemoteRef.create( NameRepository.get().getName( orgObject ), orgObject ) );
071              }
072              }*/
073        }
074    
075        /**
076         * Fill the field values when the forwarding is disabled for this
077         * object..
078         *
079         * @param orgObject the JAC object that is being deserialized. */
080       
081        public void whenDeserialized ( SerializedJacObject orgObject ) {
082            loggerSerial.debug("DeploymentAC.whenDeserialize");
083            if ( ! orgObject.isForwarder() ) {
084                loggerSerial.debug("not a forwarder");
085                Wrappee finalObject = (Wrappee)attr("finalObject");
086                RemoteRef server;
087                if( (server = (RemoteRef) orgObject.getACInfos( ACManager.get().getName( this ) ) ) != null ) {
088                    Wrapping.wrapAll(finalObject,null,new StubWrapper(this,server));
089                } else {
090                    Iterator it = orgObject.getFields().keySet().iterator();
091                    while ( it.hasNext() ) {
092                        String fn = (String) it.next();
093                        loggerSerial.debug("setting field "+fn+" <- "+
094                                           orgObject.getField( fn ));
095                        ObjectState.setFieldValue(finalObject,fn,
096                                                  ObjectState.getField(orgObject,fn) );
097                    }
098                }
099            }
100        }   
101    
102        /**
103         * 
104         */
105       
106        public void whenCreatingRemoteAccess(Wrappee wrappee, String serverHost) {
107            Wrapping.wrapAll(wrappee,null,new StubWrapper(this,serverHost));
108        }
109    
110        public void createRemoteAccess(String nameExpr, 
111                                       String classExpr, 
112                                       String methodExpr,
113                                       String serverHost) {
114            try {
115                pointcut(nameExpr, classExpr, methodExpr,
116                         StubWrapper.class.getName(), 
117                         new Object[] {serverHost}, "invoke", null, true);
118                //         pointcut( nameExpr, classExpr, 
119                //          "whenCreatingRemoteAccess", new Object[] {serverHost} );
120            } catch ( Exception e ) {
121                logger.error("createRemoteAccess: pointcut creation failed",e);
122            }
123        }
124    
125    }