FileDocCategorySizeDatePackage
SerialContext.javaAPI DocGlassfish v2 API29149Fri May 04 22:35:08 BST 2007com.sun.enterprise.naming

SerialContext

public class SerialContext extends Object implements Context
This is a JNDI Context implementation for storing serializable objects. This is the default Context for the J2EE RI. Lookups of unqualified names (i.e. names not starting with "java:", "corbaname:" etc) are serviced by SerialContext. The namespace is implemented in the J2EE server in the SerialContextProviderImpl object, which is accessed over RMI-IIOP from clients.

NOT THREAD SAFE: mutable instance variables

Fields Summary
private static final Logger
_logger
private static final NameParser
myParser
private Hashtable
myEnv
private SerialContextProvider
provider
private static Hashtable
providerCache
private final String
myName
private final com.sun.enterprise.naming.java.javaURLContext
javaUrlContext
private static final String
JAVA_URL
private static final Boolean
threadlock
private static final ThreadLocal
stickyContext
private final boolean
isEE
Constructors Summary
public SerialContext(String name, Hashtable environment)
Constructor for the context. Initializes the object reference to the remote provider object.

     
        myEnv = (environment != null)
	    ? (Hashtable)(environment.clone()) 
	    : null;
      
        // Dont initialize provider now, this throws an exception
        // if J2EEServer is not yet started. Get it lazily when needed.
        //provider = SerialContext.getProvider(myEnv);
        
        this.myName = name;
        if (_logger.isLoggable(Level.FINE))
            _logger.fine("SerialContext ==> SerialContext instance created : " + this);	

        // using these two temp variables allows instance variables
        // to be 'final'.
        javaURLContext  urlContextTemp = null;
        boolean         isEETemp    = false;
        if (myEnv.get("com.sun.appserv.ee.iiop.endpointslist") != null) {
            isEETemp = true;
            urlContextTemp = new javaURLContext(myEnv, this);
        }
        else {
            urlContextTemp = new javaURLContext(myEnv, null);
        }
        javaUrlContext  = urlContextTemp;
        isEE    = isEETemp;
    
public SerialContext(Hashtable env)
This constructor takes the component id as an argument. All name arguments to operations are prepended by the component id.

        this("", env);
    
Methods Summary
public java.lang.ObjectaddToEnvironment(java.lang.String propName, java.lang.Object propVal)
Add to the environment for the current context.

exception
NamingException if there is a naming exception.

        if (myEnv == null) {
            myEnv = new Hashtable(5, 0.75f);
        } 
        return myEnv.put(propName, propVal);
    
public voidbind(java.lang.String name, java.lang.Object obj)
Bind the object to the specified name.

param
the name that the object is being bound to.
param
the object that is being bound.
exception
NamingException if there is a naming exception.


	name = getRelativeName(name);
	if (isjavaURL(name)) {	    
	    javaUrlContext.bind(name, obj);
	} else {
	    try {
	        getProvider().bind(name, obj);
	    } catch (RemoteException ex) {
	        throw new CommunicationException(ex.toString());
	    }
	}
    
public voidbind(javax.naming.Name name, java.lang.Object obj)
Bind the object to the specified name.

param
the name that the object is being bound to.
param
the object that is being bound.
exception
NamingException if there is a naming exception.

        // Flat namespace; no federation; just call string version
        bind(name.toString(), obj);
    
public voidclose()
Set the environment for the current context to null when close is called.

exception
NamingException if there is a naming exception.

        myEnv = null;
    
public java.lang.StringcomposeName(java.lang.String name, java.lang.String prefix)

        Name result = composeName(new CompositeName(name),
                                  new CompositeName(prefix));
        return result.toString();
    
public javax.naming.NamecomposeName(javax.naming.Name name, javax.naming.Name prefix)

        Name result = (Name)(prefix.clone());
        result.addAll(name);
        return result;
    
public javax.naming.ContextcreateSubcontext(java.lang.String name)
Create the specified subcontext.

