buildframework/helium/sf/java/core/src/com/nokia/helium/core/ant/taskdefs/LDAPTask.java
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
parent 588 c7c26511138f
permissions -rw-r--r--
helium_11.0.0-e00f171ca185
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     1
/*
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     2
 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     3
 * All rights reserved.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     4
 * This component and the accompanying materials are made available
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     5
 * under the terms of the License "Eclipse Public License v1.0"
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     6
 * which accompanies this distribution, and is available
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     7
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     8
 *
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
     9
 * Initial Contributors:
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    10
 * Nokia Corporation - initial contribution.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    11
 *
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    12
 * Contributors:
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    13
 *
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    14
 * Description: 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    15
 *
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    16
 */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    17
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    18
package com.nokia.helium.core.ant.taskdefs;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    19
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    20
import org.apache.tools.ant.BuildException;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    21
import org.apache.tools.ant.Project;
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    22
import org.apache.tools.ant.Task;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    23
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    24
import com.nokia.helium.core.LDAPException;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    25
import com.nokia.helium.core.LDAPHelper;
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    26
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    27
/**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    28
 * Task is to search data from LDAP server.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    29
 * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    30
 * <pre>
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    31
 * Usage: &lt;hlm:ldap url=&quot;${ldap.server.url}&quot; 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    32
 *                  rootdn=&quot;${ldap.root.dn}&quot; 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    33
 *                  filter=&quot;uid=${env.USERNAME}&quot; 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    34
 *                  outputproperty=&quot;email.from&quot; 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    35
 *                  key=&quot;mail&quot;/&gt;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    36
 * </pre>
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    37
 * @ant.task name="ldap" category="Core" 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    38
 */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    39
public class LDAPTask extends Task {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    40
    private String url;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    41
    private String rootdn;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    42
    private String filter;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    43
    private String key;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    44
    private String property;
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    45
    private boolean failOnError = true;
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    46
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    47
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    48
     * Method executes current task.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    49
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    50
    public void execute() {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    51
        if (url == null) {
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    52
            throw new BuildException("'url' attribute is not defined");
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    53
        }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    54
        if (rootdn == null) {
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    55
            throw new BuildException("'rootdn' attribute is not defined");
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    56
        }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    57
        if (filter == null) {
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    58
            throw new BuildException("'filter' attribute is not defined");
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    59
        }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    60
        if (property == null) {
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    61
            throw new BuildException("'property' attribute is not defined");
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    62
        }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    63
        if (key == null) {
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    64
            throw new BuildException("'key' attribute is not defined");
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    65
        }
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    66
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    67
        try {
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    68
            LDAPHelper helper = new LDAPHelper(url, rootdn);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    69
            String value = helper.getAttributeAsString(filter, key);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    70
            if (value != null) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    71
                getProject().setNewProperty(property, value);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    72
            } else {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    73
                log("Could not find value for key: " + key, Project.MSG_WARN);
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    74
            }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    75
        } catch (LDAPException exc) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    76
            log(exc.getMessage(), Project.MSG_ERR);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    77
            if (failOnError) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    78
                throw new BuildException(exc.getMessage(), exc);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    79
            }
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    80
        } 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    81
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    82
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    83
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    84
     * Return the LDAP server URL.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    85
     * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    86
     * @return the LDAP server URL.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    87
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    88
    public String getUrl() {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    89
        return url;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    90
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    91
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    92
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    93
     * Set LDAP server URL.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    94
     * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    95
     * @param url is the LDAP server URL to set.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    96
     * @ant.required
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    97
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    98
    public void setUrl(String url) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
    99
        this.url = url;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   100
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   101
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   102
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   103
     * Return LDAP root distinguished name.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   104
     * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   105
     * @return LDAP root distinguished name
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   106
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   107
    public String getRootdn() {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   108
        return rootdn;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   109
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   110
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   111
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   112
     * Set LDAP root distinguished name.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   113
     * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   114
     * @param rootdn is the LDAP root distinguished name to set.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   115
     * @ant.required
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   116
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   117
    public void setRootdn(String rootdn) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   118
        this.rootdn = rootdn;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   119
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   120
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   121
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   122
     * Return object name to search in the LDAP.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   123
     * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   124
     * @return the object name to search in the LDAP
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   125
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   126
    public String getFilter() {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   127
        return filter;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   128
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   129
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   130
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   131
     * Set the object name to search in the LDAP.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   132
     * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   133
     * @param filter is the object name to set.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   134
     * @ant.required
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   135
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   136
    public void setFilter(String filter) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   137
        this.filter = filter;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   138
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   139
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   140
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   141
     * Return the output property to set.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   142
     * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   143
     * @return is the output property to set.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   144
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   145
    public String getOutputProperty() {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   146
        return property;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   147
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   148
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   149
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   150
     * Set the output property if the user found.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   151
     * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   152
     * @param property is the property to be set.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   153
     * @ant.required
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   154
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   155
    public void setOutputProperty(String property) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   156
        this.property = property;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   157
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   158
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   159
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   160
     * Return the key search element to search user information.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   161
     * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   162
     * @return the key search element.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   163
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   164
    public String getKey() {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   165
        return key;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   166
    }
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   167
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   168
    /**
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   169
     * Set the key search element to search the user information.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   170
     * 
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   171
     * @param key is the key search element to set.
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   172
     * @ant.required
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   173
     */
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   174
    public void setKey(String key) {
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   175
        this.key = key;
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   176
    }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   177
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   178
    /**
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   179
     * Defines if the task should fail on error or not found.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   180
     * @param failOnError
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   181
     * @ant.not-required Default is true
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   182
     */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   183
    public void setFailOnError(boolean failOnError) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   184
        this.failOnError = failOnError;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   185
    }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   186
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents:
diff changeset
   187
}