KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > security > test > opends > OpenDSUtil


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.security.test.opends;
8
9 import java.io.File JavaDoc;
10 import java.net.URL JavaDoc;
11 import java.util.StringTokenizer JavaDoc;
12
13 import org.jboss.logging.Logger;
14 import org.opends.server.tools.LDAPCompare;
15 import org.opends.server.tools.LDAPDelete;
16 import org.opends.server.tools.LDAPModify;
17 import org.opends.server.tools.LDAPSearch;
18
19 /**
20  * Utility class that deals with the integrated ldap (OpenDS)
21  * @author <a HREF="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
22  * @version $Revision$
23  * @since Sep 15, 2006
24  */

25 public class OpenDSUtil
26 {
27    private static final Logger log = Logger.getLogger(OpenDSUtil.class);
28    
29    public OpenDSUtil()
30    {
31    }
32    
33    /**
34     * Add a LDIF file into the Directory Server
35     * @param serverHost Server Host (Use getServerHost() of JBossTestxxx)
36     * @param port Port for the DS
37     * @param admin admin dn ("cn=Directory Manager")
38     * @param adminpwd (password)
39     * @param ldifURL (use getDeployURL of JBossTestxxx)
40     * @return whether the add was success
41     */

42    public boolean addLDIF(String JavaDoc serverHost, String JavaDoc port, String JavaDoc admin,
43          String JavaDoc adminpwd, URL JavaDoc ldifURL)
44    {
45       File JavaDoc ldifFile = new File JavaDoc(ldifURL.getPath());
46       if(!ldifFile.exists())
47          throw new IllegalArgumentException JavaDoc("LDIF file:"+ ldifURL + " does not exist");
48       String JavaDoc[] cmd = new String JavaDoc[] {"-h", serverHost, "-p",
49             port, "-D", admin,
50             "-w", adminpwd, "-a", "-f",ldifFile.getPath()};
51       log.debug("addLDIF:" + print(cmd));
52       return LDAPModify.mainModify(cmd) == 0;
53    }
54    
55    /**
56     * Delete a DN in the Directory Server
57    * @param serverHost Server Host (Use getServerHost() of JBossTestxxx)
58     * @param port Port for the DS
59     * @param admin admin dn ("cn=Directory Manager")
60     * @param adminpwd (password)
61     * @param dnToDelete DN to delete (Eg: dc=jboss,dc=org)
62     * @param recursive should children also go?
63     * @return whether the delete op was success
64     */

65    public boolean deleteDN(String JavaDoc serverHost, String JavaDoc port, String JavaDoc admin,
66          String JavaDoc adminpwd, String JavaDoc dnToDelete, boolean recursive)
67    {
68       String JavaDoc rec = recursive ? "-x" : " ";
69       
70       String JavaDoc[] cmd = new String JavaDoc[] {"-h", serverHost, "-p",
71             port, "-D", admin,
72             "-w", adminpwd, rec,dnToDelete};
73       log.debug("deleteDN:" + print(cmd));
74       return LDAPDelete.mainDelete(cmd) == 0;
75    }
76    
77    /**
78     * Check whether a DN exists. Typically before you do a ldap delete
79     * @param serverHost
80     * @param port
81     * @param dn
82     * @return whether the DN exists?
83     */

84    public boolean existsDN(String JavaDoc serverHost, String JavaDoc port, String JavaDoc dn)
85    {
86       String JavaDoc[] cmd = new String JavaDoc[] {"-h", serverHost, "-p",
87             port, "-b", dn ,"-s", "sub", "objectclass=*"};
88       log.debug("existsDN:" + print(cmd));
89       return LDAPSearch.mainSearch(cmd) == 0;
90    }
91    
92    /**
93     * Issue a ldapCompare in the standard ldapCompare cmd line syntax
94     * (Eg: "-h localhost -p 1389 -D "cn=..." -w password -a -f ldif.txt)
95     * @param cmdline
96     * @return whether ldapCompare was success
97     */

98    public boolean ldapCompare(String JavaDoc cmdline)
99    {
100       String JavaDoc[] strArr = getStringArr(cmdline);
101       log.debug("ldapCompare:"+print(strArr));
102       return LDAPCompare.mainCompare(strArr) == 0;
103    }
104    
105    /**
106     * Issue a ldapdelete in the standard ldapdelete cmd line syntax
107     * (Eg: "-h localhost -p 1389 -D "cn=..." -w password -a -f ldif.txt)
108     * @param cmdline
109     * @return whether ldapmodify was success
110     */

111    public boolean ldapDelete(String JavaDoc cmdline)
112    {
113       String JavaDoc[] strArr = getStringArr(cmdline);
114       log.debug("ldapDelete:"+print(strArr));
115       return LDAPDelete.mainDelete(strArr) == 0;
116    }
117    
118    /**
119     * Issue a ldapmodify in the standard ldapmodify cmd line syntax
120     * (Eg: "-h localhost -p 1389 -D "cn=..." -w password -a -f ldif.txt)
121     * @param cmdline
122     * @return whether ldapmodify was success
123     */

124    public boolean ldapModify(String JavaDoc cmdline)
125    {
126       String JavaDoc[] strArr = getStringArr(cmdline);
127       log.debug("ldapModify:"+print(strArr));
128       return LDAPModify.mainModify(strArr) == 0;
129    }
130   
131    //***************************************************************
132
// PRIVATE METHODS
133
//***************************************************************
134
private String JavaDoc[] getStringArr(String JavaDoc str)
135    {
136       StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(str);
137       int num = st.countTokens();
138       String JavaDoc[] strarr = new String JavaDoc[num];
139       int i = 0;
140       while(st.hasMoreTokens())
141       {
142          strarr[i++] = st.nextToken();
143       }
144       return strarr;
145    }
146    
147    private String JavaDoc print(String JavaDoc[] arr)
148    {
149       StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
150       int len = arr != null ? arr.length : 0;
151       for(int i=0; i < len; i++)
152          sb.append(arr[i]);
153       return sb.toString();
154    }
155 }
156
Popular Tags