Search in sources :

Example 41 with Binding

use of javax.naming.Binding 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 42 with Binding

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

the class NamingContext method removeBinding.

/*------------------------------------------------*/
public void removeBinding(Name name) {
    String key = name.toString();
    if (__log.isDebugEnabled())
        __log.debug("Removing binding with key=" + key);
    Binding binding = _bindings.remove(key);
    if (binding != null) {
        Collection<Listener> list = findListeners();
        for (Listener listener : list) listener.unbind(this, binding);
    }
}
Also used : Binding(javax.naming.Binding)

Example 43 with Binding

use of javax.naming.Binding 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 44 with Binding

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

the class EnvConfiguration method configure.

@Override
public void configure(WebAppContext context) throws Exception {
    if (LOG.isDebugEnabled())
        LOG.debug("Created java:comp/env for webapp " + context.getContextPath());
    //look in WEB-INF/jetty-env.xml
    if (jettyEnvXmlUrl == null) {
        //look for a file called WEB-INF/jetty-env.xml
        //and process it if it exists
        org.eclipse.jetty.util.resource.Resource web_inf = context.getWebInf();
        if (web_inf != null && web_inf.isDirectory()) {
            org.eclipse.jetty.util.resource.Resource jettyEnv = web_inf.addPath("jetty-env.xml");
            if (jettyEnv.exists()) {
                jettyEnvXmlUrl = jettyEnv.getURL();
            }
        }
    }
    if (jettyEnvXmlUrl != null) {
        synchronized (localContextRoot.getRoot()) {
            // create list and listener to remember the bindings we make.
            final List<Bound> bindings = new ArrayList<Bound>();
            NamingContext.Listener listener = new NamingContext.Listener() {

                public void unbind(NamingContext ctx, Binding binding) {
                }

                public Binding bind(NamingContext ctx, Binding binding) {
                    bindings.add(new Bound(ctx, binding.getName()));
                    return binding;
                }
            };
            try {
                localContextRoot.getRoot().addListener(listener);
                XmlConfiguration configuration = new XmlConfiguration(jettyEnvXmlUrl);
                WebAppClassLoader.runWithServerClassAccess(() -> {
                    configuration.configure(context);
                    return null;
                });
            } finally {
                localContextRoot.getRoot().removeListener(listener);
                context.setAttribute(JETTY_ENV_BINDINGS, bindings);
            }
        }
    }
    //add java:comp/env entries for any EnvEntries that have been defined so far
    bindEnvEntries(context);
}
Also used : Binding(javax.naming.Binding) ArrayList(java.util.ArrayList) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) NamingContext(org.eclipse.jetty.jndi.NamingContext)

Example 45 with Binding

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

the class ManagerServlet method printResources.

/**
     * List the resources of the given context.
     * @param writer Writer to render to
     * @param prefix Path for recursion
     * @param namingContext The naming context for lookups
     * @param type Fully qualified class name of the resource type of interest,
     *  or <code>null</code> to list resources of all types
     * @param clazz The resource class or <code>null</code> to list
     *  resources of all types
     * @param smClient i18n support for current client's locale
     */
protected void printResources(PrintWriter writer, String prefix, javax.naming.Context namingContext, String type, Class<?> clazz, StringManager smClient) {
    try {
        NamingEnumeration<Binding> items = namingContext.listBindings("");
        while (items.hasMore()) {
            Binding item = items.next();
            if (item.getObject() instanceof javax.naming.Context) {
                printResources(writer, prefix + item.getName() + "/", (javax.naming.Context) item.getObject(), type, clazz, smClient);
            } else {
                if ((clazz != null) && (!(clazz.isInstance(item.getObject())))) {
                    continue;
                }
                writer.print(prefix + item.getName());
                writer.print(':');
                writer.print(item.getClassName());
                // Do we want a description if available?
                writer.println();
            }
        }
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        log("ManagerServlet.resources[" + type + "]", t);
        writer.println(smClient.getString("managerServlet.exception", t.toString()));
    }
}
Also used : Binding(javax.naming.Binding) Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext)

Aggregations

Binding (javax.naming.Binding)69 NamingException (javax.naming.NamingException)36 Context (javax.naming.Context)31 InitialContext (javax.naming.InitialContext)29 NameNotFoundException (javax.naming.NameNotFoundException)22 Name (javax.naming.Name)19 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)19 NotContextException (javax.naming.NotContextException)18 OperationNotSupportedException (javax.naming.OperationNotSupportedException)18 Reference (javax.naming.Reference)17 CompoundName (javax.naming.CompoundName)16 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)10 NamingContext (org.eclipse.jetty.jndi.NamingContext)9 CompositeName (javax.naming.CompositeName)8 Test (org.junit.Test)8 HashMap (java.util.HashMap)7 NamingEnumeration (javax.naming.NamingEnumeration)5 ServletContext (javax.servlet.ServletContext)5 MalformedURLException (java.net.MalformedURLException)4