KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > util > UrlBuilder


1 /*
2  * ################################################################
3  *
4  * ProActive: The Java(TM) library for Parallel, Distributed,
5  * Concurrent computing with Security and Mobility
6  *
7  * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8  * Contact: proactive-support@inria.fr
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  *
25  * Initial developer(s): The ProActive Team
26  * http://www.inria.fr/oasis/ProActive/contacts.html
27  * Contributor(s):
28  *
29  * ################################################################
30  */

31 package org.objectweb.proactive.core.util;
32
33 import org.objectweb.proactive.core.Constants;
34
35
36 /**
37  * This class is a utility class to perform modifications and operations on urls.
38  */

39
40 public class UrlBuilder {
41     private static String JavaDoc[] LOCAL_URLS = { "///", "//localhost", "//127.0.0.1" };
42     private final static int DEFAULT_REGISTRY_PORT = 1099;
43
44    
45     //
46
//------------Constructor--------------------------------
47
//
48
public UrlBuilder() {
49     }
50
51     //
52
//-------------------Public methods-------------------------
53
//
54

55     /**
56      * Checks if the given url is well-formed
57      * @param url the url to check
58      * @return String the url if well-formed
59      * @throws UnknownHostException if the url is not well-formed
60      */

61     public static String JavaDoc checkUrl(String JavaDoc url)
62         throws java.net.UnknownHostException JavaDoc {
63         String JavaDoc protocol = getProtocol(url);
64         String JavaDoc noProtocolUrl = internalCheck(removeProtocol(url, protocol));
65         return readHostAndName(noProtocolUrl, protocol);
66     }
67
68    
69     public static String JavaDoc buildUrl(String JavaDoc host, String JavaDoc name, String JavaDoc protocol) {
70         return buildUrl(host, name, protocol, DEFAULT_REGISTRY_PORT);
71     }
72
73     //
74
public static String JavaDoc buildUrl(String JavaDoc host, String JavaDoc name, String JavaDoc protocol,
75         int port) {
76         String JavaDoc 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     /**
85      * This method build an url in the form protocol://host:port/name where the port
86      * is given from system propeties, except when the protocol is jini. In that case the url
87      * looks like jini://host/name.
88      * @param host
89      * @param name
90      * @param protocol
91      * @return an Url built from properties
92      */

93     public static String JavaDoc buildUrlFromProperties(String JavaDoc host, String JavaDoc name, String JavaDoc protocol)
94     {
95         String JavaDoc 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 JavaDoc(port).intValue());
98     }
99     
100     public static String JavaDoc buildVirtualNodeUrl(String JavaDoc url)
101         throws java.net.UnknownHostException JavaDoc {
102         String JavaDoc vnName = getNameFromUrl(url);
103         vnName = vnName.concat("_VN");
104         String JavaDoc host = getHostNameFromUrl(url);
105         String JavaDoc protocol = getProtocol(url);
106         int port = getPortFromUrl(url);
107         return buildUrl(host, vnName, protocol, port);
108     }
109
110     public static String JavaDoc appendVnSuffix(String JavaDoc name) {
111         return name.concat("_VN");
112     }
113
114     public static String JavaDoc removeVnSuffix(String JavaDoc url) {
115         // System.out.println("########vn url "+url);
116
int index = url.lastIndexOf("_VN");
117         if (index == -1) {
118             return url;
119         }
120         return url.substring(0, index);
121     }
122
123     /**
124      * Returns the name included in the url
125      * @param url
126      * @return the name included in the url
127      */

128     public static String JavaDoc getNameFromUrl(String JavaDoc url) {
129         int n = url.lastIndexOf("/");
130         String JavaDoc name = url.substring(n + 1);
131         return name;
132     }
133
134     /**
135      * Return the protocol specified in the string
136      * The same convention as in URL is used
137      */

138     public static String JavaDoc getProtocol(String JavaDoc 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     /**
150      * Returns the url without protocol
151      */

152     public static String JavaDoc removeProtocol(String JavaDoc url, String JavaDoc protocol) {
153         if (url.startsWith(protocol)) {
154             return url.substring(protocol.length());
155         }
156         return url;
157     }
158
159     public static String JavaDoc getHostNameFromUrl(String JavaDoc url)
160         throws java.net.UnknownHostException JavaDoc {
161         String JavaDoc validUrl = checkUrl(url);
162         int n = validUrl.indexOf("//");
163         int m = validUrl.lastIndexOf("/"); // looking for the end of the host
164
//check if there is a port
165
return getHostFromUrl(validUrl.substring(n + 2, m));
166     }
167
168     public static String JavaDoc checkProtocol(String JavaDoc protocol) {
169         if (protocol.indexOf(":") == -1) {
170             return protocol.concat(":");
171         }
172         return protocol;
173     }
174     
175     public static String JavaDoc removePortFromHost(String JavaDoc hostname){
176         int n = hostname.lastIndexOf(":");
177         if(n>-1) return hostname.substring(0,n);
178         return hostname;
179     }
180
181     /**
182      * Returns port number included in the url or 1099 if no port is specified
183      * @param url
184      * @return the port number included in the url or 1099 if no port is specified
185      */

186     public static int getPortFromUrl(String JavaDoc url) {
187         try {
188             String JavaDoc 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 JavaDoc e) {
193             e.printStackTrace();
194             return DEFAULT_REGISTRY_PORT;
195         }
196     }
197
198     //
199
//-----------------Private Static Methods-----------------------------
200
//
201
// private static String internalCheckUrl(String url) throws java.net.UnknownHostException{
202
// String validlUrl = internalCheck(url);
203
// String noProtocolUrl
204
// if (noProtocolUrl.charAt(noProtocolUrl.length()-1) == '/')
205
// noProtocolUrl = noProtocolUrl.substring(0,noProtocolUrl.length()-1);
206
// if (! noProtocolUrl.startsWith("//"))
207
// throw new java.net.UnknownHostException("Malformed URL = "+url);
208
// return noProtocolUrl;
209
// }
210

211     
212     private static String JavaDoc buildUrl(String JavaDoc host, String JavaDoc 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 JavaDoc readHostAndName(String JavaDoc urlToRead, String JavaDoc protocol)
222         throws java.net.UnknownHostException JavaDoc {
223         java.net.InetAddress JavaDoc hostInetAddress = java.net.InetAddress.getLocalHost();
224         for (int i = 0; i < LOCAL_URLS.length; i++) {
225             if (urlToRead.toLowerCase().startsWith(LOCAL_URLS[i])) {
226                 // local url
227
String JavaDoc name = urlToRead.substring(LOCAL_URLS[i].length());
228                 if (name.startsWith("/")) {
229                     //there is no port and the name starts with /
230
return buildUrl(hostInetAddress.getCanonicalHostName(),
231                         name.substring(1), protocol);
232                 } else {
233                     //there is no port and only the name
234
if (name.indexOf(":")<0) return buildUrl(hostInetAddress.getCanonicalHostName(),name, protocol);
235                     //port is define, extract port and build url
236
return buildUrl(hostInetAddress.getCanonicalHostName(),
237                         name.substring(name.lastIndexOf("/")+1, name.length()),
238                         protocol,
239                         new Integer JavaDoc(name.substring(1, name.lastIndexOf("/"))).intValue());
240                 }
241             }
242         }
243
244         // non local url
245
int n = urlToRead.indexOf('/', 2); // looking for the end of the host
246
if (n < 3) {
247             throw new java.net.UnknownHostException JavaDoc(
248                 "Cannot determine the name of the host in this url=" +
249                 urlToRead);
250         }
251
252         // get the host
253
String JavaDoc hostname = getHostFromUrl(urlToRead.substring(2, n));
254
255         // get the real host name
256
hostInetAddress = java.net.InetAddress.getByName(hostname);
257         //get the port
258
int portNumber = getPortNumber(urlToRead.substring(2, n));
259         String JavaDoc name = urlToRead.substring(n + 1);
260
261         
262          return buildUrl(hostInetAddress.getHostName(), name, protocol,
263                 portNumber);
264         
265     }
266
267     /**
268      * This method extract the port from a string in the form host:port or host
269      * @param url
270      * @return port number or 0 if there is no port
271      */

272     private static int getPortNumber(String JavaDoc 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     /**
282      * This method extract the host name from a string in the form host:port or host
283      * @param url
284      * @return
285      */

286     private static String JavaDoc getHostFromUrl(String JavaDoc url) {
287         int index = url.lastIndexOf(":");
288
289         //there is a port
290
if (index > -1) {
291             return url.substring(0, index);
292         }
293
294         //there is no port
295
return url;
296     }
297
298     private static String JavaDoc internalCheck(String JavaDoc 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