1 7 package org.jboss.test.security.test.opends; 8 9 import java.io.File ; 10 import java.net.URL ; 11 import java.util.StringTokenizer ; 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 25 public class OpenDSUtil 26 { 27 private static final Logger log = Logger.getLogger(OpenDSUtil.class); 28 29 public OpenDSUtil() 30 { 31 } 32 33 42 public boolean addLDIF(String serverHost, String port, String admin, 43 String adminpwd, URL ldifURL) 44 { 45 File ldifFile = new File (ldifURL.getPath()); 46 if(!ldifFile.exists()) 47 throw new IllegalArgumentException ("LDIF file:"+ ldifURL + " does not exist"); 48 String [] cmd = new String [] {"-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 65 public boolean deleteDN(String serverHost, String port, String admin, 66 String adminpwd, String dnToDelete, boolean recursive) 67 { 68 String rec = recursive ? "-x" : " "; 69 70 String [] cmd = new String [] {"-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 84 public boolean existsDN(String serverHost, String port, String dn) 85 { 86 String [] cmd = new String [] {"-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 98 public boolean ldapCompare(String cmdline) 99 { 100 String [] strArr = getStringArr(cmdline); 101 log.debug("ldapCompare:"+print(strArr)); 102 return LDAPCompare.mainCompare(strArr) == 0; 103 } 104 105 111 public boolean ldapDelete(String cmdline) 112 { 113 String [] strArr = getStringArr(cmdline); 114 log.debug("ldapDelete:"+print(strArr)); 115 return LDAPDelete.mainDelete(strArr) == 0; 116 } 117 118 124 public boolean ldapModify(String cmdline) 125 { 126 String [] strArr = getStringArr(cmdline); 127 log.debug("ldapModify:"+print(strArr)); 128 return LDAPModify.mainModify(strArr) == 0; 129 } 130 131 private String [] getStringArr(String str) 135 { 136 StringTokenizer st = new StringTokenizer (str); 137 int num = st.countTokens(); 138 String [] strarr = new String [num]; 139 int i = 0; 140 while(st.hasMoreTokens()) 141 { 142 strarr[i++] = st.nextToken(); 143 } 144 return strarr; 145 } 146 147 private String print(String [] arr) 148 { 149 StringBuilder sb = new StringBuilder (); 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 |