View Javadoc

1   /*
2    * Licensed to the University Corporation for Advanced Internet Development, 
3    * Inc. (UCAID) under one or more contributor license agreements.  See the 
4    * NOTICE file distributed with this work for additional information regarding
5    * copyright ownership. The UCAID licenses this file to You under the Apache 
6    * License, Version 2.0 (the "License"); you may not use this file except in 
7    * compliance with the License.  You may obtain a copy of the License at
8    *
9    *    http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package edu.internet2.middleware.shibboleth.common.attribute.resolver.provider.dataConnector;
19  
20  import edu.vt.middleware.ldap.Ldap;
21  import edu.vt.middleware.ldap.pool.BlockingLdapPool;
22  import edu.vt.middleware.ldap.pool.LdapFactory;
23  import edu.vt.middleware.ldap.pool.LdapPool;
24  import edu.vt.middleware.ldap.pool.LdapPoolConfig;
25  import edu.vt.middleware.ldap.pool.SoftLimitLdapPool;
26  
27  /**
28   * Ldap pool strategy backed by the vt-ldap library. 
29   */
30  public class LdapPoolVTStrategy implements LdapPoolStrategy {
31  
32      /** Underlying pool. */
33      private LdapPool<Ldap> ldapPool;
34  
35      /** Ldap pool configuration. */
36      private LdapPoolConfig ldapPoolConfig;
37  
38      /** Factory for making ldap objects. */
39      private LdapFactory<Ldap> ldapFactory;
40  
41      /** Whether to block when empty. */
42      private boolean blockWhenEmpty;
43  
44      /**
45       * Default constructor.
46       */
47      public LdapPoolVTStrategy() {}
48  
49      /** {@inheritDoc} */
50      public void setLdapPoolConfig(LdapPoolConfig config) {
51          ldapPoolConfig = config;
52      }
53  
54      /** {@inheritDoc} */
55      public void setLdapFactory(LdapFactory<Ldap> factory) {
56          ldapFactory = factory;
57      }
58  
59      /** {@inheritDoc} */
60      public void setBlockWhenEmpty(boolean block) {
61          blockWhenEmpty = block;
62      }
63  
64      /** {@inheritDoc} */
65      public void initialize() {
66          ldapPoolConfig.setPruneTimerPeriod(ldapPoolConfig.getValidateTimerPeriod());
67          if (blockWhenEmpty) {
68              ldapPool = new BlockingLdapPool(ldapPoolConfig, ldapFactory);
69          } else {
70              ldapPool = new SoftLimitLdapPool(ldapPoolConfig, ldapFactory);
71          }
72          ldapPool.initialize();
73      }
74  
75      /** {@inheritDoc} */
76      public Ldap checkOut() throws Exception {
77          return ldapPool.checkOut();
78      }
79  
80      /** {@inheritDoc} */
81      public void checkIn(Ldap l) throws Exception {
82          ldapPool.checkIn(l);
83      }
84  }