1 30 package org.objectweb.carol.cmi; 31 32 import java.net.MalformedURLException ; 33 34 public class NamingContext { 35 public String scheme = ""; 36 public NamingContextHostPort[] hp; 37 public String name = ""; 38 39 public NamingContext(String input) throws MalformedURLException { 40 if (input == null || input.length() == 0) { 41 throw new MalformedURLException ("null or empty registry URL"); 42 } 43 if (input.indexOf("//") == -1) { 44 throw new MalformedURLException ( 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 e) { 51 throw new MalformedURLException ( 52 "badly formed registry URL " + input + " - " + e.getMessage()); 53 } catch (Exception e) { 54 throw new MalformedURLException ( 55 "badly formed registry URL " + input); 56 } 57 } 58 59 private void parseScheme(String inputscheme) throws MalformedURLException { 60 if (inputscheme.length() == 0) { this.scheme = ""; 62 } else { 63 if (inputscheme.length() > 1 && inputscheme.endsWith(":")) { 64 this.scheme = 66 inputscheme.substring(0, inputscheme.length() - 1); 67 } else { 68 throw new MalformedURLException ("badly formed protocol"); 69 } 70 if (!scheme.equals("cmi")) { 71 throw new MalformedURLException ("Invalid protocol : " + scheme); 72 } 73 } 74 } 75 76 private void parseName(String inputurl) throws MalformedURLException { 77 String inputname = ""; 79 String inputhostport = ""; 80 int n = inputurl.indexOf("/", 2); if (n == -1) { if (inputurl.length() > 2) { inputhostport = inputurl.substring(2); 84 } 85 } 86 if (n == 2) { if (inputurl.length() > 3) { inputname = inputurl.substring(3); 89 } else { throw new MalformedURLException ("non-empty name expected after third /"); 91 } 92 } 93 if (n > 2) { 94 if (inputurl.length() > n + 1) { 96 inputname = inputurl.substring(n + 1); 98 } else { throw new MalformedURLException ("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 inputhostport, java.util.ArrayList hp) 108 throws MalformedURLException { 109 String inputhost = ""; 110 String inputport = ""; 111 int m = inputhostport.indexOf(':'); 112 if (m == -1) { inputhost = inputhostport; 114 } else { 115 if (m == 0) { if (inputhostport.length() > 1) { 117 inputport = inputhostport.substring(1); 119 } else { throw new MalformedURLException ("non-empty port expected after :"); 121 } 122 } else { inputhost = inputhostport.substring(0, m); 124 if (inputhostport.length() > m + 1) { inputport = inputhostport.substring(m + 1); 126 } else { throw new MalformedURLException ("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 e) { 139 throw new MalformedURLException ("port must be a number"); 140 } 141 } 142 hp.add(nchp); 143 } 144 145 private void parseHostsPorts(String inputhostsports) 146 throws MalformedURLException { 147 int start = 0; 148 java.util.ArrayList hostsports = new java.util.ArrayList (); 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 |