KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > cmi > NamingContext


1 /***
2  * Jonathan: an Open Distributed Processing Environment
3  * Copyright (C) 1999 France Telecom R&D
4  * Copyright (C) 2002, Simon Nieuviarts
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Release: 2.0
21  *
22  * Contact: jonathan@objectweb.org
23  *
24  * Author: Kathleen Milsted
25  *
26  * with contributions from:
27  * Bruno Dumant
28  *
29  */

30 package org.objectweb.carol.cmi;
31
32 import java.net.MalformedURLException JavaDoc;
33
34 public class NamingContext {
35     public String JavaDoc scheme = "";
36     public NamingContextHostPort[] hp;
37     public String JavaDoc name = "";
38
39     public NamingContext(String JavaDoc input) throws MalformedURLException JavaDoc {
40         if (input == null || input.length() == 0) {
41             throw new MalformedURLException JavaDoc("null or empty registry URL");
42         }
43         if (input.indexOf("//") == -1) {
44             throw new MalformedURLException JavaDoc(
45                 "badly formed registry URL " + input);
46         }
47         try {
48             this.parseScheme(input.substring(0, input.indexOf("//")));
49             this.parseName(input.substring(input.indexOf("//")));
50         } catch (MalformedURLException JavaDoc e) {
51             throw new MalformedURLException JavaDoc(
52                 "badly formed registry URL " + input + " - " + e.getMessage());
53         } catch (Exception JavaDoc e) {
54             throw new MalformedURLException JavaDoc(
55                 "badly formed registry URL " + input);
56         }
57     }
58
59     private void parseScheme(String JavaDoc inputscheme) throws MalformedURLException JavaDoc {
60         if (inputscheme.length() == 0) { // scheme can be empty
61
this.scheme = "";
62         } else {
63             if (inputscheme.length() > 1 && inputscheme.endsWith(":")) {
64                 // non-empty scheme must contain at least one character and end with :
65
this.scheme =
66                     inputscheme.substring(0, inputscheme.length() - 1);
67             } else {
68                 throw new MalformedURLException JavaDoc("badly formed protocol");
69             }
70             if (!scheme.equals("cmi")) {
71                 throw new MalformedURLException JavaDoc("Invalid protocol : " + scheme);
72             }
73         }
74     }
75
76     private void parseName(String JavaDoc inputurl) throws MalformedURLException JavaDoc {
77         // inputurl is expected to start with //
78
String JavaDoc inputname = "";
79         String JavaDoc inputhostport = "";
80         int n = inputurl.indexOf("/", 2); // find third / if any
81
if (n == -1) { // no name
82
if (inputurl.length() > 2) { // host and/or port specified
83
inputhostport = inputurl.substring(2);
84             }
85         }
86         if (n == 2) { // no host, no port
87
if (inputurl.length() > 3) { // non-empty name preceded by /
88
inputname = inputurl.substring(3);
89             } else { // empty name preceded by / ; URL consists of ///
90
throw new MalformedURLException JavaDoc("non-empty name expected after third /");
91             }
92         }
93         if (n > 2) {
94             // possibly non-empty host, non-empty port, non-empty name
95
if (inputurl.length() > n + 1) {
96                 // non-empty name preceded by /
97
inputname = inputurl.substring(n + 1);
98             } else { // empty name preceded by / ; URL consists of //hostport/
99
throw new MalformedURLException JavaDoc("non-empty name expected after third /");
100             }
101             inputhostport = inputurl.substring(2, n);
102         }
103         this.name = inputname;
104         this.parseHostsPorts(inputhostport);
105     }
106
107     private void parseHostPort(String JavaDoc inputhostport, java.util.ArrayList JavaDoc hp)
108         throws MalformedURLException JavaDoc {
109         String JavaDoc inputhost = "";
110         String JavaDoc inputport = "";
111         int m = inputhostport.indexOf(':');
112         if (m == -1) { // no port
113
inputhost = inputhostport;
114         } else {
115             if (m == 0) { // no host
116
if (inputhostport.length() > 1) {
117                     // non-empty port preceded by /
118
inputport = inputhostport.substring(1);
119                 } else { // empty port preceded by : ; URL consists of //:
120
throw new MalformedURLException JavaDoc("non-empty port expected after :");
121                 }
122             } else { // non-empty host, maybe non-empty port
123
inputhost = inputhostport.substring(0, m);
124                 if (inputhostport.length() > m + 1) { // port specified
125
inputport = inputhostport.substring(m + 1);
126                 } else { // empty port preceded by : ; URL consists of //host:/
127
throw new MalformedURLException JavaDoc("non-empty port expected after :");
128                 }
129             }
130         }
131         NamingContextHostPort nchp = new NamingContextHostPort();
132         if (!inputhost.equals("")) {
133             nchp.host = inputhost;
134         }
135         if (!inputport.equals("")) {
136             try {
137                 nchp.port = Integer.parseInt(inputport);
138             } catch (NumberFormatException JavaDoc e) {
139                 throw new MalformedURLException JavaDoc("port must be a number");
140             }
141         }
142         hp.add(nchp);
143     }
144
145     private void parseHostsPorts(String JavaDoc inputhostsports)
146         throws MalformedURLException JavaDoc {
147         int start = 0;
148         java.util.ArrayList JavaDoc hostsports = new java.util.ArrayList JavaDoc();
149         do {
150             int end = inputhostsports.indexOf(',', start);
151             if (end < 0)
152                 parseHostPort(inputhostsports.substring(start), hostsports);
153             else
154                 parseHostPort(
155                     inputhostsports.substring(start, end),
156                     hostsports);
157             start = end + 1;
158         } while (start > 0);
159         int n = hostsports.size();
160         hp = new NamingContextHostPort[n];
161         for (int i = 0; i < n; i++)
162             hp[i] = (NamingContextHostPort) hostsports.get(i);
163     }
164 }
165
Popular Tags