KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > alt > config > ValidationTable


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: ValidationTable.java 1988 2005-07-09 08:51:00Z dblevins $
44  */

45 package org.openejb.alt.config;
46
47 import java.io.File JavaDoc;
48 import java.net.URL JavaDoc;
49 import java.sql.Connection JavaDoc;
50 import java.sql.DriverManager JavaDoc;
51 import java.sql.PreparedStatement JavaDoc;
52 import java.sql.ResultSet JavaDoc;
53 import java.sql.SQLException JavaDoc;
54 import java.sql.Statement JavaDoc;
55 import java.util.Properties JavaDoc;
56
57 import org.openejb.util.FileUtils;
58 import org.openejb.util.JarUtils;
59 import org.openejb.OpenEJB;
60 import org.openejb.loader.SystemInstance;
61
62 /**
63  * Beans should be validated, but only when:
64  *
65  * - the validator version has changed since last validation
66  * - the jar has changed since last validation
67  * - the jar has never been validated
68  *
69  *
70  * This class works, but it causes problems elsewhere. It seems that
71  * using InstantDB just causes us to not be able to shutdown the VM.
72  * Obviously, InstantDB is starting user threads.
73  *
74  * This probably needs to be rewritten to not use InstantDB.
75  *
76  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
77  */

78 public class ValidationTable {
79
80     /** Singleton ValidationTable instance */
81     private static ValidationTable table;
82
83     /**
84      * It's possible that we can create/drop this table for each release version
85      * or just keep a validator version id in the table. For now, I've added a
86      * version number to the table.
87      *
88      * It's important that we make the distinction because someone could update
89      * their OpenEJB version, which may have more/better validation functionality.
90      *
91      * Table looks as such:
92      *
93      * CREATE TABLE validation (
94      *
95      * jar_path CHAR(150) PRIMARY KEY,
96      * last_validated CHAR(13),
97      * validator_version CHAR(20)
98      *
99      * )
100      */

101     private static final String JavaDoc _createTable = "CREATE TABLE validation ( jar_path CHAR(150) PRIMARY KEY, last_validated CHAR(13), validator_version CHAR(20))";
102     private static final String JavaDoc _selectValidated = "select last_validated, validator_version from validation where jar_path = ?";
103     private static final String JavaDoc _updateValidated = "update validation set last_validated = (?), validator_version = ? where jar_path = ?";
104     private static final String JavaDoc _insertValidated = "insert into validation (jar_path, last_validated, validator_version) values (?,?,?)";
105
106     private static final String JavaDoc jdbcDriver = "org.enhydra.instantdb.jdbc.idbDriver";
107     private static final String JavaDoc jdbcUrl = "jdbc:idb:conf/registry.properties";
108     private static final String JavaDoc userName = "system";
109     private static final String JavaDoc password = "system";
110
111     private Connection JavaDoc conn;
112     //physicalConn = DriverManager.getConnection(jdbcUrl, rxInfo.getUserName(), rxInfo.getPassword());
113

114     private ValidationTable(){
115         try{
116             // Load the driver
117
ClassLoader JavaDoc cl = OpenEJB.getContextClassLoader();
118             Class.forName( jdbcDriver, true, cl);
119             // Get a connection
120
conn = getConnection();
121         } catch (Exception JavaDoc e){
122             e.printStackTrace();
123         }
124         try{
125             // try and create the table
126
// if it's already there, an exception will
127
// be thrown. We can ignore it.
128
Statement JavaDoc stmt = conn.createStatement();
129             stmt.execute(_createTable);
130             stmt.close();
131         } catch (Exception JavaDoc e){
132             // We can ignore this.
133
} finally {
134             try{
135                 conn.close();
136             } catch (Exception JavaDoc e){}
137         }
138     }
139
140     private Connection JavaDoc getConnection() throws SQLException JavaDoc{
141         return DriverManager.getConnection(jdbcUrl, userName, password);
142     }
143
144     public static ValidationTable getInstance(){
145         if (table == null) {
146             table = new ValidationTable();
147         }
148
149         return table;
150     }
151
152     public boolean isValidated(String JavaDoc jarFile){
153         try{
154             File JavaDoc jar = SystemInstance.get().getBase().getFile(jarFile);
155             long lastModified = jar.lastModified();
156             long lastValidated = getLastValidated(jar);
157             //System.out.println(" -- modified "+lastModified);
158
//System.out.println(" -- validated "+lastValidated);
159
return (lastValidated > lastModified);
160         } catch (Exception JavaDoc e){
161             return false;
162         }
163     }
164
165     public void setValidated(String JavaDoc jarFile){
166         setLastValidated(jarFile, System.currentTimeMillis());
167     }
168
169
170     public long getLastValidated(File JavaDoc jar){
171         long validated = 0L;
172         try{
173             conn = getConnection();
174
175             String JavaDoc jarFileURL = jar.toURL().toExternalForm();
176
177             //System.out.println("[] getLastValidated "+jarFileURL);
178
PreparedStatement JavaDoc stmt = conn.prepareStatement(_selectValidated);
179             stmt.setString(1, jarFileURL);
180
181             ResultSet JavaDoc results = stmt.executeQuery();
182             if (results.next()) {
183                 String JavaDoc version = results.getString(2);
184                 //System.out.println("[] version "+version);
185
if (version == null || version.equals(getVersion())) {
186                     validated = results.getLong(1);
187                     //System.out.println("[] validated "+validated);
188
}
189             }
190             stmt.close();
191         } catch (Exception JavaDoc e){
192             // TODO:1: Log something...
193
//e.printStackTrace();
194
} finally {
195             try{conn.close();} catch (Exception JavaDoc e){}
196         }
197         return validated;
198     }
199
200
201     /**
202      * Same as the above getLastValidated, except that this
203      * will return the last validated date regardless of
204      * the validator version.
205      *
206      * @param jarFileURL
207      * @return long
208      */

209     private long _getLastValidated(String JavaDoc jarFileURL){
210         long validated = 0L;
211         try{
212             conn = getConnection();
213
214             PreparedStatement JavaDoc stmt = conn.prepareStatement(_selectValidated);
215             stmt.setString(1, jarFileURL);
216
217             ResultSet JavaDoc results = stmt.executeQuery();
218             if (results.next()) {
219                 validated = results.getLong(1);
220             }
221             stmt.close();
222         } catch (Exception JavaDoc e){
223             // TODO:1: Log something...
224
//e.printStackTrace();
225
} finally {
226             try{conn.close();} catch (Exception JavaDoc e){}
227         }
228         return validated;
229     }
230
231     public void setLastValidated(String JavaDoc jarFile, long timeValidated){
232         try{
233             conn = getConnection();
234             File JavaDoc jar = SystemInstance.get().getBase().getFile(jarFile);
235             String JavaDoc jarFileURL = jar.toURL().toExternalForm();
236             //System.out.println("[] setLastValidated "+jarFileURL );
237
//System.out.println(" -- time "+timeValidated );
238
PreparedStatement JavaDoc stmt = null;
239             if (_getLastValidated(jarFileURL) != 0L) {
240                 stmt = conn.prepareStatement(_updateValidated);
241                 stmt.setLong(1, timeValidated);
242                 stmt.setString(2, getVersion());
243                 stmt.setString(3, jarFileURL);
244             } else {
245                 stmt = conn.prepareStatement(_insertValidated);
246                 stmt.setString(1, jarFileURL);
247                 stmt.setLong(2, timeValidated);
248                 stmt.setString(3, getVersion());
249             }
250
251             stmt.executeUpdate();
252             stmt.close();
253         } catch (Exception JavaDoc e){
254             // TODO:1: Log something...
255
//e.printStackTrace();
256
} finally {
257             try{conn.close();} catch (Exception JavaDoc e){}
258         }
259     }
260
261     private String JavaDoc version = null;
262
263     private String JavaDoc getVersion(){
264         if (version == null) {
265             /*
266              * Output startup message
267              */

268             Properties JavaDoc versionInfo = new Properties JavaDoc();
269
270             try {
271                 JarUtils.setHandlerSystemProperty();
272                 versionInfo.load( new URL JavaDoc( "resource:/openejb-version.properties" ).openConnection().getInputStream() );
273             } catch (java.io.IOException JavaDoc e) {
274             }
275             version = (String JavaDoc)versionInfo.get( "version" );
276         }
277         return version;
278     }
279 }
Popular Tags