return
the created subcontext.
param
the name of the subcontext.
exception
NamingException if there is a naming exception.

	Context c = null;
	name = getRelativeName (name);
	if (isjavaURL(name)) {	    
	    return javaUrlContext.createSubcontext(name);
	} else {
	    try {
	        c = getProvider().createSubcontext(name);
		/* this simulates the transient context structure on the
		 * client side. Have to do this - as reference to
		 * Transient Context is not resolved properly due to rmi
		 */
		if (c instanceof Context){
		    c = new SerialContext (name, myEnv);
		}
	    } catch(RemoteException e) {
	        CommunicationException ce = 
		  new CommunicationException(e.toString());
		ce.initCause(e);
		throw ce;
	    }
	    return c;
	}
    
public javax.naming.ContextcreateSubcontext(javax.naming.Name name)
Create the specified subcontext.

return
the created subcontext.
param
the name of the subcontext.
exception
NamingException if there is a naming exception.

        // Flat namespace; no federation; just call string version
        return createSubcontext(name.toString());
    
public voiddestroySubcontext(java.lang.String name)
Destroy the specified subcontext.

param
the name of the subcontext.
exception
NamingException if there is a naming exception.

	name = getRelativeName (name);
	if (isjavaURL(name)) {	    
	    javaUrlContext.destroySubcontext(name);
	} else {
	    try {
	        getProvider().destroySubcontext(name);
	    } catch(RemoteException e) {
	        CommunicationException ce = 
		  new CommunicationException(e.toString());
		ce.initCause(e);
		throw ce;
	    }
	}
    
public voiddestroySubcontext(javax.naming.Name name)
Destroy the specified subcontext.

param
the name of the subcontext.
exception
NamingException if there is a naming exception.

        // Flat namespace; no federation; just call string version
        destroySubcontext(name.toString());
    
public voidgenerateEntryContext(java.lang.Object context)
Generates an entry context and give it to ondemand initialization framework.

        ServerEntryHelper.generateJndiEntryContext((String) context);
    
public java.util.HashtablegetEnvironment()
Return the environment for the current context.

exception
NamingException if there is a naming exception.

        if (myEnv == null) {
            // Must return non-null
            myEnv = new Hashtable(3, 0.75f);
        }
        return myEnv;
    
public java.lang.StringgetNameInNamespace()
The getNameInNamespace API is not supported in this context.

exception
NamingException if there is a naming exception.

	return myName;
    
public javax.naming.NameParsergetNameParser(java.lang.String name)
Allow access to the name parser object.

param
String JNDI name, is ignored since there is only one Name Parser object.
exception
NamingException
return
NameParser object

        return myParser;
    
public javax.naming.NameParsergetNameParser(javax.naming.Name name)
Allow access to the name parser object.

param
String JNDI name, is ignored since there is only one Name Parser object.
exception
NamingException
return
NameParser object

        // Flat namespace; no federation; just call string version
        return getNameParser(name.toString());
    
private SerialContextProvidergetProvider()


	try {
	    if (provider == null) {
		if (Switch.getSwitch().getContainerType() ==
		    Switch.EJBWEB_CONTAINER) {
		    if (_logger.isLoggable(Level.FINE)) {
			_logger.fine("lookup call inside the server VM...");
		    }
		    provider = Switch.getSwitch().getProviderManager().getLocalProvider();		   
		    
		} else {
		    if (_logger.isLoggable(Level.FINE)) {
			_logger.fine("lookup call outside the server VM...");
		    }
		    
		    // get SerialContextProvider from naming service
		    
		    // Get orb to use for connecting to NameService
		    ORB orb = (ORB) myEnv.get(ORBManager.JNDI_CORBA_ORB_PROPERTY);
		    
		    if ( orb == null ) {
			orb = ORBManager.getORB();
		    }
		    
		    if (_logger.isLoggable(Level.FINE))
			print(orb);
		    
		    org.omg.CORBA.Object ref = null;
		    //if the following is not null, then we are in EE world
		    //else PE
		    //if (myEnv.get("com.sun.appserv.ee.iiop.endpointslist") != null) {
		    if (isEE == true) {
			ref = orb.string_to_object((String)(myEnv.get(
					     "com.sun.appserv.ee.iiop.endpointslist")));
			provider = narrowProvider(ref);		  
			
		    } else {
			provider = (SerialContextProvider) providerCache.get(orb);
            // thread-safety: race condition
			if (provider == null) {
			    ref = orb.resolve_initial_references("NameService");
			    provider = narrowProvider(ref);
			    providerCache.put(orb, provider);
			}		   
		    }
		}				
	    }
	} catch (Exception ex) {
	    setSticky(null);
	    CommunicationException ce = new CommunicationException
		("Can't find SerialContextProvider");
	    ce.initCause(ex);
	    throw ce;
	}		  
	
	return provider;
    
