Search in sources :

Example 21 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 22 with NameNotFoundException

use of javax.naming.NameNotFoundException in project spring-framework by spring-projects.

the class JndiTemplateTests method testLookupReturnsNull.

@Test
public void testLookupReturnsNull() throws Exception {
    String name = "foo";
    final Context context = mock(Context.class);
    given(context.lookup(name)).willReturn(null);
    JndiTemplate jt = new JndiTemplate() {

        @Override
        protected Context createInitialContext() {
            return context;
        }
    };
    try {
        jt.lookup(name);
        fail("Should have thrown NamingException");
    } catch (NameNotFoundException ex) {
    // Ok
    }
    verify(context).close();
}
Also used : Context(javax.naming.Context) NameNotFoundException(javax.naming.NameNotFoundException) Test(org.junit.Test)

Example 23 with NameNotFoundException

use of javax.naming.NameNotFoundException in project spring-framework by spring-projects.

the class JndiTemplateTests method testLookupFails.

@Test
public void testLookupFails() throws Exception {
    NameNotFoundException ne = new NameNotFoundException();
    String name = "foo";
    final Context context = mock(Context.class);
    given(context.lookup(name)).willThrow(ne);
    JndiTemplate jt = new JndiTemplate() {

        @Override
        protected Context createInitialContext() {
            return context;
        }
    };
    try {
        jt.lookup(name);
        fail("Should have thrown NamingException");
    } catch (NameNotFoundException ex) {
    // Ok
    }
    verify(context).close();
}
Also used : Context(javax.naming.Context) NameNotFoundException(javax.naming.NameNotFoundException) Test(org.junit.Test)

Example 24 with NameNotFoundException

use of javax.naming.NameNotFoundException in project spring-framework by spring-projects.

the class SimpleNamingContextTests method testNamingContextBuilder.

@Test
public void testNamingContextBuilder() throws NamingException {
    SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
    InitialContextFactory factory = builder.createInitialContextFactory(null);
    DataSource ds = new StubDataSource();
    builder.bind("java:comp/env/jdbc/myds", ds);
    Object obj = new Object();
    builder.bind("myobject", obj);
    Context context1 = factory.getInitialContext(null);
    assertTrue("Correct DataSource registered", context1.lookup("java:comp/env/jdbc/myds") == ds);
    assertTrue("Correct Object registered", context1.lookup("myobject") == obj);
    Hashtable<String, String> env2 = new Hashtable<>();
    env2.put("key1", "value1");
    Context context2 = factory.getInitialContext(env2);
    assertTrue("Correct DataSource registered", context2.lookup("java:comp/env/jdbc/myds") == ds);
    assertTrue("Correct Object registered", context2.lookup("myobject") == obj);
    assertTrue("Correct environment", context2.getEnvironment() != env2);
    assertTrue("Correct key1", "value1".equals(context2.getEnvironment().get("key1")));
    Integer i = new Integer(0);
    context1.rebind("myinteger", i);
    String s = "";
    context2.bind("mystring", s);
    Context context3 = (Context) context2.lookup("");
    context3.rename("java:comp/env/jdbc/myds", "jdbc/myds");
    context3.unbind("myobject");
    assertTrue("Correct environment", context3.getEnvironment() != context2.getEnvironment());
    context3.addToEnvironment("key2", "value2");
    assertTrue("key2 added", "value2".equals(context3.getEnvironment().get("key2")));
    context3.removeFromEnvironment("key1");
    assertTrue("key1 removed", context3.getEnvironment().get("key1") == null);
    assertTrue("Correct DataSource registered", context1.lookup("jdbc/myds") == ds);
    try {
        context1.lookup("myobject");
        fail("Should have thrown NameNotFoundException");
    } catch (NameNotFoundException ex) {
    // expected
    }
    assertTrue("Correct Integer registered", context1.lookup("myinteger") == i);
    assertTrue("Correct String registered", context1.lookup("mystring") == s);
    assertTrue("Correct DataSource registered", context2.lookup("jdbc/myds") == ds);
    try {
        context2.lookup("myobject");
        fail("Should have thrown NameNotFoundException");
    } catch (NameNotFoundException ex) {
    // expected
    }
    assertTrue("Correct Integer registered", context2.lookup("myinteger") == i);
    assertTrue("Correct String registered", context2.lookup("mystring") == s);
    assertTrue("Correct DataSource registered", context3.lookup("jdbc/myds") == ds);
    try {
        context3.lookup("myobject");
        fail("Should have thrown NameNotFoundException");
    } catch (NameNotFoundException ex) {
    // expected
    }
    assertTrue("Correct Integer registered", context3.lookup("myinteger") == i);
    assertTrue("Correct String registered", context3.lookup("mystring") == s);
    Map<String, Binding> bindingMap = new HashMap<>();
    NamingEnumeration<?> bindingEnum = context3.listBindings("");
    while (bindingEnum.hasMoreElements()) {
        Binding binding = (Binding) bindingEnum.nextElement();
        bindingMap.put(binding.getName(), binding);
    }
    assertTrue("Correct jdbc subcontext", bindingMap.get("jdbc").getObject() instanceof Context);
    assertTrue("Correct jdbc subcontext", SimpleNamingContext.class.getName().equals(bindingMap.get("jdbc").getClassName()));
    Context jdbcContext = (Context) context3.lookup("jdbc");
    jdbcContext.bind("mydsX", ds);
    Map<String, Binding> subBindingMap = new HashMap<>();
    NamingEnumeration<?> subBindingEnum = jdbcContext.listBindings("");
    while (subBindingEnum.hasMoreElements()) {
        Binding binding = (Binding) subBindingEnum.nextElement();
        subBindingMap.put(binding.getName(), binding);
    }
    assertTrue("Correct DataSource registered", ds.equals(subBindingMap.get("myds").getObject()));
    assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(subBindingMap.get("myds").getClassName()));
    assertTrue("Correct DataSource registered", ds.equals(subBindingMap.get("mydsX").getObject()));
    assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(subBindingMap.get("mydsX").getClassName()));
    assertTrue("Correct Integer registered", i.equals(bindingMap.get("myinteger").getObject()));
    assertTrue("Correct Integer registered", Integer.class.getName().equals(bindingMap.get("myinteger").getClassName()));
    assertTrue("Correct String registered", s.equals(bindingMap.get("mystring").getObject()));
    assertTrue("Correct String registered", String.class.getName().equals(bindingMap.get("mystring").getClassName()));
    context1.createSubcontext("jdbc").bind("sub/subds", ds);
    Map<String, String> pairMap = new HashMap<>();
    NamingEnumeration<?> pairEnum = context2.list("jdbc");
    while (pairEnum.hasMore()) {
        NameClassPair pair = (NameClassPair) pairEnum.next();
        pairMap.put(pair.getName(), pair.getClassName());
    }
    assertTrue("Correct sub subcontext", SimpleNamingContext.class.getName().equals(pairMap.get("sub")));
    Context subContext = (Context) context2.lookup("jdbc/sub");
    Map<String, String> subPairMap = new HashMap<>();
    NamingEnumeration<?> subPairEnum = subContext.list("");
    while (subPairEnum.hasMoreElements()) {
        NameClassPair pair = (NameClassPair) subPairEnum.next();
        subPairMap.put(pair.getName(), pair.getClassName());
    }
    assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(subPairMap.get("subds")));
    assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("myds")));
    assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("mydsX")));
    pairMap.clear();
    pairEnum = context1.list("jdbc/");
    while (pairEnum.hasMore()) {
        NameClassPair pair = (NameClassPair) pairEnum.next();
        pairMap.put(pair.getName(), pair.getClassName());
    }
    assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("myds")));
    assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("mydsX")));
}
Also used : SimpleNamingContextBuilder(org.springframework.tests.mock.jndi.SimpleNamingContextBuilder) InitialContext(javax.naming.InitialContext) SimpleNamingContext(org.springframework.tests.mock.jndi.SimpleNamingContext) Context(javax.naming.Context) Binding(javax.naming.Binding) NameNotFoundException(javax.naming.NameNotFoundException) HashMap(java.util.HashMap) Hashtable(java.util.Hashtable) InitialContextFactory(javax.naming.spi.InitialContextFactory) DataSource(javax.sql.DataSource) NameClassPair(javax.naming.NameClassPair) Test(org.junit.Test)

