001: /*
002: * transformica 2
003: * Code generator
004: * Copyright (C) 2004 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.pavelvlasov.com/pv/content/menu.show@id=products.transformica.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.transformica;
024:
025: import java.io.File;
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.io.InputStreamReader;
029: import java.sql.PreparedStatement;
030: import java.sql.ResultSet;
031: import java.sql.SQLException;
032:
033: import biz.hammurapi.CarryOverException;
034: import biz.hammurapi.sql.ConstructorProjector;
035: import biz.hammurapi.sql.Parameterizer;
036: import biz.hammurapi.sql.RowProcessor;
037: import biz.hammurapi.sql.SQLExceptionEx;
038: import biz.hammurapi.sql.SQLProcessor;
039: import biz.hammurapi.sql.Transaction;
040: import biz.hammurapi.sql.hypersonic.HypersonicStandaloneDataSource;
041:
042: /**
043: * @author Pavel Vlasov
044: * @version $Revision: 1.1 $
045: */
046: public class HypersonicTouchDetector extends AbstractTouchDetector
047: implements TouchDetector {
048:
049: private File database;
050:
051: /**
052: *
053: */
054: public HypersonicTouchDetector(File database, File genRoot,
055: boolean ignoreTimeStamp) throws TransformicaException {
056: this .database = database;
057: this .genRoot = genRoot;
058: this .ignoreTimeStamp = ignoreTimeStamp;
059: try {
060: this .fileInfoProjector = new ConstructorProjector(
061: FileInfo.class.getConstructor(new Class[] {
062: String.class, long.class, long.class,
063: long.class }), null);
064: } catch (NoSuchMethodException e) {
065: throw new TransformicaException(e);
066: }
067: }
068:
069: protected FileInfo lookupFileInfo(final String fileName)
070: throws TransformicaException {
071: final FileInfo[] ret = { null };
072: try {
073: sqlProcessor
074: .processSelect(
075: "SELECT NAME, SIZE, CHECKSUM, LAST_MODIFIED FROM FILE WHERE NAME=?",
076: new Parameterizer() {
077: public void parameterize(
078: PreparedStatement ps)
079: throws SQLException {
080: ps.setString(1, fileName);
081: }
082: }, new RowProcessor() {
083: public boolean process(ResultSet rs)
084: throws SQLException {
085: ret[0] = (FileInfo) fileInfoProjector
086: .project(rs);
087: return false;
088: }
089: });
090: } catch (SQLException e) {
091: throw new TransformicaException(e);
092: }
093: return ret[0];
094: }
095:
096: private HypersonicStandaloneDataSource datasource;
097: private SQLProcessor sqlProcessor;
098:
099: private ConstructorProjector fileInfoProjector;
100:
101: public void init() throws TransformicaException {
102: try {
103: datasource = new HypersonicStandaloneDataSource(database
104: .getName(), new Transaction() {
105:
106: public boolean execute(SQLProcessor processor)
107: throws SQLException {
108: try {
109: String initScript = "com/pavelvlasov/transformica/HypersonicTouchDetector.sql";
110: InputStream in = getClass().getClassLoader()
111: .getResourceAsStream(initScript);
112: if (in == null) {
113: throw new IOException(
114: "Resource not found: " + initScript);
115: }
116: processor.executeScript(new InputStreamReader(
117: in));
118: } catch (IOException e) {
119: throw new SQLExceptionEx("Caused by " + e, e);
120: }
121: return true;
122: }
123:
124: });
125: sqlProcessor = new SQLProcessor(datasource, null);
126: sqlProcessor
127: .processSelect(
128: "SELECT NAME, SIZE, CHECKSUM, LAST_MODIFIED FROM FILE",
129: null, new RowProcessor() {
130: public boolean process(
131: final ResultSet rs)
132: throws SQLException {
133: if (!((FileInfo) fileInfoProjector
134: .project(rs))
135: .exists(genRoot)) {
136: sqlProcessor
137: .processUpdate(
138: "DELETE FROM FILE WHERE NAME=?",
139: new Parameterizer() {
140: public void parameterize(
141: PreparedStatement ps)
142: throws SQLException {
143: ps
144: .setString(
145: 1,
146: rs
147: .getString("NAME"));
148: }
149: });
150: }
151: return true;
152: }
153: });
154: } catch (ClassNotFoundException e) {
155: throw new TransformicaException(e);
156: } catch (SQLException e) {
157: throw new TransformicaException(e);
158: }
159: }
160:
161: public void register(final File file) throws TransformicaException {
162: try {
163: try {
164: Parameterizer parameterizer = new Parameterizer() {
165: public void parameterize(PreparedStatement ps)
166: throws SQLException {
167: try {
168: FileInfo fi = new FileInfo(genRoot, file);
169: ps.setLong(1, fi.getSize());
170: ps.setLong(2, fi.getCheckSum());
171: ps.setLong(3, fi.getLastModified());
172: ps.setString(4, fi.getName());
173: } catch (TransformicaException e) {
174: throw new CarryOverException(e);
175: }
176: }
177: };
178:
179: if (lookupFileInfo(new FileInfo(genRoot, file)
180: .getName()) == null) {
181: sqlProcessor
182: .processUpdate(
183: "INSERT INTO FILE (SIZE, CHECKSUM, LAST_MODIFIED, NAME) VALUES (?, ?, ?, ?)",
184: parameterizer);
185: } else {
186: sqlProcessor
187: .processUpdate(
188: "UPDATE FILE SET SIZE=?, CHECKSUM=?, LAST_MODIFIED=? WHERE NAME=?",
189: parameterizer);
190: }
191: } catch (CarryOverException e) {
192: throw (TransformicaException) e.getCause();
193: }
194: } catch (SQLException e) {
195: throw new TransformicaException(e);
196: }
197:
198: }
199:
200: public void destroy() throws TransformicaException {
201: if (datasource != null) {
202: datasource.shutdown();
203: }
204: }
205:
206: public static void main(String[] args) throws Exception {
207: File genRoot = new File("test_gen_root");
208: TouchDetector td = new HypersonicTouchDetector(new File(
209: "TestTouchDB"), genRoot, true);
210: td.init();
211: File menu_xml = new File(genRoot, "menu.xml");
212: System.out.println(td.isToBeGenerated(menu_xml));
213: td.register(menu_xml);
214: td.register(new File(genRoot, "menu.dtd_2"));
215: td.destroy();
216: }
217:
218: }
|