KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > db > CmsDbUtil


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/db/CmsDbUtil.java,v $
3  * Date : $Date: 2006/03/27 14:52:26 $
4  * Version: $Revision: 1.25 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.db;
33
34 import org.opencms.file.CmsDataAccessException;
35 import org.opencms.main.CmsException;
36 import org.opencms.main.CmsLog;
37
38 import java.sql.Connection JavaDoc;
39 import java.sql.DriverManager JavaDoc;
40 import java.sql.PreparedStatement JavaDoc;
41 import java.sql.ResultSet JavaDoc;
42 import java.sql.SQLException JavaDoc;
43 import java.sql.Timestamp JavaDoc;
44 import java.util.Hashtable JavaDoc;
45
46 import org.apache.commons.logging.Log;
47
48 /**
49  * This class is used to create primary keys as integers for Cms database tables that
50  * don't have a UUID primary key.<p>
51  *
52  *
53  * @author Thomas Weckert
54  * @author Carsten Weinholz
55  *
56  * @version $Revision: 1.25 $
57  *
58  * @since 6.0.0
59  */

60 public final class CmsDbUtil {
61
62     /** Indicates an unknown (or not set) id. */
63     public static final int UNKNOWN_ID = -1;
64
65     /** Hashtable with border id's. */
66     private static Hashtable JavaDoc c_borderId;
67
68     /** Hashtable with next available id's. */
69     private static Hashtable JavaDoc c_currentId;
70
71     /** The name of the default pool. */
72     private static String JavaDoc c_dbPoolUrl;
73
74     /** Grow value. */
75     private static final int GROW_VALUE = 10;
76
77     /** The log object for this class. */
78     private static final Log LOG = CmsLog.getLog(CmsDbUtil.class);
79
80     /**
81      * Default constructor.<p>
82      *
83      * Nobody is allowed to create an instance of this class!
84      */

85     private CmsDbUtil() {
86
87         super();
88     }
89
90     /**
91      * This method tries to get the timestamp several times, because there
92      * is a timing problem in the mysql driver.<p>
93      *
94      * @param result the resultset to get the stamp from
95      * @param column the column to read the timestamp from
96      * @return the timestamp
97      * @throws SQLException if something goes wrong
98      */

99     public static Timestamp JavaDoc getTimestamp(ResultSet JavaDoc result, String JavaDoc column) throws SQLException JavaDoc {
100
101         int i = 0;
102         for (;;) {
103             try {
104                 return (result.getTimestamp(column));
105             } catch (SQLException JavaDoc exc) {
106                 i++;
107                 if (i >= 10) {
108                     throw exc;
109                 } else {
110                     if (LOG.isWarnEnabled()) {
111                         LOG.warn(Messages.get().getBundle().key(Messages.LOG_GET_TIMESTAMP_2, column, new Integer JavaDoc(i)));
112                     }
113                 }
114             }
115         }
116     }
117
118     /**
119      * Initilizes this DB utils.<p>
120      */

121     public static void init() {
122
123         c_currentId = new Hashtable JavaDoc();
124         c_borderId = new Hashtable JavaDoc();
125         c_dbPoolUrl = "";
126     }
127
128     /**
129      * Creates a new primary key ID for a given table.<p>
130      *
131      * @param tableName the name of the table to create a new primary key ID
132      * @return a new primary key ID for the given table
133      * @throws CmsException if something goes wrong
134      */

135     public static int nextId(String JavaDoc tableName) throws CmsException {
136
137         return nextId(c_dbPoolUrl, tableName);
138     }
139
140     /**
141      * Creates a new primary key ID for a given table using JDBC connection specified by a pool URL.<p>
142      *
143      * @param dbPoolUrl the URL to access the connection pool
144      * @param tableName the name of the table to create a new primary key ID
145      * @return a new primary key ID for the given table
146      * @throws CmsDataAccessException if something goes wrong
147      */

148     public static synchronized int nextId(String JavaDoc dbPoolUrl, String JavaDoc tableName) throws CmsDataAccessException {
149
150         String JavaDoc cacheKey = dbPoolUrl + '.' + tableName;
151
152         // generated primary keys are cached!
153
if (c_currentId.containsKey(cacheKey)) {
154             int id = ((Integer JavaDoc)c_currentId.get(cacheKey)).intValue();
155             int borderId = ((Integer JavaDoc)c_borderId.get(cacheKey)).intValue();
156             if (id < borderId) {
157                 int nextId = id + 1;
158                 c_currentId.put(cacheKey, new Integer JavaDoc(nextId));
159                 return id;
160             }
161         }
162
163         // there is no primary key ID for the given table yet in the cache.
164
// we generate a new primary key ID based on the last primary key
165
// entry in the CMS_SYSTEMID table instead
166
generateNextId(dbPoolUrl, tableName, cacheKey);
167
168         // afterwards, return back to this method to take the new primary key
169
// ID out of the cache...
170
return nextId(dbPoolUrl, tableName);
171     }
172
173     /**
174      * Sets the URL of the connection pool.<p>
175      *
176      * @param dbPoolUrl the URL to access the connection pool
177      */

178     public static void setDefaultPool(String JavaDoc dbPoolUrl) {
179
180         c_dbPoolUrl = dbPoolUrl;
181     }
182
183     /**
184      * Creates a new primary key ID for a given table in the CMS_SYSTEMID table.<p>
185      *
186      * @param conn the connection to access the database
187      * @param tableName the name of the table to read the primary key ID
188      * @param newId the new primary key ID
189      *
190      * @throws CmsDbSqlException if something gows wrong
191      */

192     private static void createId(Connection JavaDoc conn, String JavaDoc tableName, int newId) throws CmsDbSqlException {
193
194         PreparedStatement JavaDoc stmt = null;
195
196         try {
197             stmt = conn.prepareStatement("INSERT INTO CMS_SYSTEMID (TABLE_KEY,ID) VALUES (?,?)");
198             stmt.setString(1, tableName);
199             stmt.setInt(2, newId);
200             stmt.executeUpdate();
201         } catch (SQLException JavaDoc e) {
202             throw new CmsDbSqlException(org.opencms.db.generic.Messages.get().container(
203                 org.opencms.db.generic.Messages.ERR_GENERIC_SQL_1,
204                 CmsDbSqlException.getErrorQuery(stmt)), e);
205         } finally {
206             if (stmt != null) {
207                 try {
208                     stmt.close();
209                 } catch (SQLException JavaDoc exc) {
210                     // nothing to do here
211
}
212             }
213         }
214     }
215
216     /**
217      * Creates a new primary key ID for a given table based on the last primary key ID for this
218      * table in the CMS_SYSTEMID table.<p>
219      *
220      * @param dbPoolUrl the URL to access the connection pool
221      * @param tableName the name of the table to create a new primary key ID
222      * @param cacheKey the key to store the new primary key ID in the cache
223      *
224      * @throws CmsDbSqlException if something goes wrong
225      */

226     private static void generateNextId(String JavaDoc dbPoolUrl, String JavaDoc tableName, String JavaDoc cacheKey) throws CmsDbSqlException {
227
228         Connection JavaDoc con = null;
229         int id;
230         int borderId;
231
232         try {
233             if (!dbPoolUrl.startsWith(CmsDbPool.DBCP_JDBC_URL_PREFIX)) {
234                 dbPoolUrl = CmsDbPool.DBCP_JDBC_URL_PREFIX + dbPoolUrl;
235             }
236
237             con = DriverManager.getConnection(dbPoolUrl);
238             // repeat this operation, until the nextId is valid and can be saved
239
// (this is for clustering of several OpenCms)
240
do {
241                 id = readId(con, tableName);
242
243                 if (id == CmsDbUtil.UNKNOWN_ID) {
244                     // there was no entry - set it to 0
245
// EF: set id to 1 because the table contains
246
// the next available id
247
id = 1;
248                     createId(con, tableName, id);
249                 }
250                 borderId = id + GROW_VALUE;
251                 // save the next id for future requests
252
} while (!writeId(con, tableName, id, borderId));
253             // store the generated values in the cache
254
c_currentId.put(cacheKey, new Integer JavaDoc(id));
255             c_borderId.put(cacheKey, new Integer JavaDoc(borderId));
256         } catch (SQLException JavaDoc e) {
257             throw new CmsDbSqlException(org.opencms.db.generic.Messages.get().container(
258                 org.opencms.db.generic.Messages.ERR_GENERIC_SQL_0), e);
259         } finally {
260             // close all db-resources
261
if (con != null) {
262                 try {
263                     con.close();
264                 } catch (SQLException JavaDoc exc) {
265                     // nothing to do here
266
}
267             }
268         }
269     }
270
271     /**
272      * Reads the last primary key ID for a given table.<p>
273      *
274      * @param conn the connection to access the database
275      * @param tableName the name of the table to read the primary key ID
276      *
277      * @return the primary key ID or UNKNOWN_ID if there is no entry for the given table
278      *
279      * @throws CmsDbSqlException if something gows wrong
280      */

281     private static int readId(Connection JavaDoc conn, String JavaDoc tableName) throws CmsDbSqlException {
282
283         PreparedStatement JavaDoc stmt = null;
284         ResultSet JavaDoc res = null;
285         try {
286             stmt = conn.prepareStatement("SELECT CMS_SYSTEMID.ID FROM CMS_SYSTEMID WHERE CMS_SYSTEMID.TABLE_KEY=?");
287             stmt.setString(1, tableName);
288
289             res = stmt.executeQuery();
290             if (res.next()) {
291                 return res.getInt(1);
292             } else {
293                 return CmsDbUtil.UNKNOWN_ID;
294             }
295         } catch (SQLException JavaDoc e) {
296             throw new CmsDbSqlException(org.opencms.db.generic.Messages.get().container(
297                 org.opencms.db.generic.Messages.ERR_GENERIC_SQL_1,
298                 CmsDbSqlException.getErrorQuery(stmt)), e);
299         } finally {
300             // close all db-resources
301
if (res != null) {
302                 try {
303                     res.close();
304                 } catch (SQLException JavaDoc exc) {
305                     // nothing to do here
306
}
307             }
308             if (stmt != null) {
309                 try {
310                     stmt.close();
311                 } catch (SQLException JavaDoc exc) {
312                     // nothing to do here
313
}
314             }
315         }
316     }
317
318     /**
319      * Updates the CMS_SYSTEMID table with a new primary key ID for a given table.<p>
320      *
321      * @param conn the connection to access the database
322      * @param tableName the name of the table to read the primary key ID
323      * @param oldId the last primary key ID
324      * @param newId the new primary key ID
325      *
326      * @return true if the number of affected rows is 1
327      *
328      * @throws CmsDbSqlException if something gows wrong
329      */

330     private static boolean writeId(Connection JavaDoc conn, String JavaDoc tableName, int oldId, int newId) throws CmsDbSqlException {
331
332         PreparedStatement JavaDoc stmt = null;
333
334         try {
335             stmt = conn.prepareStatement("UPDATE CMS_SYSTEMID SET ID=? WHERE CMS_SYSTEMID.TABLE_KEY=? AND CMS_SYSTEMID.ID=?");
336             stmt.setInt(1, newId);
337             stmt.setString(2, tableName);
338             stmt.setInt(3, oldId);
339             int amount = stmt.executeUpdate();
340             // return, if the update had succeeded
341
return (amount == 1);
342         } catch (SQLException JavaDoc e) {
343             throw new CmsDbSqlException(org.opencms.db.generic.Messages.get().container(
344                 org.opencms.db.generic.Messages.ERR_GENERIC_SQL_1,
345                 CmsDbSqlException.getErrorQuery(stmt)), e);
346         } finally {
347             if (stmt != null) {
348                 try {
349                     stmt.close();
350                 } catch (SQLException JavaDoc exc) {
351                     // nothing to do here
352
}
353             }
354         }
355     }
356 }
Popular Tags