org/objectweb/proactive/core/util/UrlBuilder.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.util;
00032 
00033 import java.net.InetAddress;
00034 import java.net.UnknownHostException;
00035 
00036 import org.apache.log4j.Logger;
00037 import org.objectweb.proactive.core.Constants;
00038 import org.objectweb.proactive.core.config.ProActiveConfiguration;
00039 import org.objectweb.proactive.core.util.log.Loggers;
00040 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00041 
00042 
00046 public class UrlBuilder {
00047     private static String[] LOCAL_URLS = {
00048             "///", "//localhost.localdomain", "//localhost", "//127.0.0.1"
00049         };
00050     private final static int DEFAULT_REGISTRY_PORT = 1099;
00051     static Logger logger = ProActiveLogger.getLogger(Loggers.UTIL);
00052 
00053     //
00054     //------------Constructor--------------------------------
00055     //
00056     public UrlBuilder() {
00057     }
00058 
00059     //
00060     //-------------------Public methods-------------------------
00061     //
00062 
00069     public static String checkUrl(String url)
00070         throws java.net.UnknownHostException {
00071         String protocol = getProtocol(url);
00072         String noProtocolUrl = internalCheck(removeProtocol(url, protocol));
00073         return readHostAndName(noProtocolUrl, protocol);
00074     }
00075 
00076     public static String buildUrl(String host, String name, String protocol) {
00077         return buildUrl(host, name, protocol, DEFAULT_REGISTRY_PORT);
00078     }
00079 
00088     public static String buildUrl(String host, String name, String protocol,
00089         int port) {
00090         protocol = checkProtocol(protocol);
00091         String noProtocolUrl = buildUrl(host, name, port);
00092         if (protocol.equals(Constants.DEFAULT_PROTOCOL_IDENTIFIER)) {
00093             return noProtocolUrl;
00094         } else {
00095             return protocol + noProtocolUrl;
00096         }
00097     }
00098 
00107     public static String buildUrlFromProperties(String host, String name) {
00108         String port = null;
00109         String protocol = System.getProperty("proactive.communication.protocol");
00110         if (protocol.equals("rmi") || protocol.equals("ibis")) {
00111             port = System.getProperty("proactive.rmi.port");
00112         }
00113         if (protocol.equals("http")) {
00114             port = System.getProperty("proactive.http.port");
00115         }
00116         if (checkProtocol(protocol).equals("jini:") || (port == null)) {
00117             return buildUrl(host, name, protocol);
00118         } else {
00119             return buildUrl(host, name, protocol, new Integer(port).intValue());
00120         }
00121     }
00122 
00123     public static String buildVirtualNodeUrl(String url)
00124         throws java.net.UnknownHostException {
00125         String vnName = getNameFromUrl(url);
00126         vnName = vnName.concat("_VN");
00127         String host = getHostNameFromUrl(url);
00128         String protocol = getProtocol(url);
00129         int port = getPortFromUrl(url);
00130         return buildUrl(host, vnName, protocol, port);
00131     }
00132 
00133     public static String appendVnSuffix(String name) {
00134         return name.concat("_VN");
00135     }
00136 
00137     public static String removeVnSuffix(String url) {
00138         //              System.out.println("########vn url "+url);
00139         int index = url.lastIndexOf("_VN");
00140         if (index == -1) {
00141             return url;
00142         }
00143         return url.substring(0, index);
00144     }
00145 
00151     public static String getNameFromUrl(String url) {
00152         int n = url.lastIndexOf("/");
00153         String name = url.substring(n + 1);
00154         return name;
00155     }
00156 
00163     public static String getHostNameAndPortFromUrl(String url)
00164         throws UnknownHostException {
00165         String validUrl = checkUrl(url);
00166         int n = validUrl.indexOf("//");
00167         int m = validUrl.lastIndexOf("/"); // looking for the end of the host
00168         if (m == (n + 1)) {
00169             //the url has no name i.e it is a host url
00170             // 
00171             return validUrl.substring(n + 2, validUrl.length());
00172         } else {
00173             //check if there is a port
00174             return validUrl.substring(n + 2, m);
00175         }
00176     }
00177 
00182     public static String getProtocol(String nodeURL) {
00183         if (nodeURL == null) {
00184             return Constants.DEFAULT_PROTOCOL_IDENTIFIER;
00185         }
00186         int n = nodeURL.indexOf("://");
00187         if (n <= 0) {
00188             return Constants.DEFAULT_PROTOCOL_IDENTIFIER;
00189         }
00190         return nodeURL.substring(0, n + 1);
00191     }
00192 
00196     public static String removeProtocol(String url, String protocol) {
00197         protocol = checkProtocol(protocol);
00198         if (url.startsWith(protocol)) {
00199             return url.substring(protocol.length());
00200         }
00201         return url;
00202     }
00203 
00204     public static String getHostNameFromUrl(String url)
00205         throws java.net.UnknownHostException {
00206         String validUrl = checkUrl(url);
00207         int n = validUrl.indexOf("//");
00208         int m = validUrl.lastIndexOf("/"); // looking for the end of the host
00209         if (m == (n + 1)) {
00210             //the url has no name i.e it is a host url
00211             // 
00212             return getHostFromUrl(validUrl.substring(n + 2, validUrl.length()));
00213         } else {
00214             //check if there is a port
00215             return getHostFromUrl(validUrl.substring(n + 2, m));
00216         }
00217     }
00218 
00219     public static String checkProtocol(String protocol) {
00220         if (protocol.indexOf(":") == -1) {
00221             return protocol.concat(":");
00222         }
00223         return protocol;
00224     }
00225 
00226     public static String removePortFromHost(String hostname) {
00227         int n = hostname.lastIndexOf(":");
00228         if (n > -1) {
00229             return hostname.substring(0, n);
00230         }
00231         return hostname;
00232     }
00233 
00239     public static int getPortFromUrl(String url) {
00240         try {
00241             String validUrl = checkUrl(url);
00242             int n = validUrl.indexOf("//");
00243             int m = validUrl.lastIndexOf("/");
00244             if (m == (n + 1)) {
00245                 return getPortNumber(validUrl.substring(n + 2));
00246             } else {
00247                 return getPortNumber(validUrl.substring(n + 2, m));
00248             }
00249         } catch (java.net.UnknownHostException e) {
00250             logger.info("Cannot resolve hostname " + e.getMessage());
00251             int n = url.indexOf("//");
00252             int m = url.lastIndexOf("/");
00253             if (m == (n + 1)) {
00254                 return getPortNumber(url.substring(n + 2));
00255             } else {
00256                 return getPortNumber(url.substring(n + 2, m));
00257             }
00258         }
00259     }
00260 
00261     public static String getHostNameorIP(InetAddress address) {
00262         if (System.getProperty("proactive.hostname") != null) {
00263             return System.getProperty("proactive.hostname");
00264         }
00265         if ("true".equals(System.getProperty("proactive.useIPaddress"))) {
00266             return address.getHostAddress();
00267         } else {
00268             return address.getCanonicalHostName();
00269         }
00270     }
00271 
00272     public static String removeUsername(String url) {
00273         //this method is used to extract the username, that might be necessary for the callback
00274         //it updates the hostable.
00275         int index = url.indexOf("@");
00276 
00277         if (index >= 0) {
00278             String username = url.substring(0, index);
00279             url = url.substring(index + 1, url.length());
00280 
00281             try {
00282                 HostsInfos.setUserName(getHostNameFromUrl(url), username);
00283             } catch (UnknownHostException e) {
00284                 e.printStackTrace();
00285             }
00286         }
00287 
00288         return url;
00289     }
00290 
00291     //
00292     //-----------------Private Static Methods-----------------------------
00293     //
00294     //  private static String internalCheckUrl(String url) throws java.net.UnknownHostException{
00295     //          String validlUrl = internalCheck(url);
00296     //          String noProtocolUrl
00297     //    if (noProtocolUrl.charAt(noProtocolUrl.length()-1) == '/')
00298     //      noProtocolUrl = noProtocolUrl.substring(0,noProtocolUrl.length()-1);
00299     //    if (! noProtocolUrl.startsWith("//"))
00300     //      throw new java.net.UnknownHostException("Malformed URL = "+url);
00301     //    return noProtocolUrl;
00302     //  }
00303     private static String buildUrl(String host, String name, int port) {
00304         if (port == DEFAULT_REGISTRY_PORT) {
00305             if (name.equals("")) {
00306                 return "//" + host;
00307             } else {
00308                 return "//" + host + "/" + name;
00309             }
00310         } else {
00311             if (name.equals("")) {
00312                 return "//" + host + ":" + port;
00313             } else {
00314                 return "//" + host + ":" + port + "/" + name;
00315             }
00316         }
00317     }
00318 
00319     private static String readHostAndName(String urlToRead, String protocol)
00320         throws java.net.UnknownHostException {
00321         java.net.InetAddress hostInetAddress = java.net.InetAddress.getLocalHost();
00322         for (int i = 0; i < LOCAL_URLS.length; i++) {
00323             if (urlToRead.toLowerCase().startsWith(LOCAL_URLS[i])) {
00324                 // local url
00325                 String name = urlToRead.substring(LOCAL_URLS[i].length());
00326                 if (name.startsWith("/")) {
00327                     //there is no port and the name starts with /
00328                     return buildUrl(UrlBuilder.getHostNameorIP(hostInetAddress),
00329                         name.substring(1), protocol);
00330                 } else {
00331                     //there is no port and only the name 
00332                     if (name.indexOf(":") < 0) {
00333                         return buildUrl(UrlBuilder.getHostNameorIP(
00334                                 hostInetAddress), name, protocol);
00335                     } else if (name.indexOf("/") < 0) {
00336                         // there is a port and no name, it is a host url
00337                         return buildHostUrl("//" +
00338                             UrlBuilder.getHostNameorIP(hostInetAddress) + name,
00339                             protocol);
00340                     }
00341 
00342                     //port is define, extract port and build url
00343                     return buildUrl(UrlBuilder.getHostNameorIP(hostInetAddress),
00344                         name.substring(name.lastIndexOf("/") + 1, name.length()),
00345                         protocol,
00346                         new Integer(name.substring(1, name.lastIndexOf("/"))).intValue());
00347                 }
00348             }
00349         }
00350 
00351         // non local url
00352         int n = urlToRead.indexOf('/', 2); // looking for the end of the host
00353         if (n < 3) {
00354             if (n == -1) {
00355                 //It means that there is no name, so return just the complete url of the host
00356                 return buildHostUrl(urlToRead, protocol);
00357             } else {
00358                 throw new java.net.UnknownHostException(
00359                     "Cannot determine the name of the host in this url=" +
00360                     urlToRead);
00361             }
00362         }
00363 
00364         // get the host
00365         String hostname = getHostFromUrl(urlToRead.substring(2, n));
00366 
00367         // get the real host name
00368         hostInetAddress = java.net.InetAddress.getByName(hostname);
00369         //get the port 
00370         int portNumber = getPortNumber(urlToRead.substring(2, n));
00371 
00372         String name = urlToRead.substring(n + 1);
00373 
00374         return buildUrl(UrlBuilder.getHostNameorIP(hostInetAddress), name,
00375             protocol, portNumber);
00376     }
00377 
00383     private static String buildHostUrl(String urlToRead, String protocol)
00384         throws UnknownHostException {
00385         protocol = checkProtocol(protocol);
00386         String urlTemp;
00387         String hostname = getHostFromUrl(urlToRead.substring(2,
00388                     urlToRead.length()));
00389 
00390         //java.net.InetAddress hostInetAddress = java.net.InetAddress.getByName(hostname);
00391         int portNumber = getPortNumber(urlToRead.substring(2, urlToRead.length()));
00392         if (portNumber == DEFAULT_REGISTRY_PORT) {
00393             urlTemp = "//" + hostname;
00394         } else {
00395             urlTemp = "//" + hostname + ":" + portNumber;
00396         }
00397         if (protocol.equals(Constants.DEFAULT_PROTOCOL_IDENTIFIER)) {
00398             return urlTemp;
00399         } else {
00400             return protocol + urlTemp;
00401         }
00402     }
00403 
00409     private static int getPortNumber(String url) {
00410         //        System.out.println("URL === " + url);
00411         int portNumber = DEFAULT_REGISTRY_PORT;
00412         int index = url.lastIndexOf(":");
00413         if (index > -1) {
00414             portNumber = Integer.parseInt(url.substring(index + 1, url.length()));
00415         }
00416 
00417         //       System.out.println("*/** " + ProActiveConfiguration.osgiServletEnabled() + " -- " + index + " -- " + url );
00418         //                
00419         //        
00420         if (ProActiveConfiguration.osgiServletEnabled()) {
00421             if (index != -1) {
00422                 String suite = url.substring(index);
00423 
00424                 //            System.out.println("----------- suite = " + suite );
00425                 int portLength = suite.indexOf('/');
00426 
00427                 //            System.out.println(portLength);
00428                 if (portLength != -1) {
00429                     String portString = suite.substring(1, portLength);
00430 
00431                     //            System.out.println("PORT = " + portString);
00432                     return Integer.parseInt(portString);
00433                 }
00434             }
00435         }
00436 
00437         return portNumber;
00438     }
00439 
00445     private static String getHostFromUrl(String url) {
00446         int index = url.lastIndexOf(":");
00447 
00448         //there is a port
00449         if (index > -1) {
00450             return url.substring(0, index);
00451         }
00452 
00453         //there is no port
00454         return url;
00455     }
00456 
00457     private static String internalCheck(String url) {
00458         if (!url.startsWith("//")) {
00459             url = "//" + url;
00460         }
00461         if (url.charAt(url.length() - 1) == '/') {
00462             url = url.substring(0, url.length() - 1);
00463         }
00464         return url;
00465     }
00466 }

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