1 21 22 package com.coi.tools.os.win; 23 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 37 public class RegistryImpl implements MSWinConstants 38 { 39 40 private static final String DEFAULT_PLACEHOLDER = "__#$&DEFAULT_PLACEHODER_VALUE#$?"; 41 42 private int currentRoot = HKEY_CURRENT_USER; 43 44 private List logging = new ArrayList (); 45 46 private boolean doLogging = false; 47 48 51 public RegistryImpl() 52 { 53 super(); 54 } 55 56 61 public int getRoot() 62 { 63 return currentRoot; 64 } 65 66 71 public void setRoot(int i) 72 { 73 currentRoot = i; 74 } 75 76 84 public RegDataContainer getValue(String key, String value) throws NativeLibException 85 { 86 if( key == null) 87 key = ""; 88 return (getValue(currentRoot, key, value)); 89 } 90 91 100 public Object getValueAsObject(String key, String value) throws NativeLibException 101 { 102 if( key == null) 103 key = ""; 104 return (getValue(currentRoot, key, value).getDataAsObject()); 105 } 106 107 114 public String [] getSubkeys(String key) throws NativeLibException 115 { 116 if( key == null) 117 key = ""; 118 return (getSubkeyNames(currentRoot, key)); 119 } 120 121 128 public String [] getValueNames(String key) throws NativeLibException 129 { 130 if( key == null) 131 key = ""; 132 return (getValueNames(currentRoot, key)); 133 } 134 135 141 public void createKey(String key) throws NativeLibException 142 { 143 createKey(currentRoot, key); 144 } 145 146 159 public void createKey(int root, String key) throws NativeLibException 160 { 161 int pathEnd = key.lastIndexOf('\\'); 162 if( pathEnd > 0 ) 163 { 164 String subkey = key.substring(0, pathEnd); 165 if (!exist(root, subkey)) 166 { createKey(root, subkey); 168 } 169 } 170 createKeyN(root, key); 172 RegistryLogItem rli = new RegistryLogItem(RegistryLogItem.CREATED_KEY, root, key, null, 173 null, null); 174 log(rli); 175 176 } 177 178 185 public boolean keyExist(String key) throws NativeLibException 186 { 187 return (keyExist(currentRoot, key)); 188 } 189 190 198 public boolean keyExist(int root, String key) throws NativeLibException 199 { 200 try 201 { 202 return (exist(root, key)); 203 } 204 catch (NativeLibException ne) 205 { 206 String em = ne.getLibMessage(); 207 if ("functionFailed.RegOpenKeyEx".equals(em)) { return (false); } 208 throw (ne); 209 } 210 } 211 212 220 public boolean valueExist(String key, String value) throws NativeLibException 221 { 222 if( key == null) 223 key = ""; 224 try 225 { 226 this.getValue(currentRoot, key, value); 227 } 228 catch (NativeLibException ne) 229 { 230 String em = ne.getLibMessage(); 231 if ("functionFailed.RegOpenKeyEx".equals(em) 232 || "functionFailed.RegQueryValueEx".equals(em)) { return (false); } 233 throw (ne); 234 } 235 return (true); 236 } 237 238 247 public void setValue(String key, String value, String contents) throws NativeLibException 248 { 249 250 setValue(currentRoot, key, value, new RegDataContainer(contents)); 251 } 252 253 262 public void setValue(String key, String value, String [] contents) throws NativeLibException 263 { 264 265 setValue(currentRoot, key, value, new RegDataContainer(contents)); 266 } 267 268 277 public void setValue(String key, String value, byte[] contents) throws NativeLibException 278 { 279 280 setValue(currentRoot, key, value, new RegDataContainer(contents)); 281 } 282 283 292 public void setValue(String key, String value, long contents) throws NativeLibException 293 { 294 setValue(currentRoot, key, value, new RegDataContainer(contents)); 295 } 296 297 307 public void setValue(String key, String value, RegDataContainer contents) 308 throws NativeLibException 309 { 310 setValue(currentRoot, key, value, contents); 311 } 312 313 324 public void setValue(int root, String key, String value, RegDataContainer contents) 325 throws NativeLibException 326 { 327 RegDataContainer oldContents = null; 328 String localValue = value; 329 if( key == null) 330 key = ""; 331 if(value == null) value = ""; 333 key = key.replace('/', '\\'); 335 336 synchronized (logging) 337 { 338 try 339 { 340 oldContents = getValue(currentRoot, key, value); 341 } 342 catch (NativeLibException ne) 343 { 344 String em = ne.getLibMessage(); 345 if ("functionFailed.RegOpenKeyEx".equals(em) 346 || "functionFailed.RegQueryValueEx".equals(em)) 347 { 348 setValueR(root, key, value, contents); 349 return; 350 } 351 throw (ne); 352 } 353 setValueN(root, key, value, contents); 354 if (value.length() == 0) localValue = DEFAULT_PLACEHOLDER; 360 RegistryLogItem rli = new RegistryLogItem(RegistryLogItem.CHANGED_VALUE, root, key, 361 localValue, contents, oldContents); 362 log(rli); 363 } 364 } 365 366 372 public void deleteKey(String key) throws NativeLibException 373 { 374 deleteKeyL(currentRoot, key); 375 } 376 377 383 public void deleteKeyIfEmpty(String key) throws NativeLibException 384 { 385 deleteKeyIfEmpty(currentRoot, key); 386 } 387 388 395 public void deleteKeyIfEmpty(int root, String key) throws NativeLibException 396 { 397 if (keyExist(root, key) && isKeyEmpty(root, key)) deleteKeyL(root, key); 398 399 } 400 401 408 public void deleteValue(String key, String value) throws NativeLibException 409 { 410 deleteValueL(currentRoot, key, value); 411 } 412 413 420 private void deleteKeyL(int root, String key) throws NativeLibException 421 { 422 RegistryLogItem rli = new RegistryLogItem(RegistryLogItem.REMOVED_KEY, root, key, null, 423 null, null); 424 log(rli); 425 deleteKeyN(root, key); 426 } 427 428 436 private void deleteValueL(int root, String key, String value) throws NativeLibException 437 { 438 if( key == null) 439 key = ""; 440 RegDataContainer oldContents = getValue(currentRoot, key, value); 441 RegistryLogItem rli = new RegistryLogItem(RegistryLogItem.REMOVED_VALUE, root, key, value, 442 null, oldContents); 443 log(rli); 444 deleteValueN(currentRoot, key, value); 445 } 446 447 453 public void rewind() throws IllegalArgumentException , NativeLibException 454 { 455 synchronized (logging) 456 { 457 Iterator iter = logging.iterator(); 458 suspendLogging(); 459 460 while (iter.hasNext()) 461 { 462 RegistryLogItem rli = (RegistryLogItem) iter.next(); 463 String rliValueName = (DEFAULT_PLACEHOLDER.equals(rli.getValueName())) ? "" : rli 464 .getValueName(); 465 switch (rli.getType()) 466 { 467 case RegistryLogItem.CREATED_KEY: 468 deleteKeyIfEmpty(rli.getRoot(), rli.getKey()); 469 break; 470 case RegistryLogItem.REMOVED_KEY: 471 createKeyN(rli.getRoot(), rli.getKey()); 472 break; 473 case RegistryLogItem.CREATED_VALUE: 474 RegDataContainer currentContents = null; 475 try 477 { 478 currentContents = getValue(rli.getRoot(), rli.getKey(), rliValueName); 479 } 480 catch (NativeLibException nle) 481 { 482 break; 483 } 484 if (currentContents.equals(rli.getNewValue())) 485 { 486 deleteValueN(rli.getRoot(), rli.getKey(), rliValueName); 487 } 488 break; 490 case RegistryLogItem.REMOVED_VALUE: 491 try 493 { 494 getValue(rli.getRoot(), rli.getKey(), rliValueName); 495 } 496 catch (NativeLibException nle) 497 { 498 setValueN(rli.getRoot(), rli.getKey(), rliValueName, rli 499 .getOldValue()); 500 } 501 break; 502 case RegistryLogItem.CHANGED_VALUE: 503 try 507 { 508 currentContents = getValue(rli.getRoot(), rli.getKey(), rliValueName); 509 } 510 catch (NativeLibException nle) 511 { 512 break; 513 } 514 if (currentContents.equals(rli.getNewValue())) 515 { 516 setValueN(rli.getRoot(), rli.getKey(), rliValueName, rli 517 .getOldValue()); 518 } 519 break; 520 } 521 } 522 } 523 524 } 525 526 537 private void setValueR(int root, String key, String value, RegDataContainer contents) 538 throws NativeLibException 539 { 540 String localValue = value; 541 if (!exist(root, key)) 542 { createKey(root, key); 544 } 545 setValueN(root, key, value, contents); 547 if (value.length() == 0) localValue = DEFAULT_PLACEHOLDER; StringBuffer sb = new StringBuffer (); 553 sb.append("SetValue;").append(Integer.toString(root)).append(";").append(key).append(";") 554 .append(localValue); 555 RegistryLogItem rli = new RegistryLogItem(RegistryLogItem.CREATED_VALUE, root, key, 556 localValue, contents, null); 557 log(rli); 558 } 559 560 private native boolean exist(int root, String key) throws NativeLibException; 561 562 private native void createKeyN(int root, String key) throws NativeLibException; 563 564 private native void setValueN(int root, String key, String value, RegDataContainer contents) 565 throws NativeLibException; 566 567 private native RegDataContainer getValue(int root, String key, String value) 568 throws NativeLibException; 569 570 private native void deleteValueN(int root, String key, String value) throws NativeLibException; 571 572 private native void deleteKeyN(int root, String key) throws NativeLibException; 573 574 private native boolean isKeyEmpty(int root, String key) throws NativeLibException; 575 576 private native String [] getSubkeyNames(int root, String key) throws NativeLibException; 577 578 private native String [] getValueNames(int root, String key) throws NativeLibException; 579 580 600 603 public void resetLogging() 604 { 605 logging = new ArrayList (); 606 activateLogging(); 607 } 608 609 612 public void suspendLogging() 613 { 614 doLogging = false; 615 } 616 617 620 public void activateLogging() 621 { 622 doLogging = true; 623 } 624 625 630 public List getLoggingInfo() 631 { 632 ArrayList retval = new ArrayList (logging.size()); 633 Iterator iter = logging.iterator(); 634 while (iter.hasNext()) 635 try 636 { 637 retval.add(((RegistryLogItem) iter.next()).clone()); 638 } 639 catch (CloneNotSupportedException e) 640 { e.printStackTrace(); 642 } 643 return (retval); 644 } 645 646 652 public void setLoggingInfo(List info) 653 { 654 resetLogging(); 655 addLoggingInfo(info); 656 } 657 658 663 public void addLoggingInfo(List info) 664 { 665 Iterator iter = info.iterator(); 666 while (iter.hasNext()) 667 try 668 { 669 logging.add(((RegistryLogItem) iter.next()).clone()); 670 } 671 catch (CloneNotSupportedException e) 672 { e.printStackTrace(); 674 } 675 676 } 677 678 684 private void log(RegistryLogItem item) 685 { 686 if (doLogging && logging != null) logging.add(0, item); 687 } 688 689 } 690 | Popular Tags |