1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ldap.pool;
18
19 import javax.naming.NamingException;
20 import javax.naming.directory.DirContext;
21 import javax.naming.ldap.Control;
22 import javax.naming.ldap.ExtendedRequest;
23 import javax.naming.ldap.ExtendedResponse;
24 import javax.naming.ldap.LdapContext;
25
26 import org.apache.commons.lang.Validate;
27 import org.apache.commons.pool.KeyedObjectPool;
28 import org.springframework.ldap.pool.factory.PoolingContextSource;
29
30
31
32
33
34
35
36
37 public class DelegatingLdapContext extends DelegatingDirContext implements LdapContext {
38 private LdapContext delegateLdapContext;
39
40
41
42
43
44
45
46
47
48 public DelegatingLdapContext(KeyedObjectPool keyedObjectPool, LdapContext delegateLdapContext, DirContextType dirContextType) {
49 super(keyedObjectPool, delegateLdapContext, dirContextType);
50 Validate.notNull(delegateLdapContext, "delegateLdapContext may not be null");
51
52 this.delegateLdapContext = delegateLdapContext;
53 }
54
55
56 //
57
58
59
60
61 public LdapContext getDelegateLdapContext() {
62 return this.delegateLdapContext;
63 }
64
65
66 public DirContext getDelegateDirContext() {
67 return this.getDelegateLdapContext();
68 }
69
70
71
72
73
74
75 public LdapContext getInnermostDelegateLdapContext() {
76 final LdapContext delegateLdapContext = this.getDelegateLdapContext();
77
78 if (delegateLdapContext instanceof DelegatingLdapContext) {
79 return ((DelegatingLdapContext)delegateLdapContext).getInnermostDelegateLdapContext();
80 }
81
82 return delegateLdapContext;
83 }
84
85 protected void assertOpen() throws NamingException {
86 if (this.delegateLdapContext == null) {
87 throw new NamingException("LdapContext is closed.");
88 }
89
90 super.assertOpen();
91 }
92
93
94 //
95
96
97
98
99 public boolean equals(Object obj) {
100 if (this == obj) {
101 return true;
102 }
103 if (!(obj instanceof LdapContext)) {
104 return false;
105 }
106
107 final LdapContext thisLdapContext = this.getInnermostDelegateLdapContext();
108 LdapContext otherLdapContext = (LdapContext)obj;
109 if (otherLdapContext instanceof DelegatingLdapContext) {
110 otherLdapContext = ((DelegatingLdapContext)otherLdapContext).getInnermostDelegateLdapContext();
111 }
112
113 return thisLdapContext == otherLdapContext || (thisLdapContext != null && thisLdapContext.equals(otherLdapContext));
114 }
115
116
117
118
119 public int hashCode() {
120 final LdapContext context = this.getInnermostDelegateLdapContext();
121 return (context != null ? context.hashCode() : 0);
122 }
123
124
125
126
127 public String toString() {
128 final LdapContext context = this.getInnermostDelegateLdapContext();
129 return (context != null ? context.toString() : "LdapContext is closed");
130 }
131
132
133 //
134
135
136
137
138 public ExtendedResponse extendedOperation(ExtendedRequest request) throws NamingException {
139 this.assertOpen();
140 return this.getDelegateLdapContext().extendedOperation(request);
141 }
142
143
144
145
146 public Control[] getConnectControls() throws NamingException {
147 this.assertOpen();
148 return this.getDelegateLdapContext().getConnectControls();
149 }
150
151
152
153
154 public Control[] getRequestControls() throws NamingException {
155 this.assertOpen();
156 return this.getDelegateLdapContext().getRequestControls();
157 }
158
159
160
161
162 public Control[] getResponseControls() throws NamingException {
163 this.assertOpen();
164 return this.getDelegateLdapContext().getResponseControls();
165 }
166
167
168
169
170 public LdapContext newInstance(Control[] requestControls) throws NamingException {
171 throw new UnsupportedOperationException("Cannot call newInstance on a pooled context");
172 }
173
174
175
176
177 public void reconnect(Control[] connCtls) throws NamingException {
178 throw new UnsupportedOperationException("Cannot call reconnect on a pooled context");
179 }
180
181
182
183
184 public void setRequestControls(Control[] requestControls) throws NamingException {
185 throw new UnsupportedOperationException("Cannot call setRequestControls on a pooled context");
186 }
187
188
189
190
191 public void close() throws NamingException {
192 if (this.delegateLdapContext == null) {
193 return;
194 }
195
196 super.close();
197 this.delegateLdapContext = null;
198 }
199 }