Search in sources :

Example 1 with NameAlreadyBoundException

use of javax.naming.NameAlreadyBoundException in project jetty.project by eclipse.

the class TestJNDI method testIt.

public void testIt() throws Exception {
    //set up some classloaders
    Thread currentThread = Thread.currentThread();
    ClassLoader currentLoader = currentThread.getContextClassLoader();
    ClassLoader childLoader1 = new URLClassLoader(new URL[0], currentLoader);
    ClassLoader childLoader2 = new URLClassLoader(new URL[0], currentLoader);
    try {
        //Uncomment to aid with debug
            javaRootURLContext.getRoot().addListener(new NamingContext.Listener()
                public void unbind(NamingContext ctx, Binding binding)
                    System.err.println("java unbind "+binding+" from "+ctx.getName());
                public Binding bind(NamingContext ctx, Binding binding)
                    System.err.println("java bind "+binding+" to "+ctx.getName());
                    return binding;
            localContextRoot.getRoot().addListener(new NamingContext.Listener()
                public void unbind(NamingContext ctx, Binding binding)
                    System.err.println("local unbind "+binding+" from "+ctx.getName());
                public Binding bind(NamingContext ctx, Binding binding)
                    System.err.println("local bind "+binding+" to "+ctx.getName());
                    return binding;
        //Set up the tccl before doing any jndi operations
        InitialContext initCtx = new InitialContext();
        //Test we can lookup the root java: naming tree
        Context sub0 = (Context) initCtx.lookup("java:");
        //already be bound 
        try {
            Context sub1 = sub0.createSubcontext("comp");
            fail("Comp should already be bound");
        } catch (NameAlreadyBoundException e) {
        //expected exception
        //check bindings at comp
        Context sub1 = (Context) initCtx.lookup("java:comp");
        Context sub2 = sub1.createSubcontext("env");
        initCtx.bind("java:comp/env/rubbish", "abc");
        assertEquals("abc", initCtx.lookup("java:comp/env/rubbish"));
        //check binding LinkRefs
        LinkRef link = new LinkRef("java:comp/env/rubbish");
        initCtx.bind("java:comp/env/poubelle", link);
        assertEquals("abc", initCtx.lookup("java:comp/env/poubelle"));
        //check binding References
        StringRefAddr addr = new StringRefAddr("blah", "myReferenceable");
        Reference ref = new Reference(java.lang.String.class.getName(), addr, MyObjectFactory.class.getName(), null);
        initCtx.bind("java:comp/env/quatsch", ref);
        assertEquals(MyObjectFactory.myString, initCtx.lookup("java:comp/env/quatsch"));
        //test binding something at java:
        Context sub3 = initCtx.createSubcontext("java:zero");
        initCtx.bind("java:zero/one", "ONE");
        assertEquals("ONE", initCtx.lookup("java:zero/one"));
        //change the current thread's classloader to check distinct naming
        Context otherSub1 = (Context) initCtx.lookup("java:comp");
        assertTrue(!(sub1 == otherSub1));
        try {
            fail("env should not exist for this classloader");
        } catch (NameNotFoundException e) {
        //put the thread's classloader back
        //test rebind with existing binding
        initCtx.rebind("java:comp/env/rubbish", "xyz");
        assertEquals("xyz", initCtx.lookup("java:comp/env/rubbish"));
        //test rebind with no existing binding
        initCtx.rebind("java:comp/env/mullheim", "hij");
        assertEquals("hij", initCtx.lookup("java:comp/env/mullheim"));
        //test that the other bindings are already there
        assertEquals("xyz", initCtx.lookup("java:comp/env/poubelle"));
        //test java:/comp/env/stuff
        assertEquals("xyz", initCtx.lookup("java:/comp/env/poubelle/"));
        //test list Names
        NamingEnumeration nenum = initCtx.list("java:comp/env");
        HashMap results = new HashMap();
        while (nenum.hasMore()) {
            NameClassPair ncp = (NameClassPair);
            results.put(ncp.getName(), ncp.getClassName());
        assertEquals(4, results.size());
        assertEquals("java.lang.String", results.get("rubbish"));
        assertEquals("javax.naming.LinkRef", results.get("poubelle"));
        assertEquals("java.lang.String", results.get("mullheim"));
        assertEquals("javax.naming.Reference", results.get("quatsch"));
        //test list Bindings
        NamingEnumeration benum = initCtx.list("java:comp/env");
        assertEquals(4, results.size());
        //test NameInNamespace
        assertEquals("comp/env", sub2.getNameInNamespace());
        //test close does nothing
        Context closeCtx = (Context) initCtx.lookup("java:comp/env");
        //test what happens when you close an initial context
        InitialContext closeInit = new InitialContext();
        //check locking the context
        Context ectx = (Context) initCtx.lookup("java:comp");
        ectx.bind("crud", "xxx");
        ectx.addToEnvironment("org.eclipse.jndi.immutable", "TRUE");
        assertEquals("xxx", initCtx.lookup("java:comp/crud"));
        try {
            ectx.bind("crud2", "xxx2");
        } catch (NamingException ne) {
        //expected failure to modify immutable context
    } finally {
        //make some effort to clean up
        InitialContext ic = new InitialContext();
        Context java = (Context) ic.lookup("java:");
        Context comp = (Context) ic.lookup("java:comp");
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) NamingContext(org.eclipse.jetty.jndi.NamingContext) NameNotFoundException(javax.naming.NameNotFoundException) HashMap(java.util.HashMap) Reference(javax.naming.Reference) NamingEnumeration(javax.naming.NamingEnumeration) InitialContext(javax.naming.InitialContext) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) StringRefAddr(javax.naming.StringRefAddr) NameClassPair(javax.naming.NameClassPair) URLClassLoader( URLClassLoader( NamingException(javax.naming.NamingException) LinkRef(javax.naming.LinkRef) Test(org.junit.Test)

Example 2 with NameAlreadyBoundException

use of javax.naming.NameAlreadyBoundException in project jetty.project by eclipse.

the class NamingContext method createSubcontext.

     * Create a context as a child of this one
     * @param name a <code>Name</code> value
     * @return a <code>Context</code> value
     * @exception NamingException if an error occurs
public Context createSubcontext(Name name) throws NamingException {
    if (isLocked()) {
        NamingException ne = new NamingException("This context is immutable");
        throw ne;
    Name cname = toCanonicalName(name);
    if (cname == null)
        throw new NamingException("Name is null");
    if (cname.size() == 0)
        throw new NamingException("Name is empty");
    if (cname.size() == 1) {
        //not permitted to bind if something already bound at that name
        Binding binding = getBinding(cname);
        if (binding != null)
            throw new NameAlreadyBoundException(cname.toString());
        Context ctx = new NamingContext((Hashtable) _env.clone(), cname.get(0), this, _parser);
        addBinding(cname, ctx);
        return ctx;
    //If the name has multiple subcontexts, walk the hierarchy by
    //fetching the first one. All intermediate subcontexts in the
    //name must already exist.
    String firstComponent = cname.get(0);
    Object ctx = null;
    if (firstComponent.equals(""))
        ctx = this;
    else {
        Binding binding = getBinding(firstComponent);
        if (binding == null)
            throw new NameNotFoundException(firstComponent + " is not bound");
        ctx = binding.getObject();
        if (ctx instanceof Reference) {
            //deference the object
            if (__log.isDebugEnabled())
                __log.debug("Object bound at " + firstComponent + " is a Reference");
            try {
                ctx = NamingManager.getObjectInstance(ctx, getNameParser("").parse(firstComponent), this, _env);
            } catch (NamingException e) {
                throw e;
            } catch (Exception e) {
                __log.warn("", e);
                throw new NamingException(e.getMessage());
    if (ctx instanceof Context) {
        return ((Context) ctx).createSubcontext(cname.getSuffix(1));
    } else
        throw new NotContextException(firstComponent + " is not a Context");
Also used : Binding(javax.naming.Binding) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) NotContextException(javax.naming.NotContextException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) NamingException(javax.naming.NamingException) NameNotFoundException(javax.naming.NameNotFoundException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) IOException( NotContextException(javax.naming.NotContextException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) CompoundName(javax.naming.CompoundName) Name(javax.naming.Name)

Example 3 with NameAlreadyBoundException

use of javax.naming.NameAlreadyBoundException in project tomcat by apache.

the class NamingContext method bind.

     * Binds a name to an object. All intermediate contexts and the target
     * context (that named by all but terminal atomic component of the name)
     * must already exist.
     * @param name the name to bind; may not be empty
     * @param obj the object to bind; possibly null
     * @param rebind if true, then perform a rebind (ie, overwrite)
     * @exception NameAlreadyBoundException if name is already bound
     * @exception if object
     * did not supply all mandatory attributes
     * @exception NamingException if a naming exception is encountered
protected void bind(Name name, Object obj, boolean rebind) throws NamingException {
    if (!checkWritable()) {
    while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1);
    if (name.isEmpty())
        throw new NamingException(sm.getString("namingContext.invalidName"));
    NamingEntry entry = bindings.get(name.get(0));
    if (name.size() > 1) {
        if (entry == null) {
            throw new NameNotFoundException(sm.getString("namingContext.nameNotBound", name, name.get(0)));
        if (entry.type == NamingEntry.CONTEXT) {
            if (rebind) {
                ((Context) entry.value).rebind(name.getSuffix(1), obj);
            } else {
                ((Context) entry.value).bind(name.getSuffix(1), obj);
        } else {
            throw new NamingException(sm.getString("namingContext.contextExpected"));
    } else {
        if ((!rebind) && (entry != null)) {
            throw new NameAlreadyBoundException(sm.getString("namingContext.alreadyBound", name.get(0)));
        } else {
            // Getting the type of the object and wrapping it within a new
            // NamingEntry
            Object toBind = NamingManager.getStateToBind(obj, name, this, env);
            if (toBind instanceof Context) {
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.CONTEXT);
            } else if (toBind instanceof LinkRef) {
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.LINK_REF);
            } else if (toBind instanceof Reference) {
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE);
            } else if (toBind instanceof Referenceable) {
                toBind = ((Referenceable) toBind).getReference();
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE);
            } else {
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.ENTRY);
            bindings.put(name.get(0), entry);
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) Referenceable(javax.naming.Referenceable) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) LinkRef(javax.naming.LinkRef)

Example 4 with NameAlreadyBoundException

use of javax.naming.NameAlreadyBoundException in project head by mifos.

the class ApplicationInitializer method initJNDIforPentaho.

private void initJNDIforPentaho(ApplicationContext applicationContext) {
    try {
        InitialContext ic = new InitialContext();
        //check if ds is already bound
        boolean dataSourceBound = true;
        boolean dataSourceBoundDw = true;
        try {
            DataSource datasource = (DataSource) ic.lookup("jdbc/SourceDB");
            if (datasource != null) {
                dataSourceBound = true;
        } catch (Exception ex) {
            dataSourceBound = false;
        try {
            DataSource datasourcedw = (DataSource) ic.lookup("jdbc/DestinationDB");
            if (datasourcedw != null) {
                dataSourceBoundDw = true;
        } catch (Exception ex) {
            dataSourceBoundDw = false;
        if (!dataSourceBound) {
            Object dataSource = applicationContext.getBean("dataSource");
            try {
            } catch (NameAlreadyBoundException ex) {
      "Subcontext jdbc was already bound");
            ic.bind("jdbc/SourceDB", dataSource);
  "Bound datasource to jdbc/SourceDB");
        } else {
  "jdbc/SourceDB is already bound");
        if (!dataSourceBoundDw) {
            Object dataSourcedw = applicationContext.getBean("dataSourcePentahoDW");
            try {
            } catch (NameAlreadyBoundException ex) {
      "Subcontext jdbc was already bound");
            ic.bind("jdbc/DestinationDB", dataSourcedw);
  "Bound datasource to jdbc/DestinationDB");
        } else {
  "jdbc/DestinationDB is already bound");
    } catch (Exception ex) {
        logger.error("Unable to bind dataSource to JNDI");
Also used : NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) InitialContext(javax.naming.InitialContext) SystemException(org.mifos.framework.exceptions.SystemException) TaskSystemException(org.mifos.framework.components.batchjobs.exceptions.TaskSystemException) XMLReaderException(org.mifos.framework.exceptions.XMLReaderException) HibernateProcessException(org.mifos.framework.exceptions.HibernateProcessException) PersistenceException(org.mifos.framework.exceptions.PersistenceException) ConfigurationException(org.mifos.config.exceptions.ConfigurationException) AppNotConfiguredException(org.mifos.framework.exceptions.AppNotConfiguredException) AccountException(org.mifos.accounts.exceptions.AccountException) FinancialException( NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) IOException( HibernateStartUpException(org.mifos.framework.exceptions.HibernateStartUpException) ApplicationException(org.mifos.framework.exceptions.ApplicationException) DataSource(javax.sql.DataSource)

Example 5 with NameAlreadyBoundException

use of javax.naming.NameAlreadyBoundException in project ACS by ACS-Community.

the class ManagerImpl method bind.

	 * Bind object to remote directory.
	 * Use INS syntax specified in the INS specification.
	 * In short, the syntax is that components are left-to-right slash ('/') separated and case-sensitive.
	 * The id and kind of each component are separated by the period character ('.').
	 * @param	remoteDirectory	remote directory context to be used.
	 * @param	name	name of the object, code non-<code>null</code>
	 * @param	object	object to be binded
private void bind(Context remoteDirectory, String name, String type, Object object) {
    assert (name != null);
    // do not bind interdomain names
    if (name.startsWith(CURL_URI_SCHEMA))
    if (remoteDirectory != null) {
        try {
            // hierarchical name check
            int pos = name.indexOf('/');
            if (pos != -1) {
                if (pos == 0 || pos == name.length())
                    throw new IllegalArgumentException("Invalid hierarchical name '" + name + "'.");
                String parent = name.substring(0, pos);
                String child = name.substring(pos + 1);
                // lookup for subcontext, if not found create one
                Context parentContext = null;
                try {
                    parentContext = (Context) remoteDirectory.lookup(parent);
                } catch (NameNotFoundException nnfe) {
                // noop
                if (parentContext == null) {
                    parentContext = remoteDirectory.createSubcontext(parent);
                /// @todo temp. commented out
                //generateHiearchyContexts(remoteDirectory, parent, parentContext);
                // delegate
                bind(parentContext, child, type, object);
            NameParser parser = remoteDirectory.getNameParser("");
            Name n;
            if (type != null)
                n = parser.parse(name + "." + type);
                n = parser.parse(name);
            // special case
            if (name.endsWith(".D")) {
                remoteDirectory.rebind(n, object);
            /// @todo temp. commented out
            //generateHiearchyContexts(remoteDirectory, name, (Context)remoteDirectory.lookup(name));
            } else
                remoteDirectory.bind(n, object);
        } catch (NameAlreadyBoundException nabe) {
            rebind(remoteDirectory, name, type, object);
        } catch (NamingException ne) {
            CoreException ce = new CoreException("Failed to bind name '" + name + "' to the remote directory.", ne);
            logger.log(Level.FINE, ce.getMessage(), ce);
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) CoreException(com.cosylab.acs.maci.CoreException) NameNotFoundException(javax.naming.NameNotFoundException) NamingException(javax.naming.NamingException) NameParser(javax.naming.NameParser) Name(javax.naming.Name)


NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)33 InitialContext (javax.naming.InitialContext)17 Context (javax.naming.Context)16 NamingException (javax.naming.NamingException)15 NameNotFoundException (javax.naming.NameNotFoundException)12 Reference (javax.naming.Reference)8 File ( OperationNotSupportedException (javax.naming.OperationNotSupportedException)6 IOException ( HashMap (java.util.HashMap)5 BeanContext (org.apache.openejb.BeanContext)5 InvalidNameException (javax.naming.InvalidNameException)4 LinkRef (javax.naming.LinkRef)4 Name (javax.naming.Name)4 AppContext (org.apache.openejb.AppContext)4 Properties (java.util.Properties)3 CreationalContext (javax.enterprise.context.spi.CreationalContext)3 Referenceable (javax.naming.Referenceable)3 DataSource (javax.sql.DataSource)3 InvalidObjectException (