KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > keygenerator > hilo > HiLoKeyGeneratorFactory


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

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 JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.naming.Context JavaDoc;
35 import javax.naming.NamingException JavaDoc;
36 import javax.sql.DataSource JavaDoc;
37 import javax.transaction.TransactionManager JavaDoc;
38 import java.io.Serializable JavaDoc;
39 import java.sql.SQLException JavaDoc;
40 import java.sql.Connection JavaDoc;
41 import java.sql.Statement JavaDoc;
42 import java.sql.ResultSet JavaDoc;
43
44 /**
45  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
46  * @version <tt>$Revision: 45367 $</tt>
47  * @jmx.mbean name="jboss.system:service=KeyGeneratorFactory,type=HiLo"
48  * extends="org.jboss.system.ServiceMBean"
49  */

50 public class HiLoKeyGeneratorFactory
51    extends ServiceMBeanSupport
52    implements KeyGeneratorFactory, HiLoKeyGeneratorFactoryMBean, Serializable JavaDoc
53 {
54    private ObjectName JavaDoc dataSource;
55    private transient DataSource JavaDoc ds;
56    private transient TransactionManager JavaDoc tm;
57
58    private String JavaDoc jndiName;
59    private String JavaDoc tableName;
60    private String JavaDoc sequenceColumn;
61    private String JavaDoc sequenceName;
62    private String JavaDoc idColumnName;
63    private String JavaDoc createTableDdl;
64    private String JavaDoc selectHiSql;
65    private long blockSize;
66
67    private boolean createTable = true;
68    private boolean dropTable;
69
70    /**
71     * @jmx.managed-attribute
72     */

73    public void setFactoryName(String JavaDoc factoryName)
74    {
75       this.jndiName = factoryName;
76    }
77
78    /**
79     * @jmx.managed-attribute
80     */

81    public String JavaDoc getFactoryName()
82    {
83       return jndiName;
84    }
85
86    /**
87     * @jmx.managed-attribute
88     */

89    public void setDataSource(ObjectName JavaDoc dataSource) throws Exception JavaDoc
90    {
91       if(getState() == STARTED && !dataSource.equals(this.dataSource))
92       {
93          ds = lookupDataSource(dataSource);
94       }
95       this.dataSource = dataSource;
96    }
97
98    /**
99     * @jmx.managed-attribute
100     */

101    public ObjectName JavaDoc getDataSource()
102    {
103       return dataSource;
104    }
105
106    /**
107     * @jmx.managed-operation
108     */

109    public String JavaDoc getTableName()
110    {
111       return tableName;
112    }
113
114    /**
115     * @jmx.managed-operation
116     */

117    public void setTableName(String JavaDoc tableName)
118       throws Exception JavaDoc
119    {
120       if(getState() == STARTED && !tableName.equals(this.tableName))
121       {
122          initSequence(tableName, sequenceColumn, sequenceName, idColumnName);
123       }
124       this.tableName = tableName;
125    }
126
127    /**
128     * @jmx.managed-attribute
129     */

130    public String JavaDoc getSequenceColumn()
131    {
132       return sequenceColumn;
133    }
134
135    /**
136     * @jmx.managed-attribute
137     */

138    public void setSequenceColumn(String JavaDoc sequenceColumn)
139    {
140       this.sequenceColumn = sequenceColumn;
141    }
142
143    /**
144     * @jmx.managed-attribute
145     */

146    public String JavaDoc getSequenceName()
147    {
148       return sequenceName;
149    }
150
151    /**
152     * @jmx.managed-attribute
153     */

154    public void setSequenceName(String JavaDoc sequenceName)
155    {
156       this.sequenceName = sequenceName;
157    }
158
159    /**
160     * @jmx.managed-attribute
161     */

162    public String JavaDoc getIdColumnName()
163    {
164       return idColumnName;
165    }
166
167    /**
168     * @jmx.managed-attribute
169     */

170    public void setIdColumnName(String JavaDoc idColumnName)
171    {
172       this.idColumnName = idColumnName;
173    }
174
175    /**
176     * @jmx.managed-attribute
177     */

178    public String JavaDoc getCreateTableDdl()
179    {
180       return createTableDdl;
181    }
182
183    /**
184     * @jmx.managed-attribute
185     */

186    public void setCreateTableDdl(String JavaDoc createTableDdl)
187    {
188       this.createTableDdl = createTableDdl;
189    }
190
191    /**
192     * @jmx.managed-attribute
193     */

194    public String JavaDoc getSelectHiSql()
195    {
196       return selectHiSql;
197    }
198
199    /**
200     * @jmx.managed-attribute
201     */

202    public void setSelectHiSql(String JavaDoc selectHiSql)
203    {
204       this.selectHiSql = selectHiSql;
205    }
206
207    /**
208     * @jmx.managed-attribute
209     */

210    public long getBlockSize()
211    {
212       return blockSize;
213    }
214
215    /**
216     * @jmx.managed-attribute
217     */

218    public void setBlockSize(long blockSize)
219    {
220       this.blockSize = blockSize;
221    }
222
223    /**
224     * @jmx.managed-attribute
225     */

226    public boolean isCreateTable()
227    {
228       return createTable;
229    }
230
231    /**
232     * @jmx.managed-attribute
233     */

234    public void setCreateTable(boolean createTable)
235    {
236       this.createTable = createTable;
237    }
238
239    /**
240     * @jmx.managed-attribute
241     */

242    public boolean isDropTable()
243    {
244       return dropTable;
245    }
246
247    /**
248     * @jmx.managed-attribute
249     */

250    public void setDropTable(boolean dropTable)
251    {
252       this.dropTable = dropTable;
253    }
254
255    // KeyGeneratorFactory implementation
256

257    public KeyGenerator getKeyGenerator() throws Exception JavaDoc
258    {
259       return new HiLoKeyGenerator(ds, tableName, sequenceColumn, sequenceName, idColumnName, selectHiSql, blockSize, tm);
260    }
261
262    // ServiceMBeanSupport overrides
263

264    public void startService()
265       throws Exception JavaDoc
266    {
267       Context JavaDoc ctx = new InitialContext JavaDoc();
268
269       // bind the factory
270
Util.rebind(ctx, getFactoryName(), this);
271
272       tm = (TransactionManager JavaDoc)ctx.lookup("java:/TransactionManager");
273
274       ds = lookupDataSource(dataSource);
275       initSequence(tableName, sequenceColumn, sequenceName, idColumnName);
276    }
277
278    public void stopService()
279       throws Exception JavaDoc
280    {
281       if(dropTable)
282       {
283          dropTableIfExists(tableName);
284       }
285
286       ds = null;
287       tm = null;
288
289       // unbind the factory
290
Context JavaDoc ctx = new InitialContext JavaDoc();
291       Util.unbind(ctx, getFactoryName());
292    }
293
294    // Private
295

296    private void initSequence(String JavaDoc tableName, String JavaDoc sequenceColumn, String JavaDoc sequenceName, String JavaDoc idColumnName)
297       throws SQLException JavaDoc, DeploymentException
298    {
299       if(createTable)
300       {
301          createTableIfNotExists(tableName);
302       }
303
304       Connection JavaDoc con = null;
305       Statement JavaDoc st = null;
306       ResultSet JavaDoc rs = null;
307       try
308       {
309          String JavaDoc 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 JavaDoc insertSt = con.createStatement();
327             try
328             {
329                final int i = insertSt.executeUpdate(sql);
330                if(i != 1)
331                {
332                   throw new SQLException JavaDoc("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 JavaDoc tableName)
354       throws SQLException JavaDoc, DeploymentException
355    {
356       Connection JavaDoc con = null;
357       Statement JavaDoc 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 JavaDoc tableName)
377       throws SQLException JavaDoc, DeploymentException
378    {
379       Connection JavaDoc con = null;
380       Statement JavaDoc st = null;
381       try
382       {
383          if(SQLUtil.tableExists(tableName, ds))
384          {
385             final String JavaDoc 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 JavaDoc lookupDataSource(ObjectName JavaDoc dataSource)
401       throws Exception JavaDoc
402    {
403       try
404       {
405          String JavaDoc dsJndi = (String JavaDoc) server.getAttribute(dataSource, "BindName");
406          return (DataSource JavaDoc)new InitialContext JavaDoc().lookup(dsJndi);
407       }
408       catch(NamingException JavaDoc e)
409       {
410          throw new Exception JavaDoc("Failed to lookup data source: " + dataSource);
411       }
412    }
413 }
414
Popular Tags