Search in sources :

Example 61 with Name

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

the class NamingContext method rebind.

/*------------------------------------------------*/
/**
     * Overwrite or create a binding
     *
     * @param name a <code>Name</code> value
     * @param obj an <code>Object</code> value
     * @exception NamingException if an error occurs
     */
public void rebind(Name name, Object obj) throws NamingException {
    if (isLocked())
        throw new NamingException("This context is immutable");
    Name cname = toCanonicalName(name);
    if (cname == null)
        throw new NamingException("Name is null");
    if (cname.size() == 0)
        throw new NamingException("Name is empty");
    //if no subcontexts, just bind it
    if (cname.size() == 1) {
        //check if it is a Referenceable
        Object objToBind = NamingManager.getStateToBind(obj, name, this, _env);
        if (objToBind instanceof Referenceable) {
            objToBind = ((Referenceable) objToBind).getReference();
        }
        removeBinding(cname);
        addBinding(cname, objToBind);
    } else {
        //walk down the subcontext hierarchy
        if (__log.isDebugEnabled())
            __log.debug("Checking for existing binding for name=" + cname + " for first element of name=" + cname.get(0));
        String firstComponent = cname.get(0);
        Object ctx = null;
        if (firstComponent.equals(""))
            ctx = this;
        else {
            Binding binding = getBinding(name.get(0));
            if (binding == null)
                throw new NameNotFoundException(name.get(0) + " is not bound");
            ctx = binding.getObject();
            if (ctx instanceof Reference) {
                //deference the object
                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) {
            ((Context) ctx).rebind(cname.getSuffix(1), obj);
        } else
            throw new NotContextException("Object bound at " + firstComponent + " is not a Context");
    }
}
Also used : Binding(javax.naming.Binding) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) NotContextException(javax.naming.NotContextException) Referenceable(javax.naming.Referenceable) 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(java.io.IOException) NotContextException(javax.naming.NotContextException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) CompoundName(javax.naming.CompoundName) Name(javax.naming.Name)

Example 62 with Name

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

the class NamingUtil method bind.

/* ------------------------------------------------------------ */
/**
     * Bind an object to a context ensuring all sub-contexts
     * are created if necessary
     *
     * @param ctx the context into which to bind
     * @param nameStr the name relative to context to bind
     * @param obj the object to be bound
     * @return the bound context
     * @exception NamingException if an error occurs
     */
public static Context bind(Context ctx, String nameStr, Object obj) throws NamingException {
    Name name = ctx.getNameParser("").parse(nameStr);
    //no name, nothing to do
    if (name.size() == 0)
        return null;
    Context subCtx = ctx;
    //last component of the name will be the name to bind
    for (int i = 0; i < name.size() - 1; i++) {
        try {
            subCtx = (Context) subCtx.lookup(name.get(i));
            if (__log.isDebugEnabled())
                __log.debug("Subcontext " + name.get(i) + " already exists");
        } catch (NameNotFoundException e) {
            subCtx = subCtx.createSubcontext(name.get(i));
            if (__log.isDebugEnabled())
                __log.debug("Subcontext " + name.get(i) + " created");
        }
    }
    subCtx.rebind(name.get(name.size() - 1), obj);
    if (__log.isDebugEnabled())
        __log.debug("Bound object to " + name.get(name.size() - 1));
    return subCtx;
}
Also used : Context(javax.naming.Context) NameNotFoundException(javax.naming.NameNotFoundException) Name(javax.naming.Name)

Example 63 with Name

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

the class localContextRoot method lookup.

/**
     *
     *
     * @see javax.naming.Context#lookup(javax.naming.Name)
     */
public Object lookup(Name name) throws NamingException {
    synchronized (__root) {
        if (__log.isDebugEnabled())
            __log.debug("Looking up name=\"" + name + "\"");
        Name cname = __root.toCanonicalName(name);
        if ((cname == null) || (cname.size() == 0)) {
            __log.debug("Null or empty name, returning copy of this context");
            NamingContext ctx = new NamingContext(_env, null, null, __root.getNameParser(""));
            ctx.setBindings(__root.getBindings());
            return ctx;
        }
        if (cname.size() == 1) {
            Binding binding = __root.getBinding(cname);
            if (binding == null) {
                NameNotFoundException nnfe = new NameNotFoundException();
                nnfe.setRemainingName(cname);
                throw nnfe;
            }
            Object o = binding.getObject();
            //handle links by looking up the link
            if (o instanceof LinkRef) {
                //if link name starts with ./ it is relative to current context
                String linkName = ((LinkRef) o).getLinkName();
                if (linkName.startsWith("./"))
                    return lookup(linkName.substring(2));
                else {
                    //link name is absolute
                    InitialContext ictx = new InitialContext();
                    return ictx.lookup(linkName);
                }
            } else if (o instanceof Reference) {
                //deference the object
                try {
                    return NamingManager.getObjectInstance(o, cname, __root, _env);
                } catch (NamingException e) {
                    throw e;
                } catch (final Exception e) {
                    throw new NamingException(e.getMessage()) {

                        {
                            initCause(e);
                        }
                    };
                }
            } else
                return o;
        }
        //it is a multipart name, get the first subcontext
        String firstComponent = cname.get(0);
        Object ctx = null;
        if (firstComponent.equals(""))
            ctx = this;
        else {
            Binding binding = __root.getBinding(firstComponent);
            if (binding == null) {
                NameNotFoundException nnfe = new NameNotFoundException();
                nnfe.setRemainingName(cname);
                throw nnfe;
            }
            //as we have bound a reference to an object factory
            //for the component specific contexts
            //at "comp" we need to resolve the reference
            ctx = binding.getObject();
            if (ctx instanceof Reference) {
                //deference the object
                try {
                    ctx = NamingManager.getObjectInstance(ctx, getNameParser("").parse(firstComponent), __root, _env);
                } catch (NamingException e) {
                    throw e;
                } catch (Exception e) {
                    __log.warn("", e);
                    throw new NamingException(e.getMessage());
                }
            }
        }
        if (!(ctx instanceof Context))
            throw new NotContextException();
        return ((Context) ctx).lookup(cname.getSuffix(1));
    }
}
Also used : Binding(javax.naming.Binding) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) NamingContext(org.eclipse.jetty.jndi.NamingContext) NotContextException(javax.naming.NotContextException) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) NamingContext(org.eclipse.jetty.jndi.NamingContext) InitialContext(javax.naming.InitialContext) NamingException(javax.naming.NamingException) NameNotFoundException(javax.naming.NameNotFoundException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) NotContextException(javax.naming.NotContextException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) CompoundName(javax.naming.CompoundName) Name(javax.naming.Name) NamingException(javax.naming.NamingException) LinkRef(javax.naming.LinkRef)

