FileDocCategorySizeDatePackage
corbanameURLContextFactory.javaAPI DocGlassfish v2 API11230Fri May 04 22:36:18 BST 2007com.sun.jndi.url.corbaname

corbanameURLContextFactory

public class corbanameURLContextFactory extends Object implements ObjectFactory
A corbaname URL context factory. (INS 12-03-99) = "corbaname:" ["#" ] = stringified Name | empty_string = ["/" ] = [ ","]* = | = | = "rir:" = = ":" | "iiop:" = [":" ] = DNS-style host name | IP address = "." "@" | empty_string = number = number = number = | empty_string NOTES: 1. Does NOT support having a in its . 2. Does NOT support multiple "rir" in single URL
author
Rosanna Lee

Fields Summary
static Logger
_logger
private static final String
DEFAULT_HOST
private static final String
DEFAULT_PORT
public static final String[]
tests
Constructors Summary
Methods Summary
public java.lang.ObjectgetObjectInstance(java.lang.Object urlInfo, javax.naming.Name name, javax.naming.Context nameCtx, java.util.Hashtable env)


           
				        

	if (urlInfo == null) {
	    return new corbanameURLContext(env);
	}
	if (urlInfo instanceof String) {
	    return getUsingURL((String)urlInfo, env);
	} else if (urlInfo instanceof String[]) {
	    return getUsingURLs((String[])urlInfo, env);
	} else {
	    throw (new IllegalArgumentException(
		    "iiopURLContextFactory.getObjectInstance: " +
		    "argument must be an iiop URL String or array of iiop URLs"));
	}
    
private static java.lang.ObjectgetUsingURL(java.lang.String url, java.util.Hashtable env)

	ResolveResult res = getUsingURLIgnoreRest(url, env);

	Context ctx = (Context)res.getResolvedObj();
	try {
	    return ctx.lookup(res.getRemainingName());
	} finally {
	    ctx.close();
	}
    
static javax.naming.spi.ResolveResultgetUsingURLIgnoreRest(java.lang.String url, java.util.Hashtable env)
Resolves 'name' into a target context with remaining name. It only resolves the hostname/port number. The remaining name contains the rest of the name found in the URL. For example, with a corbaname URL: corbaname:iiop://localhost:900#rest/of/name this method resolves corbaname:iiop://localhost:900/ to the "NameService" context on for the ORB at 'localhost' on port 900, and returns as the remaining name "rest/of/name".


	ORB inOrb = (env != null) 
	    ? (ORB) env.get("java.naming.corba.orb") 
	    : null;
		
	if (inOrb != null) {
	    try {
		CorbanameUrl parsedUrl = new CorbanameUrl(url);

		// See if ORB can handle corbaname URL directly
		org.omg.CORBA.Object ncRef = null;

		try {
		    // Get object ref for NameService specified in corbaname URL
		    ncRef = inOrb.string_to_object(parsedUrl.getLocation());
		} catch (Exception e) {
		}

		if (ncRef != null) {
		    // Convert to JNDI Context
		    Context ctx = CNCtxHelper.getInstance(inOrb, ncRef, env);

		    return new ResolveResult(ctx, parsedUrl.getCosName());
		}
	    } catch (MalformedURLException e) {
		throw new ConfigurationException(e.getMessage());
	    }
	}

	// Rewrite to iiop URL for handling by JNDI/COS provider
	url = rewriteUrl(url);	
	    
	return CNCtx.createUsingURL(url, env);
    
private static java.lang.ObjectgetUsingURLs(java.lang.String[] urls, java.util.Hashtable env)

	for (int i = 0; i < urls.length; i++) {
	    String url = urls[i];
	    try {
		Object obj = getUsingURL(url, env);
		if (obj != null) {
		    return obj;
		}
	    } catch (NamingException e) {
	    }
	}
	return null;	// %%% exception??
    
public static voidmain(java.lang.String[] args)


         

	for (int i = 0; i < tests.length; i++) {
      /** IASRI 4660742
	    System.out.println(tests[i]);
      **/
      //START OF IASRI 4660742
      if (_logger.isLoggable(Level.FINE))
          _logger.log(Level.FINE,tests[i]);
      //END OF IASRI 4660742

	    try {
    /** IASRI 4660742
		System.out.println("    " + rewriteUrl(tests[i]));
    **/
    //START OF IASRI 4660742
		if (_logger.isLoggable(Level.FINE))
   	 _logger.log(Level.FINE,"    " + rewriteUrl(tests[i]));
    //END OF IASRI 4660742
	    } catch (NamingException e) {
    /** IASRI 4660742
		System.out.println("    " + e);
    **/
    //START OF IASRI 4660742
    _logger.log(Level.SEVERE,"java_jndi.excep_in_main", e);
    //END OF IASRI 4660742
	    }
	}
    
