1 23 24 package com.sun.jndi.url.corbaname; 25 26 import javax.naming.*; 27 import javax.naming.spi.*; 28 29 import java.util.Hashtable ; 30 import java.net.MalformedURLException ; 31 import com.sun.jndi.cosnaming.CNCtx; 32 import org.omg.CORBA.ORB ; 33 34 import java.util.logging.*; 36 import com.sun.logging.*; 37 39 65 66 public class corbanameURLContextFactory implements ObjectFactory { 67 static Logger _logger=LogDomains.getLogger(LogDomains.JNDI_LOGGER); 69 71 private static final String DEFAULT_HOST = "localhost"; 72 private static final String DEFAULT_PORT = "2089"; 73 74 public Object getObjectInstance(Object urlInfo, Name name, Context nameCtx, 75 Hashtable env) throws Exception { 76 77 if (urlInfo == null) { 78 return new corbanameURLContext(env); 79 } 80 if (urlInfo instanceof String ) { 81 return getUsingURL((String )urlInfo, env); 82 } else if (urlInfo instanceof String []) { 83 return getUsingURLs((String [])urlInfo, env); 84 } else { 85 throw (new IllegalArgumentException ( 86 "iiopURLContextFactory.getObjectInstance: " + 87 "argument must be an iiop URL String or array of iiop URLs")); 88 } 89 } 90 91 103 static ResolveResult getUsingURLIgnoreRest(String url, Hashtable env) 104 throws NamingException { 105 106 ORB inOrb = (env != null) 107 ? (ORB ) env.get("java.naming.corba.orb") 108 : null; 109 110 if (inOrb != null) { 111 try { 112 CorbanameUrl parsedUrl = new CorbanameUrl(url); 113 114 org.omg.CORBA.Object ncRef = null; 116 117 try { 118 ncRef = inOrb.string_to_object(parsedUrl.getLocation()); 120 } catch (Exception e) { 121 } 122 123 if (ncRef != null) { 124 Context ctx = CNCtxHelper.getInstance(inOrb, ncRef, env); 126 127 return new ResolveResult(ctx, parsedUrl.getCosName()); 128 } 129 } catch (MalformedURLException e) { 130 throw new ConfigurationException(e.getMessage()); 131 } 132 } 133 134 url = rewriteUrl(url); 136 137 return CNCtx.createUsingURL(url, env); 138 } 139 140 private static Object getUsingURL(String url, Hashtable env) 141 throws NamingException { 142 ResolveResult res = getUsingURLIgnoreRest(url, env); 143 144 Context ctx = (Context)res.getResolvedObj(); 145 try { 146 return ctx.lookup(res.getRemainingName()); 147 } finally { 148 ctx.close(); 149 } 150 } 151 152 private static Object getUsingURLs(String [] urls, Hashtable env) { 153 for (int i = 0; i < urls.length; i++) { 154 String url = urls[i]; 155 try { 156 Object obj = getUsingURL(url, env); 157 if (obj != null) { 158 return obj; 159 } 160 } catch (NamingException e) { 161 } 162 } 163 return null; } 165 166 167 194 static String rewriteUrl(String url) throws NamingException { 195 196 String stringName = null, corbaloc; 198 int hash = url.indexOf('#'); 199 if (hash >= 0) { 200 stringName = url.substring(hash+1); 201 202 corbaloc = url.substring(10, hash); 204 } else { 205 corbaloc = url.substring(10); } 207 208 String objAddrList; 210 int firstSlash = corbaloc.indexOf('/'); 211 if (firstSlash >= 0) { 212 String keyString = corbaloc.substring(firstSlash+1); 214 if ("".equals(keyString)) { 216 keyString = "NameService"; 217 } else if (!"NameService".equals(keyString)) { 218 throw new InvalidNameException( 219 "Support is available only for the NameService key string"); 220 } 221 222 objAddrList = corbaloc.substring(0, firstSlash); 223 } else { 224 objAddrList = corbaloc; 225 } 226 227 229 int len = objAddrList.length(); 230 int colon, start = 0, comma; 231 String prot, addr; 232 StringBuffer newUrl = new StringBuffer ("iiopname://"); 233 while (start < len) { 234 colon = objAddrList.indexOf(':', start); 235 prot = objAddrList.substring(start, colon); 236 if (prot.equals("") || prot.equals("iiop")) { 237 comma = objAddrList.indexOf(',', colon+1); 239 if (comma < 0) { 240 addr = objAddrList.substring(colon+1, len); 242 start = len; 243 } else { 244 addr = objAddrList.substring(colon+1, comma); 245 start = comma + 1; 246 } 247 248 newUrl.append(addr); 249 250 if (addr.indexOf(':') < 0) { 252 newUrl.append(':'); 253 newUrl.append(DEFAULT_PORT); 254 } 255 256 if (comma >= 0) { 257 newUrl.append(','); 259 } 260 } else if (prot.equals("rir")) { 261 newUrl.append(DEFAULT_HOST); 262 newUrl.append(':'); 263 newUrl.append(DEFAULT_PORT); 264 265 start = colon + 1; 267 if (start != len) { 268 throw new InvalidNameException("Only one rir is supported"); 269 } 270 } else { 271 throw new InvalidNameException("Unknown subscheme: " + url); 272 } 273 } 274 275 if (stringName != null) { 276 newUrl.append('/'); 277 newUrl.append(stringName); 278 } 279 280 return newUrl.toString(); 281 } 282 283 public static final String [] tests = { 284 "corbaname:rir:", 285 "corbaname:rir:/NameService", 286 "corbaname:rir:/dev/NameService", 287 "corbaname:rir:,rir:/NameService", 288 "corbaname::", 289 "corbaname::orbhost:999#this/is/a/name", 290 "corbaname::orbhost:999,:webhost#this/is/a/name", 291 "corbaname::orbhost:999,:webhost/key/String#this/is/a/name", 292 "corbaname:iiop:", 293 "corbaname:iiop:orbhost:999#this/is/a/name", 294 "corbaname:iiop:orbhost:999,iiop:webhost#this/is/a/name", 295 "corbaname:iiop:orbhost:999,iiop:webhost/key/String#this/is/a/name", 296 "corbaname:iiop:orbhost:999,:webhost#this/is/a/name", 297 "corbaname::orbhost:999,iiop:webhost#this/is/a/name", 298 "corbaname:foo:bar#this/is/a/name" 299 }; 300 301 public static void main(String [] args) { 302 303 for (int i = 0; i < tests.length; i++) { 304 307 if (_logger.isLoggable(Level.FINE)) 309 _logger.log(Level.FINE,tests[i]); 310 312 try { 313 316 if (_logger.isLoggable(Level.FINE)) 318 _logger.log(Level.FINE," " + rewriteUrl(tests[i])); 319 } catch (NamingException e) { 321 324 _logger.log(Level.SEVERE,"java_jndi.excep_in_main", e); 326 } 328 } 329 } 330 } 331 | Popular Tags |