org/objectweb/proactive/core/body/BodyForwarderImpl.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.body;
00032 
00033 import java.io.IOException;
00034 import java.security.PublicKey;
00035 import java.security.cert.X509Certificate;
00036 import java.util.ArrayList;
00037 import java.util.HashMap;
00038 
00039 import org.objectweb.proactive.Body;
00040 import org.objectweb.proactive.core.UniqueID;
00041 import org.objectweb.proactive.core.body.ft.internalmsg.FTMessage;
00042 import org.objectweb.proactive.core.body.future.FuturePool;
00043 import org.objectweb.proactive.core.body.reply.Reply;
00044 import org.objectweb.proactive.core.body.request.Request;
00045 import org.objectweb.proactive.core.component.request.Shortcut;
00046 import org.objectweb.proactive.core.exceptions.NonFunctionalException;
00047 import org.objectweb.proactive.core.exceptions.manager.NFEListener;
00048 import org.objectweb.proactive.core.runtime.ProActiveRuntimeForwarderImpl;
00049 import org.objectweb.proactive.core.runtime.ProActiveRuntimeImpl;
00050 import org.objectweb.proactive.ext.security.Communication;
00051 import org.objectweb.proactive.ext.security.SecurityContext;
00052 import org.objectweb.proactive.ext.security.crypto.KeyExchangeException;
00053 import org.objectweb.proactive.ext.security.exceptions.RenegotiateSessionException;
00054 import org.objectweb.proactive.ext.security.exceptions.SecurityNotAvailableException;
00055 
00056 
00067 public class BodyForwarderImpl implements UniversalBodyForwarder {
00068 
00070     private HashMap<UniqueID, BodyAdapter> bodies;
00071 
00077     private HashMap<UniqueID, Object> createdBodies;
00078 
00079     //
00080     // -- CONSTRUCTORS -----------------------------------------------
00081     //
00082     public BodyForwarderImpl() {
00083         bodies = new HashMap<UniqueID, BodyAdapter>();
00084         createdBodies = new HashMap<UniqueID, Object>();
00085     }
00086 
00091     public synchronized void add(BodyAdapter rBody) {
00092         
00093         if (!bodies.containsKey(rBody.getID())) {
00094             // We do not want to overwrite an already existing entry. 
00095             // Not checking that this entry do not already exist can lead to
00096             // a deadlock
00097             bodies.put(rBody.getID(), rBody);
00098         }
00099     }
00100 
00105     public synchronized void addcreatedBody(UniqueID id) {
00106         createdBodies.put(id, null);
00107     }
00108 
00109     //
00110     // --- Method call forwarding
00111     //
00112 
00114     public void createShortcut(UniqueID id, Shortcut shortcut)
00115         throws IOException {
00116         BodyAdapter rbody = bodies.get(id);
00117         if (rbody != null) {
00118             rbody.createShortcut(shortcut);
00119         } else {
00120             throw new IOException("No BodyAdapter associated to id=" + id);
00121         }
00122     }
00123 
00125     public void disableAC(UniqueID id) throws IOException {
00126         BodyAdapter rbody =  bodies.get(id);
00127         if (rbody != null) {
00128             rbody.disableAC();
00129         } else {
00130             throw new IOException("No BodyAdapter associated to id=" + id);
00131         }
00132     }
00133 
00135     public void enableAC(UniqueID id) throws IOException {
00136         BodyAdapter rbody =  bodies.get(id);
00137         if (rbody != null) {
00138             rbody.enableAC();
00139         } else {
00140             throw new IOException("No BodyAdapter associated to id=" + id);
00141         }
00142     }
00143 
00145     public X509Certificate getCertificate(UniqueID id)
00146         throws SecurityNotAvailableException, IOException {
00147         BodyAdapter rbody =  bodies.get(id);
00148         if (rbody != null) {
00149             return rbody.getCertificate();
00150         } else {
00151             throw new IOException("No BodyAdapter associated to id=" + id);
00152         }
00153     }
00154 
00156     public byte[] getCertificateEncoded(UniqueID id)
00157         throws SecurityNotAvailableException, IOException {
00158         BodyAdapter rbody =  bodies.get(id);
00159         if (rbody != null) {
00160             return rbody.getCertificateEncoded();
00161         } else {
00162             throw new IOException("No BodyAdapter associated to id=" + id);
00163         }
00164     }
00165 
00167     public ArrayList getEntities(UniqueID id)
00168         throws SecurityNotAvailableException, IOException {
00169         BodyAdapter rbody =  bodies.get(id);
00170         if (rbody != null) {
00171             return rbody.getEntities();
00172         } else {
00173             throw new IOException("No BodyAdapter associated to id=" + id);
00174         }
00175     }
00176 
00178     public UniqueID getID(UniqueID id) {
00179         BodyAdapter rbody =  bodies.get(id);
00180         if (rbody != null) {
00181             return rbody.getID();
00182         } else {
00183             bodyLogger.info(
00184                 "Cannot retieve associated BodyAdapter: Invalid ID " + id);
00185             return null;
00186         }
00187     }
00188 
00190     public String getNodeURL(UniqueID id) {
00191         BodyAdapter rbody =  bodies.get(id);
00192         if (rbody != null) {
00193             return rbody.getNodeURL();
00194         } else {
00195             bodyLogger.info(
00196                 "Cannot retieve associated BodyAdapter: Invalid ID " + id);
00197             return null;
00198         }
00199     }
00200 
00202     public SecurityContext getPolicy(UniqueID id,
00203         SecurityContext securityContext)
00204         throws SecurityNotAvailableException, IOException {
00205         BodyAdapter rbody =  bodies.get(id);
00206         if (rbody != null) {
00207             return rbody.getPolicy(securityContext);
00208         } else {
00209             throw new IOException("No BodyAdapter associated to id=" + id);
00210         }
00211     }
00212 
00214     public PublicKey getPublicKey(UniqueID id)
00215         throws SecurityNotAvailableException, IOException {
00216         BodyAdapter rbody =  bodies.get(id);
00217         if (rbody != null) {
00218             return rbody.getPublicKey();
00219         } else {
00220             throw new IOException("No BodyAdapter associated to id=" + id);
00221         }
00222     }
00223 
00225     public BodyAdapter getRemoteAdapter(UniqueID id) {
00226         BodyAdapter rbody =  bodies.get(id);
00227         ProActiveRuntimeForwarderImpl partf = (ProActiveRuntimeForwarderImpl) ProActiveRuntimeImpl.getProActiveRuntime();
00228         return new BodyAdapterForwarder(partf.getBodyAdapterForwarder(), rbody);
00229     }
00230 
00232     public byte[][] publicKeyExchange(UniqueID id, long sessionID,
00233         byte[] my_pub, byte[] my_cert, byte[] sig_code)
00234         throws SecurityNotAvailableException, RenegotiateSessionException,
00235             KeyExchangeException, IOException {
00236         BodyAdapter rbody =  bodies.get(id);
00237         if (rbody != null) {
00238             return rbody.publicKeyExchange(sessionID, my_pub, my_cert, sig_code);
00239         } else {
00240             throw new IOException("No BodyAdapter associated to id=" + id);
00241         }
00242     }
00243 
00245     public Object receiveFTMessage(UniqueID id, FTMessage ev)
00246         throws IOException {
00247         BodyAdapter rbody =  bodies.get(id);
00248         if (rbody != null) {
00249             return rbody.receiveFTMessage(ev);
00250         } else {
00251             throw new IOException("No BodyAdapter associated to id=" + id);
00252         }
00253     }
00254 
00256     public int receiveReply(UniqueID id, Reply r) throws IOException {
00257         // a FuturProxy can be present inside r and must know if it is running
00258         // on a body forwarder or not. See FuturProxy.writeObject
00259         FuturePool.addMeAsBodyForwarder();
00260         try {
00261             BodyAdapter rbody =  bodies.get(id);
00262             if (rbody != null) {
00263                 return rbody.receiveReply(r);
00264             } else {
00265                 throw new IOException("No BodyAdapter associated to id=" + id +
00266                     " request=" + r);
00267             }
00268         } finally {
00269             FuturePool.removeMeFromBodyForwarders();
00270         }
00271     }
00272 
00274     public int receiveRequest(UniqueID id, Request request)
00275         throws IOException, RenegotiateSessionException {
00276         // a FuturProxy can be present inside r and must know if it is running
00277         // on a body forwarder or not. See FuturProxy.writeObject
00278         FuturePool.addMeAsBodyForwarder();
00279         try {
00280             BodyAdapter rbody =  bodies.get(id);
00281             if (rbody != null) {
00282                 return rbody.receiveRequest(request);
00283             } else {
00284                 throw new IOException("No BodyAdapter associated to id=" + id);
00285             }
00286         } finally {
00287             FuturePool.removeMeFromBodyForwarders();
00288         }
00289     }
00290 
00292     public void removeImmediateService(UniqueID id, String methodName,
00293         Class[] parametersTypes) throws IOException {
00294         BodyAdapter rbody =  bodies.get(id);
00295         if (rbody != null) {
00296             rbody.removeImmediateService(methodName, parametersTypes);
00297         } else {
00298             throw new IOException("No BodyAdapter associated to id=" + id);
00299         }
00300     }
00301 
00303     public byte[][] secretKeyExchange(UniqueID id, long sessionID, byte[] tmp,
00304         byte[] tmp1, byte[] tmp2, byte[] tmp3, byte[] tmp4)
00305         throws SecurityNotAvailableException, RenegotiateSessionException,
00306             IOException {
00307         BodyAdapter rbody =  bodies.get(id);
00308         if (rbody != null) {
00309             return rbody.secretKeyExchange(sessionID, tmp, tmp1, tmp2, tmp3,
00310                 tmp4);
00311         } else {
00312             throw new IOException("No BodyAdapter associated to id=" + id);
00313         }
00314     }
00315 
00317     public void setImmediateService(UniqueID id, String methodName,
00318         Class[] parametersTypes) throws IOException {
00319         BodyAdapter rbody =  bodies.get(id);
00320         if (rbody != null) {
00321             rbody.setImmediateService(methodName, parametersTypes);
00322         } else {
00323             throw new IOException("No BodyAdapter associated to id=" + id);
00324         }
00325     }
00326 
00328     public void setImmediateService(UniqueID id, String methodName)
00329         throws IOException {
00330         BodyAdapter rbody =  bodies.get(id);
00331         if (rbody != null) {
00332             rbody.setImmediateService(methodName);
00333         } else {
00334             throw new IOException("No BodyAdapter associated to id=" + id);
00335         }
00336     }
00337 
00339     public long startNewSession(UniqueID id, Communication policy)
00340         throws SecurityNotAvailableException, IOException,
00341             RenegotiateSessionException {
00342         BodyAdapter rbody =  bodies.get(id);
00343         if (rbody != null) {
00344             return rbody.startNewSession(policy);
00345         } else {
00346             throw new IOException("No BodyAdapter associated to id=" + id);
00347         }
00348     }
00349 
00351     public void terminate(UniqueID id) throws IOException {
00352         BodyAdapter rbody =  bodies.get(id);
00353         if (rbody != null) {
00354             // TODO probably remove this AO from our cache
00355             rbody.terminate();
00356         } else {
00357             throw new IOException("No BodyAdapter associated to id=" + id);
00358         }
00359     }
00360 
00362     public void terminateSession(UniqueID id, long sessionID)
00363         throws IOException, SecurityNotAvailableException {
00364         BodyAdapter rbody =  bodies.get(id);
00365         if (rbody != null) {
00366             rbody.terminateSession(sessionID);
00367         } else {
00368             throw new IOException("No BodyAdapter associated to id=" + id);
00369         }
00370     }
00371 
00373     public byte[] randomValue(UniqueID id, long sessionID, byte[] cl_rand)
00374         throws SecurityNotAvailableException, RenegotiateSessionException,
00375             IOException {
00376         BodyAdapter rbody =  bodies.get(id);
00377         if (rbody != null) {
00378             return rbody.randomValue(sessionID, cl_rand);
00379         } else {
00380             throw new IOException("No BodyAdapter associated to id=" + id);
00381         }
00382     }
00383 
00385     public void updateLocation(UniqueID id, UniqueID uid, UniversalBody body)
00386         throws IOException {
00387         BodyAdapter rbody =  bodies.get(id);
00388         if (rbody != null) {
00389             rbody.updateLocation(uid, body);
00390         } else {
00391             throw new IOException("No BodyAdapter associated to id=" + id);
00392         }
00393     }
00394 
00396     public String getJobID(UniqueID id) throws IOException {
00397         BodyAdapter rbody =  bodies.get(id);
00398         if (rbody != null) {
00399             return rbody.getJobID();
00400         } else {
00401             throw new IOException("No BodyAdapter associated to id=" + id);
00402         }
00403     }
00404 
00406     public void addNFEListener(UniqueID id, NFEListener listener)
00407         throws IOException {
00408         BodyAdapter rbody =  bodies.get(id);
00409         if (rbody != null) {
00410             rbody.addNFEListener(listener);
00411         } else {
00412             throw new IOException("No BodyAdapter associated to id=" + id);
00413         }
00414     }
00415 
00417     public void removeNFEListener(UniqueID id, NFEListener listener)
00418         throws IOException {
00419         BodyAdapter rbody =  bodies.get(id);
00420         if (rbody != null) {
00421             rbody.removeNFEListener(listener);
00422         } else {
00423             throw new IOException("No BodyAdapter associated to id=" + id);
00424         }
00425     }
00426 
00428     public void changeProxiedBody(UniqueID id, Body newBody)
00429         throws IOException {
00430         BodyAdapter rbody =  bodies.get(id);
00431         if (rbody != null) {
00432             rbody.changeProxiedBody(newBody);
00433         } else {
00434             throw new IOException("No BodyAdapter associated to id=" + id);
00435         }
00436     }
00437 
00439     public int fireNFE(UniqueID id, NonFunctionalException e)
00440         throws IOException {
00441         BodyAdapter rbody =  bodies.get(id);
00442         if (rbody != null) {
00443             return rbody.fireNFE(e);
00444         } else {
00445             throw new IOException("No BodyAdapter associated to id=" + id);
00446         }
00447     }
00448 
00450     public UniversalBody lookup(UniqueID id, String url)
00451         throws IOException {
00452         BodyAdapter rbody =  bodies.get(id);
00453         if (rbody != null) {
00454             return rbody.lookup(url);
00455         } else {
00456             throw new IOException("No BodyAdapter associated to id=" + id);
00457         }
00458     }
00459 
00461     public void register(UniqueID id, String url) throws IOException {
00462         BodyAdapter rbody =  bodies.get(id);
00463         if (rbody != null) {
00464             rbody.register(url);
00465         } else {
00466             throw new IOException("No BodyAdapter associated to id=" + id);
00467         }
00468     }
00469 
00471     public void unregister(UniqueID id, String url) throws IOException {
00472         BodyAdapter rbody =  bodies.get(id);
00473         if (rbody != null) {
00474             rbody.unregister(url);
00475         } else {
00476             throw new IOException("No BodyAdapter associated to id=" + id);
00477         }
00478     }
00479 }

Generated on Mon Jan 22 15:16:05 2007 for ProActive by  doxygen 1.5.1