buildframework/helium/tools/common/java/src/com/nokia/ant/taskdefs/LDAP.java
changeset 1 be27ed110b50
child 179 d8ac696cc51f
equal deleted inserted replaced
0:044383f39525 1:be27ed110b50
       
     1 /*
       
     2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17  
       
    18 package com.nokia.ant.taskdefs;
       
    19 
       
    20 import org.apache.tools.ant.Task;
       
    21 import org.apache.tools.ant.BuildException;
       
    22 import javax.naming.*;
       
    23 import javax.naming.directory.*;
       
    24 import java.util.Hashtable;
       
    25 
       
    26 /**
       
    27  * Task is to search data from LDAP server.
       
    28  *
       
    29  * Usage: <hlm:ldap url="${ldap.server.url}" rootdn="${ldap.root.dn}" filter="uid=${env.USERNAME}" outputproperty="email.from" key="mail"/>
       
    30  */
       
    31 public class LDAP extends Task 
       
    32 {
       
    33     private String url;
       
    34     private String rootdn;
       
    35     private String filter;
       
    36     private String key;
       
    37     private String property;
       
    38         
       
    39     public void execute()
       
    40     {
       
    41         
       
    42         if (url == null)
       
    43             throw new BuildException("'url' attribute is not defined");
       
    44         if (rootdn == null)
       
    45             throw new BuildException("'rootdn' attribute is not defined");
       
    46         if (filter == null)
       
    47             throw new BuildException("'filter' attribute is not defined");
       
    48         if (property == null)
       
    49             throw new BuildException("'property' attribute is not defined");
       
    50         if (key == null)
       
    51             throw new BuildException("'key' attribute is not defined");    
       
    52         
       
    53         // Set up environment for creating initial context
       
    54         Hashtable<String, String> env = new Hashtable<String, String>();
       
    55         env.put(Context.INITIAL_CONTEXT_FACTORY,
       
    56                 "com.sun.jndi.ldap.LdapCtxFactory");
       
    57         env.put(Context.PROVIDER_URL, url + "/" + rootdn);
       
    58 
       
    59         // Create initial context
       
    60         try 
       
    61         {
       
    62             DirContext ctx = new InitialDirContext(env);
       
    63             SearchControls controls = new SearchControls();
       
    64             controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
       
    65             NamingEnumeration<SearchResult> en = ctx.search("", filter, controls);
       
    66             if (en.hasMore()) 
       
    67             {
       
    68                 SearchResult sr = en.next();
       
    69                 getProject().setProperty(property, (String)sr.getAttributes().get(key).get());
       
    70                 return;
       
    71             }
       
    72         } 
       
    73         catch (NamingException exc) 
       
    74         {
       
    75             throw new BuildException(exc);
       
    76         }
       
    77     }
       
    78 
       
    79     public String getUrl() 
       
    80     {
       
    81         return url;
       
    82     }
       
    83 
       
    84     public void setUrl(String url) 
       
    85     {
       
    86         this.url = url;
       
    87     }
       
    88     
       
    89 
       
    90     public String getRootdn() 
       
    91     {
       
    92         return rootdn;
       
    93     }
       
    94 
       
    95     public void setRootdn(String rootdn) 
       
    96     {
       
    97         this.rootdn = rootdn;
       
    98     }
       
    99 
       
   100     public String getFilter() 
       
   101     {
       
   102         return filter;
       
   103     }
       
   104 
       
   105     public void setFilter(String filter) 
       
   106     {
       
   107         this.filter = filter;
       
   108     }
       
   109 
       
   110     public String getOutputProperty() 
       
   111     {
       
   112         return property;
       
   113     }
       
   114 
       
   115     public void setOutputProperty(String property) 
       
   116     {
       
   117         this.property = property;
       
   118     }
       
   119 
       
   120     public String getKey() 
       
   121     {
       
   122         return key;
       
   123     }
       
   124 
       
   125     public void setKey(String key) 
       
   126     {
       
   127         this.key = key;
       
   128     }
       
   129 }