001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.config;
017:
018: import java.io.File;
019: import java.sql.Connection;
020: import java.sql.DriverManager;
021: import java.sql.PreparedStatement;
022: import java.sql.ResultSet;
023: import java.sql.SQLException;
024: import java.sql.Statement;
025:
026: import org.apache.openejb.util.OpenEjbVersion;
027: import org.apache.openejb.ClassLoaderUtil;
028: import org.apache.openejb.loader.SystemInstance;
029:
030: public class ValidationTable {
031:
032: private static ValidationTable table;
033:
034: private static final String _createTable = "CREATE TABLE validation ( jar_path CHAR(150) PRIMARY KEY, last_validated CHAR(13), validator_version CHAR(20))";
035: private static final String _selectValidated = "select last_validated, validator_version from validation where jar_path = ?";
036: private static final String _updateValidated = "update validation set last_validated = (?), validator_version = ? where jar_path = ?";
037: private static final String _insertValidated = "insert into validation (jar_path, last_validated, validator_version) values (?,?,?)";
038:
039: private static final String jdbcDriver = "org.enhydra.instantdb.jdbc.idbDriver";
040: private static final String jdbcUrl = "jdbc:idb:conf/registry.properties";
041: private static final String userName = "system";
042: private static final String password = "system";
043:
044: private Connection conn;
045:
046: private ValidationTable() {
047: try {
048:
049: ClassLoader cl = ClassLoaderUtil.getContextClassLoader();
050: Class.forName(jdbcDriver, true, cl);
051:
052: conn = getConnection();
053: } catch (Exception e) {
054: e.printStackTrace();
055: }
056: try {
057:
058: Statement stmt = conn.createStatement();
059: stmt.execute(_createTable);
060: stmt.close();
061: } catch (Exception e) {
062:
063: } finally {
064: try {
065: conn.close();
066: } catch (Exception e) {
067: }
068: }
069: }
070:
071: private Connection getConnection() throws SQLException {
072: return DriverManager.getConnection(jdbcUrl, userName, password);
073: }
074:
075: public static ValidationTable getInstance() {
076: if (table == null) {
077: table = new ValidationTable();
078: }
079:
080: return table;
081: }
082:
083: public boolean isValidated(String jarFile) {
084: try {
085: File jar = SystemInstance.get().getBase().getFile(jarFile);
086: long lastModified = jar.lastModified();
087: long lastValidated = getLastValidated(jar);
088:
089: return (lastValidated > lastModified);
090: } catch (Exception e) {
091: return false;
092: }
093: }
094:
095: public void setValidated(String jarFile) {
096: setLastValidated(jarFile, System.currentTimeMillis());
097: }
098:
099: public long getLastValidated(File jar) {
100: long validated = 0L;
101: try {
102: conn = getConnection();
103:
104: String jarFileURL = jar.toURL().toExternalForm();
105:
106: PreparedStatement stmt = conn
107: .prepareStatement(_selectValidated);
108: stmt.setString(1, jarFileURL);
109:
110: ResultSet results = stmt.executeQuery();
111: if (results.next()) {
112: String version = results.getString(2);
113:
114: if (version == null || version.equals(getVersion())) {
115: validated = results.getLong(1);
116:
117: }
118: }
119: stmt.close();
120: } catch (Exception e) {
121:
122: } finally {
123: try {
124: conn.close();
125: } catch (Exception e) {
126: }
127: }
128: return validated;
129: }
130:
131: private long _getLastValidated(String jarFileURL) {
132: long validated = 0L;
133: try {
134: conn = getConnection();
135:
136: PreparedStatement stmt = conn
137: .prepareStatement(_selectValidated);
138: stmt.setString(1, jarFileURL);
139:
140: ResultSet results = stmt.executeQuery();
141: if (results.next()) {
142: validated = results.getLong(1);
143: }
144: stmt.close();
145: } catch (Exception e) {
146:
147: } finally {
148: try {
149: conn.close();
150: } catch (Exception e) {
151: }
152: }
153: return validated;
154: }
155:
156: public void setLastValidated(String jarFile, long timeValidated) {
157: try {
158: conn = getConnection();
159: File jar = SystemInstance.get().getBase().getFile(jarFile);
160: String jarFileURL = jar.toURL().toExternalForm();
161:
162: PreparedStatement stmt = null;
163: if (_getLastValidated(jarFileURL) != 0L) {
164: stmt = conn.prepareStatement(_updateValidated);
165: stmt.setLong(1, timeValidated);
166: stmt.setString(2, getVersion());
167: stmt.setString(3, jarFileURL);
168: } else {
169: stmt = conn.prepareStatement(_insertValidated);
170: stmt.setString(1, jarFileURL);
171: stmt.setLong(2, timeValidated);
172: stmt.setString(3, getVersion());
173: }
174:
175: stmt.executeUpdate();
176: stmt.close();
177: } catch (Exception e) {
178:
179: } finally {
180: try {
181: conn.close();
182: } catch (Exception e) {
183: }
184: }
185: }
186:
187: private String version = null;
188:
189: private String getVersion() {
190: if (version == null) {
191: /*
192: * Output startup message
193: */
194: OpenEjbVersion versionInfo = OpenEjbVersion.get();
195: version = versionInfo.getVersion();
196: }
197: return version;
198: }
199: }
|