001    /*
002      Copyright (C) 2001-2002 Renaud Pawlak, 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 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.core.dist;
019    
020    
021    import org.objectweb.jac.core.AspectComponent;
022    import org.objectweb.jac.core.NameRepository;
023    import org.objectweb.jac.core.Wrappee;
024    import org.objectweb.jac.core.Wrapping;
025    import java.io.Serializable;
026    
027    /**
028     * This class deploies a set of objects on a set of host given a
029     * deployment mapping style (a function that gives the objects
030     * location in the topology).
031     * 
032     * <p>It can be used by an aspect component that would implement a
033     * distribution aspect.
034     *
035     * @author <a href="http://cedric.cnam.fr/~pawlak/index-english.html">Renaud Pawlak</a>
036     * @author <a href="http://www-src.lip6.fr/homepages/Lionel.Seinturier/index-eng.html">Lionel Seinturier</a> */
037    
038    public class Deployment implements Serializable {
039    
040       /** Store the topology. */
041       Topology topology;
042    
043       transient AspectComponent ac;
044       
045    
046       /**
047        * Creates a new deployment. For the moment, only the MS_ONE2ONE is
048        * available. Anyway, it is the most usual strategy.
049        *
050        * @param topology the topology used by the deployment
051        */
052       public Deployment(AspectComponent ac, Topology topology) {
053          this.topology = topology;
054          this.ac = ac;
055       }
056    
057       /**
058        * Deploys a set of JAC object that are centralized on the local
059        * host to the Topology.<p>
060        *
061        * Do not copy the state of the objects.
062        * 
063        * @param objects a set of object to deploy on the topology
064        * @param forward if true, create a stub wrapper on each local
065        * object that forwards all the calls to the remotly deployed
066        * objects
067        * @return remote references on the deployed objects
068        *
069        * @see #deploy(Object[],boolean) */
070    
071       public RemoteRef[] deployStruct(Object[] objects, boolean forward) {
072          
073          RemoteContainer[] rcs = topology.getDistContainers ();
074          RemoteRef[] ret = new RemoteRef[objects.length];
075    
076          for (int i = 0; i < objects.length; i++) {
077    
078             System.out.println( "Deploying " + 
079                                 NameRepository.get().getName(objects[i]) + 
080                                 " on " + rcs[i].getName());
081             RemoteRef rr = RemoteRef.create( 
082                NameRepository.get().getName( objects[i] ) );
083    
084             // THIS COMMENT MAY CAUSE REGRESSION
085             /*Collaboration.get().setCurApp(
086                ApplicationRepository
087                .getOwningApplication((Wrappee)objects[i]).getName());*/
088             rr.remoteNew( rcs[i].getName(),
089                           objects[i].getClass().getName() );
090             if ( forward ) {
091                StubWrapper w = new StubWrapper(ac,rr);
092                Wrapping.wrapAll((Wrappee)objects[i],null,w);
093             }
094             ret[i] = rr;
095          }      
096          return ret;
097       }
098    
099       /**
100        * Equals to deployStruct( objects, false ).
101        *
102        * @param objects the object to deploy
103        * @return the array of the remote references pointing on the
104        * deployed objects
105        *
106        * @see #deploy(Object[],boolean) */
107       
108       public RemoteRef[] deployStruct(Object[] objects) {
109          return deployStruct(objects, false);
110       }  
111    
112       /**
113        * Deploys a set of JAC object that are centralized on the local
114        * host to the Topology. This is a static deployment and the
115        * references between the different local objects must have already
116        * been built (it means that the application must have been
117        * initialized in a centralized way.
118        *
119        * @param objects a set of object to deploy on the topology
120        * @param forward if true, create a stub wrapper on each local
121        * object that forwards all the calls to the remotly deployed
122        * objects
123        * @return remote references on the deployed objects
124        *
125        * @see #deployStruct(Object[],boolean) */
126    
127       public RemoteRef[] deploy(Object[] objects, boolean forward) {
128          
129          RemoteRef[] ret = new RemoteRef[objects.length];
130    
131          for (int i = 0; i < objects.length; i++) {
132    
133             System.out.println( "Deploying " + NameRepository.get().getName(objects[i]) + 
134                                 " on " + topology.getContainer(i).getName());
135             RemoteRef rr = RemoteRef.create( NameRepository.get().getName( objects[i] ) );
136             
137             // THIS COMMENT MAY CAUSE REGRESSION
138             /*Collaboration.get().setCurApp(
139                ApplicationRepository
140                .getOwningApplication((Wrappee)objects[i]).getName());*/
141             rr.remoteNewWithCopy( topology.getContainer(i).getName(),
142                                   objects[i].getClass().getName(),
143                                   objects[i] );
144             if ( forward ) {
145                StubWrapper w = new StubWrapper(ac,rr);
146                Wrapping.wrapAll((Wrappee)objects[i],null,w);
147             }
148             ret[i] = rr;
149          }      
150          return ret;
151       }
152    
153       /**
154        * Equals to deploy( objects, false ).
155        *
156        * @param objects the object to deploy
157        * @return the array of the remote references pointing on the
158        * deployed objects
159        *
160        * @see #deploy(Object[],boolean) */
161       
162       public RemoteRef[] deploy(Object[] objects) {
163          return deploy (objects, false);
164       }
165    
166       /**
167        * Replicates a JAC object that is located on the local
168        * host to the hosts of the Topology.
169        *
170        * @param object the local object to replicate
171        * @param forwardTo the index of the topology where the local
172        * object will forward the calls (if -1 or not valid for the
173        * topology then do not forward any call)
174        * @return remote references on the replicated objects
175        *
176        * @see #replicateStruct(Object,int) */
177    
178       public RemoteRef[] replicate(Object object, int forwardTo) {
179    
180          RemoteContainer[] rcs = topology.getDistContainers ();
181          RemoteRef[] ret = new RemoteRef[rcs.length];
182          RemoteRef rr = null;
183          String name = NameRepository.get().getName(object);
184    
185          for (int i = 0; i < rcs.length; i++) {
186    
187             System.out.println( "Replicating " + name + 
188                                 " on " + rcs[i].getName());
189             
190             if( rcs[i].bindTo( name ) != null ) {
191                System.out.println( name + " already replicated on "
192                                    + rcs[i].getName());
193                continue; 
194             }
195    
196             // THIS COMMENT MAY CAUSE REGRESSION
197    
198             /*         Collaboration.get().setCurApp(
199                ApplicationRepository.
200                getOwningApplication((Wrappee)object).getName());*/
201    
202             rr = RemoteRef.create( name );
203             rr.remoteNewWithCopy( rcs[i].getName(),
204                                   object.getClass().getName(),
205                                   object );
206             
207             if ( forwardTo == i ) {
208                StubWrapper w = new StubWrapper(ac,rr);
209                Wrapping.wrapAll((Wrappee)object,null,w);
210             }
211             ret[i] = rr;
212          }
213          return ret;
214       }
215    
216       /**
217        * Equals to replicate( object, -1 ).
218        *
219        * @param object the object to replicate
220        * @return the array of the remote references pointing on the
221        * replicated objects
222        *
223        * @see #replicate(Object,int) */
224    
225       public RemoteRef[] replicate(Object object) {
226          return replicate(object, -1);
227       }
228    
229       /**
230        * Replicates a JAC object that is located on the local
231        * host to the hosts of the Topology.
232        *
233        * <p>Do not copy the objects states.
234        *
235        * @param object the local object to replicate
236        * @param forwardTo the index of the topology where the local
237        * object will forward the calls (if -1 or not valid for the
238        * topology then do not forward any call)
239        * @return remote references on the replicated objects
240        *
241        * @see #replicate(Object,int) */
242    
243       public RemoteRef[] replicateStruct(Object object, int forwardTo) {
244    
245          RemoteContainer[] rcs = topology.getDistContainers ();
246          RemoteRef[] ret = new RemoteRef[rcs.length];
247          RemoteRef rr = null;
248          String name = NameRepository.get().getName(object);
249          
250          for (int i = 0; i < rcs.length; i++) {
251    
252             if( rcs[i].bindTo( name ) != null ) {
253                System.out.println( name + " already replicated on "
254                                    + rcs[i].getName());
255                continue; 
256             }
257    
258             System.out.println( "Replicating (struct) " + 
259                                 NameRepository.get().getName(object) + 
260                                 " on " + rcs[i].getName());
261             
262             // THIS COMMENT MAY CAUSE REGRESSION
263    
264             /*         Collaboration.get().setCurApp(
265                ApplicationRepository
266                .getOwningApplication((Wrappee)object).getName());*/
267    
268             rr = RemoteRef.create( NameRepository.get().getName( object ) );
269             rr.remoteNew( rcs[i].getName(), object.getClass().getName() );
270             if ( forwardTo == i ) {
271                StubWrapper w = new StubWrapper(ac,rr);
272                Wrapping.wrapAll((Wrappee)object,null,w);
273             }
274             ret[i] = rr;
275          }
276          return ret;
277       }
278    
279       /**
280        * Equals to replicateStruct( object, -1 ).
281        *
282        * @param object the object to replicate
283        * @return the array of the remote references pointing on the
284        * replicated objects
285        *
286        * @see #replicateStruct(Object,int) */
287       
288       public RemoteRef[] replicateStruct(Object object) {
289          return replicateStruct (object, -1);
290       }
291    
292    
293    }
294    
295    
296    
297    
298    
299    
300