1 8 9 package javax.sql.rowset.spi; 10 11 import java.util.Map ; 12 import java.util.Hashtable ; 13 import java.util.Enumeration ; 14 import java.util.Vector ; 15 import java.util.Properties ; 16 import java.util.Collection ; 17 import java.util.StringTokenizer ; 18 import java.util.logging.*; 19 import java.util.*; 20 21 import java.sql.*; 22 import javax.sql.*; 23 24 import java.io.FileInputStream ; 25 import java.io.IOException ; 26 import java.io.FileNotFoundException ; 27 28 import javax.naming.*; 29 30 188 public class SyncFactory { 189 190 194 private static SyncFactory syncFactory = null; 195 196 202 private SyncFactory() {}; 203 204 208 public static String ROWSET_SYNC_PROVIDER = 209 "rowset.provider.classname"; 210 211 215 public static String ROWSET_SYNC_VENDOR = 216 "rowset.provider.vendor"; 217 218 222 public static String ROWSET_SYNC_PROVIDER_VERSION = 223 "rowset.provider.version"; 224 225 228 private static String ROWSET_PROPERTIES = "rowset.properties"; 229 230 233 private static String default_provider = 234 "com.sun.rowset.providers.RIOptimisticProvider"; 235 236 240 private static Context ic; 241 242 245 private static Logger rsLogger; 246 247 250 private static Level rsLevel; 251 252 257 private static Hashtable implementations; 258 259 262 private static Object logSync = new Object (); 263 264 267 private static java.io.PrintWriter logWriter = null; 268 269 296 public static synchronized void registerProvider(String providerID) 297 throws SyncFactoryException { 298 299 ProviderImpl impl = new ProviderImpl(); 300 impl.setClassname(providerID); 301 initMapIfNecessary(); 302 implementations.put(providerID, impl); 303 304 } 305 306 311 public static SyncFactory getSyncFactory(){ 312 313 320 325 if(syncFactory == null){ 326 synchronized(SyncFactory .class) { 327 if(syncFactory == null){ 328 syncFactory = new SyncFactory (); 329 } } } return syncFactory; 333 } 334 335 343 public static synchronized void unregisterProvider(String providerID) 344 throws SyncFactoryException { 345 initMapIfNecessary(); 346 if (implementations.containsKey(providerID)) { 347 implementations.remove(providerID); 348 } 349 } 350 351 private static String colon = ":"; 352 private static String strFileSep = "/"; 353 354 private static synchronized void initMapIfNecessary() throws SyncFactoryException { 355 356 Properties properties = new Properties (); 360 361 if (implementations == null) { 362 implementations = new Hashtable (); 363 364 try { 365 366 370 374 377 380 String strRowsetProperties = System.getProperty("rowset.properties"); 381 if ( strRowsetProperties != null) { 382 ROWSET_PROPERTIES = strRowsetProperties; 385 properties.load(new FileInputStream (ROWSET_PROPERTIES)); 386 parseProperties(properties); 387 } 388 389 392 ROWSET_PROPERTIES = "javax" + strFileSep + "sql" + 393 strFileSep + "rowset" + strFileSep + 394 "rowset.properties"; 395 398 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 399 400 properties.load(cl.getResourceAsStream(ROWSET_PROPERTIES)); 401 parseProperties(properties); 402 403 405 } catch (FileNotFoundException e) { 406 throw new SyncFactoryException ("Cannot locate properties file: " + e); 407 } catch (IOException e) { 408 throw new SyncFactoryException ("IOException: " + e); 409 } 410 411 415 properties.clear(); 416 String providerImpls = System.getProperty(ROWSET_SYNC_PROVIDER); 417 418 if (providerImpls != null) { 419 int i = 0; 420 if (providerImpls.indexOf(colon) > 0) { 421 StringTokenizer tokenizer = new StringTokenizer (providerImpls, colon); 422 while (tokenizer.hasMoreElements()) { 423 properties.put(ROWSET_SYNC_PROVIDER + "." + i, tokenizer.nextToken()); 424 i++; 425 } 426 } else { 427 properties.put(ROWSET_SYNC_PROVIDER, providerImpls); 428 } 429 parseProperties(properties); 430 } 431 } 432 } 433 434 438 private static boolean jndiCtxEstablished = false; 439 440 443 private static boolean debug = false; 444 445 449 private static int providerImplIndex = 0; 450 451 455 private static void parseProperties(Properties p) { 456 457 ProviderImpl impl = null; 458 String key = null; 459 String [] propertyNames = null; 460 461 for (Enumeration e = p.propertyNames(); e.hasMoreElements() ;) { 462 463 String str = (String )e.nextElement(); 464 465 int w = str.length(); 466 467 if (str.startsWith(SyncFactory.ROWSET_SYNC_PROVIDER)) { 468 469 impl = new ProviderImpl(); 470 impl.setIndex(providerImplIndex++); 471 472 if (w == (SyncFactory.ROWSET_SYNC_PROVIDER).length()) { 473 propertyNames = getPropertyNames(false); 475 } else { 476 propertyNames = getPropertyNames(true, str.substring(w-1)); 478 } 479 480 key = p.getProperty(propertyNames[0]); 481 impl.setClassname(key); 482 impl.setVendor(p.getProperty(propertyNames[1])); 483 impl.setVersion(p.getProperty(propertyNames[2])); 484 implementations.put(key, impl); 485 } 486 } 487 } 488 489 492 private static String [] getPropertyNames(boolean append) { 493 return getPropertyNames(append, null); 494 } 495 496 500 private static String [] getPropertyNames(boolean append, 501 String propertyIndex) { 502 String dot = "."; 503 String [] propertyNames = 504 new String [] {SyncFactory.ROWSET_SYNC_PROVIDER, 505 SyncFactory.ROWSET_SYNC_VENDOR, 506 SyncFactory.ROWSET_SYNC_PROVIDER_VERSION}; 507 if (append) { 508 for (int i = 0; i < propertyNames.length; i++) { 509 propertyNames[i] = propertyNames[i] + 510 dot + 511 propertyIndex; 512 } 513 return propertyNames; 514 } else { 515 return propertyNames; 516 } 517 } 518 519 522 private static void showImpl(ProviderImpl impl) { 523 System.out.println("Provider implementation:"); 524 System.out.println("Classname: " + impl.getClassname()); 525 System.out.println("Vendor: " + impl.getVendor()); 526 System.out.println("Version: " + impl.getVersion()); 527 System.out.println("Impl index: " + impl.getIndex()); 528 } 529 530 538 public static SyncProvider getInstance(String providerID) 539 throws SyncFactoryException { 540 initMapIfNecessary(); initJNDIContext(); 543 ProviderImpl impl = (ProviderImpl)implementations.get(providerID); 544 545 if (impl == null) { 546 return new com.sun.rowset.providers.RIOptimisticProvider(); 548 } 549 550 Class c = null; 552 try { 553 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 554 555 561 562 c = Class.forName(providerID, true, cl); 563 564 if (c != null) { 565 return (SyncProvider )c.newInstance(); 566 } else { 567 return new com.sun.rowset.providers.RIOptimisticProvider(); 568 } 569 570 } catch (IllegalAccessException e) { 571 throw new SyncFactoryException ("IllegalAccessException: " + e.getMessage()); 572 } catch (InstantiationException e) { 573 throw new SyncFactoryException ("InstantiationException: " + e.getMessage()); 574 } catch (ClassNotFoundException e) { 575 throw new SyncFactoryException ("ClassNotFoundException: " + e.getMessage()); 576 } 577 } 578 590 public static Enumeration <SyncProvider > getRegisteredProviders() 591 throws SyncFactoryException { 592 initMapIfNecessary(); 593 return implementations.elements(); 596 } 597 598 607 public static void setLogger(Logger logger) { 608 rsLogger = logger; 609 } 610 611 622 public static void setLogger(Logger logger, Level level) { 623 625 rsLogger = logger; 626 rsLogger.setLevel(level); 627 } 628 629 635 public static Logger getLogger() throws SyncFactoryException { 636 if(rsLogger == null){ 638 throw new SyncFactoryException ("(SyncFactory) : No logger has been set"); 639 } 640 return rsLogger; 641 } 642 643 650 public static void setJNDIContext(javax.naming.Context ctx) 651 throws SyncFactoryException { 652 if (ctx == null) { 653 throw new SyncFactoryException ("Invalid JNDI context supplied"); 654 } 655 ic = ctx; 656 jndiCtxEstablished = true; 657 } 658 659 664 private static void initJNDIContext() throws SyncFactoryException { 665 666 if (jndiCtxEstablished && (ic != null) && (lazyJNDICtxRefresh == false)) { 667 try { 668 parseProperties(parseJNDIContext()); 669 lazyJNDICtxRefresh = true; } catch (NamingException e) { 671 e.printStackTrace(); 672 throw new SyncFactoryException ("SPI: NamingException: " + e.getExplanation()); 673 } catch (Exception e) { 674 e.printStackTrace(); 675 throw new SyncFactoryException ("SPI: Exception: " + e.getMessage()); 676 } 677 } 678 } 679 682 private static boolean lazyJNDICtxRefresh = false; 683 684 688 private static Properties parseJNDIContext() throws NamingException { 689 690 NamingEnumeration bindings = ic.listBindings(""); 691 Properties properties = new Properties (); 692 693 enumerateBindings(bindings, properties); 695 696 return properties; 697 } 698 699 705 private static void enumerateBindings(NamingEnumeration bindings, 706 Properties properties) throws NamingException { 707 708 boolean syncProviderObj = false; 710 try { 711 Binding bd = null; 712 Object elementObj = null; 713 String element = null; 714 while (bindings.hasMore()) { 715 bd = (Binding)bindings.next(); 716 element = bd.getName(); 717 elementObj = bd.getObject(); 718 719 if (!(ic.lookup(element) instanceof Context)) { 720 if (ic.lookup(element) instanceof SyncProvider ) { 722 syncProviderObj = true; 723 } 724 } 725 726 if (syncProviderObj) { 727 SyncProvider sync = (SyncProvider )elementObj; 728 properties.put(SyncFactory.ROWSET_SYNC_PROVIDER, 729 sync.getProviderID()); 730 syncProviderObj = false; } 732 733 } 734 } catch (javax.naming.NotContextException e) { 735 bindings.next(); 736 enumerateBindings(bindings, properties); 738 } 739 } 740 } 741 742 746 class ProviderImpl extends SyncProvider { 747 private String className = null; 748 private String vendorName = null; 749 private String ver = null; 750 private int index; 751 752 public void setClassname(String classname) { 753 className = classname; 754 } 755 756 public String getClassname() { 757 return className; 758 } 759 760 public void setVendor(String vendor) { 761 vendorName = vendor; 762 } 763 764 public String getVendor() { 765 return vendorName; 766 } 767 768 public void setVersion(String providerVer) { 769 ver = providerVer; 770 } 771 772 public String getVersion() { 773 return ver; 774 } 775 776 public void setIndex(int i) { 777 index = i; 778 } 779 780 public int getIndex() { 781 return index; 782 } 783 784 public int getDataSourceLock() throws SyncProviderException { 785 786 int dsLock = 0; 787 try 788 { 789 dsLock = SyncFactory.getInstance(className).getDataSourceLock(); 790 } catch(SyncFactoryException sfEx) { 791 792 throw new SyncProviderException (sfEx.getMessage()); 793 } 794 795 return dsLock; 796 } 797 798 public int getProviderGrade() { 799 800 int grade = 0; 801 802 try 803 { 804 grade = SyncFactory.getInstance(className).getProviderGrade(); 805 } catch(SyncFactoryException sfEx) { 806 } 808 809 return grade; 810 } 811 812 public String getProviderID() { 813 return className; 814 } 815 816 826 827 public javax.sql.RowSetReader getRowSetReader() { 828 829 RowSetReader rsReader = null;; 830 831 try 832 { 833 rsReader = SyncFactory.getInstance(className).getRowSetReader(); 834 } catch(SyncFactoryException sfEx) { 835 } 837 838 return rsReader; 839 840 } 841 842 public javax.sql.RowSetWriter getRowSetWriter() { 843 844 RowSetWriter rsWriter = null; 845 try 846 { 847 rsWriter = SyncFactory.getInstance(className).getRowSetWriter(); 848 } catch(SyncFactoryException sfEx) { 849 } 851 852 return rsWriter; 853 } 854 public void setDataSourceLock(int param) 855 throws SyncProviderException { 856 857 try 858 { 859 SyncFactory.getInstance(className).setDataSourceLock(param); 860 } catch(SyncFactoryException sfEx) { 861 862 throw new SyncProviderException (sfEx.getMessage()); 863 } 864 } 865 866 public int supportsUpdatableView() { 867 868 int view = 0; 869 870 try 871 { 872 view = SyncFactory.getInstance(className).supportsUpdatableView(); 873 } catch(SyncFactoryException sfEx) { 874 } 876 877 return view; 878 } 879 880 } 881 882 | Popular Tags |