Example 64 with Name

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

the class EnvConfiguration method bindEnvEntries.

/**
     * Bind all EnvEntries that have been declared, so that the processing of the
     * web.xml file can potentially override them.
     *
     * We first bind EnvEntries declared in Server scope, then WebAppContext scope.
     * @param context the context to use for the object scope
     * @throws NamingException if unable to bind env entries
     */
public void bindEnvEntries(WebAppContext context) throws NamingException {
    LOG.debug("Binding env entries from the jvm scope");
    InitialContext ic = new InitialContext();
    Context envCtx = (Context) ic.lookup("java:comp/env");
    Object scope = null;
    List<Object> list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
    Iterator<Object> itor = list.iterator();
    while (itor.hasNext()) {
        EnvEntry ee = (EnvEntry) itor.next();
        ee.bindToENC(ee.getJndiName());
        Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
        //also save the EnvEntry in the context so we can check it later
        NamingUtil.bind(envCtx, namingEntryName.toString(), ee);
    }
    LOG.debug("Binding env entries from the server scope");
    scope = context.getServer();
    list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
    itor = list.iterator();
    while (itor.hasNext()) {
        EnvEntry ee = (EnvEntry) itor.next();
        ee.bindToENC(ee.getJndiName());
        Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
        //also save the EnvEntry in the context so we can check it later
        NamingUtil.bind(envCtx, namingEntryName.toString(), ee);
    }
    LOG.debug("Binding env entries from the context scope");
    scope = context;
    list = NamingEntryUtil.lookupNamingEntries(scope, EnvEntry.class);
    itor = list.iterator();
    while (itor.hasNext()) {
        EnvEntry ee = (EnvEntry) itor.next();
        ee.bindToENC(ee.getJndiName());
        Name namingEntryName = NamingEntryUtil.makeNamingEntryName(null, ee);
        //also save the EnvEntry in the context so we can check it later
        NamingUtil.bind(envCtx, namingEntryName.toString(), ee);
    }
}
Also used : InitialContext(javax.naming.InitialContext) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) NamingContext(org.eclipse.jetty.jndi.NamingContext) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) EnvEntry(org.eclipse.jetty.plus.jndi.EnvEntry) Name(javax.naming.Name)

Example 65 with Name

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

the class TestNamingEntryUtil method testMakeNamingEntryName.

@Test
public void testMakeNamingEntryName() throws Exception {
    Name name = NamingEntryUtil.makeNamingEntryName(null, "fee/fi/fo/fum");
    assertNotNull(name);
    assertEquals(NamingEntry.__contextName + "/fee/fi/fo/fum", name.toString());
}
Also used : Name(javax.naming.Name) Test(org.junit.Test)

Aggregations

Name (javax.naming.Name)107 CompositeName (javax.naming.CompositeName)48 NamingException (javax.naming.NamingException)38 Context (javax.naming.Context)37 InitialContext (javax.naming.InitialContext)36 NameNotFoundException (javax.naming.NameNotFoundException)34 Test (org.junit.Test)32 Reference (javax.naming.Reference)24 NotContextException (javax.naming.NotContextException)22 Binding (javax.naming.Binding)19 CompoundName (javax.naming.CompoundName)19 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)19 NameParser (javax.naming.NameParser)17 OperationNotSupportedException (javax.naming.OperationNotSupportedException)16 InvalidNameException (javax.naming.InvalidNameException)10 NamingContext (org.eclipse.jetty.jndi.NamingContext)10 IOException (java.io.IOException)8 LinkRef (javax.naming.LinkRef)7 ServiceName (org.jboss.msc.service.ServiceName)7 Referenceable (javax.naming.Referenceable)6