org/objectweb/proactive/core/body/http/HttpRemoteBodyImpl.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.http;
00032 
00033 import java.io.IOException;
00034 import java.security.PublicKey;
00035 import java.security.cert.X509Certificate;
00036 import java.util.ArrayList;
00037 
00038 import org.objectweb.proactive.Body;
00039 import org.objectweb.proactive.core.ProActiveException;
00040 import org.objectweb.proactive.core.UniqueID;
00041 import org.objectweb.proactive.core.body.RemoteBody;
00042 import org.objectweb.proactive.core.body.UniversalBody;
00043 import org.objectweb.proactive.core.body.ft.internalmsg.FTMessage;
00044 import org.objectweb.proactive.core.body.http.util.exceptions.HTTPRemoteException;
00045 import org.objectweb.proactive.core.body.http.util.exceptions.HTTPUnexpectedException;
00046 import org.objectweb.proactive.core.body.http.util.messages.BodyRequest;
00047 import org.objectweb.proactive.core.body.http.util.messages.HttpReply;
00048 import org.objectweb.proactive.core.body.http.util.messages.HttpRequest;
00049 import org.objectweb.proactive.core.body.reply.Reply;
00050 import org.objectweb.proactive.core.body.request.Request;
00051 import org.objectweb.proactive.core.component.request.Shortcut;
00052 import org.objectweb.proactive.core.exceptions.NonFunctionalException;
00053 import org.objectweb.proactive.core.exceptions.manager.NFEListener;
00054 import org.objectweb.proactive.core.rmi.ClassServer;
00055 import org.objectweb.proactive.core.runtime.RuntimeFactory;
00056 import org.objectweb.proactive.ext.security.Communication;
00057 import org.objectweb.proactive.ext.security.SecurityContext;
00058 import org.objectweb.proactive.ext.security.crypto.KeyExchangeException;
00059 import org.objectweb.proactive.ext.security.exceptions.RenegotiateSessionException;
00060 import org.objectweb.proactive.ext.security.exceptions.SecurityNotAvailableException;
00061 import org.objectweb.proactive.ext.security.securityentity.Entity;
00062 
00063 
00074 public class HttpRemoteBodyImpl implements RemoteBody {
00075 
00076     static {
00077         try {
00078             RuntimeFactory.getDefaultRuntime();
00079         } catch (ProActiveException e) {
00080             e.printStackTrace();
00081         }
00082     }
00083 
00088     protected transient UniversalBody body;
00089     UniqueID bodyID;
00090     String jobID;
00091     String url;
00092     private boolean isLocal;
00093 
00094     //    int port; // port is no more necessary .. it is also included in the URL ...
00095     public HttpRemoteBodyImpl(UniversalBody body) {
00096         isLocal = true;
00097         this.body = body;
00098         this.bodyID = body.getID();
00099         this.url = ClassServer.getUrl();
00100         this.jobID = body.getJobID();
00101     }
00102 
00108     public int receiveRequest(Request request)
00109         throws IOException, RenegotiateSessionException {
00110         if (isLocal) {
00111             return body.receiveRequest(request);
00112         } else {
00113             try {
00114                 HttpRequest req = new HttpRequest(request, bodyID, this.url);
00115                 req.send();
00116                 return req.getReturnedObject();
00117             } catch (Exception e) {
00118                 e.printStackTrace();
00119             }
00120             return 0;
00121         }
00122     }
00123 
00127     public int receiveReply(Reply reply) throws IOException {
00128         if (isLocal) {
00129             return body.receiveReply(reply);
00130         } else {
00131             try {
00132                 HttpReply rep = new HttpReply(reply, bodyID, this.url);
00133                 rep.send();
00134 
00135                 return rep.getReturnedObject();
00136             } catch (Exception e) {
00137                 e.printStackTrace();
00138             }
00139             return 0;
00140         }
00141     }
00142 
00146     public void terminate() throws java.io.IOException {
00147         if (isLocal) {
00148             body.terminate();
00149         } else {
00150             (new BodyRequest("terminate", new ArrayList<Object>(), bodyID, this.url)).send();
00151         }
00152     }
00153 
00158     public String getNodeURL() throws HTTPRemoteException {
00159         if (isLocal) {
00160             return body.getNodeURL();
00161         } else {
00162             BodyRequest br = new BodyRequest("getNodeURL", new ArrayList<Object>(),
00163                     bodyID, this.url);
00164             br.send();
00165             try {
00166                 return (String) br.getReturnedObject();
00167             } catch (Exception e) {
00168                 throw new HTTPRemoteException("Unexpected exception", e);
00169             }
00170         }
00171     }
00172 
00176     public UniqueID getID() {
00177         return bodyID;
00178     }
00179 
00183     public void updateLocation(UniqueID id, UniversalBody body)
00184         throws IOException {
00185         if (isLocal) {
00186             body.updateLocation(id, body);
00187         } else {
00188             ArrayList<Object> paramsList = new ArrayList<Object>();
00189             paramsList.add(id);
00190             paramsList.add(body);
00191             (new BodyRequest("updateLocation", paramsList, bodyID, this.url)).send();
00192         }
00193     }
00194 
00198     public void enableAC() throws IOException {
00199         if (isLocal) {
00200             body.enableAC();
00201         } else {
00202             (new BodyRequest("enableAC", new ArrayList<Object>(), bodyID, this.url)).send();
00203         }
00204     }
00205 
00209     public void disableAC() throws IOException {
00210         if (isLocal) {
00211             body.disableAC();
00212         } else {
00213             (new BodyRequest("disableAC", new ArrayList<Object>(), bodyID, this.url)).send();
00214         }
00215     }
00216 
00220     public void setImmediateService(String methodName)
00221         throws IOException {
00222         if (isLocal) {
00223             body.setImmediateService(methodName);
00224         } else {
00225             ArrayList<Object> paramsList = new ArrayList<Object>();
00226             paramsList.add(methodName);
00227             (new BodyRequest("setImmediateService", paramsList, bodyID, this.url)).send();
00228         }
00229     }
00230 
00231     public void setImmediateService(String methodName, Class[] parametersTypes)
00232         throws IOException {
00233         if (isLocal) {
00234             body.setImmediateService(methodName, parametersTypes);
00235         } else {
00236             ArrayList<Object> paramsList = new ArrayList<Object>();
00237             paramsList.add(methodName);
00238             paramsList.add(parametersTypes);
00239             new BodyRequest("setImmediateService", paramsList, bodyID, this.url).send();
00240         }
00241     }
00242 
00243     public void removeImmediateService(String methodName,
00244         Class[] parametersTypes) throws IOException {
00245         if (isLocal) {
00246             body.removeImmediateService(methodName, parametersTypes);
00247         } else {
00248             ArrayList<Object> paramsList = new ArrayList<Object>();
00249             paramsList.add(methodName);
00250             paramsList.add(parametersTypes);
00251             new BodyRequest("removeImmediateService", paramsList, bodyID,
00252                 this.url).send();
00253         }
00254     }
00255 
00259     public void terminateSession(long sessionID)
00260         throws IOException, SecurityNotAvailableException {
00261         if (isLocal) {
00262             body.terminateSession(sessionID);
00263         } else {
00264             ArrayList<Object> paramsList = new ArrayList<Object>();
00265             paramsList.add(new Long(sessionID));
00266             new BodyRequest("terminateSession", paramsList, bodyID, this.url).send();
00267         }
00268     }
00269 
00273     public X509Certificate getCertificate()
00274         throws SecurityNotAvailableException, IOException {
00275         if (isLocal) {
00276             return body.getCertificate();
00277         } else {
00278             BodyRequest req = new BodyRequest("getCertificate",
00279                     new ArrayList<Object>(), bodyID, this.url);
00280             req.send();
00281             try {
00282                 return (X509Certificate) req.getReturnedObject();
00283             } catch (SecurityNotAvailableException ex) {
00284                 throw ex;
00285             } catch (Exception e) {
00286                 throw new HTTPRemoteException("Unexpected exception", e);
00287             }
00288         }
00289     }
00290 
00294     public long startNewSession(Communication policy)
00295         throws SecurityNotAvailableException, IOException, 
00296             RenegotiateSessionException {
00297         if (isLocal) {
00298             return body.startNewSession(policy);
00299         } else {
00300             ArrayList<Object> paramsList = new ArrayList<Object>();
00301             paramsList.add(policy);
00302             BodyRequest req = new BodyRequest("startNewSession", paramsList,
00303                     bodyID, this.url);
00304             req.send();
00305             try {
00306                 return ((Long) req.getReturnedObject()).longValue();
00307             } catch (SecurityNotAvailableException ex) {
00308                 throw ex;
00309             } catch (RenegotiateSessionException ex1) {
00310                 throw ex1;
00311             } catch (Exception e) {
00312                 throw new HTTPUnexpectedException("Unexpected exception", e);
00313             }
00314         }
00315     }
00316 
00320     public PublicKey getPublicKey()
00321         throws SecurityNotAvailableException, IOException {
00322         if (isLocal) {
00323             return body.getPublicKey();
00324         } else {
00325             BodyRequest req = new BodyRequest("getPublicKey", new ArrayList<Object>(),
00326                     bodyID, this.url);
00327             req.send();
00328 
00329             try {
00330                 return (PublicKey) req.getReturnedObject();
00331             } catch (SecurityNotAvailableException ex) {
00332                 throw ex;
00333             } catch (Exception e) {
00334                 throw new HTTPUnexpectedException("Unexpected exception", e);
00335             }
00336         }
00337     }
00338 
00342     public byte[] randomValue(long sessionID, byte[] cl_rand)
00343         throws SecurityNotAvailableException, IOException, 
00344             RenegotiateSessionException {
00345         if (isLocal) {
00346             return body.randomValue(sessionID, cl_rand);
00347         } else {
00348             ArrayList<Object> paramsList = new ArrayList<Object>();
00349             paramsList.add(new Long(sessionID));
00350             paramsList.add(cl_rand);
00351 
00352             BodyRequest req = new BodyRequest("randomValue", paramsList,
00353                     bodyID, this.url);
00354             req.send();
00355             try {
00356                 return (byte[]) req.getReturnedObject();
00357             } catch (SecurityNotAvailableException ex) {
00358                 throw ex;
00359             } catch (RenegotiateSessionException e) {
00360                 throw e;
00361             } catch (Exception e) {
00362                 throw new HTTPUnexpectedException("Unexpected exception", e);
00363             }
00364         }
00365     }
00366 
00370     public byte[][] publicKeyExchange(long sessionID, byte[] my_pub,
00371         byte[] my_cert, byte[] sig_code)
00372         throws SecurityNotAvailableException, IOException, KeyExchangeException, 
00373             RenegotiateSessionException {
00374         if (isLocal) {
00375             return body.publicKeyExchange(sessionID, my_pub, my_cert, sig_code);
00376         } else {
00377             ArrayList<Object> paramsList = new ArrayList<Object>();
00378             paramsList.add(new Long(sessionID));
00379             paramsList.add(my_pub);
00380             paramsList.add(my_cert);
00381             paramsList.add(sig_code);
00382 
00383             BodyRequest req = new BodyRequest("publicKeyExchange", paramsList,
00384                     bodyID, this.url);
00385             req.send();
00386 
00387             try {
00388                 return (byte[][]) req.getReturnedObject();
00389             } catch (SecurityNotAvailableException ex) {
00390                 throw ex;
00391             } catch (RenegotiateSessionException e) {
00392                 throw e;
00393             } catch (KeyExchangeException e) {
00394                 throw e;
00395             } catch (Exception e) {
00396                 throw new HTTPUnexpectedException("Unexpected exception", e);
00397             }
00398         }
00399     }
00400 
00405     public byte[][] secretKeyExchange(long sessionID, byte[] tmp, byte[] tmp1,
00406         byte[] tmp2, byte[] tmp3, byte[] tmp4)
00407         throws SecurityNotAvailableException, IOException, 
00408             RenegotiateSessionException {
00409         if (isLocal) {
00410             return body.secretKeyExchange(sessionID, tmp, tmp1, tmp2, tmp3, tmp4);
00411         } else {
00412             ArrayList<Object> paramsList = new ArrayList<Object>();
00413             paramsList.add(new Long(sessionID));
00414             paramsList.add(tmp);
00415             paramsList.add(tmp1);
00416             paramsList.add(tmp2);
00417             paramsList.add(tmp3);
00418             paramsList.add(tmp4);
00419 
00420             BodyRequest req = new BodyRequest("secretKeyExchange", paramsList,
00421                     bodyID, this.url);
00422             req.send();
00423             try {
00424                 return (byte[][]) req.getReturnedObject();
00425             } catch (SecurityNotAvailableException ex) {
00426                 throw ex;
00427             } catch (RenegotiateSessionException e) {
00428                 throw e;
00429             } catch (Exception e) {
00430                 throw new HTTPUnexpectedException("Unexpected exception", e);
00431             }
00432         }
00433     }
00434 
00438     public SecurityContext getPolicy(SecurityContext securityContext)
00439         throws SecurityNotAvailableException, IOException {
00440         if (isLocal) {
00441             return body.getPolicy(securityContext);
00442         } else {
00443             ArrayList<Object> paramsList = new ArrayList<Object>();
00444             paramsList.add(securityContext);
00445 
00446             BodyRequest req = new BodyRequest("getPolicy", paramsList, bodyID,
00447                     this.url);
00448             req.send();
00449 
00450             try {
00451                 return (SecurityContext) req.getReturnedObject();
00452             } catch (SecurityNotAvailableException ex) {
00453                 throw ex;
00454             } catch (Exception e) {
00455                 throw new HTTPUnexpectedException("Unexpected exception", e);
00456             }
00457         }
00458     }
00459 
00463     public byte[] getCertificateEncoded()
00464         throws SecurityNotAvailableException, IOException {
00465         if (isLocal) {
00466             return body.getCertificateEncoded();
00467         } else {
00468             BodyRequest req = new BodyRequest("getCertificateEncoded",
00469                     new ArrayList<Object>(), bodyID, this.url);
00470             req.send();
00471 
00472             try {
00473                 return (byte[]) req.getReturnedObject();
00474             } catch (SecurityNotAvailableException ex) {
00475                 throw ex;
00476             } catch (Exception e) {
00477                 throw new HTTPUnexpectedException("Unexpected exception", e);
00478             }
00479         }
00480     }
00481 
00485     public ArrayList<Entity> getEntities()
00486         throws SecurityNotAvailableException, IOException {
00487         if (isLocal) {
00488             return body.getEntities();
00489         } else {
00490             BodyRequest req = new BodyRequest("getEntities", new ArrayList<Object>(),
00491                     bodyID, this.url);
00492             req.send();
00493 
00494             try {
00495                 
00496                 return (ArrayList<Entity>) req.getReturnedObject();
00497             } catch (SecurityNotAvailableException ex) {
00498                 throw ex;
00499             } catch (Exception e) {
00500                 throw new HTTPUnexpectedException("Unexpected exception", e);
00501             }
00502         }
00503     }
00504 
00508     public String getJobID() {
00509         return this.jobID;
00510     }
00511 
00516     public Object receiveFTMessage(FTMessage ev) throws IOException {
00517         return null;
00518     }
00519 
00523     public void createShortcut(Shortcut shortcut) throws IOException {
00524         if (bodyLogger.isDebugEnabled()) {
00525             bodyLogger.debug(
00526                 "shortcuts are currently not implemented for http communications");
00527         }
00528     }
00529 
00530     //-------------------------------------
00531     //  NFEProducer implementation
00532     //-------------------------------------
00533     public void addNFEListener(NFEListener listener) throws HTTPRemoteException {
00534         if (isLocal) {
00535             body.addNFEListener(listener);
00536         } else {
00537             ArrayList<Object> paramList = new ArrayList<Object>();
00538             paramList.add(listener);
00539 
00540             new BodyRequest("addNFEListener", paramList, bodyID, url).send();
00541         }
00542     }
00543 
00544     public void removeNFEListener(NFEListener listener)
00545         throws HTTPRemoteException {
00546         if (isLocal) {
00547             body.removeNFEListener(listener);
00548         } else {
00549             ArrayList<Object> paramList = new ArrayList<Object>();
00550             paramList.add(listener);
00551 
00552             new BodyRequest("removeNFEListener", paramList, bodyID, url).send();
00553         }
00554     }
00555 
00556     public int fireNFE(NonFunctionalException e) throws HTTPRemoteException {
00557         if (isLocal) {
00558             return body.fireNFE(e);
00559         } else {
00560             ArrayList<Object> paramList = new ArrayList<Object>();
00561             paramList.add(e);
00562             BodyRequest br = new BodyRequest("fireNFE", paramList, bodyID, url);
00563             br.send();
00564             try {
00565                 return ((Integer) br.getReturnedObject()).intValue();
00566             } catch (Exception e1) {
00567                 throw new HTTPUnexpectedException("Unexpected exception", e);
00568             }
00569         }
00570     }
00571 
00575     public void changeProxiedBody(Body newBody) throws IOException {
00576         if (isLocal) {
00577             this.body = newBody;
00578         } else {
00579             throw new IOException("Cannot change the body remotely");
00580         }
00581     }
00582 
00583     /*
00584        public boolean equals(Object o) {
00585                if (!(o instanceof HttpRemoteBodyImpl)) {
00586                        return false;
00587                }
00588                HttpRemoteBodyImpl rba = (HttpRemoteBodyImpl) o;
00589                return (remoteBodyAdapter.url.equals(rba.getURL()) && remoteBodyAdapter.bodyID.equals(rba.getBodyID())) &&
00590                    (remoteBodyAdapter.port == rba.getPort());
00591        }
00592      */
00593 
00594     //---------------------------------------------
00595     // Private methods
00596     //---------------------------------------------
00597     private void readObject(java.io.ObjectInputStream in)
00598         throws IOException, ClassNotFoundException {
00599         in.defaultReadObject();
00600         this.isLocal = false;
00601     }
00602 }

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