org/objectweb/proactive/core/body/rmi/RmiBodyAdapter.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.rmi;
00032 
00033 import java.net.MalformedURLException;
00034 import java.rmi.ConnectException;
00035 
00036 import org.objectweb.proactive.core.ProActiveException;
00037 import org.objectweb.proactive.core.body.BodyAdapterImpl;
00038 import org.objectweb.proactive.core.body.UniversalBody;
00039 import org.objectweb.proactive.core.util.UrlBuilder;
00040 
00041 
00050 public class RmiBodyAdapter extends BodyAdapterImpl {
00051     //
00052     // -- CONSTRUCTORS -----------------------------------------------
00053     //
00054     public RmiBodyAdapter() {
00055     }
00056 
00057     protected RmiBodyAdapter(RmiRemoteBody remoteBody)
00058         throws ProActiveException {
00059         construct(remoteBody);
00060     }
00061 
00062     public RmiBodyAdapter(UniversalBody body) throws ProActiveException {
00063         try {
00064             RmiRemoteBody remoteBody = new RmiRemoteBodyImpl(body);
00065             construct(remoteBody);
00066         } catch (java.rmi.RemoteException e) {
00067             throw new ProActiveException(e);
00068         }
00069     }
00070 
00078     public void register(String url) throws java.io.IOException {
00079         try {
00080             java.rmi.Naming.rebind(url, (RmiRemoteBody) proxiedRemoteBody);
00081         } catch (ConnectException e) {
00082             //failed to unbind at port 1099 
00083             // try at proactive.rmi.port
00084             String url2 = UrlBuilder.getProtocol(url) + "//" +
00085                 UrlBuilder.getHostNameFromUrl(url) + ":" +
00086                 System.getProperty("proactive.rmi.port") + "/" +
00087                 UrlBuilder.getNameFromUrl(url);
00088 
00089             java.rmi.Naming.rebind(url2, (RmiRemoteBody) proxiedRemoteBody);
00090         }
00091     }
00092 
00098     public void unregister(String url) throws java.io.IOException {
00099         try {
00100             try {
00101                 java.rmi.Naming.unbind(url);
00102             } catch (ConnectException e) {
00103                 //failed to unbind at port 1099 
00104                 // try at proactive.rmi.port
00105                 String url2 = UrlBuilder.getProtocol(url) + "//" +
00106                     UrlBuilder.getHostNameFromUrl(url) + ":" +
00107                     System.getProperty("proactive.rmi.port") + "/" +
00108                     UrlBuilder.getNameFromUrl(url);
00109 
00110                 java.rmi.Naming.unbind(url2);
00111             }
00112         } catch (java.rmi.NotBoundException e) {
00113             throw new java.io.IOException(
00114                 "No object is bound to the given url : " + url);
00115         }
00116     }
00117 
00127     public UniversalBody lookup(String url) throws java.io.IOException {
00128         Object o = null;
00129 
00130         // Try if URL is the address of a RmiRemoteBody
00131         try {
00132             try {
00133                 o = java.rmi.Naming.lookup(url);
00134             } catch (ConnectException e) {
00135                 // connection failed, try to find a rmiregistry at proactive.rmi.port port
00136                 String url2 = UrlBuilder.getProtocol(url) + "//" +
00137                     UrlBuilder.getHostNameFromUrl(url) + ":" +
00138                     System.getProperty("proactive.rmi.port") + "/" +
00139                     UrlBuilder.getNameFromUrl(url);
00140 
00141                 o = java.rmi.Naming.lookup(url2);
00142             }
00143         } catch (java.rmi.NotBoundException e) { // there are one rmiregistry on target computer but node isn t bound
00144             throw new java.io.IOException("The url " + url +
00145                 " is not bound to any known object");
00146         }
00147 
00148         //catch (java.rmi.ConnectException e)
00149         if (o instanceof RmiRemoteBody) {
00150             try {
00151                 construct((RmiRemoteBody) o);
00152             } catch (ProActiveException e1) {
00153                 throw new java.io.IOException(
00154                                 "The remote object at " + url + " is not accessible ");
00155             }
00156             return this;
00157         } 
00158         
00159             throw new java.io.IOException(
00160                 "The given url does exist but doesn't point to a remote body  url=" +
00161                 url + " class found is " + o.getClass().getName());
00162     }
00163 
00164 
00171     public String [] list(String url) throws java.io.IOException {
00172         String [] names = null;
00173 
00174         // Try if URL is the address of a RmiRemoteBody
00175             try {
00176                 names = java.rmi.Naming.list(url);
00177             } catch (MalformedURLException e) {
00178                 // connection failed, try to find a rmiregistry at proactive.rmi.port port
00179                 // ie change the port specified in url. 
00180                 // Is this needed ? 
00181                 String url2 = UrlBuilder.getProtocol(url) + "//" +
00182                     UrlBuilder.getHostNameFromUrl(url) + ":" +
00183                     System.getProperty("proactive.rmi.port") + "/" ;
00184 
00185                 names = java.rmi.Naming.list(url2);
00186             } catch (ConnectException e) {
00187                 return new String [] {};
00188             }
00189             return names;
00190     }
00191 }

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