Example 25 with NameNotFoundException

use of javax.naming.NameNotFoundException in project spring-security by spring-projects.

the class ActiveDirectoryLdapAuthenticationProviderTests method failedUserSearchCausesBadCredentials.

@Test(expected = BadCredentialsException.class)
public void failedUserSearchCausesBadCredentials() throws Exception {
    DirContext ctx = mock(DirContext.class);
    when(ctx.getNameInNamespace()).thenReturn("");
    when(ctx.search(any(Name.class), any(String.class), any(Object[].class), any(SearchControls.class))).thenThrow(new NameNotFoundException());
    provider.contextFactory = createContextFactoryReturning(ctx);
    provider.authenticate(joe);
}
Also used : NameNotFoundException(javax.naming.NameNotFoundException) SearchControls(javax.naming.directory.SearchControls) DirContext(javax.naming.directory.DirContext) Name(javax.naming.Name) DistinguishedName(org.springframework.ldap.core.DistinguishedName) Test(org.junit.Test)

Aggregations

NameNotFoundException (javax.naming.NameNotFoundException)112 InitialContext (javax.naming.InitialContext)56 Context (javax.naming.Context)51 NamingException (javax.naming.NamingException)51 Test (org.junit.Test)36 Name (javax.naming.Name)33 Reference (javax.naming.Reference)27 NotContextException (javax.naming.NotContextException)24 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)23 CompositeName (javax.naming.CompositeName)22 Binding (javax.naming.Binding)21 OperationNotSupportedException (javax.naming.OperationNotSupportedException)21 CompoundName (javax.naming.CompoundName)16 NamingContext (org.eclipse.jetty.jndi.NamingContext)12 IOException (java.io.IOException)11 InvalidNameException (javax.naming.InvalidNameException)7 ContainerSystem (org.apache.openejb.spi.ContainerSystem)7 ArrayList (java.util.ArrayList)6 LinkRef (javax.naming.LinkRef)6 Properties (java.util.Properties)5