KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > rmi > iiop > client > UrlInfo


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.rmi.iiop.client;
20
21 import gcc.*;
22 import gcc.properties.*;
23 import gcc.rmi.iiop.*;
24 import gcc.util.*;
25 import java.net.*;
26 import java.util.*;
27
28 public class UrlInfo
29 {
30     public static UrlInfo getInstance(String url)
31     {
32         UrlInfo object = new UrlInfo();
33         object.init(url);
34         return object;
35     }
36
37     public static List getList(String urls)
38     {
39         ArrayList list = new ArrayList(3);
40         for (Iterator i = ListUtil.getCommaSeparatedList(urls).iterator(); i.hasNext();)
41         {
42             String url = (String)i.next();
43             list.add(getInstance(url));
44         }
45         return list;
46     }
47
48     // private data
49

50     private int _protocol;
51
52     private String _host;
53
54     private int _port;
55
56     private String _objectKey;
57
58     private PropertyMap _properties;
59
60     // internal methods
61

62     protected void init(String urlString)
63     {
64         int cssPos = urlString.indexOf("://");
65         if (cssPos == -1)
66         {
67             throw new IllegalArgumentException(urlString);
68         }
69         _protocol = Protocol.getNumber(urlString.substring(0, cssPos));
70         try
71         {
72             URL url = new URL("http" + urlString.substring(cssPos));
73             _host = url.getHost();
74             _port = url.getPort();
75             if (_port == -1)
76             {
77                 switch (_protocol)
78                 {
79                     case Protocol.HTTP:
80                         _port = 80; // see http://www.iana.org/assignments/port-numbers
81
break;
82                     case Protocol.HTTPS:
83                         _port = 443; // see http://www.iana.org/assignments/port-numbers
84
break;
85                     case Protocol.IIOP:
86                         _port = 683; // see http://www.iana.org/assignments/port-numbers
87
break;
88                     case Protocol.IIOPS:
89                         _port = 684; // see http://www.iana.org/assignments/port-numbers
90
break;
91                     default:
92                         throw new IllegalStateException("url = " + urlString);
93                 }
94             }
95             _objectKey = url.getFile();
96             if (_objectKey == null)
97             {
98                 _objectKey = "";
99             }
100             int queryPos = _objectKey.indexOf('?');
101             if (queryPos != -1)
102             {
103                 _objectKey = _objectKey.substring(0, queryPos);
104             }
105             _objectKey = StringUtil.removePrefix(_objectKey, "/");
106             if (_objectKey.length() == 0)
107             {
108                 _objectKey = "NameService";
109             }
110             String query = url.getQuery();
111             if (query == null)
112             {
113                 query = "";
114             }
115             String props = StringUtil.removePrefix(query, "?").replace('&', ',');
116             _properties = new NamedValueList(props).getProperties();
117         }
118         catch (Exception ex)
119         {
120             throw new SystemException(ex);
121         }
122     }
123
124     // public methods
125

126     public int getProtocol()
127     {
128         return _protocol;
129     }
130
131     public String getHost()
132     {
133         return _host;
134     }
135
136     public int getPort()
137     {
138         return _port;
139     }
140
141     public String getObjectKey()
142     {
143         return _objectKey;
144     }
145
146     public PropertyMap getProperties()
147     {
148         return _properties;
149     }
150
151     public String toString()
152     {
153         return Protocol.getName(_protocol) + "://" + _host + ":" + _port + "/" + _objectKey;
154     }
155 }
156
Popular Tags