| Home >> All >> cvebrowser >> dictionary >> data >> persistence >> [ util Javadoc ] |
Source code: cvebrowser/dictionary/data/persistence/util/PrepStatementCache.java
1 package cvebrowser.dictionary.data.persistence.util; 2 3 import java.util.HashMap; 4 import java.util.ResourceBundle; 5 import java.util.Locale; 6 import java.util.Iterator; 7 8 import java.sql.PreparedStatement; 9 import java.sql.SQLException; 10 11 import java.io.ObjectOutputStream; 12 import java.io.ObjectInputStream; 13 14 /** 15 * PrepStatementCache - Specialized cache that stores a set of PreparedStatements for later retrieval. 16 * @author Jose Vicente Nunez Zuleta (josevnz@users.sourceforge.net) 17 * @version 0.1 - 08/17/2003 18 */ 19 public final class PrepStatementCache { 20 21 private static ResourceBundle _bundle = null; 22 // hide the constructor 23 private PrepStatementCache() {}; 24 25 private static PrepStatementCache _instance; 26 27 private static HashMap _prepMap; 28 29 /** 30 * Return an instance for this Singleton. 31 * @return PrepStatementCache 32 * @since 0.1 33 */ 34 public static synchronized PrepStatementCache getInstance() { 35 if (_instance == null) { 36 _instance = new PrepStatementCache(); 37 _prepMap = new HashMap(); 38 _bundle = ResourceBundle.getBundle(PrepStatementCache.class.getName(), Locale.getDefault()); 39 } 40 return _instance; 41 } 42 43 /** 44 * Add a element to the cache 45 * @param prep_ 46 * @param name_ 47 * @throws IllegalArgumentException if any of the arguments is null 48 * @since 0.1 49 */ 50 public void put(PreparedStatement prep_, String name_) throws IllegalArgumentException { 51 if (prep_ == null) { 52 throw new IllegalArgumentException(_bundle.getString("cvebrowser.dictionary.data.persistence.util.PrepStatementCache.error.missingParameter") + " 'prep_'"); 53 } 54 if (name_ == null) { 55 throw new IllegalArgumentException(_bundle.getString("cvebrowser.dictionary.data.persistence.util.PrepStatementCache.error.missingParameter") + " 'name_'"); 56 } 57 _prepMap.put(name_, prep_); 58 } 59 60 /** 61 * Get the prepared statement back to the caller 62 * @param name_ 63 * @return PreparedStatement. Can be null if the prepared statement instance is not registered by that name. 64 * @since 0.1 65 */ 66 public PreparedStatement get(String name_) { 67 if (name_ == null) { 68 throw new IllegalArgumentException(_bundle.getString("cvebrowser.dictionary.data.persistence.util.PrepStatementCache.error.missingParameter") + " 'name_'"); 69 } 70 return (PreparedStatement) _prepMap.get(name_); 71 } 72 73 /** 74 * Clean the internal cache. Closes all the opened prepared statements and cleans the cache. 75 */ 76 public synchronized void clean() { 77 Iterator iterator = _prepMap.keySet().iterator(); 78 while (iterator.hasNext()) { 79 try { 80 PreparedStatement prep = (PreparedStatement) _prepMap.get((String) iterator.next()); 81 if (prep != null) { 82 prep.close(); 83 } 84 } catch (SQLException ignore) { }; 85 } 86 // clearn the map 87 _prepMap.clear(); 88 } 89 90 /** 91 * Final resource liberation 92 */ 93 public void finalize() { 94 clean(); 95 } 96 97 /** 98 * Make this class uncloneable. Anyone who wants to use this class must use the constructor. 99 * @throws CloneNotSupportedException 100 */ 101 public final Object clone() throws java.lang.CloneNotSupportedException { 102 throw new java.lang.CloneNotSupportedException(); 103 } 104 105 /** 106 * Make this class unserializable. Any attempt to serialize will throw an exception. 107 * @param out_ 108 * @throws IOException 109 */ 110 private final void writeObject(ObjectOutputStream out_) throws java.io.IOException { 111 throw new java.io.IOException(); 112 } 113 114 /** 115 * Make this class undeserializeable. Throw an exception if this method is ever called. 116 * @param in_ 117 * @throws IOException 118 */ 119 private final void readObject(ObjectInputStream in_) throws java.io.IOException { 120 throw new java.io.IOException(); 121 } 122 }