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);
}
}
}
}
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();
}
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();
}
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")));
}
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);
}
Aggregations