KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > rmi > iiop > client > UrlInfo


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
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 implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.rmi.iiop.client;
19
20 import java.net.URL JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.geronimo.interop.SystemException;
26 import org.apache.geronimo.interop.properties.PropertyMap;
27 import org.apache.geronimo.interop.rmi.iiop.Protocol;
28 import org.apache.geronimo.interop.util.ListUtil;
29 import org.apache.geronimo.interop.util.NamedValueList;
30 import org.apache.geronimo.interop.util.StringUtil;
31
32 public class UrlInfo {
33
34     private String JavaDoc namePrefix = "";
35     private int ver;
36     
37     final static int IIOP_1_0 = 0;
38     final static int IIOP_1_1 = 1;
39     final static int IIOP_1_2 = 2;
40
41     public static UrlInfo getInstance(String JavaDoc url) {
42         UrlInfo object = new UrlInfo();
43         object.init(url);
44         return object;
45     }
46
47     public static List JavaDoc getList(String JavaDoc urls) {
48         ArrayList JavaDoc list = new ArrayList JavaDoc(3);
49         for (Iterator JavaDoc i = ListUtil.getCommaSeparatedList(urls).iterator(); i.hasNext();) {
50             String JavaDoc url = (String JavaDoc) i.next();
51             list.add(getInstance(url));
52         }
53         return list;
54     }
55
56     private int protocol;
57
58     private String JavaDoc host;
59
60     private int port;
61
62     private String JavaDoc objectKey;
63
64     private PropertyMap properties;
65
66     private boolean handleUrl(String JavaDoc url)
67     {
68         if(!UrlScheme.canProcess(url))
69             return false;
70
71         UrlScheme us = new UrlScheme(url);
72         us.process();
73         
74         host = us.getHost(0);
75         port = us.getPort(0);
76         ver = us.getVersion(0);
77         objectKey = us.getObjectKey();
78         namePrefix = us.getNamePrefix();
79         properties = new PropertyMap();
80         protocol = Protocol.IIOP;
81         
82         return true;
83     }
84
85     protected void init(String JavaDoc urlString)
86     {
87         if(handleUrl(urlString))
88         {
89             return;
90         }
91
92         int cssPos = urlString.indexOf("://");
93         if (cssPos == -1) {
94             throw new IllegalArgumentException JavaDoc(urlString);
95         }
96         protocol = Protocol.getNumber(urlString.substring(0, cssPos));
97         try {
98             URL JavaDoc url = new URL JavaDoc("http" + urlString.substring(cssPos));
99             host = url.getHost();
100             port = url.getPort();
101             if (port == -1) {
102                 switch (protocol) {
103                     case Protocol.HTTP:
104                         port = 80; // see http://www.iana.org/assignments/port-numbers
105
break;
106                     case Protocol.HTTPS:
107                         port = 443; // see http://www.iana.org/assignments/port-numbers
108
break;
109                     case Protocol.IIOP:
110                         port = 683; // see http://www.iana.org/assignments/port-numbers
111
break;
112                     case Protocol.IIOPS:
113                         port = 684; // see http://www.iana.org/assignments/port-numbers
114
break;
115                     default:
116                         throw new IllegalStateException JavaDoc("url = " + urlString);
117                 }
118             }
119             objectKey = url.getFile();
120             if (objectKey == null) {
121                 objectKey = "";
122             }
123             int queryPos = objectKey.indexOf('?');
124             if (queryPos != -1) {
125                 objectKey = objectKey.substring(0, queryPos);
126             }
127             objectKey = StringUtil.removePrefix(objectKey, "/");
128             if (objectKey.length() == 0) {
129                 objectKey = "NameService";
130             }
131             String JavaDoc query = url.getQuery();
132             if (query == null) {
133                 query = "";
134             }
135             String JavaDoc props = StringUtil.removePrefix(query, "?").replace('&', ',');
136             properties = new NamedValueList(props).getProperties();
137         } catch (Exception JavaDoc ex) {
138             throw new SystemException(ex);
139         }
140     }
141
142     public int getProtocol() {
143         return protocol;
144     }
145
146     public String JavaDoc getHost() {
147         return host;
148     }
149
150     public int getPort() {
151         return port;
152     }
153
154     public String JavaDoc getObjectKey() {
155         return objectKey;
156     }
157
158     public PropertyMap getProperties() {
159         return properties;
160     }
161
162     public String JavaDoc getNamePrefix()
163     {
164         return namePrefix;
165     }
166
167     public int getVersion()
168     {
169         return ver;
170     }
171
172     public String JavaDoc toString() {
173         return Protocol.getName(protocol) + "://" + host + ":" + port + "/" + objectKey;
174     }
175 }
176
Popular Tags