00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00055
00056 public UrlBuilder() {
00057 }
00058
00059
00060
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
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("/");
00168 if (m == (n + 1)) {
00169
00170
00171 return validUrl.substring(n + 2, validUrl.length());
00172 } else {
00173
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("/");
00209 if (m == (n + 1)) {
00210
00211
00212 return getHostFromUrl(validUrl.substring(n + 2, validUrl.length()));
00213 } else {
00214
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
00274
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
00293
00294
00295
00296
00297
00298
00299
00300
00301
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
00325 String name = urlToRead.substring(LOCAL_URLS[i].length());
00326 if (name.startsWith("/")) {
00327
00328 return buildUrl(UrlBuilder.getHostNameorIP(hostInetAddress),
00329 name.substring(1), protocol);
00330 } else {
00331
00332 if (name.indexOf(":") < 0) {
00333 return buildUrl(UrlBuilder.getHostNameorIP(
00334 hostInetAddress), name, protocol);
00335 } else if (name.indexOf("/") < 0) {
00336
00337 return buildHostUrl("//" +
00338 UrlBuilder.getHostNameorIP(hostInetAddress) + name,
00339 protocol);
00340 }
00341
00342
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
00352 int n = urlToRead.indexOf('/', 2);
00353 if (n < 3) {
00354 if (n == -1) {
00355
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
00365 String hostname = getHostFromUrl(urlToRead.substring(2, n));
00366
00367
00368 hostInetAddress = java.net.InetAddress.getByName(hostname);
00369
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
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
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
00418
00419
00420 if (ProActiveConfiguration.osgiServletEnabled()) {
00421 if (index != -1) {
00422 String suite = url.substring(index);
00423
00424
00425 int portLength = suite.indexOf('/');
00426
00427
00428 if (portLength != -1) {
00429 String portString = suite.substring(1, portLength);
00430
00431
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
00449 if (index > -1) {
00450 return url.substring(0, index);
00451 }
00452
00453
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 }