1 package com.ca.directory.jxplorer; 2 3 import com.ca.directory.jxplorer.broker.Broker; 4 import com.ca.commons.naming.*; 5 6 import java.util.Hashtable ; 7 import java.util.Vector ; 8 import java.util.ArrayList ; 9 import javax.naming.NamingException ; 10 11 17 18 public class DataQuery 19 { 20 23 26 public static final int UNKNOWN = 0; 27 public static final int EXISTS = 1; 28 public static final int READENTRY = 2; 29 public static final int LIST = 4; 30 public static final int SEARCH = 8; 31 public static final int MODIFY = 16; 32 public static final int COPY = 32; 33 public static final int GETRECOC = 128; 35 public static final int EXTENDED = 256; 36 37 protected Vector listeners = new Vector (); 38 39 protected int type = 0; 40 41 static int noRequests = 0; 42 43 public int id; 45 protected boolean myStatus = false; 46 47 protected ArrayList myArrayList = null; 48 49 protected DXEntry myEntry = null; 50 51 protected DXNamingEnumeration myEnum = null; 52 53 protected Exception myEx = null; 54 55 56 59 protected DN requestDN = null; 60 protected DN oldDN = null; 61 62 protected DXEntry oldEntry = null; 63 protected DXEntry newEntry = null; 64 65 protected String filter = null; 66 67 protected String [] returnAttrs = null; 68 69 protected int searchLevel = 2; 71 protected Hashtable extendedData = null; 73 74 80 protected boolean ready = false; 82 protected boolean cancelled = false; 84 protected boolean working = false; 86 protected boolean squelched = false; 88 90 public DataQuery() 91 { 92 id = noRequests++; 93 } 94 95 98 99 public DataQuery(int type) 100 { 101 this.type = type; 102 103 if (type == EXTENDED) 104 extendedData = new Hashtable (); 105 106 id = noRequests++; 107 } 108 109 112 113 public DataQuery(int type, DN dn) 114 { 115 this(type); 116 requestDN = dn; 117 if (type != DataQuery.LIST && type != DataQuery.READENTRY && type != DataQuery.EXISTS ) setException(new Exception ("Bad Constructor call (ii) for DataQuery of Type " + type)); 119 } 120 121 124 125 public DataQuery(int type, DN oldDN, DN newDN) 126 { 127 this(type); 128 requestDN = newDN; 129 this.oldDN = oldDN; 130 if (type != DataQuery.COPY) 131 setException(new Exception ("Bad Constructor call (iii) for DataQuery of Type " + type)); 132 } 133 134 137 138 public DataQuery(int type, DXEntry oldEntry, DXEntry newEntry) 139 { 140 this(type); 141 this.oldEntry = oldEntry; 142 this.newEntry = newEntry; 143 if (type != DataQuery.MODIFY) 144 setException(new Exception ("Bad Constructor call (iv) for DataQuery of Type " + type)); 145 } 146 147 150 151 public DataQuery(int type, DN dn, String filter, int level, String [] returnAttrs) 152 { 153 this(type); 154 requestDN = dn; 155 this.filter = filter; 156 this.returnAttrs = returnAttrs; 157 searchLevel = level; 158 if (type != DataQuery.SEARCH) 159 setException(new Exception ("Bad Constructor call (v) for DataQuery of Type " + type)); 160 } 161 162 167 168 public void squelch() 169 { 170 squelched = true; 171 172 myArrayList = null; 173 myEntry = null; 174 myEnum = null; 175 myEx = null; 176 requestDN = null; 177 oldDN = null; 178 oldEntry = null; 179 newEntry = null; 180 filter = null; 181 } 182 183 184 188 189 public boolean isSquelched() { return squelched; } 190 191 196 197 public void cancel() 198 { 199 cancelled = true; 200 setException(new Exception ("Request Cancelled")); 201 } 202 203 206 207 public boolean isCancelled() 208 { 209 return cancelled; 210 } 211 212 216 217 public void setRunning() 218 { 219 working = true; 220 } 221 222 225 226 public boolean isRunning() 227 { 228 return working; 229 } 230 231 232 233 238 239 public synchronized void finish() 240 { 241 if (ready == true) return; 243 ready = true; 244 if (!cancelled) 245 { 246 notifyAll(); 247 fireDataEvent(); 248 } 249 working = false; 250 } 251 252 257 258 protected void fireDataEvent() 259 { 260 while(listeners.size() > 0 && !squelched) { 262 DataListener current = (DataListener)listeners.lastElement(); 263 listeners.removeElementAt(listeners.size()-1); 264 current.dataReady(this); 265 } 266 } 267 268 271 272 public String getTypeString() 273 { 274 switch (type) 275 { 276 case UNKNOWN: return "Unknown"; 277 case EXISTS: return "Existance check"; 278 case READENTRY: return "Read entry"; 279 case LIST: return "List"; 280 case SEARCH: return "Search"; 281 case MODIFY: return "Modify"; 282 case COPY: return "Copy"; 283 case EXTENDED: return "Extended request"; 286 } 287 return "UNDEFINED"; 288 } 289 290 293 294 public String toString() 295 { 296 String ret = "query ("+id+"): " + getTypeString(); 297 298 if (type == SEARCH) 299 ret += " filter: " + filter; 300 else if (requestDN != null) 301 ret += " on " + requestDN; 302 303 if (ready() == false) 304 return (ret + " : (request pending)"); 305 else 306 return (ret + " : (completed)"); 307 308 317 } 318 319 323 324 public Object getResult() 325 { 326 try 327 { 328 switch (type) 329 { 330 case UNKNOWN: return "UNKNOWN"; 331 case EXISTS: return new Boolean (getStatus()); 332 case READENTRY: return getEntry(); 333 case LIST: return getEnumeration(); 334 case SEARCH: return getEnumeration(); 335 case MODIFY: return new Boolean (getStatus()); 336 case COPY: return new Boolean (getStatus()); 337 case EXTENDED: return getAllExtendedData(); 340 } 341 return "UNDEFINED"; 342 } 343 catch (NamingException e) 344 { 345 return e; 346 } 347 } 348 349 350 351 public DataQuery setArrayList(ArrayList v) { myArrayList = v; return this;} 352 353 public DataQuery setStatus(boolean b) { myStatus = b; return this;} 354 355 public DataQuery setEntry(DXEntry d) { myEntry = d; return this;} 356 357 public DataQuery setEnum(DXNamingEnumeration d) { myEnum = d; return this;} 358 359 public DataQuery setException(Exception e) { myEx = e; return this;} 360 361 363 364 365 366 373 public boolean ready() { return ready; } 374 375 391 392 public synchronized void addDataListener(DataListener l) 393 { 394 if (ready) 395 l.dataReady(this); else 397 listeners.add(l); } 399 400 401 synchronized void keepWaiting() 402 throws NamingException 403 { 404 410 String current = Thread.currentThread().getName(); 411 412 if ("jndiBroker Thread searchBroker Thread schemaBroker Thread".indexOf(current)>-1) 413 { 414 System.err.println("ERROR - Thread " + current + " possibly blocking on self"); 415 throw new NamingException ("Thread Blockage (?) during Naming operation - attempt to force immediate data read from: " + current + " thread. Consider using unthreaded Broker classes rather than DataQuery."); 416 } 417 418 try 419 { 420 notifyAll(); 421 wait(); 422 } catch (InterruptedException e) 423 { 424 System.out.println("Thread: " + Thread.currentThread().getName() + " interrupted in DataQuery:keepWaiting() loop \n " + e); 425 }; } 427 428 443 444 public boolean getStatus() 445 throws NamingException 446 { 447 while (!ready()) keepWaiting(); 448 449 if (type == MODIFY || type == COPY || type == EXISTS) 450 return myStatus; 451 else 452 throw new NamingException ("Improper call of getStatus for a DataQuery of Type " + getTypeString()); 453 } 454 470 public DXNamingEnumeration getEnumeration() 471 throws NamingException 472 { 473 try 474 { 475 while (!ready()) keepWaiting(); 476 477 if (type == LIST || type == SEARCH) 478 return myEnum; 479 else 480 throw new NamingException ("Improper call of getNameClassPairs for a DataQuery of Type " + getTypeString()); 481 482 } 483 catch (NamingException e) 484 { 485 throw new NamingException (e.toString()); 486 } 487 } 488 489 502 public DXEntry getEntry() 503 throws NamingException 504 { 505 while (!ready()) keepWaiting(); 506 507 if (type == READENTRY) 508 return myEntry; 509 else 510 throw new NamingException ("Improper call of getEntry for a DataQuery of Type " + getTypeString()); 511 } 512 526 527 public ArrayList getObjectClasses() 528 throws NamingException 529 { 530 while (!ready()) keepWaiting(); 531 532 if (type == GETRECOC) 534 return myArrayList; 535 else 536 throw new NamingException ("Improper call of getObjectClasses for a DataQuery of Type " + getTypeString()); 537 } 538 539 548 549 public Exception getException() 550 { 551 try 552 { 553 while (!ready()) keepWaiting(); 554 } 555 catch (NamingException e) { return e; } 556 557 return myEx; } 559 560 569 570 public boolean hasException() 571 { 572 try 573 { 574 while (!ready()) keepWaiting(); 575 } 576 catch (NamingException e) 577 { 578 myEx = e; 579 return true; 580 } 581 582 return (myEx!=null); 583 } 584 585 586 595 public void clearException() 596 { 597 myEx = null; 598 } 599 600 601 602 public int getType() { return type; } 603 604 605 606 608 public DN requestDN() { return (requestDN==null)?null:new DN(requestDN); } 609 610 public DN oldDN() { return (oldDN==null)?null:new DN(oldDN); } 611 612 public DXEntry oldEntry() { return (oldEntry==null)?null:new DXEntry(oldEntry); } 613 614 public DXEntry newEntry() { return (newEntry==null)?null:new DXEntry(newEntry); } 615 616 public String filter() { return (filter==null)?null:filter; } 617 618 public String [] returnAttributes() { return (returnAttrs==null)?null:returnAttrs; } 619 620 public int searchLevel() { return searchLevel; } 621 622 623 628 629 public Hashtable getAllExtendedData() { return extendedData; } 630 631 635 636 public Object getExtendedData(String name) { return extendedData.get(name.toLowerCase()); } 637 638 642 643 public void setExtendedData(String name, Object o) { if (extendedData != null) extendedData.put(name.toLowerCase(),o); } 644 645 651 652 public void doExtendedRequest(Broker b) { return; } 653 654 } 655 656 657 | Popular Tags |