1 25 package org.jresearch.gossip.dao.drivers.keygen; 26 27 import java.math.BigDecimal ; 28 import java.sql.Connection ; 29 import java.sql.PreparedStatement ; 30 import java.sql.ResultSet ; 31 import java.sql.SQLException ; 32 import java.text.MessageFormat ; 33 34 import org.apache.log.Logger; 35 import org.jresearch.gossip.exception.ConfiguratorException; 36 import org.jresearch.gossip.exception.SystemException; 37 import org.jresearch.gossip.log.avalon.JGossipLog; 38 39 50 public class KeyKeeper { 51 52 55 private Logger log; 56 57 private static final String DATA_TYPE = "DATA_TYPE"; 58 59 private static int increment = -1; 60 61 private int columnType = -1; 62 63 66 Object high; 67 68 int low; 69 70 String [] primaryKey; 71 72 75 String tableName; 76 77 KeyKeeper(String tableName, String [] primaryKey) { 78 this.tableName = tableName; 79 this.primaryKey = primaryKey; 80 try { 81 log = JGossipLog.getInstance().getAppLogger(); 82 } catch (SystemException e) { 83 } 84 } 85 86 93 Object fetchHigh(Connection connection) throws SQLException { 94 Object identity = null; 95 96 String tableColumn; 97 String table; 98 String keyColumn; 99 try { 100 table = KeyGeneratorFactory.conf 102 .get(IKeyGenConst.KEY_KEEPER_TABLE_NAME); 103 tableColumn = KeyGeneratorFactory.conf 105 .get(IKeyGenConst.KEY_KEEPER_TABLE_COLUMN); 106 keyColumn = KeyGeneratorFactory.conf 108 .get(IKeyGenConst.KEY_KEEPER_KEY_COLUMN); 109 } catch (ConfiguratorException ce) { 110 if (log.isErrorEnabled()) { 111 log.error( 112 "Configurator error. Check your config files. Cause: ", 113 ce); 114 } 115 table = "JRF_KEY_KEEPER"; 118 tableColumn = "table_name"; 119 keyColumn = "next_key"; 120 } 121 StringBuffer condition = new StringBuffer (); 122 condition.append(tableColumn + " = ?"); 123 124 ResultSet rs = null; 125 PreparedStatement pstmt = null; 126 PreparedStatement pstmt2 = null; 127 try { 128 130 if (columnType == -1) { 132 columnType = getColumnType(connection, table, keyColumn); 133 } 134 135 if (columnType != java.sql.Types.INTEGER 136 && columnType != java.sql.Types.NUMERIC 137 && columnType != java.sql.Types.DECIMAL) { 138 throw new SQLException (MessageFormat.format( 139 "Key keeper has a SQL type[{0}] of key column[{1}]", 140 new Object [] { new Integer (columnType), keyColumn })); 141 142 } 143 144 String sql = getStatement(keyColumn, table, condition.toString()); 145 pstmt = connection.prepareStatement(sql); 146 pstmt.setString(1, tableName); 147 148 rs = pstmt.executeQuery(); 149 150 int count = 0; 152 if (rs.next()) { 153 Object lastIdentity = rs.getObject(1); 154 rs.close(); 155 156 if (columnType == java.sql.Types.INTEGER) { 157 identity = new Integer (((Number ) lastIdentity).intValue() 158 + getGrabSize()); 159 } else { 160 identity = new BigDecimal (((Number ) lastIdentity) 161 .doubleValue() 162 + getGrabSize()); 163 } 164 165 condition.append(" and " + keyColumn + "=?"); 166 167 sql = getUpdateStatement(keyColumn, table, condition.toString()); 169 pstmt2 = connection.prepareStatement(sql); 170 pstmt2.setObject(1, identity); 171 pstmt2.setString(2, tableName); 172 pstmt2.setObject(3, lastIdentity); 173 count = pstmt2.executeUpdate(); 174 } 175 176 if (count < 1) 177 throw new SQLException ("persist.highlowFailed"); 178 if (connection.getAutoCommit() == false) { 179 connection.commit(); 180 } 181 } catch (SQLException sqle) { 182 log.error("persist.highlowFailed", sqle); 183 } finally { 184 try { 185 if (rs != null) 186 rs.close(); 187 188 if (pstmt != null) { 189 pstmt.close(); 190 } 191 pstmt = null; 192 193 if (pstmt2 != null) { 194 pstmt2.close(); 195 } 196 pstmt2 = null; 197 198 } catch (SQLException excep) { 199 } 203 } 204 205 return identity; 206 } 207 208 private final int getColumnType(Connection conn, String table, String column) 209 throws SQLException { 210 return java.sql.Types.INTEGER; 211 221 } 222 223 private final String getStatement(String column, String table, 224 String condition) { 225 final String sql = "SELECT {0} FROM {1} WHERE {2} "; return MessageFormat.format(sql, new Object [] { column, table, 228 condition }); 229 } 230 231 private final String getUpdateStatement(String column, String table, 232 String condition) { 233 final String sql = "UPDATE {1} SET {0}=? WHERE {2}"; 235 return MessageFormat.format(sql, new Object [] { column, table, 236 condition }); 237 } 238 239 248 synchronized Object nextKey(Connection con) throws SQLException { 249 if (high == null || low >= getGrabSize()) { 250 high = fetchHigh(con); 251 low = 0; 252 } 253 254 Class highClass = high.getClass(); 255 if (highClass.equals(Integer .class)) { 256 return new Integer (((Integer ) high).intValue() - getGrabSize() 257 + low++); 258 } else if (highClass.equals(BigDecimal .class)) { 259 return new BigDecimal (((BigDecimal ) high).intValue() 260 - getGrabSize() + low++); 261 262 } 263 throw new SQLException (MessageFormat.format( 264 "KeyKeeper[{0}] can't handle primary key of type[{1}]", 265 new Object [] { getClass().getName(), highClass })); 266 } 267 268 private static int getGrabSize() 269 270 { 271 if (-1 == increment) { 272 synchronized (KeyKeeper.class) { 273 if (-1 == increment) { 274 try { 275 final String value = KeyGeneratorFactory.conf 276 .get(IKeyGenConst.KEY_KEEPER_INCREMENT); 277 increment = Integer.parseInt(value); 278 } catch (Exception e) { 279 increment = 10; 280 try { 281 if (JGossipLog.getInstance().getAppLogger() 282 .isErrorEnabled()) { 283 JGossipLog 284 .getInstance() 285 .getAppLogger() 286 .error( 287 "Can't retreive increment value from configuration", 288 e); 289 } 290 } catch (SystemException e1) { 291 } 292 } 293 } 294 } 295 } 296 return increment; 297 } 298 } 299 | Popular Tags |