View Javadoc

1   /*
2    * Copyright 2005-2008 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Used by {@link PoolingContextSource} to wrap a {@link LdapContext}, delegating most methods
32   * to the underlying context. This class extends {@link DelegatingDirContext} which handles returning
33   * the context to the pool on a call to {@link #close()}
34   * 
35   * @author Eric Dalquist
36   */
37  public class DelegatingLdapContext extends DelegatingDirContext implements LdapContext {
38      private LdapContext delegateLdapContext;
39  
40      /**
41       * Create a new delegating ldap context for the specified pool, context and context type.
42       * 
43       * @param keyedObjectPool The pool the delegate context was checked out from.
44       * @param delegateLdapContext The ldap context to delegate operations to.
45       * @param dirContextType The type of context, used as a key for the pool.
46       * @throws IllegalArgumentException if any of the arguments are null
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      //***** Helper Methods *****//
57      
58      /**
59       * @return The direct delegate for this ldap context proxy
60       */
61      public LdapContext getDelegateLdapContext() {
62          return this.delegateLdapContext;
63      }
64      
65      // cannot return subtype in overridden method unless Java5
66      public DirContext getDelegateDirContext() {
67          return this.getDelegateLdapContext();
68      }
69  
70      /**
71       * Recursivley inspect delegates until a non-delegating ldap context is found.
72       * 
73       * @return The innermost (real) DirContext that is being delegated to.
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      //***** Object methods *****//
95  
96      /**
97       * @see java.lang.Object#equals(java.lang.Object)
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      * @see java.lang.Object#hashCode()
118      */
119     public int hashCode() {
120         final LdapContext context = this.getInnermostDelegateLdapContext();
121         return (context != null ? context.hashCode() : 0);
122     }
123 
124     /**
125      * @see java.lang.Object#toString()
126      */
127     public String toString() {
128         final LdapContext context = this.getInnermostDelegateLdapContext();
129         return (context != null ? context.toString() : "LdapContext is closed");
130     }
131     
132     
133     //***** LdapContext Interface Delegates *****//
134 
135     /**
136      * @see javax.naming.ldap.LdapContext#extendedOperation(javax.naming.ldap.ExtendedRequest)
137      */
138     public ExtendedResponse extendedOperation(ExtendedRequest request) throws NamingException {
139         this.assertOpen();
140         return this.getDelegateLdapContext().extendedOperation(request);
141     }
142 
143     /**
144      * @see javax.naming.ldap.LdapContext#getConnectControls()
145      */
146     public Control[] getConnectControls() throws NamingException {
147         this.assertOpen();
148         return this.getDelegateLdapContext().getConnectControls();
149     }
150 
151     /**
152      * @see javax.naming.ldap.LdapContext#getRequestControls()
153      */
154     public Control[] getRequestControls() throws NamingException {
155         this.assertOpen();
156         return this.getDelegateLdapContext().getRequestControls();
157     }
158 
159     /**
160      * @see javax.naming.ldap.LdapContext#getResponseControls()
161      */
162     public Control[] getResponseControls() throws NamingException {
163         this.assertOpen();
164         return this.getDelegateLdapContext().getResponseControls();
165     }
166 
167     /**
168      * @see javax.naming.ldap.LdapContext#newInstance(javax.naming.ldap.Control[])
169      */
170     public LdapContext newInstance(Control[] requestControls) throws NamingException {
171         throw new UnsupportedOperationException("Cannot call newInstance on a pooled context");
172     }
173 
174     /**
175      * @see javax.naming.ldap.LdapContext#reconnect(javax.naming.ldap.Control[])
176      */
177     public void reconnect(Control[] connCtls) throws NamingException {
178         throw new UnsupportedOperationException("Cannot call reconnect on a pooled context");
179     }
180 
181     /**
182      * @see javax.naming.ldap.LdapContext#setRequestControls(javax.naming.ldap.Control[])
183      */
184     public void setRequestControls(Control[] requestControls) throws NamingException {
185         throw new UnsupportedOperationException("Cannot call setRequestControls on a pooled context");
186     }
187 
188     /**
189      * @see DelegatingDirContext#close()
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 }