1 22 package org.jboss.ejb.plugins.keygenerator.hilo; 23 24 import org.jboss.system.ServiceMBeanSupport; 25 import org.jboss.ejb.plugins.keygenerator.KeyGeneratorFactory; 26 import org.jboss.ejb.plugins.keygenerator.KeyGenerator; 27 import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil; 28 import org.jboss.ejb.plugins.cmp.jdbc.SQLUtil; 29 import org.jboss.naming.Util; 30 import org.jboss.deployment.DeploymentException; 31 32 import javax.management.ObjectName ; 33 import javax.naming.InitialContext ; 34 import javax.naming.Context ; 35 import javax.naming.NamingException ; 36 import javax.sql.DataSource ; 37 import javax.transaction.TransactionManager ; 38 import java.io.Serializable ; 39 import java.sql.SQLException ; 40 import java.sql.Connection ; 41 import java.sql.Statement ; 42 import java.sql.ResultSet ; 43 44 50 public class HiLoKeyGeneratorFactory 51 extends ServiceMBeanSupport 52 implements KeyGeneratorFactory, HiLoKeyGeneratorFactoryMBean, Serializable 53 { 54 private ObjectName dataSource; 55 private transient DataSource ds; 56 private transient TransactionManager tm; 57 58 private String jndiName; 59 private String tableName; 60 private String sequenceColumn; 61 private String sequenceName; 62 private String idColumnName; 63 private String createTableDdl; 64 private String selectHiSql; 65 private long blockSize; 66 67 private boolean createTable = true; 68 private boolean dropTable; 69 70 73 public void setFactoryName(String factoryName) 74 { 75 this.jndiName = factoryName; 76 } 77 78 81 public String getFactoryName() 82 { 83 return jndiName; 84 } 85 86 89 public void setDataSource(ObjectName dataSource) throws Exception 90 { 91 if(getState() == STARTED && !dataSource.equals(this.dataSource)) 92 { 93 ds = lookupDataSource(dataSource); 94 } 95 this.dataSource = dataSource; 96 } 97 98 101 public ObjectName getDataSource() 102 { 103 return dataSource; 104 } 105 106 109 public String getTableName() 110 { 111 return tableName; 112 } 113 114 117 public void setTableName(String tableName) 118 throws Exception 119 { 120 if(getState() == STARTED && !tableName.equals(this.tableName)) 121 { 122 initSequence(tableName, sequenceColumn, sequenceName, idColumnName); 123 } 124 this.tableName = tableName; 125 } 126 127 130 public String getSequenceColumn() 131 { 132 return sequenceColumn; 133 } 134 135 138 public void setSequenceColumn(String sequenceColumn) 139 { 140 this.sequenceColumn = sequenceColumn; 141 } 142 143 146 public String getSequenceName() 147 { 148 return sequenceName; 149 } 150 151 154 public void setSequenceName(String sequenceName) 155 { 156 this.sequenceName = sequenceName; 157 } 158 159 162 public String getIdColumnName() 163 { 164 return idColumnName; 165 } 166 167 170 public void setIdColumnName(String idColumnName) 171 { 172 this.idColumnName = idColumnName; 173 } 174 175 178 public String getCreateTableDdl() 179 { 180 return createTableDdl; 181 } 182 183 186 public void setCreateTableDdl(String createTableDdl) 187 { 188 this.createTableDdl = createTableDdl; 189 } 190 191 194 public String getSelectHiSql() 195 { 196 return selectHiSql; 197 } 198 199 202 public void setSelectHiSql(String selectHiSql) 203 { 204 this.selectHiSql = selectHiSql; 205 } 206 207 210 public long getBlockSize() 211 { 212 return blockSize; 213 } 214 215 218 public void setBlockSize(long blockSize) 219 { 220 this.blockSize = blockSize; 221 } 222 223 226 public boolean isCreateTable() 227 { 228 return createTable; 229 } 230 231 234 public void setCreateTable(boolean createTable) 235 { 236 this.createTable = createTable; 237 } 238 239 242 public boolean isDropTable() 243 { 244 return dropTable; 245 } 246 247 250 public void setDropTable(boolean dropTable) 251 { 252 this.dropTable = dropTable; 253 } 254 255 257 public KeyGenerator getKeyGenerator() throws Exception 258 { 259 return new HiLoKeyGenerator(ds, tableName, sequenceColumn, sequenceName, idColumnName, selectHiSql, blockSize, tm); 260 } 261 262 264 public void startService() 265 throws Exception 266 { 267 Context ctx = new InitialContext (); 268 269 Util.rebind(ctx, getFactoryName(), this); 271 272 tm = (TransactionManager )ctx.lookup("java:/TransactionManager"); 273 274 ds = lookupDataSource(dataSource); 275 initSequence(tableName, sequenceColumn, sequenceName, idColumnName); 276 } 277 278 public void stopService() 279 throws Exception 280 { 281 if(dropTable) 282 { 283 dropTableIfExists(tableName); 284 } 285 286 ds = null; 287 tm = null; 288 289 Context ctx = new InitialContext (); 291 Util.unbind(ctx, getFactoryName()); 292 } 293 294 296 private void initSequence(String tableName, String sequenceColumn, String sequenceName, String idColumnName) 297 throws SQLException , DeploymentException 298 { 299 if(createTable) 300 { 301 createTableIfNotExists(tableName); 302 } 303 304 Connection con = null; 305 Statement st = null; 306 ResultSet rs = null; 307 try 308 { 309 String sql = "select " + idColumnName + " from " + tableName + " where " + sequenceColumn + "='" + sequenceName + "'"; 310 log.debug("Executing SQL: " + sql); 311 312 con = ds.getConnection(); 313 st = con.createStatement(); 314 rs = st.executeQuery(sql); 315 if(!rs.next()) 316 { 317 sql = "insert into " + 318 tableName + 319 "(" + 320 sequenceColumn + 321 ", " + 322 idColumnName + 323 ") values ('" + sequenceName + "', 0)"; 324 log.debug("Executing SQL: " + sql); 325 326 final Statement insertSt = con.createStatement(); 327 try 328 { 329 final int i = insertSt.executeUpdate(sql); 330 if(i != 1) 331 { 332 throw new SQLException ("Expected one updated row but got: " + i); 333 } 334 } 335 finally 336 { 337 JDBCUtil.safeClose(insertSt); 338 } 339 } 340 else 341 { 342 HiLoKeyGenerator.setHighestHi(rs.getLong(1)); 343 } 344 } 345 finally 346 { 347 JDBCUtil.safeClose(rs); 348 JDBCUtil.safeClose(st); 349 JDBCUtil.safeClose(con); 350 } 351 } 352 353 private void createTableIfNotExists(String tableName) 354 throws SQLException , DeploymentException 355 { 356 Connection con = null; 357 Statement st = null; 358 try 359 { 360 if(!SQLUtil.tableExists(tableName, ds)) 361 { 362 log.debug("Executing DDL: " + createTableDdl); 363 364 con = ds.getConnection(); 365 st = con.createStatement(); 366 st.executeUpdate(createTableDdl); 367 } 368 } 369 finally 370 { 371 JDBCUtil.safeClose(st); 372 JDBCUtil.safeClose(con); 373 } 374 } 375 376 private void dropTableIfExists(String tableName) 377 throws SQLException , DeploymentException 378 { 379 Connection con = null; 380 Statement st = null; 381 try 382 { 383 if(SQLUtil.tableExists(tableName, ds)) 384 { 385 final String ddl = "drop table " + tableName; 386 log.debug("Executing DDL: " + ddl); 387 388 con = ds.getConnection(); 389 st = con.createStatement(); 390 st.executeUpdate(ddl); 391 } 392 } 393 finally 394 { 395 JDBCUtil.safeClose(st); 396 JDBCUtil.safeClose(con); 397 } 398 } 399 400 private DataSource lookupDataSource(ObjectName dataSource) 401 throws Exception 402 { 403 try 404 { 405 String dsJndi = (String ) server.getAttribute(dataSource, "BindName"); 406 return (DataSource )new InitialContext ().lookup(dsJndi); 407 } 408 catch(NamingException e) 409 { 410 throw new Exception ("Failed to lookup data source: " + dataSource); 411 } 412 } 413 } 414 | Popular Tags |