Search in sources :

Example 1 with NameNotFoundException

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

the class ResourceAnnotationHandler method handleMethod.

/**
     * Process a Resource annotation on a Method.
     * <p>
     * This will generate a JNDI entry, and an Injection to be
     * processed when an instance of the class is created.
     * 
     * @param clazz the class to process 
     * @param method the method to process
     */
public void handleMethod(Class<?> clazz, Method method) {
    Resource resource = (Resource) method.getAnnotation(Resource.class);
    if (resource != null) {
        //JavaEE Spec 5.2.3: Method cannot be static
        if (Modifier.isStatic(method.getModifiers())) {
            LOG.warn("Skipping Resource annotation on " + clazz.getName() + "." + method.getName() + ": cannot be static");
            return;
        }
        // only 1 parameter
        if (!method.getName().startsWith("set")) {
            LOG.warn("Skipping Resource annotation on " + clazz.getName() + "." + method.getName() + ": invalid java bean, does not start with 'set'");
            return;
        }
        if (method.getParameterCount() != 1) {
            LOG.warn("Skipping Resource annotation on " + clazz.getName() + "." + method.getName() + ": invalid java bean, not single argument to method");
            return;
        }
        if (Void.TYPE != method.getReturnType()) {
            LOG.warn("Skipping Resource annotation on " + clazz.getName() + "." + method.getName() + ": invalid java bean, not void");
            return;
        }
        //default name is the javabean property name
        String name = method.getName().substring(3);
        name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1);
        name = clazz.getCanonicalName() + "/" + name;
        name = (resource.name() != null && !resource.name().trim().equals("") ? resource.name() : name);
        String mappedName = (resource.mappedName() != null && !resource.mappedName().trim().equals("") ? resource.mappedName() : null);
        Class<?> paramType = method.getParameterTypes()[0];
        Class<?> resourceType = resource.type();
        //Servlet Spec 3.0 p. 76
        //If a descriptor has specified at least 1 injection target for this
        //resource, then it overrides this annotation
        MetaData metaData = _context.getMetaData();
        if (metaData.getOriginDescriptor("resource-ref." + name + ".injection") != null) {
            //it overrides this annotation
            return;
        }
        //check if an injection has already been setup for this target by web.xml
        InjectionCollection injections = (InjectionCollection) _context.getAttribute(InjectionCollection.INJECTION_COLLECTION);
        if (injections == null) {
            injections = new InjectionCollection();
            _context.setAttribute(InjectionCollection.INJECTION_COLLECTION, injections);
        }
        Injection injection = injections.getInjection(name, clazz, method, paramType);
        if (injection == null) {
            try {
                //try binding name to environment
                //try the webapp's environment first
                boolean bound = org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_context, name, mappedName);
                //try the server's environment
                if (!bound)
                    bound = org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(_context.getServer(), name, mappedName);
                //try the jvm's environment
                if (!bound)
                    bound = org.eclipse.jetty.plus.jndi.NamingEntryUtil.bindToENC(null, name, mappedName);
                //NamingEntry, just a value bound in java:comp/env
                if (!bound) {
                    try {
                        InitialContext ic = new InitialContext();
                        String nameInEnvironment = (mappedName != null ? mappedName : name);
                        ic.lookup("java:comp/env/" + nameInEnvironment);
                        bound = true;
                    } catch (NameNotFoundException e) {
                        bound = false;
                    }
                }
                if (bound) {
                    LOG.debug("Bound " + (mappedName == null ? name : mappedName) + " as " + name);
                    //   Make the Injection for it
                    injection = new Injection();
                    injection.setTarget(clazz, method, paramType, resourceType);
                    injection.setJndiName(name);
                    injection.setMappingName(mappedName);
                    injections.add(injection);
                    //TODO - an @Resource is equivalent to a resource-ref, resource-env-ref, message-destination
                    metaData.setOrigin("resource-ref." + name + ".injection", resource, clazz);
                } else if (!isEnvEntryType(paramType)) {
                    // JavaEE Spec. sec 5.4.1.3
                    throw new IllegalStateException("No resource at " + (mappedName == null ? name : mappedName));
                }
            } catch (NamingException e) {
                // JavaEE Spec. sec 5.4.1.3
                if (!isEnvEntryType(paramType))
                    throw new IllegalStateException(e);
            }
        }
    }
}
Also used : InjectionCollection(org.eclipse.jetty.plus.annotation.InjectionCollection) NameNotFoundException(javax.naming.NameNotFoundException) MetaData(org.eclipse.jetty.webapp.MetaData) Resource(javax.annotation.Resource) NamingException(javax.naming.NamingException) Injection(org.eclipse.jetty.plus.annotation.Injection) InitialContext(javax.naming.InitialContext)