private java.lang.StringgetRelativeName(java.lang.String name)

	if(!myName.equals("")) {
	    name = myName + "/" + name;
	} 
	return name;
    
public static com.sun.enterprise.naming.SerialContext$ThreadLocalICgetSticky()

        return (ThreadLocalIC) stickyContext.get();
    
public static javax.naming.ContextgetStickyContext()
This method is called from SerialInitContextFactory & S1ASCtxFactory to check if sticky context is set.

        return getSticky().getStickyContext();
    
private booleanisjavaURL(java.lang.String name)
method to check if the name to look up starts with "java:"

     
        if (name.startsWith(JAVA_URL)) {	    	    
	    return true;
	} else return false;
    
public javax.naming.NamingEnumerationlist(java.lang.String name)
List the contents of the specified context.

return
an enumeration of the contents.
param
the context name.
exception
NamingException if there is a naming exception.

        if (name.equals("")) {
            // listing this context
            try {
                Hashtable bindings = getProvider().list(myName);
                return new RepNames(bindings);
            } catch (RemoteException ex) {
                throw new CommunicationException(ex.toString());
            }
        } 

	name = getRelativeName(name);
	if (isjavaURL(name)) {	    
	    return javaUrlContext.list(name);
	} else {
	    // Perhaps 'name' names a context
	    Object target = lookup(name);
	    if (target instanceof Context) {
	        return ((Context)target).list("");
	    }
	    throw new NotContextException(name + " cannot be listed");
	}
    
public javax.naming.NamingEnumerationlist(javax.naming.Name name)
List the contents of the specified context.

return
an enumeration of the contents.
param
the context name.
exception
NamingException if there is a naming exception.

        // Flat namespace; no federation; just call string version
        return list(name.toString());
    
public javax.naming.NamingEnumerationlistBindings(java.lang.String name)
List the bindings in the specified context.

return
an enumeration of the bindings.
param
the context name.
exception
NamingException if there is a naming exception.

        if (name.equals("")) {
            // listing this context
            try {
                Hashtable bindings = getProvider().list(myName);
                return new RepBindings(bindings);
            } catch (RemoteException ex) {
                CommunicationException ce = 
                    new CommunicationException(ex.toString());
                ce.initCause(ex);
                throw ce;
            }
        } 

	name = getRelativeName(name);
	if (isjavaURL(name)) {	    
	    return javaUrlContext.listBindings(name);
	} else {
	    // Perhaps 'name' names a context
	    Object target = lookup(name);
	    if (target instanceof Context) {
	        return ((Context)target).listBindings("");
	    }
	    throw new NotContextException(name + " cannot be listed");
	}
    
public javax.naming.NamingEnumerationlistBindings(javax.naming.Name name)
List the bindings in the specified context.

return
an enumeration of the bindings.
param
the context name.
exception
NamingException if there is a naming exception.

        // Flat namespace; no federation; just call string version
        return listBindings(name.toString());
    
public java.lang.Objectlookup(java.lang.String name)
Lookup the specified name in the context. Returns the resolved object.

