1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.jetspeed.security.spi.impl.ldap; |
18 |
|
|
19 |
|
import java.lang.reflect.InvocationHandler; |
20 |
|
import java.lang.reflect.InvocationTargetException; |
21 |
|
import java.lang.reflect.Method; |
22 |
|
import java.lang.reflect.Proxy; |
23 |
|
import java.util.Properties; |
24 |
|
|
25 |
|
import javax.naming.CommunicationException; |
26 |
|
import javax.naming.Context; |
27 |
|
import javax.naming.NamingException; |
28 |
|
import javax.naming.ServiceUnavailableException; |
29 |
|
import javax.naming.ldap.InitialLdapContext; |
30 |
|
import javax.naming.ldap.LdapContext; |
31 |
|
|
32 |
|
import org.apache.commons.lang.StringUtils; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
public class LdapContextProxy implements InvocationHandler |
41 |
|
{ |
42 |
|
private Properties env; |
43 |
|
private LdapContext ctx; |
44 |
|
|
45 |
|
public static LdapContext createProxy(LdapBindingConfig config) |
46 |
|
{ |
47 |
0 |
LdapContext proxy = config.getContext(); |
48 |
|
|
49 |
0 |
if ( proxy == null || !(Proxy.getInvocationHandler(proxy) instanceof LdapContextProxy)) |
50 |
|
{ |
51 |
0 |
proxy = (LdapContext)Proxy.newProxyInstance(LdapContext.class.getClassLoader(),new Class[]{LdapContext.class}, new LdapContextProxy(config)); |
52 |
0 |
config.setContext(proxy); |
53 |
|
} |
54 |
0 |
return proxy; |
55 |
|
} |
56 |
|
|
57 |
|
private LdapContextProxy(LdapBindingConfig ldapBindingConfig) |
58 |
0 |
{ |
59 |
0 |
env = new Properties(); |
60 |
0 |
env.put(Context.INITIAL_CONTEXT_FACTORY, ldapBindingConfig.getInitialContextFactory()); |
61 |
0 |
env.put(Context.PROVIDER_URL, ldapBindingConfig.getLdapScheme() + "://" + ldapBindingConfig.getLdapServerName() + ":" |
62 |
|
+ ldapBindingConfig.getLdapServerPort() + "/" + ldapBindingConfig.getRootContext()); |
63 |
0 |
env.put(Context.SECURITY_PRINCIPAL, ldapBindingConfig.getRootDn()); |
64 |
0 |
env.put(Context.SECURITY_CREDENTIALS, ldapBindingConfig.getRootPassword()); |
65 |
0 |
env.put(Context.SECURITY_AUTHENTICATION, ldapBindingConfig.getLdapSecurityLevel()); |
66 |
0 |
if ( !StringUtils.isEmpty(ldapBindingConfig.getLdapSecurityProtocol()) ) |
67 |
|
{ |
68 |
0 |
env.put(Context.SECURITY_PROTOCOL, ldapBindingConfig.getLdapSecurityProtocol()); |
69 |
|
} |
70 |
0 |
if ( !StringUtils.isEmpty(ldapBindingConfig.getLdapSocketFactory()) ) |
71 |
|
{ |
72 |
0 |
env.put("java.naming.ldap.factory.socket", ldapBindingConfig.getLdapSocketFactory()); |
73 |
|
} |
74 |
0 |
} |
75 |
|
|
76 |
|
private LdapContext getCtx() throws NamingException |
77 |
|
{ |
78 |
0 |
if ( ctx == null ) |
79 |
|
{ |
80 |
0 |
ctx = new InitialLdapContext(env, null); |
81 |
|
} |
82 |
0 |
return ctx; |
83 |
|
} |
84 |
|
|
85 |
|
private void closeCtx() |
86 |
|
{ |
87 |
0 |
if ( ctx != null ) |
88 |
|
{ |
89 |
|
try |
90 |
|
{ |
91 |
0 |
ctx.close(); |
92 |
|
} |
93 |
0 |
catch (Exception e) |
94 |
|
{ |
95 |
0 |
} |
96 |
0 |
ctx = null; |
97 |
|
} |
98 |
0 |
} |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
public synchronized Object invoke(Object proxy, Method m, Object[] args) throws Throwable |
106 |
|
{ |
107 |
0 |
Object result = null; |
108 |
0 |
boolean close = "close".equals(m.getName()) && args.length == 0; |
109 |
0 |
if ( close && ctx == null ) |
110 |
|
{ |
111 |
|
|
112 |
|
; |
113 |
|
} |
114 |
|
else |
115 |
|
{ |
116 |
0 |
LdapContext ctx = getCtx(); |
117 |
|
|
118 |
|
try |
119 |
|
{ |
120 |
0 |
result = m.invoke(ctx,args); |
121 |
0 |
if ( close ) |
122 |
|
{ |
123 |
0 |
closeCtx(); |
124 |
|
} |
125 |
|
} |
126 |
0 |
catch (Throwable t) |
127 |
|
{ |
128 |
0 |
closeCtx(); |
129 |
|
|
130 |
0 |
if ( t instanceof InvocationTargetException) |
131 |
|
{ |
132 |
0 |
t = ((InvocationTargetException)t).getTargetException(); |
133 |
|
} |
134 |
0 |
if (t instanceof ServiceUnavailableException || t instanceof CommunicationException) |
135 |
|
{ |
136 |
|
try |
137 |
|
{ |
138 |
0 |
ctx = getCtx(); |
139 |
0 |
result = m.invoke(ctx,args); |
140 |
|
} |
141 |
0 |
catch (Throwable t2) |
142 |
|
{ |
143 |
0 |
closeCtx(); |
144 |
0 |
if ( t2 instanceof InvocationTargetException) |
145 |
|
{ |
146 |
0 |
t2 = ((InvocationTargetException)t2).getTargetException(); |
147 |
|
} |
148 |
|
|
149 |
0 |
throw t2; |
150 |
0 |
} |
151 |
|
} |
152 |
0 |
throw t; |
153 |
0 |
} |
154 |
|
} |
155 |
0 |
return result; |
156 |
|
} |
157 |
|
} |