001    package org.objectweb.jac.aspects.distribution;
002    
003    
004    import org.aopalliance.intercept.ConstructorInvocation;
005    import org.aopalliance.intercept.MethodInvocation;
006    import org.apache.log4j.Logger;
007    import org.objectweb.jac.core.AspectComponent;
008    import org.objectweb.jac.core.Interaction;
009    import org.objectweb.jac.core.Wrapper;
010    import org.objectweb.jac.core.dist.Deployment;
011    import org.objectweb.jac.core.dist.Topology;
012    import org.objectweb.jac.util.Log;
013    
014    /**
015     * This wrapper wraps constructors in order to deploy the objects on
016     * remote host(s) after their initialization. */
017    
018    public class DeploymentWrapper extends Wrapper {
019        static Logger logger = Logger.getLogger("deployment");
020    
021            String hostExpr;
022            boolean state = true;
023    
024            /**
025             * The constructor.
026             *
027             * @param ac the aspect component that owns this wrapper
028             * @param hostExpr a regular expression that gives the host where
029             * the wrapped object should be deployed
030             * @param state a flag that tells if the state of the deployed
031             * object should be copied on the remote host(s) or not */
032    
033            public DeploymentWrapper(
034                    AspectComponent ac,
035                    String hostExpr,
036                    Boolean state) {
037                    super(ac);
038                    this.hostExpr = hostExpr;
039                    this.state = state.booleanValue();
040            }
041    
042            public Object invoke(MethodInvocation invocation) throws Throwable {
043                    throw new Exception("This wrapper does not support invocation wrapping");
044            }
045    
046            public Object construct(ConstructorInvocation invocation)
047                    throws Throwable 
048        {
049                    return deploy((Interaction) invocation);
050            }
051    
052            /**
053             * Actually performs the deployment on a constructor
054             * interaction. */
055    
056            public Object deploy(Interaction i) {
057                    Object o = proceed(i);
058                    logger.debug("deploy upcalled with " + hostExpr
059                         + " wrappee=" + i.wrappee
060                         + ", topology=" + Topology.get());
061                    Topology topology = Topology.getPartialTopology(hostExpr);
062                    Deployment dep = new Deployment(ac, topology);
063                    if (state) {
064                            dep.replicate(i.wrappee);
065                    } else {
066                            dep.replicateStruct(i.wrappee);
067                    }
068                    return o;
069            }
070    }