return
the resolved object.
exception
NamingException if there is a naming exception.


        //Before enything call ondemand initialization framework.
        generateEntryContext(name);

        /**
	 * In case a user is creating an IC with env passed
	 * in constructor; env specifies endpoints in some form
	 * in that case, the sticky IC should be stores as a thread local 
	 * variable. 
	 *
	 */
        if (myEnv.get("com.sun.appserv.ee.iiop.endpointslist") != null) {
	    if (getSticky() == null) {
	        ThreadLocalIC threadLocal = new ThreadLocalIC(this,1);	
		setSticky(threadLocal);
	    } else getSticky().incrementCount();
	}

        if (_logger.isLoggable(Level.FINE))
	    _logger.fine("SerialContext ==> doing lookup with " + this);
        if (name.equals("")) {
	    resetSticky();
            // Asking to look up this context itself.  Create and return
            // a new instance with its own independent environment.
            return (new SerialContext(myName, myEnv));
        } 
	name = getRelativeName(name);
        if (_logger.isLoggable(Level.FINE))
	    _logger.fine("SerialContext ==> looking up : " + name);

        try {
	    if (isjavaURL(name)) {
	        resetSticky();
		return javaUrlContext.lookup(name);
	    } else {	        
		Object obj = getProvider().lookup(name);
		if(obj instanceof Context) {
		    resetSticky();
		    return new SerialContext(name, myEnv);
		}
		Object retObj = 
		    javax.naming.spi.NamingManager.getObjectInstance(obj, 
								     new CompositeName(name),
								     null, myEnv);
		resetSticky();

		return retObj;
	    }
	} catch (NamingException nnfe) {
	    setSticky(null);
	    throw nnfe;
	} catch (Exception ex) {
	    setSticky(null);
            _logger.log(Level.SEVERE,
                        "enterprise_naming.serialctx_communication_exception",
                        ex);
	    //temp fix for 6320008
	    //this should be removed once we change the transient NS implementation to persistent
	    if (ex instanceof java.rmi.MarshalException && 
		ex.getCause() instanceof org.omg.CORBA.COMM_FAILURE) {
	        provider = null; 
		_logger.fine("Resetting provider to NULL. Will get new obj ref for provider since previous obj ref was stale...");
		return lookup(name);		
	    } else {
	        CommunicationException ce = 
		  new CommunicationException("serial context communication ex");
		ce.initCause(ex);
		throw ce;
	    }
	}
	
    
public java.lang.Objectlookup(javax.naming.Name name)
Lookup the specifed name in the context. Returns the resolved object.

return
the resolved object.
exception
NamingException if there is a naming exception.

        // Flat namespace; no federation; just call string version
        return lookup(name.toString()); 
    
public java.lang.ObjectlookupLink(java.lang.String name)
Links are not treated specially.

param
the name of the link.
return
the resolved object.
exception
NamingException if there is a naming exception.

	name = getRelativeName(name);
	if (isjavaURL(name)) {	    
	    return javaUrlContext.lookupLink(name);
	} else {
            // This flat context does not treat links specially
	    return lookup(name);
	}
    
public java.lang.ObjectlookupLink(javax.naming.Name name)
Links are not treated specially.

param
the name of the link.
return
the resolved object.
exception
NamingException if there is a naming exception.

        // Flat namespace; no federation; just call string version
        return lookupLink(name.toString());
    
private SerialContextProvidernarrowProvider(org.omg.CORBA.Object ref)
method to narrow down the provider. common code that is called from getProvider()


        NamingContext nctx = NamingContextHelper.narrow(ref);
	NameComponent[] path = 
	{ new NameComponent("SerialContextProvider", "") };
	synchronized (threadlock) {	
	    provider = (SerialContextProvider)
	      PortableRemoteObject.narrow(nctx.resolve(path),
					  SerialContextProvider.class);
	}
	return provider;
    
