1 15 package org.apache.tapestry.vlib.ejb.impl; 16 17 import java.sql.Connection ; 18 import java.sql.PreparedStatement ; 19 import java.sql.ResultSet ; 20 import java.sql.SQLException ; 21 import java.util.LinkedList ; 22 23 import javax.ejb.SessionBean ; 24 import javax.ejb.SessionContext ; 25 import javax.naming.Context ; 26 import javax.naming.InitialContext ; 27 import javax.naming.NamingException ; 28 import javax.sql.DataSource ; 29 30 import org.apache.tapestry.contrib.ejb.XEJBException; 31 32 53 54 public class KeyAllocatorBean implements SessionBean 55 { 56 private static final String PROPERTY_NAME = "next-key"; 57 58 private SessionContext context; 59 60 65 66 private LinkedList keys; 67 68 73 74 private int blockSize = 0; 75 76 81 82 private DataSource dataSource; 83 84 89 90 public void ejbCreate() 91 { 92 Context initial; 93 Context environment; 94 Integer blockSizeProperty; 95 96 try 97 { 98 initial = new InitialContext (); 99 environment = (Context ) initial.lookup("java:comp/env"); 100 } 101 catch (NamingException ex) 102 { 103 throw new XEJBException("Could not lookup environment.", ex); 104 } 105 106 try 107 { 108 blockSizeProperty = (Integer ) environment.lookup("blockSize"); 109 } 110 catch (NamingException ex) 111 { 112 throw new XEJBException("Could not lookup blockSize property.", ex); 113 } 114 115 blockSize = blockSizeProperty.intValue(); 116 117 try 118 { 119 dataSource = (DataSource ) environment.lookup("jdbc/dataSource"); 120 } 121 catch (NamingException ex) 122 { 123 throw new XEJBException("Could not lookup data source.", ex); 124 } 125 126 if (keys == null) 127 keys = new LinkedList (); 128 } 129 130 133 134 public void ejbPassivate() 135 { 136 } 137 138 public void setSessionContext(SessionContext value) 139 { 140 context = value; 141 } 142 143 147 148 public void ejbActivate() 149 { 150 } 151 152 160 161 public void ejbRemove() 162 { 163 } 165 166 171 172 public Integer allocateKey() 173 { 174 if (keys.isEmpty()) 175 allocateBlock(1); 176 177 return (Integer ) keys.removeFirst(); 178 } 179 180 186 187 public Integer [] allocateKeys(int count) 188 { 189 Integer [] result; 190 int i; 191 192 if (keys.size() < count) 193 allocateBlock(count); 194 195 result = new Integer [count]; 196 197 for (i = 0; i < count; i++) 198 { 199 result[i] = (Integer ) keys.removeFirst(); 200 } 201 202 return result; 203 } 204 205 214 215 protected void allocateBlock(int count) 216 { 217 Connection connection = null; 218 PreparedStatement statement = null; 219 ResultSet set = null; 220 int nextKey; 221 int allocationCount; 222 int i; 223 224 allocationCount = Math.max(count, blockSize); 225 226 try 227 { 228 connection = getConnection(); 229 230 statement = connection.prepareStatement("select PROP_VALUE from PROP where NAME = ?"); 231 statement.setString(1, PROPERTY_NAME); 232 233 set = statement.executeQuery(); 234 235 237 set.next(); 238 239 nextKey = set.getInt(1); 240 241 set.close(); 242 set = null; 243 244 statement.close(); 245 statement = null; 246 247 249 for (i = 0; i < allocationCount; i++) 250 keys.add(new Integer (nextKey++)); 251 252 254 statement = 255 connection.prepareStatement("update PROP\n" + "set PROP_VALUE = ?\n" + "where NAME = ?"); 256 statement.setInt(1, nextKey); 257 statement.setString(2, PROPERTY_NAME); 258 259 statement.executeUpdate(); 260 } 261 catch (SQLException ex) 262 { 263 ex.printStackTrace(); 264 265 throw new XEJBException("Unable to allocate keys from the database.", ex); 266 } 267 finally 268 { 269 if (set != null) 270 { 271 try 272 { 273 set.close(); 274 } 275 catch (SQLException ex) 276 { 277 } 278 } 279 280 if (statement != null) 281 { 282 try 283 { 284 statement.close(); 285 } 286 catch (SQLException ex) 287 { 288 } 289 } 290 291 if (connection != null) 292 { 293 try 294 { 295 connection.close(); 296 } 297 catch (SQLException ex) 298 { 299 } 300 } 301 } 302 303 } 304 305 312 313 protected Connection getConnection() 314 { 315 try 316 { 317 return dataSource.getConnection(); 318 } 319 catch (SQLException ex) 320 { 321 throw new XEJBException("Unable to get database connection from pool.", ex); 322 } 323 } 324 } | Popular Tags |