org/objectweb/proactive/core/body/http/HttpBodyAdapter.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.net.MalformedURLException;
00035 import java.net.URL;
00036 import java.rmi.ConnectException;
00037 import java.util.Enumeration;
00038 import java.util.Hashtable;
00039 
00040 import org.objectweb.proactive.core.ProActiveException;
00041 import org.objectweb.proactive.core.body.BodyAdapterImpl;
00042 import org.objectweb.proactive.core.body.RemoteBody;
00043 import org.objectweb.proactive.core.body.UniversalBody;
00044 import org.objectweb.proactive.core.body.http.util.exceptions.HTTPUnexpectedException;
00045 import org.objectweb.proactive.core.body.http.util.messages.HttpLookupMessage;
00046 import org.objectweb.proactive.core.rmi.ClassServer;
00047 import org.objectweb.proactive.core.util.UrlBuilder;
00048 
00049 
00058 public class HttpBodyAdapter extends BodyAdapterImpl {
00059 
00064     protected static transient Hashtable<String, HttpBodyAdapter> urnBodys = new Hashtable<String, HttpBodyAdapter>();
00065 
00066     //
00067     // -- CONSTRUCTORS -----------------------------------------------
00068     //
00069     public HttpBodyAdapter() {
00070     }
00071 
00072     public HttpBodyAdapter(UniversalBody body) throws ProActiveException {
00073         RemoteBody remoteBody = new HttpRemoteBodyImpl(body);
00074         construct(remoteBody);
00075         
00076         
00077     }
00078 
00079     //
00080     // -- PUBLIC METHODS -----------------------------------------------
00081     //
00082 
00088     public void register(String urn) throws java.io.IOException {
00089 
00090         URL u = null;
00091         String url = null;
00092         int port = ClassServer.getServerSocketPort();
00093         try {
00094             u = new URL(urn);
00095             port = u.getPort();
00096             if (port != ClassServer.getServerSocketPort()) {
00097                 throw new IOException(
00098                     "Bad registering port. You have to register on the same port as the runtime");
00099             }
00100             url = u.toString();
00101             urn = u.getPath();
00102         } catch (MalformedURLException e) {
00103             url = ClassServer.getUrl() + urn;
00104             
00105         }
00106         urnBodys.put(urn, this);
00107         //        urn = urn.substring(urn.lastIndexOf('/') + 1);
00108         if (bodyLogger.isInfoEnabled()) {
00109             bodyLogger.info("register object  at " + url);
00110             bodyLogger.info(urnBodys);
00111         }
00112     }
00113 
00118     public void unregister(String urn) throws java.io.IOException {
00119         urnBodys.put(urn, null);
00120     }
00121 
00127     public UniversalBody lookup(String urn) throws java.io.IOException {
00128 //        try {
00129 
00130                 URL u = null;
00131                 int port = 0;
00132                 try {                   
00133                         u = new URL (urn);
00134                         port = u.getPort();
00135                         
00136                 } catch (MalformedURLException e) {
00137                         if (!urn.startsWith("http://")) {
00138                                 urn = "http://" + urn;
00139                                 return lookup(urn);
00140                         }
00141                         throw e;
00142                 }
00143                         if (port == 0) {
00144                                 throw new HTTPUnexpectedException("You have to specify a port where the runtime can be reached");
00145                         }
00146                         String url = u.toString();
00147                         urn = u.getPath();
00148                 HttpLookupMessage message = new HttpLookupMessage(urn, url, port);                      
00149                 message.send();
00150                 UniversalBody result = message.getReturnedObject();
00151                 if (result == null) {
00152                     throw new java.io.IOException("The url " + url +
00153                         " is not bound to any known object");
00154                 } else {
00155                     return result;
00156                 
00157                 }
00158                 
00159 //              
00160 //              String url;
00162 //            url = urn;
00163 
00164             
00165 
00166 //            urn = urn.substring(urn.lastIndexOf('/') + 1);
00167 //
00168 //
00169 //       
00170 //
00171 //            //            message = (HttpLookupMessage) ProActiveXMLUtils.sendMessage(url,
00172 //            //                    port, message, ProActiveXMLUtils.MESSAGE);
00173 //            //UniversalBody result = (UniversalBody) message.processMessage();
00174 //
00175 //
00176 //            //System.out.println("result = " + result );
00177 //          
00178 //        } catch (Exception e) {
00179 //            throw new HTTPUnexpectedException("Unexpected exception", e);
00180 //        }
00181     }
00182 
00188     public static synchronized UniversalBody getBodyFromUrn(String urn) {
00189         return (UniversalBody) urnBodys.get(urn);
00190     }
00191     
00198     /* (non-Javadoc) 
00199      * @see org.objectweb.proactive.core.body.BodyAdapterImpl#list(java.lang.String)
00200      */
00201     public String [] list(String url) throws java.io.IOException {
00202         String [] names = null;
00203         names = new String [this.urnBodys.size()];
00204         Enumeration<String> e = urnBodys.keys();
00205         int i=0;
00206         while (e.hasMoreElements()) {
00207                         names[i++] = e.nextElement();
00208                 }
00209         
00210         return names;
00211     }
00212 }

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