public voidprint(org.omg.CORBA.ORB orb)

        _logger.fine("SerialContext ==> SerialContext instance created : " + this);

	if (orb != null) {	
	    _logger.fine("SerialContext ==> ORB instance : " + orb);

	    String host = ((com.sun.corba.ee.impl.orb.ORBImpl)orb).
	      getORBData().getORBInitialHost();
	    int port = ((com.sun.corba.ee.impl.orb.ORBImpl)orb).
	      getORBData().getORBInitialPort();                    	
	    
	    _logger.fine("SerialContext ==> ORB HOST : " + host +
			       ", ORB PORT : " + port);
	} else _logger.fine("orb is null");
    
public voidrebind(java.lang.String name, java.lang.Object obj)
Rebind the object to the specified name.

param
the name that the object is being bound to.
param
the object that is being bound.
exception
NamingException if there is a naming exception.

	
	name = getRelativeName(name);
	if (isjavaURL(name)) {	    
	    javaUrlContext.rebind(name, obj);
	} else {
	    try {
	        getProvider().rebind(name, obj);
	    } catch (RemoteException ex) {
	      throw new CommunicationException(ex.toString());
	    }
	}
    
public voidrebind(javax.naming.Name name, java.lang.Object obj)
Rebind the object to the specified name.

param
the name that the object is being bound to.
param
the object that is being bound.
exception
NamingException if there is a naming exception.

        // Flat namespace; no federation; just call string version
        rebind(name.toString(), obj);
    
public java.lang.ObjectremoveFromEnvironment(java.lang.String propName)
Remove from the environment for the current context.

exception
NamingException if there is a naming exception.

        if (myEnv == null) {
            return null;
        }
        return myEnv.remove(propName);
    
public voidrename(java.lang.String oldname, java.lang.String newname)
Rename the bound object.

param
the old name that the object is bound as.
param
the new name that the object will be bound as.
exception
NamingException if there is a naming exception.

	oldname = getRelativeName(oldname);
	newname = getRelativeName(newname);
	if (isjavaURL(oldname)) {	    
	    javaUrlContext.rename(oldname, newname);
	} else {
	    try {
	        getProvider().rename(oldname, newname);
	    } catch (RemoteException ex) {
	        throw new CommunicationException(ex.toString());
	    }
	}
    
public voidrename(javax.naming.Name oldname, javax.naming.Name newname)
Rename the bound object.

param
the old name that the object is bound as.
param
the new name that the object will be bound as.
exception
NamingException if there is a naming exception.

        // Flat namespace; no federation; just call string version
        rename(oldname.toString(), newname.toString());
    
private voidresetSticky()
method for checking the count and decrementing it also resets the sticky context to null if count is 0

        if (getSticky() != null) {
	    getSticky().decrementCount();
	
	    if (getSticky().getStickyCount() == 0) {
	        setSticky(null);
	    }
	}
    
public static voidsetSticky(com.sun.enterprise.naming.SerialContext$ThreadLocalIC var)
set and get methods for preserving stickiness. This is a temporary solution to store the sticky IC as a thread local variable. This sticky IC will be used by all classes that require a context object to do lookup (if LB is enabled) SerialContext.lookup() sets a value for the thread local variable (stickyContext) before performing th lookup in case LB is enabled. If not, the thread local variable is null. At the end of the SerialContext.lookup() method, the thread local variable gets set to null. So actually speaking, more than being a global variable for the entire thread, its global only during the execution of the SerialContext.lookup() method. bug 5050591 This will be cleaned for the next release.


                                                                                                                                   
         
        stickyContext.set(var);
    
public voidunbind(java.lang.String name)
Unbind the object with the specified name.

param
the name that is being unbound.
exception
NamingException if there is a naming exception.

	name = getRelativeName(name);
	if (isjavaURL(name)) {	    
	    javaUrlContext.unbind(name);
	} else {
	    try {
	        getProvider().unbind(name);
	    } catch (RemoteException ex) {
	        throw new CommunicationException(ex.toString());
	    }
	}
    
public voidunbind(javax.naming.Name name)
Unbind the object with the specified name.

param
the name that is being unbound.
exception
NamingException if there is a naming exception.

        // Flat namespace; no federation; just call string version
        unbind(name.toString());