Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.protocol.ldap.sampler » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * 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   
   19   package org.apache.jmeter.protocol.ldap.sampler;
   20   
   21   import java.util.Hashtable;
   22   
   23   import javax.naming.Context;
   24   import javax.naming.NamingEnumeration;
   25   import javax.naming.NamingException;
   26   // import javax.naming.directory.Attributes;
   27   import javax.naming.directory.BasicAttributes;
   28   import javax.naming.directory.DirContext;
   29   import javax.naming.directory.InitialDirContext;
   30   import javax.naming.directory.ModificationItem;
   31   import javax.naming.directory.SearchControls;
   32   // import javax.naming.directory.SearchResult;
   33   
   34   import org.apache.jorphan.logging.LoggingManager;
   35   import org.apache.log.Logger;
   36   
   37   /**
   38    * Ldap Client class is main class to create, modify, search and delete all the
   39    * LDAP functionality available.
   40    *
   41    */
   42   public class LdapClient {
   43       private static final Logger log = LoggingManager.getLoggerForClass();
   44   
   45       private DirContext dirContext = null;
   46   
   47       /**
   48        * Constructor for the LdapClient object.
   49        */
   50       public LdapClient() {
   51       }
   52   
   53       /**
   54        * Connect to server.
   55        */
   56       public void connect(String host, String port, String rootdn, String username, String password)
   57               throws NamingException {
   58           Hashtable env = new Hashtable();
   59           env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); //$NON-NLS-1$
   60           env.put(Context.PROVIDER_URL, "ldap://" + host + ":" + port + "/" + rootdn); //$NON-NLS-1$ $NON-NLS-2$ $NON-NLS-3$
   61           env.put(Context.REFERRAL, "throw"); //$NON-NLS-1$
   62           env.put(Context.SECURITY_CREDENTIALS, password);
   63           env.put(Context.SECURITY_PRINCIPAL, username);
   64           dirContext = new InitialDirContext(env);
   65       }
   66   
   67       /**
   68        * Disconnect from the server.
   69        */
   70       public void disconnect() {
   71           try {
   72               if (dirContext != null) {
   73                   dirContext.close();
   74                   dirContext = null;
   75               }
   76           } catch (NamingException e) {
   77               log.error("Ldap client - ", e);
   78           }
   79       }
   80   
   81       /**
   82        * Filter the data in the ldap directory for the given search base.
   83        *
   84        * @param searchBase
   85        *            where the search should start
   86        * @param searchFilter
   87        *            filter this value from the base
   88        */
   89       public boolean searchTest(String searchBase, String searchFilter) throws NamingException {
   90           // System.out.println("Base="+searchBase+" Filter="+searchFilter);
   91           SearchControls searchcontrols = new SearchControls(SearchControls.SUBTREE_SCOPE,
   92                   1L, // count limit
   93                   0, // time limit
   94                   null,// attributes (null = all)
   95                   false,// return object ?
   96                   false);// dereference links?
   97           NamingEnumeration ne = dirContext.search(searchBase, searchFilter, searchcontrols);
   98           // System.out.println("Loop "+ne.toString()+" "+ne.hasMore());
   99           // while (ne.hasMore()){
  100           // Object tmp = ne.next();
  101           // System.out.println(tmp.getClass().getName());
  102           // SearchResult sr = (SearchResult) tmp;
  103           // Attributes at = sr.getAttributes();
  104           // System.out.println(at.get("cn"));
  105           // }
  106           // System.out.println("Done "+ne.hasMore());
  107           return ne.hasMore();
  108       }
  109   
  110       /**
  111        * Modify the attribute in the ldap directory for the given string.
  112        *
  113        * @param mods
  114        *            add all the entry in to the ModificationItem
  115        * @param string
  116        *            the string (dn) value
  117        */
  118       public void modifyTest(ModificationItem[] mods, String string) throws NamingException {
  119           dirContext.modifyAttributes(string, mods);
  120       }
  121   
  122       /**
  123        * Create the attribute in the ldap directory for the given string.
  124        *
  125        * @param basicattributes
  126        *            add all the entry in to the basicattribute
  127        * @param string
  128        *            the string (dn) value
  129        */
  130       public void createTest(BasicAttributes basicattributes, String string) throws NamingException {
  131           // DirContext dc = //TODO perhaps return this?
  132           dirContext.createSubcontext(string, basicattributes);
  133       }
  134   
  135       /**
  136        * Delete the attribute from the ldap directory.
  137        *
  138        * @param string
  139        *            the string (dn) value
  140        */
  141       public void deleteTest(String string) throws NamingException {
  142           dirContext.destroySubcontext(string);
  143       }
  144   }

Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.protocol.ldap.sampler » [javadoc | source]