1 package com.ca.commons.jndi; 2 3 import java.util.Properties ; 4 import java.util.Hashtable ; 5 import javax.naming.*; 6 import javax.naming.directory.*; 7 import java.util.logging.*; 8 9 10 20 21 public class BasicOps extends JNDIOps 22 { 23 private final static Logger log = Logger.getLogger(BasicOps.class.getName()); 24 25 protected int ldapVersion = -1; 27 String errorMsg; Exception errorException = null; 30 31 34 35 public BasicOps(DirContext c) 36 throws NamingException 37 { 38 super(c); 39 setLdapVersion(c.getEnvironment()); 40 } 41 42 50 public BasicOps(ConnectionData cData) 51 throws NamingException 52 { 53 super(cData.getJNDIEnvironment()); 54 } 55 56 64 65 public static BasicOps getInstance(ConnectionData cData) 66 throws NamingException 67 { 68 BasicOps newObject = new BasicOps(cData); 69 return newObject; 70 } 71 72 79 80 public static DirContext openContext(ConnectionData connectionData) 81 throws NamingException 82 { 83 return openContext(connectionData.getJNDIEnvironment()); 84 } 85 86 87 103 104 public static DirContext openContext(int version, String host, int port, String user, char[] pwd, 105 boolean tracing, String referralType, String aliasHandling) 106 throws NamingException 107 { 108 if (host == null) 109 throw new NamingException("Host not specified in openContext()!"); 110 111 if (port == 0) port = 389; 112 113 return openContext(version, ("ldap://" + host + ":" + port), user, pwd, tracing, referralType, aliasHandling); 114 } 115 116 117 122 123 public static DirContext openContext(String url) 124 throws NamingException 125 { 126 ConnectionData myData = new ConnectionData(); 127 myData.url = url; 128 129 return openContext(3, 130 url, 131 "", 132 null, 133 false, 134 null, 135 null, 136 false, 137 null, 138 null, 139 null, 140 null, 141 null, 142 null, 143 false); 144 } 145 146 147 155 163 164 public static DirContext openContext(int version, 165 String url, 166 String managerUserDN, 167 char[] pwd) 168 throws NamingException 169 { 170 return openContext(version, 171 url, 172 managerUserDN, 173 pwd, 174 false, 175 null, 176 null, 177 false, 178 null, 179 null, 180 null, 181 null, 182 null, 183 null, 184 false); 185 } 186 187 188 203 204 public static DirContext openContext(int version, String url, String userDN, char[] pwd, boolean tracing, String referralType, String aliasHandling) 205 throws NamingException 206 { 207 return openContext(version, 208 url, 209 userDN, 210 pwd, 211 tracing, 212 referralType, 213 aliasHandling, 214 false, 215 null, 216 null, 217 null, 218 null, 219 null, 220 null, 221 false); 222 } 223 245 246 public static DirContext openContext(int version, 247 String url, 248 String userDN, 249 char[] pwd, 250 boolean tracing, 251 String referralType, 252 String aliasType, 253 boolean useSSL, 254 String cacerts, 255 String clientcerts, 256 char[] caKeystorePwd, 257 char[] clientKeystorePwd, 258 String caKeystoreType, 259 String clientKeystoreType) 260 throws NamingException 261 { 262 return openContext(version, 263 url, 264 userDN, 265 pwd, 266 tracing, 267 referralType, 268 aliasType, 269 useSSL, 270 cacerts, 271 clientcerts, 272 caKeystorePwd, 273 clientKeystorePwd, 274 caKeystoreType, 275 clientKeystoreType, 276 false); 277 } 278 279 301 302 public static DirContext openContext(int version, 303 String url, 304 String userDN, 305 char[] pwd, 306 boolean tracing, 307 String referralType, 308 String aliasType, 309 boolean useSSL, 310 String cacerts, 311 String clientcerts, 312 char[] caKeystorePwd, 313 char[] clientKeystorePwd, 314 String caKeystoreType, 315 String clientKeystoreType, 316 boolean useGSSAPI) 317 throws NamingException 318 { 319 ConnectionData connectionData = 320 new ConnectionData(version, 321 url, 322 userDN, 323 pwd, 324 tracing, 325 referralType, 326 aliasType, 327 useSSL, 328 cacerts, 329 clientcerts, 330 caKeystorePwd, 331 clientKeystorePwd, 332 caKeystoreType, 333 clientKeystoreType, 334 useGSSAPI, null); 335 336 return JNDIOps.openContext(connectionData.getJNDIEnvironment()); 337 } 338 339 340 348 349 public static DirContext openContext(Properties env) 350 throws NamingException 351 { 352 log.fine("opening Directory Context to " + env.get(Context.PROVIDER_URL) + "\n using: " + env.get(Context.INITIAL_CONTEXT_FACTORY)); 353 354 DirContext ctx = new InitialDirContext(env); 355 356 log.fine("context successfully opened " + (ctx != null)); 357 358 if (ctx != null) 359 { 360 361 } 362 return ctx; 363 } 364 365 private void setLdapVersion(Hashtable env) throws NamingException 366 { 367 try 368 { 369 ldapVersion = Integer.parseInt(env.get("java.naming.ldap.version").toString()); 370 } 371 catch (Exception e) 372 { 373 throw new NamingException("BasicOps.openContext(): unable to determine ldap version of connection."); 374 } 375 } 376 377 378 384 385 public DirContext getSchema() throws NamingException 386 { 387 if (getContext() == null) 388 throw new NamingException("No context open to retrieve Schema from"); 389 390 log.finest("getSchema() call"); 391 392 return getContext().getSchema(""); 393 } 394 395 403 404 407 public void renameObject(Name OldDN, Name NewDN) 408 throws NamingException 409 { 410 renameEntry(OldDN, NewDN); 411 } 412 413 414 422 423 public void copyObject(Name FromDN, Name ToDN) 424 throws NamingException 425 { 426 copyEntry(FromDN, ToDN); 427 } 428 429 430 431 439 440 public void addObject(Name Dn, Attributes atts) 441 throws NamingException 442 { 443 addEntry(Dn, atts); 444 } 445 446 453 454 public void deleteObject(Name Dn) 455 throws NamingException 456 { 457 deleteEntry(Dn); 458 } 459 460 467 468 public void updateObject(Name Dn, Attributes atts) 469 throws NamingException 470 { 471 updateEntry(Dn, atts); 472 } 473 474 475 476 487 488 public NamingEnumeration searchBaseObject(Name Searchbase, String filter, int limit, int timeout) 489 throws NamingException 490 { 491 return searchBaseEntry(Searchbase, filter, limit, timeout, new String []{"objectClass"}); 492 } 493 494 495 506 507 public NamingEnumeration searchBaseObject(Name Searchbase, String filter, int limit, 508 int timeout, String [] returnAttributes) 509 throws NamingException 510 { 511 return searchBaseEntry(Searchbase, filter, limit, timeout, returnAttributes); 512 } 513 514 515 519 520 public void close() 521 throws NamingException 522 { 523 super.close(); 524 525 log.fine("closing context"); 526 527 ldapVersion = -1; 528 } 529 530 535 536 public NameParser getBaseNameParser() 537 throws NamingException 538 { 539 log.finer("getting base name parser"); 540 541 if (getContext() == null) 542 throw new NamingException("Null Directory Context\n in BasicOps.searchSubTree()\n (so can't do anything!)"); 543 544 return getContext().getNameParser(""); 545 } 546 547 550 551 public int getLdapVersion() 552 { 553 return ldapVersion; 554 } 555 556 } 557 | Popular Tags |