Example 2 with NameNotFoundException

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

the class localContextRoot method unbind.

/**
     *
     *
     * @see javax.naming.Context#unbind(javax.naming.Name)
     */
public void unbind(Name name) throws NamingException {
    synchronized (__root) {
        if (name.size() == 0)
            return;
        if (__root.isLocked())
            throw new NamingException("This context is immutable");
        Name cname = __root.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 unbind it
        if (cname.size() == 1) {
            __root.removeBinding(cname);
        } 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 = __root.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), __root, _env);
                    } catch (NamingException e) {
                        throw e;
                    } catch (Exception e) {
                        __log.warn("", e);
                        throw new NamingException(e.getMessage());
                    }
                }
            }
            if (ctx instanceof Context) {
                ((Context) ctx).unbind(cname.getSuffix(1));
            } 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) NamingContext(org.eclipse.jetty.jndi.NamingContext) NotContextException(javax.naming.NotContextException) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) 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)

Example 3 with NameNotFoundException

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

the class localContextRoot method list.

/**
     *
     *
     * @see javax.naming.Context#list(javax.naming.Name)
     */
public NamingEnumeration list(Name name) throws NamingException {
    synchronized (__root) {
        //return __root.list(getSuffix(name));
        Name cname = __root.toCanonicalName(name);
        if (cname == null) {
            List<Binding> empty = Collections.emptyList();
            return new NameEnumeration(empty.iterator());
        }
        if (cname.size() == 0) {
            return new NameEnumeration(__root.getBindings().values().iterator());
        }
        //multipart name
        String firstComponent = cname.get(0);
        Object ctx = null;
        if (firstComponent.equals(""))
            ctx = this;
        else {
            Binding binding = __root.getBinding(firstComponent);
            if (binding == null)
                throw new NameNotFoundException();
            ctx = binding.getObject();
            if (ctx instanceof Reference) {
                //deference the object
                if (__log.isDebugEnabled())
                    __log.debug("Dereferencing Reference for " + name.get(0));
                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).list(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) NameEnumeration(org.eclipse.jetty.jndi.NameEnumeration) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) 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)

Example 4 with NameNotFoundException

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

the class localContextRoot method bind.

/**
     *
     *
     * @see javax.naming.Context#bind(javax.naming.Name, java.lang.Object)
     */
public void bind(Name name, Object obj) throws NamingException {
    synchronized (__root) {
        if (__root.isLocked())
            throw new NamingException("This context is immutable");
        Name cname = __root.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) {
            //get the object to be bound
            Object objToBind = NamingManager.getStateToBind(obj, name, this, _env);
            // Check for Referenceable
            if (objToBind instanceof Referenceable) {
                objToBind = ((Referenceable) objToBind).getReference();
            }
            //anything else we should be able to bind directly
            __root.addBinding(cname, objToBind);
        } else {
            if (__log.isDebugEnabled())
                __log.debug("Checking for existing binding for name=" + cname + " for first element of name=" + cname.get(0));
            //walk down the subcontext hierarchy
            //need to ignore trailing empty "" name components
            String firstComponent = cname.get(0);
            Object ctx = null;
            if (firstComponent.equals(""))
                ctx = this;
            else {
                Binding binding = __root.getBinding(firstComponent);
                if (binding == null)
                    throw new NameNotFoundException(firstComponent + " 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).bind(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) NamingContext(org.eclipse.jetty.jndi.NamingContext) 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) NotContextException(javax.naming.NotContextException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) CompoundName(javax.naming.CompoundName) Name(javax.naming.Name)

Example 5 with NameNotFoundException

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

the class TestJNDI method testThreadContextClassloaderAndCurrentContext.

@Test
public void testThreadContextClassloaderAndCurrentContext() throws Exception {
    //create a jetty context, and start it so that its classloader it created
    //and it is the current context
    ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();
    ContextHandler ch = new ContextHandler();
    URLClassLoader chLoader = new URLClassLoader(new URL[0], currentLoader);
    ch.setClassLoader(chLoader);
    Server server = new Server();
    HandlerList hl = new HandlerList();
    server.setHandler(hl);
    hl.addHandler(ch);
    //Create another one
    ContextHandler ch2 = new ContextHandler();
    URLClassLoader ch2Loader = new URLClassLoader(new URL[0], currentLoader);
    ch2.setClassLoader(ch2Loader);
    hl.addHandler(ch2);
    try {
        ch.setContextPath("/ch");
        ch.addEventListener(new ServletContextListener() {

            private Context comp;

            private Object testObj = new Object();

            public void contextInitialized(ServletContextEvent sce) {
                try {
                    InitialContext initCtx = new InitialContext();
                    Context java = (Context) initCtx.lookup("java:");
                    assertNotNull(java);
                    comp = (Context) initCtx.lookup("java:comp");
                    assertNotNull(comp);
                    Context env = ((Context) comp).createSubcontext("env");
                    assertNotNull(env);
                    env.bind("ch", testObj);
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }

            public void contextDestroyed(ServletContextEvent sce) {
                try {
                    assertNotNull(comp);
                    assertEquals(testObj, comp.lookup("env/ch"));
                    comp.destroySubcontext("env");
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
        });
        //Starting the context makes it current and creates a classloader for it
        ch.start();
        ch2.setContextPath("/ch2");
        ch2.addEventListener(new ServletContextListener() {

            private Context comp;

            private Object testObj = new Object();

            public void contextInitialized(ServletContextEvent sce) {
                try {
                    InitialContext initCtx = new InitialContext();
                    comp = (Context) initCtx.lookup("java:comp");
                    assertNotNull(comp);
                    //another context's bindings should not be visible
                    Context env = ((Context) comp).createSubcontext("env");
                    try {
                        env.lookup("ch");
                        fail("java:comp/env visible from another context!");
                    } catch (NameNotFoundException e) {
                    //expected
                    }
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }

            public void contextDestroyed(ServletContextEvent sce) {
                try {
                    assertNotNull(comp);
                    comp.destroySubcontext("env");
                } catch (Exception e) {
                    throw new IllegalStateException(e);
                }
            }
        });
        //make the new context the current one
        ch2.start();
    } finally {
        ch.stop();
        ch2.stop();
        Thread.currentThread().setContextClassLoader(currentLoader);
    }
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) NamingContext(org.eclipse.jetty.jndi.NamingContext) Server(org.eclipse.jetty.server.Server) ServletContextListener(javax.servlet.ServletContextListener) NameNotFoundException(javax.naming.NameNotFoundException) InitialContext(javax.naming.InitialContext) NamingException(javax.naming.NamingException) NameNotFoundException(javax.naming.NameNotFoundException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Aggregations

NameNotFoundException (javax.naming.NameNotFoundException)167 NamingException (javax.naming.NamingException)80 InitialContext (javax.naming.InitialContext)74 Context (javax.naming.Context)71 Reference (javax.naming.Reference)40 Test (org.junit.Test)39 NotContextException (javax.naming.NotContextException)35 Name (javax.naming.Name)34 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)33 OperationNotSupportedException (javax.naming.OperationNotSupportedException)29 CompositeName (javax.naming.CompositeName)27 Binding (javax.naming.Binding)22 CompoundName (javax.naming.CompoundName)16 LinkRef (javax.naming.LinkRef)16 IOException (java.io.IOException)12 NamingContext (org.eclipse.jetty.jndi.NamingContext)12 ArrayList (java.util.ArrayList)10 InvalidNameException (javax.naming.InvalidNameException)10 Attributes (javax.naming.directory.Attributes)10 HashMap (java.util.HashMap)7