static java.lang.StringrewriteUrl(java.lang.String url)
corbaname:rir: -> iiopname://localhost:2089/ corbaname:rir:/NameService -> iiopname://localhost:2089/ corbaname:rir:/dev/NameService -> InvalidNameException (key_string not supported) corbaname:: -> iiopname://localhost:2089/ corbaname::orbhost:999#this/is/a/name -> iiopname://orbhost:999/this/is/a/name corbaname::orbhost:999,:webhost#this/is/a/name -> iiopname://orbhost:999,webhost:2089/this/is/a/name corbaname::orbhost:999,:webhost/key/String#this/is/a/name -> InvalidNameException (key_string not supported) corbaname:iiop: -> iiopname://localhost:2089/ corbaname:iiop:orbhost:999#this/is/a/name -> iiopname://orbhost:999/this/is/a/name corbaname:iiop:orbhost:999,iiop:webhost#this/is/a/name -> iiopname://orbhost:999,webhost:2089/this/is/a/name corbaname:iiop:orbhost:999,iiop:webhost/key/String#this/is/a/name -> InvalidNameException (key_string not supported) corbaname:iiop:orbhost:999,:webhost#this/is/a/name -> iiopname://orbhost:999,webhost:2089/this/is/a/name corbaname::orbhost:999,iiop:webhost#this/is/a/name -> iiopname://orbhost:999,webhost:2089/this/is/a/name


	// Find string_name
	String stringName = null, corbaloc;
	int hash = url.indexOf('#");
	if (hash >= 0) {
	    stringName = url.substring(hash+1);

	    // get rid of 'corbaname:' and string_name
	    corbaloc = url.substring(10, hash);  
	} else {
	    corbaloc = url.substring(10);   // get rid of 'corbaname:'
	}

	// Make sure key_string is one that we can support
	String objAddrList;
	int firstSlash = corbaloc.indexOf('/");
	if (firstSlash >= 0) {
	    String keyString = corbaloc.substring(firstSlash+1); // skip slash

	    // An empty key_string is interpreted as "NameService"
	    if ("".equals(keyString)) {
		keyString = "NameService";
	    } else if (!"NameService".equals(keyString)) {
		throw new InvalidNameException(
		    "Support is available only for the NameService key string");
	    }

	    objAddrList = corbaloc.substring(0, firstSlash);
	} else {
	    objAddrList = corbaloc;
	}
	    
	// Rewrite objAddrList into iiopname format

	int len = objAddrList.length();
	int colon, start = 0, comma;
	String prot, addr;
	StringBuffer newUrl = new StringBuffer("iiopname://");
	while (start < len) {
	    colon = objAddrList.indexOf(':", start);
	    prot = objAddrList.substring(start, colon);
	    if (prot.equals("") || prot.equals("iiop")) {
		// Find end of this address
		comma = objAddrList.indexOf(',", colon+1);
		if (comma < 0) {
		    // last address in list
		    addr = objAddrList.substring(colon+1, len);
		    start = len;
		} else {
		    addr = objAddrList.substring(colon+1, comma);
		    start = comma + 1;
		}

		newUrl.append(addr);

		// Add default port if none has been specified
		if (addr.indexOf(':") < 0) {
		    newUrl.append(':");
		    newUrl.append(DEFAULT_PORT);
		}

		if (comma >= 0) {
		    // add comma
		    newUrl.append(',");
		}
	    } else if (prot.equals("rir")) {
		newUrl.append(DEFAULT_HOST);
		newUrl.append(':");
		newUrl.append(DEFAULT_PORT);

		start = colon + 1; // skip colon

		if (start != len) {
		    throw new InvalidNameException("Only one rir is supported");
		}
	    } else {
		throw new InvalidNameException("Unknown subscheme: " + url);
	    }
	}

	if (stringName != null) {
	    newUrl.append('/");
	    newUrl.append(stringName);
	}

	return newUrl.toString();