KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jms > jdbc > JdbcManager


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jms.jdbc;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.config.types.Period;
33 import com.caucho.jdbc.JdbcMetaData;
34 import com.caucho.util.L10N;
35 import com.caucho.util.Log;
36
37 import javax.sql.DataSource JavaDoc;
38 import java.sql.Connection JavaDoc;
39 import java.sql.ResultSet JavaDoc;
40 import java.sql.SQLException JavaDoc;
41 import java.sql.Statement JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 /**
45  * Manages the JDBC configuration.
46  */

47 public class JdbcManager {
48   static final Logger JavaDoc log = Log.open(JdbcManager.class);
49   static final L10N L = new L10N(JdbcManager.class);
50   
51   private DataSource JavaDoc _dataSource;
52
53   private String JavaDoc _messageTable = "resin_jms_message";
54   
55   private String JavaDoc _destinationTable = "resin_jms_destination";
56   private String JavaDoc _destinationSequence;
57   
58   private String JavaDoc _consumerTable = "resin_jms_consumer";
59   private String JavaDoc _consumerSequence;
60   
61   private String JavaDoc _blob;
62   private String JavaDoc _longType;
63   
64   private String JavaDoc _tablespace; // oracle tablespace for blobs
65

66   private boolean _isTruncateBlob;
67
68   private long _purgeInterval = 60000L;
69
70   private JdbcMessage _jdbcMessage;
71
72   private volatile boolean _isInit;
73
74   public JdbcManager()
75   {
76   }
77
78   /**
79    * Sets the data source.
80    */

81   public void setDataSource(DataSource JavaDoc dataSource)
82   {
83     _dataSource = dataSource;
84   }
85
86   /**
87    * Gets the data source.
88    */

89   public DataSource JavaDoc getDataSource()
90   {
91     return _dataSource;
92   }
93
94   /**
95    * Returns the message table
96    */

97   public String JavaDoc getMessageTable()
98   {
99     return _messageTable;
100   }
101
102   /**
103    * Sets the message table
104    */

105   public void setMessageTable(String JavaDoc tableName)
106   {
107     _messageTable = tableName;
108   }
109
110   /**
111    * Returns the destination table
112    */

113   public String JavaDoc getDestinationTable()
114   {
115     return _destinationTable;
116   }
117
118   /**
119    * Sets the destination table
120    */

121   public void setDestinationTable(String JavaDoc tableName)
122   {
123     _destinationTable = tableName;
124   }
125
126   /**
127    * Returns the destination sequence
128    */

129   public String JavaDoc getDestinationSequence()
130   {
131     return _destinationSequence;
132   }
133
134   /**
135    * Returns the consumer table
136    */

137   public String JavaDoc getConsumerTable()
138   {
139     return _consumerTable;
140   }
141
142   /**
143    * Sets the consumer table
144    */

145   public void setConsumerTable(String JavaDoc tableName)
146   {
147     _consumerTable = tableName;
148   }
149
150   /**
151    * Returns the consumer sequence
152    */

153   public String JavaDoc getConsumerSequence()
154   {
155     return _consumerSequence;
156   }
157
158   /**
159    * Returns the meta-data.
160    */

161   public JdbcMetaData getMetaData()
162   {
163     return JdbcMetaData.create(_dataSource);
164   }
165
166   /**
167    * Returns the blob type.
168    */

169   public String JavaDoc getBlob()
170   {
171     if (_blob == null)
172       _blob = getMetaData().getBlobType();
173
174     return _blob;
175   }
176
177   /**
178    * Sets the oracle tablespace.
179    */

180   public void setTablespace(String JavaDoc tablespace)
181   {
182     _tablespace = tablespace;
183   }
184
185   /**
186    * Gets the oracle tablespace
187    */

188   public String JavaDoc getTablespace()
189   {
190     return _tablespace;
191   }
192
193   /**
194    * Returns the blob type.
195    */

196   public String JavaDoc getLongType()
197   {
198     if (_longType == null)
199       _longType = getMetaData().getLongType();
200
201     return _longType;
202   }
203
204   /**
205    * Sets the purge interval.
206    */

207   public void setPurgeInterval(Period period)
208   {
209     _purgeInterval = period.getPeriod();
210   }
211
212   /**
213    * Gets the purge interval.
214    */

215   public long getPurgeInterval()
216   {
217     return _purgeInterval;
218   }
219
220   /**
221    * Returns the JDBC message manager.
222    */

223   public JdbcMessage getJdbcMessage()
224   {
225     return _jdbcMessage;
226   }
227
228   /**
229    * Return true if blobs need to be truncated before deletion.
230    */

231   public boolean isTruncateBlob()
232   {
233       return _isTruncateBlob;
234   }
235
236   /**
237    * Initializes the JdbcManager
238    */

239   public void init()
240     throws ConfigException, SQLException JavaDoc
241   {
242     if (_isInit)
243       return;
244     _isInit = true;
245     
246     if (_dataSource == null)
247       throw new ConfigException(L.l("JdbcManager requires a <data-source> element."));
248
249     _jdbcMessage = new JdbcMessage(this);
250
251     _jdbcMessage.init();
252
253     initDestinationTable();
254     initConsumerTable();
255     
256     _isTruncateBlob = getMetaData().isTruncateBlobBeforeDelete();
257   }
258
259   /**
260    * Initializes the destination table.
261    */

262   protected void initDestinationTable()
263     throws SQLException JavaDoc
264   {
265     Connection JavaDoc conn = _dataSource.getConnection();
266     
267     if (! getMetaData().supportsIdentity())
268       _destinationSequence = _destinationTable + "_cseq";
269
270     try {
271       Statement JavaDoc stmt = conn.createStatement();
272       String JavaDoc sql = "SELECT 1 FROM " + _destinationTable + " WHERE 1=0";
273
274       try {
275     ResultSet JavaDoc rs = stmt.executeQuery(sql);
276     rs.next();
277     rs.close();
278     stmt.close();
279
280     return;
281       } catch (SQLException JavaDoc e) {
282     log.finest(e.toString());
283       }
284
285       log.info(L.l("creating JMS destination table {0}", _destinationTable));
286
287       String JavaDoc identity = "";
288
289       if (getMetaData().supportsIdentity())
290     identity = " auto_increment";
291
292       String JavaDoc longType = getLongType();
293       
294       sql = ("CREATE TABLE " + _destinationTable + " (" +
295          " id " + longType + " PRIMARY KEY " + identity + "," +
296          " name VARCHAR(255)," +
297          " is_topic INTEGER" +
298          ")");
299
300       stmt.executeUpdate(sql);
301
302       if (! getMetaData().supportsIdentity()) {
303     _destinationSequence = _destinationTable + "_cseq";
304     
305     stmt.executeUpdate(getMetaData().createSequenceSQL(_destinationSequence, 1));
306       }
307     } finally {
308       conn.close();
309     }
310   }
311
312   /**
313    * Initializes the consumer table.
314    */

315   protected void initConsumerTable()
316     throws SQLException JavaDoc
317   {
318     if (! getMetaData().supportsIdentity())
319       _consumerSequence = _consumerTable + "_cseq";
320       
321     Connection JavaDoc conn = _dataSource.getConnection();
322     try {
323       Statement JavaDoc stmt = conn.createStatement();
324       String JavaDoc sql = "SELECT 1 FROM " + _consumerTable + " WHERE 1=0";
325
326       try {
327     ResultSet JavaDoc rs = stmt.executeQuery(sql);
328     rs.next();
329     rs.close();
330     stmt.close();
331
332     return;
333       } catch (SQLException JavaDoc e) {
334     log.finest(e.toString());
335       }
336
337       log.info(L.l("creating JMS subscriber table {0}", _consumerTable));
338
339       String JavaDoc longType = getLongType();
340       String JavaDoc identity = "";
341
342       if (getMetaData().supportsIdentity())
343     identity = " auto_increment";
344       
345       sql = ("CREATE TABLE " + _consumerTable + " (" +
346          " s_id " + longType + " PRIMARY KEY " + identity + "," +
347          " queue " + longType + "," +
348          " client VARCHAR(255)," +
349          " name VARCHAR(255)," +
350          " expire " + longType + "," +
351          " read_id " + longType + "," +
352          " ack_id " + longType +
353          ")");
354
355       stmt.executeUpdate(sql);
356
357       if (_consumerSequence != null)
358     stmt.executeUpdate(getMetaData().createSequenceSQL(_consumerSequence, 1));
359     } finally {
360       conn.close();
361     }
362   }
363
364   /**
365    * Returns a hash code.
366    */

367   public int hashCode()
368   {
369     if (_dataSource == null)
370       return 0;
371     else
372       return _dataSource.hashCode();
373   }
374
375   /**
376    * Test for equality.
377    */

378   public boolean equals(Object JavaDoc o)
379   {
380     if (this == o)
381       return true;
382     else if (! (o instanceof JdbcManager))
383       return false;
384
385     JdbcManager manager = (JdbcManager) o;
386
387     return _dataSource != null && _dataSource.equals(manager._dataSource);
388   }
389 }
390
391
Popular Tags