buildframework/helium/sf/java/core/src/com/nokia/helium/core/ant/taskdefs/LDAPTask.java
changeset 628 7c4a911dc066
parent 588 c7c26511138f
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
    15  *
    15  *
    16  */
    16  */
    17 
    17 
    18 package com.nokia.helium.core.ant.taskdefs;
    18 package com.nokia.helium.core.ant.taskdefs;
    19 
    19 
       
    20 import org.apache.tools.ant.BuildException;
       
    21 import org.apache.tools.ant.Project;
    20 import org.apache.tools.ant.Task;
    22 import org.apache.tools.ant.Task;
    21 import org.apache.tools.ant.BuildException;
    23 
    22 import javax.naming.*;
    24 import com.nokia.helium.core.LDAPException;
    23 import javax.naming.directory.*;
    25 import com.nokia.helium.core.LDAPHelper;
    24 import java.util.Hashtable;
       
    25 
    26 
    26 /**
    27 /**
    27  * Task is to search data from LDAP server.
    28  * Task is to search data from LDAP server.
    28  * 
    29  * 
    29  * <pre>
    30  * <pre>
    39     private String url;
    40     private String url;
    40     private String rootdn;
    41     private String rootdn;
    41     private String filter;
    42     private String filter;
    42     private String key;
    43     private String key;
    43     private String property;
    44     private String property;
       
    45     private boolean failOnError = true;
    44 
    46 
    45     /**
    47     /**
    46      * Method executes current task.
    48      * Method executes current task.
    47      */
    49      */
    48     public void execute() {
    50     public void execute() {
    49         if (url == null)
    51         if (url == null) {
    50             throw new BuildException("'url' attribute is not defined");
    52             throw new BuildException("'url' attribute is not defined");
    51         if (rootdn == null)
    53         }
       
    54         if (rootdn == null) {
    52             throw new BuildException("'rootdn' attribute is not defined");
    55             throw new BuildException("'rootdn' attribute is not defined");
    53         if (filter == null)
    56         }
       
    57         if (filter == null) {
    54             throw new BuildException("'filter' attribute is not defined");
    58             throw new BuildException("'filter' attribute is not defined");
    55         if (property == null)
    59         }
       
    60         if (property == null) {
    56             throw new BuildException("'property' attribute is not defined");
    61             throw new BuildException("'property' attribute is not defined");
    57         if (key == null)
    62         }
       
    63         if (key == null) {
    58             throw new BuildException("'key' attribute is not defined");
    64             throw new BuildException("'key' attribute is not defined");
       
    65         }
    59 
    66 
    60         // Set up environment for creating initial context
       
    61         Hashtable<String, String> env = new Hashtable<String, String>();
       
    62         env.put(Context.INITIAL_CONTEXT_FACTORY,
       
    63                 "com.sun.jndi.ldap.LdapCtxFactory");
       
    64         env.put(Context.PROVIDER_URL, url + "/" + rootdn);
       
    65 
       
    66         // Create initial context
       
    67         try {
    67         try {
    68             DirContext ctx = new InitialDirContext(env);
    68             LDAPHelper helper = new LDAPHelper(url, rootdn);
    69             SearchControls controls = new SearchControls();
    69             String value = helper.getAttributeAsString(filter, key);
    70             controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    70             if (value != null) {
    71             NamingEnumeration<SearchResult> en = ctx.search("", filter,
    71                 getProject().setNewProperty(property, value);
    72                     controls);
    72             } else {
    73             if (en.hasMore()) {
    73                 log("Could not find value for key: " + key, Project.MSG_WARN);
    74                 SearchResult sr = en.next();
       
    75                 getProject().setProperty(property,
       
    76                         (String) sr.getAttributes().get(key).get());
       
    77                 return;
       
    78             }
    74             }
    79         } catch (NamingException exc) {
    75         } catch (LDAPException exc) {
    80             throw new BuildException(exc.getMessage());
    76             log(exc.getMessage(), Project.MSG_ERR);
       
    77             if (failOnError) {
       
    78                 throw new BuildException(exc.getMessage(), exc);
       
    79             }
    81         } 
    80         } 
    82     }
    81     }
    83 
    82 
    84     /**
    83     /**
    85      * Return the LDAP server URL.
    84      * Return the LDAP server URL.
   173      * @ant.required
   172      * @ant.required
   174      */
   173      */
   175     public void setKey(String key) {
   174     public void setKey(String key) {
   176         this.key = key;
   175         this.key = key;
   177     }
   176     }
       
   177 
       
   178     /**
       
   179      * Defines if the task should fail on error or not found.
       
   180      * @param failOnError
       
   181      * @ant.not-required Default is true
       
   182      */
       
   183     public void setFailOnError(boolean failOnError) {
       
   184         this.failOnError = failOnError;
       
   185     }
       
   186 
   178 }
   187 }