KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > distribution > DeploymentAC


1 /*
2   Copyright (C) 2001-2003 Renaud Pawlak
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.aspects.distribution;
19
20
21 //import java.util.*;
22
import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import org.apache.log4j.Logger;
25 import org.objectweb.jac.core.*;
26 import org.objectweb.jac.core.dist.*;
27 import org.objectweb.jac.core.rtti.ClassItem;
28 import org.objectweb.jac.core.rtti.ClassRepository;
29 import org.objectweb.jac.core.rtti.FieldItem;
30 import org.objectweb.jac.core.rtti.MethodItem;
31 import org.objectweb.jac.util.*;
32
33 /**
34  * This aspect component implements a generic deployment aspect.
35  *
36  * @author <a HREF="http://cedric.cnam.fr/~pawlak/index-english.html">Renaud Pawlak</a>
37  *
38  * @see DeploymentConf
39  * @see DeploymentRule */

40
41 public class DeploymentAC extends AspectComponent implements DeploymentConf {
42     static Logger logger = Logger.getLogger("deployment");
43     static Logger loggerSerial = Logger.getLogger("serialization");
44
45     /** the name of the metadata that tells whether the parameters of
46      * a methods are references (TRUE) or values (FALSE). */

47     public static final String JavaDoc REFS= "DeploymentAC.REFS";
48
49    /** already deployed objects */
50    transient HashSet JavaDoc treated = new HashSet JavaDoc();
51
52     /**
53      * Force the treatment of the objects that are being
54      * serialized. This allows the deployment aspect to be sure that
55      * an object that is passed as a parameter of a remote invocation
56      * is already deployed when arriving on the new site. If none of
57      * the deployment rule can be applied on the serialized object,
58      * then the object will not be a forwarder but a copy of the
59      * original object.
60      *
61      * @param finalObject the structure to store the serialized infos
62      */

63
64    public Wrappee whenSerialized(Wrappee orgObject,
65                                  SerializedJacObject finalObject) {
66     
67        NameRepository nr = (NameRepository)NameRepository.get();
68        String JavaDoc name = nr.getName(orgObject);
69        loggerSerial.debug("DeploymentAC.whenSerialize " + name);
70        //if ( ! isMatchingARule( orgObject ) ) {
71
finalObject.disableForwarding();
72        
73        if (Collaboration.get().getAttribute(SerializedJacObject.STATELESS)==null) {
74        
75        Object JavaDoc[] state = ObjectState.getState(orgObject);
76        String JavaDoc[] fieldsName = (String JavaDoc[])state[0];
77        Object JavaDoc[] fieldsValue = (Object JavaDoc[])state[1];
78        
79        ClassItem cl = ClassRepository.get().getClass(orgObject.getClass());
80        for (int i = 0; i < fieldsName.length; i++) {
81            loggerSerial.debug(" serializing field " + fieldsName[i] + " - " + fieldsValue[i]);
82            FieldItem fi = cl.getField(fieldsName[i]);
83            if (fi != null && fi.getAttribute("deployment.transient") == null) {
84            finalObject.addField(fieldsName[i], fieldsValue[i]);
85            }
86        }
87        
88        } else {
89        loggerSerial.debug(name+" is stateless in this context");
90        }
91        
92        /*} else {
93      DeploymentRule r = getMatchingRule( orgObject );
94      if( r != null && r.getType().equals( "dynamic client-server" ) ) {
95      finalObject.disableForwarding();
96      finalObject.setACInfos(
97      ACManager.get().getName( this ),
98      RemoteRef.create( NameRepository.get().getName( orgObject ), orgObject ) );
99      }
100      }*/

101        return orgObject;
102    }
103
104    /**
105     * Fill the field values when the forwarding is disabled for this
106     * object..
107     *
108     * @param orgObject the JAC object that is being deserialized. */

109    
110    public Wrappee whenDeserialized(SerializedJacObject orgObject,
111                                    Wrappee finalObject) {
112       loggerSerial.debug("DeploymentAC.whenDeserialize "+
113                 orgObject.getJacObjectClassName());
114       if (!orgObject.isForwarder()) {
115          loggerSerial.debug("not a forwarder, deserialize...");
116          RemoteRef server =
117             (RemoteRef)orgObject.getACInfos(ACManager.get().getName(this));
118          if (server != null) {
119             Wrapping.wrapAll(finalObject,null,
120                              new StubWrapper(this,server));
121          } else {
122             Iterator JavaDoc it = orgObject.getFields().keySet().iterator();
123             while (it.hasNext()) {
124                String JavaDoc fn = (String JavaDoc)it.next();
125                loggerSerial.debug("setting field "+fn+" <- "+
126                                   orgObject.getField(fn));
127                ObjectState.setFieldValue(
128                   finalObject,fn,orgObject.getField(fn));
129             }
130          }
131       } else {
132          loggerSerial.debug("forwarder, do not deserialize...");
133       }
134       return finalObject;
135    }
136
137    /*
138     * Actually deploys an object with a target host expression.
139     *
140     * @param wrappee the object to deploy
141     * @param hostExpr the host where to deploy the object
142     * @param state ???
143     */

144
145    /*public void deployTo(Wrappee wrappee, String hostExpr, boolean state) {
146       if (wrappee==null)
147          return;
148       if (treated.contains(wrappee))
149          return;
150       treated.add(wrappee);
151       Log.trace("deployment",
152                 "DEPLOYED UPCALLED WITH "+hostExpr+
153                 " wrappee="+wrappee+", topology="+Topology.get());
154       Topology topology = Topology.getPartialTopology(hostExpr);
155       Deployment dep = new Deployment(this,topology);
156       if (state) {
157          dep.replicate(wrappee);
158       } else {
159          dep.replicateStruct(wrappee);
160       }
161       }*/

162
163    public void deploy(String JavaDoc deploymentHost,
164                       String JavaDoc nameRegexp, String JavaDoc containerName) {
165
166       pointcut(nameRegexp,".* && !org.objectweb.jac.lib.java.util.*","CONSTRUCTORS",
167                "org.objectweb.jac.aspects.distribution.DeploymentWrapper",
168                new Object JavaDoc[] {this,containerName,new Boolean JavaDoc(true)},
169                deploymentHost,null,true);
170
171       // addDeploymentRule( new DeploymentRule(
172
// "deployment", nameRegexp, containerName, true ) );
173

174    }
175
176    public void replicate(String JavaDoc deploymentHost, String JavaDoc nameRegexp,
177                          String JavaDoc contRegexp) {
178
179       pointcut(nameRegexp,".* && !org.objectweb.jac.lib.java.util.*","CONSTRUCTORS",
180                "org.objectweb.jac.aspects.distribution.DeploymentWrapper",
181                new Object JavaDoc[] {this,contRegexp,new Boolean JavaDoc(true)},
182                deploymentHost,null,true);
183    }
184
185    public void createTypedStubsFor(String JavaDoc name, String JavaDoc serverHost,
186                                    String JavaDoc hosts,
187                                    String JavaDoc stubType) {
188       pointcut(name,".* && !org.objectweb.jac.lib.java.util.*","CONSTRUCTORS",
189                "org.objectweb.jac.aspects.distribution.DeploymentWrapper",
190                new Object JavaDoc[] {this,hosts,new Boolean JavaDoc(true)},
191                serverHost,null,true);
192       /*pointcut(name, ".*", "deployTo",
193                new Object[] { hosts, new Boolean(false) },
194                serverHost, null);*/

195       try {
196          pointcut(name, ".*", ".*",
197                   (Wrapper)Class.forName(stubType)
198                   .getConstructor(new Class JavaDoc[]{String JavaDoc.class})
199                   .newInstance(new Object JavaDoc[]{serverHost}),
200                   hosts+" && !"+serverHost, null);
201       } catch ( Exception JavaDoc e ) {
202          logger.error("createTypedStubsFor: pointcut creation failed",e);
203       }
204    }
205
206    public void createStubsFor(String JavaDoc name, String JavaDoc serverHost, String JavaDoc hosts) {
207       createTypedStubsFor(name, serverHost, hosts,
208                           "org.objectweb.jac.core.dist.StubWrapper");
209    }
210
211    public void createAsynchronousStubsFor(String JavaDoc name, String JavaDoc serverHost,
212                                           String JavaDoc hosts) {
213       createTypedStubsFor(name, serverHost, hosts,
214                           "org.objectweb.jac.core.dist.NonBlockingStubWrapper");
215    }
216
217    public void setTransient(ClassItem classItem, String JavaDoc fieldName) {
218       FieldItem fi = classItem.getField(fieldName);
219       fi.setAttribute("deployment.transient","true");
220    }
221
222     public void setParametersPassingMode(MethodItem method, Boolean JavaDoc[] refs) {
223     method.setAttribute(REFS, refs);
224     }
225
226 }
227
228
229
Popular Tags