KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > command > dml > ScriptBase


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.command.dml;
6
7 import java.io.BufferedInputStream JavaDoc;
8 import java.io.BufferedOutputStream JavaDoc;
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.FileOutputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.io.OutputStream JavaDoc;
15 import java.sql.SQLException JavaDoc;
16
17 import org.h2.command.Prepared;
18 import org.h2.engine.Constants;
19 import org.h2.engine.Database;
20 import org.h2.engine.Session;
21 import org.h2.message.Message;
22 import org.h2.security.SHA256;
23 import org.h2.store.DataHandler;
24 import org.h2.store.FileStore;
25 import org.h2.store.FileStoreInputStream;
26 import org.h2.store.FileStoreOutputStream;
27 import org.h2.tools.CompressTool;
28 import org.h2.util.FileUtils;
29 import org.h2.util.IOUtils;
30 import org.h2.value.Value;
31
32 public class ScriptBase extends Prepared implements DataHandler {
33
34     private String JavaDoc cipher;
35     private byte[] key;
36     private FileStore store;
37     private FileOutputStream JavaDoc outStream;
38     private FileInputStream JavaDoc inStream;
39     protected OutputStream JavaDoc out;
40     protected InputStream JavaDoc in;
41     protected String JavaDoc fileName;
42     
43     private String JavaDoc compressionAlgorithm;
44
45     public ScriptBase(Session session) {
46         super(session);
47     }
48
49     public void setCipher(String JavaDoc c) {
50         cipher = c;
51     }
52
53     protected boolean isEncrypted() {
54         return cipher != null;
55     }
56
57     public void setPassword(char[] password) {
58         SHA256 sha = new SHA256();
59         key = sha.getKeyPasswordHash("script", password);
60     }
61
62     public void setFileName(String JavaDoc fileName) {
63         if(fileName == null || fileName.trim().length() == 0) {
64             fileName = "script.sql";
65         }
66         this.fileName = Constants.SCRIPT_DIRECTORY + fileName;
67     }
68
69     public boolean isTransactional() {
70         return false;
71     }
72     
73     protected void deleteStore() throws SQLException JavaDoc {
74         if(fileName != null) {
75             FileUtils.delete(fileName);
76         }
77     }
78     
79     private void initStore() throws SQLException JavaDoc {
80         byte[] magic = Database.getMagic(true);
81         Database db = session.getDatabase();
82         // script files are always in text format
83
store = FileStore.open(db, fileName, magic, cipher, key);
84         store.init();
85     }
86     
87     protected void openOutput() throws SQLException JavaDoc {
88         if(fileName == null) {
89             return;
90         }
91         if(isEncrypted()) {
92             initStore();
93             out = new FileStoreOutputStream(store, this, compressionAlgorithm);
94             // always use a big buffer, otherwise end-of-block is written a lot
95
out = new BufferedOutputStream JavaDoc(out, Constants.IO_BUFFER_SIZE_COMPRESS);
96         } else {
97             try {
98                 outStream = FileUtils.openFileOutputStream(new File JavaDoc(fileName));
99             } catch (IOException JavaDoc e) {
100                 throw Message.convert(e);
101             }
102             out = new BufferedOutputStream JavaDoc(outStream, Constants.IO_BUFFER_SIZE);
103             out = CompressTool.wrapOutputStream(out, compressionAlgorithm, Constants.SCRIPT_SQL);
104         }
105     }
106
107     protected void openInput() throws SQLException JavaDoc {
108         if(fileName == null) {
109             return;
110         }
111         if(isEncrypted()) {
112             initStore();
113             in = new FileStoreInputStream(store, this, compressionAlgorithm != null);
114         } else {
115             try {
116                 inStream = new FileInputStream JavaDoc(fileName);
117             } catch (IOException JavaDoc e) {
118                 throw Message.convert(e);
119             }
120             in = new BufferedInputStream JavaDoc(inStream, Constants.IO_BUFFER_SIZE);
121             in = CompressTool.wrapInputStream(in, compressionAlgorithm, Constants.SCRIPT_SQL);
122             if(in == null) {
123                 throw Message.getSQLException(Message.FILE_NOT_FOUND_1, Constants.SCRIPT_SQL + " in " + fileName);
124             }
125         }
126     }
127
128     protected void closeIO() {
129         IOUtils.closeSilently(out);
130         out = null;
131         IOUtils.closeSilently(in);
132         in = null;
133         if(store != null) {
134             store.closeSilently();
135             store = null;
136         }
137         IOUtils.closeSilently(inStream);
138         inStream = null;
139         IOUtils.closeSilently(outStream);
140         outStream = null;
141     }
142     
143     public boolean needRecompile() {
144         return false;
145     }
146
147     public boolean getTextStorage() {
148         // script files are always in text format
149
return true;
150     }
151
152     public String JavaDoc getDatabasePath() {
153         return null;
154     }
155
156     public FileStore openFile(String JavaDoc name, boolean mustExist) throws SQLException JavaDoc {
157         return null;
158     }
159
160     public int getChecksum(byte[] data, int start, int end) {
161         return session.getDatabase().getChecksum(data, start, end);
162     }
163
164     public void checkPowerOff() throws SQLException JavaDoc {
165         session.getDatabase().checkPowerOff();
166     }
167
168     public void checkWritingAllowed() throws SQLException JavaDoc {
169         session.getDatabase().checkWritingAllowed();
170     }
171
172     public void freeUpDiskSpace() throws SQLException JavaDoc {
173         session.getDatabase().checkWritingAllowed();
174     }
175
176     public void handleInvalidChecksum() throws SQLException JavaDoc {
177         session.getDatabase().handleInvalidChecksum();
178     }
179
180     public int compareTypeSave(Value a, Value b) throws SQLException JavaDoc {
181         throw Message.getInternalError();
182     }
183
184     public int getMaxLengthInplaceLob() {
185         return session.getDatabase().getMaxLengthInplaceLob();
186     }
187
188     public int allocateObjectId(boolean b, boolean c) {
189         return session.getDatabase().allocateObjectId(b, c);
190     }
191
192     public String JavaDoc createTempFile() throws SQLException JavaDoc {
193         return session.getDatabase().createTempFile();
194     }
195
196     public String JavaDoc getLobCompressionAlgorithm(int type) {
197         return session.getDatabase().getLobCompressionAlgorithm(type);
198     }
199     
200     public void setCompressionAlgorithm(String JavaDoc algorithm) {
201         this.compressionAlgorithm = algorithm;
202     }
203
204 }
205
Popular Tags