1 31 package org.objectweb.proactive.core.util; 32 33 import org.objectweb.proactive.core.Constants; 34 35 36 39 40 public class UrlBuilder { 41 private static String [] LOCAL_URLS = { "///", "//localhost", "//127.0.0.1" }; 42 private final static int DEFAULT_REGISTRY_PORT = 1099; 43 44 45 public UrlBuilder() { 49 } 50 51 55 61 public static String checkUrl(String url) 62 throws java.net.UnknownHostException { 63 String protocol = getProtocol(url); 64 String noProtocolUrl = internalCheck(removeProtocol(url, protocol)); 65 return readHostAndName(noProtocolUrl, protocol); 66 } 67 68 69 public static String buildUrl(String host, String name, String protocol) { 70 return buildUrl(host, name, protocol, DEFAULT_REGISTRY_PORT); 71 } 72 73 public static String buildUrl(String host, String name, String protocol, 75 int port) { 76 String noProtocolUrl = buildUrl(host, name, port); 77 if (protocol.equals(Constants.DEFAULT_PROTOCOL_IDENTIFIER)) { 78 return noProtocolUrl; 79 } else { 80 return protocol + noProtocolUrl; 81 } 82 } 83 84 93 public static String buildUrlFromProperties(String host, String name, String protocol) 94 { 95 String port = System.getProperty("proactive.rmi.port"); 96 if (checkProtocol(protocol).equals("jini:") || port == null) return buildUrl(host,name,protocol); 97 else return buildUrl(host, name, protocol, new Integer (port).intValue()); 98 } 99 100 public static String buildVirtualNodeUrl(String url) 101 throws java.net.UnknownHostException { 102 String vnName = getNameFromUrl(url); 103 vnName = vnName.concat("_VN"); 104 String host = getHostNameFromUrl(url); 105 String protocol = getProtocol(url); 106 int port = getPortFromUrl(url); 107 return buildUrl(host, vnName, protocol, port); 108 } 109 110 public static String appendVnSuffix(String name) { 111 return name.concat("_VN"); 112 } 113 114 public static String removeVnSuffix(String url) { 115 int index = url.lastIndexOf("_VN"); 117 if (index == -1) { 118 return url; 119 } 120 return url.substring(0, index); 121 } 122 123 128 public static String getNameFromUrl(String url) { 129 int n = url.lastIndexOf("/"); 130 String name = url.substring(n + 1); 131 return name; 132 } 133 134 138 public static String getProtocol(String nodeURL) { 139 if (nodeURL == null) { 140 return Constants.DEFAULT_PROTOCOL_IDENTIFIER; 141 } 142 int n = nodeURL.indexOf("://"); 143 if (n <= 0) { 144 return Constants.DEFAULT_PROTOCOL_IDENTIFIER; 145 } 146 return nodeURL.substring(0, n + 1); 147 } 148 149 152 public static String removeProtocol(String url, String protocol) { 153 if (url.startsWith(protocol)) { 154 return url.substring(protocol.length()); 155 } 156 return url; 157 } 158 159 public static String getHostNameFromUrl(String url) 160 throws java.net.UnknownHostException { 161 String validUrl = checkUrl(url); 162 int n = validUrl.indexOf("//"); 163 int m = validUrl.lastIndexOf("/"); return getHostFromUrl(validUrl.substring(n + 2, m)); 166 } 167 168 public static String checkProtocol(String protocol) { 169 if (protocol.indexOf(":") == -1) { 170 return protocol.concat(":"); 171 } 172 return protocol; 173 } 174 175 public static String removePortFromHost(String hostname){ 176 int n = hostname.lastIndexOf(":"); 177 if(n>-1) return hostname.substring(0,n); 178 return hostname; 179 } 180 181 186 public static int getPortFromUrl(String url) { 187 try { 188 String validUrl = checkUrl(url); 189 int n = validUrl.indexOf("//"); 190 int m = validUrl.lastIndexOf("/"); 191 return getPortNumber(validUrl.substring(n + 2, m)); 192 } catch (java.net.UnknownHostException e) { 193 e.printStackTrace(); 194 return DEFAULT_REGISTRY_PORT; 195 } 196 } 197 198 211 212 private static String buildUrl(String host, String name, int port) { 213 if (port == DEFAULT_REGISTRY_PORT) { 214 return "//" + host + "/" + name; 215 } else { 216 return "//" + host + ":" + port + "/" + name; 217 } 218 } 219 220 221 private static String readHostAndName(String urlToRead, String protocol) 222 throws java.net.UnknownHostException { 223 java.net.InetAddress hostInetAddress = java.net.InetAddress.getLocalHost(); 224 for (int i = 0; i < LOCAL_URLS.length; i++) { 225 if (urlToRead.toLowerCase().startsWith(LOCAL_URLS[i])) { 226 String name = urlToRead.substring(LOCAL_URLS[i].length()); 228 if (name.startsWith("/")) { 229 return buildUrl(hostInetAddress.getCanonicalHostName(), 231 name.substring(1), protocol); 232 } else { 233 if (name.indexOf(":")<0) return buildUrl(hostInetAddress.getCanonicalHostName(),name, protocol); 235 return buildUrl(hostInetAddress.getCanonicalHostName(), 237 name.substring(name.lastIndexOf("/")+1, name.length()), 238 protocol, 239 new Integer (name.substring(1, name.lastIndexOf("/"))).intValue()); 240 } 241 } 242 } 243 244 int n = urlToRead.indexOf('/', 2); if (n < 3) { 247 throw new java.net.UnknownHostException ( 248 "Cannot determine the name of the host in this url=" + 249 urlToRead); 250 } 251 252 String hostname = getHostFromUrl(urlToRead.substring(2, n)); 254 255 hostInetAddress = java.net.InetAddress.getByName(hostname); 257 int portNumber = getPortNumber(urlToRead.substring(2, n)); 259 String name = urlToRead.substring(n + 1); 260 261 262 return buildUrl(hostInetAddress.getHostName(), name, protocol, 263 portNumber); 264 265 } 266 267 272 private static int getPortNumber(String url) { 273 int portNumber = DEFAULT_REGISTRY_PORT; 274 int index = url.lastIndexOf(":"); 275 if (index > -1) { 276 portNumber = Integer.parseInt(url.substring(index + 1, url.length())); 277 } 278 return portNumber; 279 } 280 281 286 private static String getHostFromUrl(String url) { 287 int index = url.lastIndexOf(":"); 288 289 if (index > -1) { 291 return url.substring(0, index); 292 } 293 294 return url; 296 } 297 298 private static String internalCheck(String url) { 299 if (!url.startsWith("//")) { 300 url = "//" + url; 301 } 302 if (url.charAt(url.length() - 1) == '/') { 303 url = url.substring(0, url.length() - 1); 304 } 305 return url; 306 } 307 308 } 309 | Popular Tags |