KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > persist > ScriptRunner


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.persist;
33
34 import java.io.EOFException JavaDoc;
35
36 import org.hsqldb.Database;
37 import org.hsqldb.HsqlException;
38 import org.hsqldb.Result;
39 import org.hsqldb.ResultConstants;
40 import org.hsqldb.Session;
41 import org.hsqldb.Trace;
42 import org.hsqldb.lib.IntKeyHashMap;
43 import org.hsqldb.lib.SimpleLog;
44 import org.hsqldb.lib.StopWatch;
45 import org.hsqldb.scriptio.ScriptReaderBase;
46
47 /**
48  * Restores the state of a Database instance from an SQL log file. <p>
49  *
50  * If there is an error, processing stops at that line and the message is
51  * logged to the application log. If memory runs out, an exception is thrown.
52  *
53  * @author fredt@users
54  * @version 1.8.0
55  * @since 1.7.2
56  */

57 public class ScriptRunner {
58
59     /**
60      * This is used to read the *.log file and manage any necessary
61      * transaction rollback.
62      *
63      * @throws HsqlException
64      */

65     public static void runScript(Database database, String JavaDoc logFilename,
66                                  int logType) throws HsqlException {
67
68         IntKeyHashMap sessionMap = new IntKeyHashMap();
69         Session sysSession = database.getSessionManager().getSysSession();
70         Session current = sysSession;
71         int currentId = 0;
72
73         database.setReferentialIntegrity(false);
74
75         ScriptReaderBase scr = null;
76
77         try {
78             StopWatch sw = new StopWatch();
79
80             scr = ScriptReaderBase.newScriptReader(database, logFilename,
81                                                    logType);
82
83             while (scr.readLoggedStatement(current)) {
84                 int sessionId = scr.getSessionNumber();
85
86                 if (currentId != sessionId) {
87                     currentId = sessionId;
88                     current = (Session) sessionMap.get(currentId);
89
90                     if (current == null) {
91                         current =
92                             database.getSessionManager().newSession(database,
93                                 sysSession.getUser(), false, true);
94
95                         sessionMap.put(currentId, current);
96                     }
97                 }
98
99                 if (current.isClosed()) {
100                     sessionMap.remove(currentId);
101
102                     continue;
103                 }
104
105                 Result result = null;
106
107                 switch (scr.getStatementType()) {
108
109                     case ScriptReaderBase.ANY_STATEMENT :
110                         result = current.sqlExecuteDirectNoPreChecks(
111                             scr.getLoggedStatement());
112
113                         if (result != null && result.isError()) {
114                             if (result.getException() != null) {
115                                 throw result.getException();
116                             }
117
118                             throw Trace.error(result);
119                         }
120                         break;
121
122                     case ScriptReaderBase.SEQUENCE_STATEMENT :
123                         scr.getCurrentSequence().reset(
124                             scr.getSequenceValue());
125                         break;
126
127                     case ScriptReaderBase.COMMIT_STATEMENT :
128                         current.commit();
129                         break;
130
131                     case ScriptReaderBase.INSERT_STATEMENT : {
132                         Object JavaDoc[] data = scr.getData();
133
134                         scr.getCurrentTable().insertNoCheckFromLog(current,
135                                 data);
136
137                         break;
138                     }
139                     case ScriptReaderBase.DELETE_STATEMENT : {
140                         Object JavaDoc[] data = scr.getData();
141
142                         scr.getCurrentTable().deleteNoCheckFromLog(current,
143                                 data);
144
145                         break;
146                     }
147                     case ScriptReaderBase.SCHEMA_STATEMENT : {
148                         current.setSchema(scr.getCurrentSchema());
149                     }
150                 }
151
152                 if (current.isClosed()) {
153                     sessionMap.remove(currentId);
154                 }
155             }
156         } catch (Throwable JavaDoc e) {
157             String JavaDoc message;
158
159             // catch out-of-memory errors and terminate
160
if (e instanceof EOFException JavaDoc) {
161
162                 // end of file - normal end
163
} else if (e instanceof OutOfMemoryError JavaDoc) {
164                 message = "out of memory processing " + logFilename
165                           + " line: " + scr.getLineNumber();
166
167                 database.logger.appLog.logContext(SimpleLog.LOG_ERROR,
168                                                   message);
169
170                 throw Trace.error(Trace.OUT_OF_MEMORY);
171             } else {
172
173                 // stop processing on bad log line
174
message = logFilename + " line: " + scr.getLineNumber() + " "
175                           + e.toString();
176
177                 database.logger.appLog.logContext(SimpleLog.LOG_ERROR,
178                                                   message);
179             }
180         } finally {
181             if (scr != null) {
182                 scr.close();
183             }
184
185             database.getSessionManager().closeAllSessions();
186             database.setReferentialIntegrity(true);
187         }
188     }
189 }
190
Popular Tags