| 1 |
|
package net.taylor.jndi; |
| 2 |
|
|
| 3 |
|
import java.util.Properties; |
| 4 |
|
|
| 5 |
|
import javax.naming.Context; |
| 6 |
|
import javax.naming.InitialContext; |
| 7 |
|
import javax.naming.NameClassPair; |
| 8 |
|
import javax.naming.NamingEnumeration; |
| 9 |
|
import javax.naming.NamingException; |
| 10 |
|
|
| 11 |
|
import org.apache.commons.logging.Log; |
| 12 |
|
|
| 13 |
0 |
public class JndiUtil { |
| 14 |
|
|
| 15 |
|
private static InitialContext initialContext; |
| 16 |
|
|
| 17 |
|
public static InitialContext getInitialContext() { |
| 18 |
0 |
if (initialContext == null) { |
| 19 |
|
try { |
| 20 |
0 |
initialContext = new InitialContext(); |
| 21 |
0 |
} catch (NamingException e) { |
| 22 |
0 |
throw new RuntimeException(e); |
| 23 |
0 |
} |
| 24 |
|
} |
| 25 |
0 |
return initialContext; |
| 26 |
|
} |
| 27 |
|
|
| 28 |
|
public static Object lookup(String beanName) { |
| 29 |
|
try { |
| 30 |
0 |
return getInitialContext().lookup(beanName); |
| 31 |
0 |
} catch (NamingException ex) { |
| 32 |
0 |
throw new RuntimeException("Couldn't lookup: " + beanName, ex); |
| 33 |
|
} |
| 34 |
|
} |
| 35 |
|
|
| 36 |
|
public static Object lookup(String beanName, String user, String password) { |
| 37 |
0 |
InitialContext ctx = null; |
| 38 |
|
try { |
| 39 |
0 |
Properties env = new Properties(); |
| 40 |
0 |
env.setProperty(Context.SECURITY_PRINCIPAL, user); |
| 41 |
0 |
env.setProperty(Context.SECURITY_CREDENTIALS, password); |
| 42 |
0 |
env.setProperty(Context.INITIAL_CONTEXT_FACTORY, |
| 43 |
|
"org.jboss.security.jndi.JndiLoginInitialContextFactory"); |
| 44 |
0 |
ctx = new InitialContext(env); |
| 45 |
|
|
| 46 |
0 |
return ctx.lookup(beanName); |
| 47 |
0 |
} catch (NamingException ex) { |
| 48 |
0 |
throw new RuntimeException("Couldn't lookup: " + beanName, ex); |
| 49 |
|
} finally { |
| 50 |
0 |
try { |
| 51 |
0 |
ctx.close(); |
| 52 |
0 |
} catch (Exception e) { |
| 53 |
0 |
throw new RuntimeException(e); |
| 54 |
0 |
} |
| 55 |
0 |
} |
| 56 |
|
} |
| 57 |
|
|
| 58 |
|
public static <T> T locate(Class<T> type) { |
| 59 |
|
try { |
| 60 |
0 |
return find(getInitialContext(), type); |
| 61 |
0 |
} catch (NamingException e) { |
| 62 |
0 |
throw new RuntimeException("Couldn't locate Object with type " |
| 63 |
|
+ type.getName()); |
| 64 |
|
} |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
@SuppressWarnings("unchecked") |
| 68 |
|
private static <T> T find(Context ctx, Class<T> ejbType) |
| 69 |
|
throws NamingException { |
| 70 |
0 |
NamingEnumeration ne = null; |
| 71 |
|
try { |
| 72 |
0 |
ne = ctx.list(""); |
| 73 |
0 |
while (ne.hasMore()) { |
| 74 |
0 |
NameClassPair element = (NameClassPair) ne.next(); |
| 75 |
|
try { |
| 76 |
0 |
Object value = ctx.lookup(element.getName()); |
| 77 |
|
|
| 78 |
0 |
if (value != null) { |
| 79 |
0 |
if (ejbType.isAssignableFrom(value.getClass())) { |
| 80 |
0 |
return (T) value; |
| 81 |
0 |
} else if (value instanceof Context) { |
| 82 |
0 |
Context subctx = (Context) value; |
| 83 |
0 |
Object ejb = find(subctx, ejbType); |
| 84 |
0 |
if (ejb != null) |
| 85 |
0 |
return (T) ejb; |
| 86 |
|
} |
| 87 |
|
} |
| 88 |
0 |
} catch (Exception e) { |
| 89 |
0 |
} |
| 90 |
0 |
} |
| 91 |
0 |
} catch (NamingException e) { |
| 92 |
|
} finally { |
| 93 |
0 |
try { |
| 94 |
0 |
if (ne != null) { |
| 95 |
0 |
ne.close(); |
| 96 |
|
} |
| 97 |
0 |
} catch (NamingException e) { |
| 98 |
0 |
} |
| 99 |
0 |
} |
| 100 |
0 |
return null; |
| 101 |
|
} |
| 102 |
|
|
| 103 |
|
public static void printTree(Log log) { |
| 104 |
0 |
log.debug("----------------------------------------------------------"); |
| 105 |
0 |
log.debug("JNDI Tree"); |
| 106 |
0 |
log.debug("----------------------------------------------------------"); |
| 107 |
0 |
print(log, initialContext, ""); |
| 108 |
0 |
log.debug("----------------------------------------------------------"); |
| 109 |
|
try { |
| 110 |
0 |
print(log, (Context) getInitialContext().lookup("java:"), ""); |
| 111 |
0 |
} catch (NamingException e) { |
| 112 |
0 |
throw new RuntimeException(e); |
| 113 |
0 |
} |
| 114 |
0 |
log.debug("----------------------------------------------------------"); |
| 115 |
0 |
} |
| 116 |
|
|
| 117 |
|
public static void print(Log log, Context ctx, String tab) { |
| 118 |
0 |
NamingEnumeration e = null; |
| 119 |
|
try { |
| 120 |
0 |
e = ctx.list(""); |
| 121 |
0 |
while (e.hasMore()) { |
| 122 |
0 |
NameClassPair element = (NameClassPair) e.next(); |
| 123 |
0 |
log.debug(tab + element); |
| 124 |
0 |
Object value = ctx.lookup(element.getName()); |
| 125 |
0 |
if (value instanceof Context) { |
| 126 |
0 |
Context subctx = (Context) value; |
| 127 |
0 |
print(log, subctx, tab + "--> "); |
| 128 |
|
} |
| 129 |
0 |
} |
| 130 |
0 |
} catch (NamingException e1) { |
| 131 |
|
} finally { |
| 132 |
0 |
try { |
| 133 |
0 |
if (e != null) { |
| 134 |
0 |
e.close(); |
| 135 |
|
} |
| 136 |
0 |
} catch (NamingException e2) { |
| 137 |
0 |
} |
| 138 |
0 |
} |
| 139 |
0 |
} |
| 140 |
|
} |