📄 syncfactory.java
字号:
*/ public static synchronized void registerProvider(String providerID) throws SyncFactoryException { ProviderImpl impl = new ProviderImpl(); impl.setClassname(providerID); initMapIfNecessary(); implementations.put(providerID, impl); } /** * Returns the <code>SyncFactory</code> singleton. * * @return the <code>SyncFactory</code> instance */ public static SyncFactory getSyncFactory(){ // This method uses the Singleton Design Pattern // with Double-Checked Locking Pattern for // 1. Creating single instance of the SyncFactory // 2. Make the class thread safe, so that at one time // only one thread enters the synchronized block // to instantiate. // if syncFactory object is already there // don't go into synchronized block and return // that object. // else go into synchronized block if(syncFactory == null){ synchronized(SyncFactory.class) { if(syncFactory == null){ syncFactory = new SyncFactory(); } //end if } //end synchronized block } //end if return syncFactory; } /** * Removes the designated currently registered synchronization provider from the * Factory SPI register. * * @param providerID The unique-id of the synchronization provider * @throws SyncFactoryException If an attempt is made to * unregister a SyncProvider implementation that was not registered. */ public static synchronized void unregisterProvider(String providerID) throws SyncFactoryException { initMapIfNecessary(); if (implementations.containsKey(providerID)) { implementations.remove(providerID); } } private static String colon = ":"; private static String strFileSep = "/"; private static synchronized void initMapIfNecessary() throws SyncFactoryException { // Local implementation class names and keys from Properties // file, translate names into Class objects using Class.forName // and store mappings Properties properties = new Properties(); if (implementations == null) { implementations = new Hashtable(); try { // check if user is supplying his Synchronisation Provider // Implementation if not use Sun's implementation. // properties.load(new FileInputStream(ROWSET_PROPERTIES)); // The rowset.properties needs to be in jdk/jre/lib when // integrated with jdk. // else it should be picked from -D option from command line. // -Drowset.properties will add to standard properties. Similar // keys will over-write /* * Dependent on application */ String strRowsetProperties = System.getProperty("rowset.properties"); if ( strRowsetProperties != null) { // Load user's implementation of SyncProvider // here. -Drowset.properties=/abc/def/pqr.txt ROWSET_PROPERTIES = strRowsetProperties; properties.load(new FileInputStream(ROWSET_PROPERTIES)); parseProperties(properties); } /* * Always available */ ROWSET_PROPERTIES = "javax" + strFileSep + "sql" + strFileSep + "rowset" + strFileSep + "rowset.properties"; // properties.load( // ClassLoader.getSystemResourceAsStream(ROWSET_PROPERTIES)); ClassLoader cl = Thread.currentThread().getContextClassLoader(); properties.load(cl.getResourceAsStream(ROWSET_PROPERTIES)); parseProperties(properties); // removed else, has properties should sum together } catch (FileNotFoundException e) { throw new SyncFactoryException("Cannot locate properties file: " + e); } catch (IOException e) { throw new SyncFactoryException("IOException: " + e); } /* * Now deal with -Drowset.provider.classname * load additional properties from -D command line */ properties.clear(); String providerImpls = System.getProperty(ROWSET_SYNC_PROVIDER); if (providerImpls != null) { int i = 0; if (providerImpls.indexOf(colon) > 0) { StringTokenizer tokenizer = new StringTokenizer(providerImpls, colon); while (tokenizer.hasMoreElements()) { properties.put(ROWSET_SYNC_PROVIDER + "." + i, tokenizer.nextToken()); i++; } } else { properties.put(ROWSET_SYNC_PROVIDER, providerImpls); } parseProperties(properties); } } } /** * The internal boolean switch that indicates whether a JNDI * context has been established or not. */ private static boolean jndiCtxEstablished = false; /** * The internal debug switch. */ private static boolean debug = false; /** * Internal registry count for the number of providers contained in the * registry. */ private static int providerImplIndex = 0; /** * Internal handler for all standard property parsing. Parses standard * ROWSET properties and stores lazy references into the the internal registry. */ private static void parseProperties(Properties p) { ProviderImpl impl = null; String key = null; String[] propertyNames = null; for (Enumeration e = p.propertyNames(); e.hasMoreElements() ;) { String str = (String)e.nextElement(); int w = str.length(); if (str.startsWith(SyncFactory.ROWSET_SYNC_PROVIDER)) { impl = new ProviderImpl(); impl.setIndex(providerImplIndex++); if (w == (SyncFactory.ROWSET_SYNC_PROVIDER).length()) { // no property index has been set. propertyNames = getPropertyNames(false); } else { // property index has been set. propertyNames = getPropertyNames(true, str.substring(w-1)); } key = p.getProperty(propertyNames[0]); impl.setClassname(key); impl.setVendor(p.getProperty(propertyNames[1])); impl.setVersion(p.getProperty(propertyNames[2])); implementations.put(key, impl); } } } /** * Used by the parseProperties methods to disassemble each property tuple. */ private static String[] getPropertyNames(boolean append) { return getPropertyNames(append, null); } /** * Disassembles each property and its associated value. Also handles * overloaded property names that contain indexes. */ private static String[] getPropertyNames(boolean append, String propertyIndex) { String dot = "."; String[] propertyNames = new String[] {SyncFactory.ROWSET_SYNC_PROVIDER, SyncFactory.ROWSET_SYNC_VENDOR, SyncFactory.ROWSET_SYNC_PROVIDER_VERSION}; if (append) { for (int i = 0; i < propertyNames.length; i++) { propertyNames[i] = propertyNames[i] + dot + propertyIndex; } return propertyNames; } else { return propertyNames; } } /** * Internal debug method that outputs the registry contents. */ private static void showImpl(ProviderImpl impl) { System.out.println("Provider implementation:"); System.out.println("Classname: " + impl.getClassname()); System.out.println("Vendor: " + impl.getVendor()); System.out.println("Version: " + impl.getVersion()); System.out.println("Impl index: " + impl.getIndex()); } /** * Returns the <code>SyncProvider</code> instance identified by <i>providerID</i>. * * @param providerID the unique identifier of the provider * @return a <code>SyncProvider</code> implementation * @throws SyncFactoryException If the SyncProvider cannot be found or * some error was encountered when trying to invoke this provider. */ public static SyncProvider getInstance(String providerID) throws SyncFactoryException { initMapIfNecessary(); // populate HashTable initJNDIContext(); // check JNDI context for any additional bindings ProviderImpl impl = (ProviderImpl)implementations.get(providerID); if (impl == null) { // Requested SyncProvider is unavailable. Return default provider. return new com.sun.rowset.providers.RIOptimisticProvider(); } // Attempt to invoke classname from registered SyncProvider list Class c = null; try { ClassLoader cl = Thread.currentThread().getContextClassLoader(); /** * The SyncProvider implementation of the user will be in * the classpath. We need to find the ClassLoader which loads * this SyncFactory and try to laod the SyncProvider class from * there. **/ c = Class.forName(providerID, true, cl); if (c != null) { return (SyncProvider)c.newInstance(); } else { return new com.sun.rowset.providers.RIOptimisticProvider(); } } catch (IllegalAccessException e) { throw new SyncFactoryException("IllegalAccessException: " + e.getMessage()); } catch (InstantiationException e) { throw new SyncFactoryException("InstantiationException: " + e.getMessage()); } catch (ClassNotFoundException e) { throw new SyncFactoryException("ClassNotFoundException: " + e.getMessage()); } } /** * Returns an Enumeration of currently registered synchronization * providers. A <code>RowSet</code> implementation may use any provider in * the enumeration as its <code>SyncProvider</code> object. * <p> * At a minimum, the reference synchronization provider allowing * RowSet content data to be stored using a JDBC driver should be * possible. * * @return Enumeration A enumeration of available synchronization * providers that are registered with this Factory
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -