1 16 17 package com.google.inject.example; 18 19 import com.google.inject.Inject; 20 import com.google.inject.Provider; 21 import javax.naming.Context ; 22 import javax.naming.NamingException ; 23 24 class JndiProvider<T> implements Provider<T> { 25 26 @Inject Context context; 27 final String name; 28 final Class <T> type; 29 30 JndiProvider(Class <T> type, String name) { 31 this.name = name; 32 this.type = type; 33 } 34 35 public T get() { 36 try { 37 return type.cast(context.lookup(name)); 38 } 39 catch (NamingException e) { 40 throw new RuntimeException (e); 41 } 42 } 43 44 48 static <T> Provider<T> fromJndi( 49 Class <T> type, String name) { 50 return new JndiProvider<T>(type, name); 51 } 52 } 53 | Popular Tags |