1 16 17 package com.google.inject.jndi; 18 19 import com.google.inject.Provider; 20 import com.google.inject.Inject; 21 import javax.naming.Context ; 22 import javax.naming.NamingException ; 23 24 30 public class JndiIntegration { 31 32 private JndiIntegration() {} 33 34 42 public static <T> Provider<T> fromJndi(Class <T> type, String name) { 43 return new JndiProvider<T>(type, name); 44 } 45 46 static class JndiProvider<T> implements Provider<T> { 47 48 @Inject Context context; 49 final Class <T> type; 50 final String name; 51 52 public JndiProvider(Class <T> type, String name) { 53 this.type = type; 54 this.name = name; 55 } 56 57 public T get() { 58 try { 59 return type.cast(context.lookup(name)); 60 } 61 catch (NamingException e) { 62 throw new RuntimeException (e); 63 } 64 } 65 } 66 } 67 | Popular Tags |