01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.apache.lucene.store.jdbc.index;
18:
19: import java.io.IOException;
20: import java.io.InputStream;
21: import java.sql.PreparedStatement;
22:
23: import org.apache.lucene.store.jdbc.JdbcDirectory;
24: import org.apache.lucene.store.jdbc.JdbcFileEntrySettings;
25: import org.apache.lucene.store.jdbc.support.InputStreamBlob;
26: import org.apache.lucene.store.jdbc.support.JdbcTemplate;
27:
28: /**
29: * @author kimchy
30: */
31: public abstract class AbstractJdbcIndexOutput extends
32: JdbcBufferedIndexOutput {
33:
34: protected String name;
35:
36: protected JdbcDirectory jdbcDirectory;
37:
38: public void configure(String name, JdbcDirectory jdbcDirectory,
39: JdbcFileEntrySettings settings) throws IOException {
40: super .configure(name, jdbcDirectory, settings);
41: this .name = name;
42: this .jdbcDirectory = jdbcDirectory;
43: }
44:
45: public void close() throws IOException {
46: super .close();
47: final long length = length();
48: doBeforeClose();
49: jdbcDirectory.getJdbcTemplate().executeUpdate(
50: jdbcDirectory.getTable().sqlInsert(),
51: new JdbcTemplate.PrepateStatementAwareCallback() {
52: public void fillPrepareStatement(
53: PreparedStatement ps) throws Exception {
54: ps.setFetchSize(1);
55: ps.setString(1, name);
56: InputStream is = openInputStream();
57: if (jdbcDirectory.getDialect()
58: .useInputStreamToInsertBlob()) {
59: ps.setBinaryStream(2, is, (int) length());
60: } else {
61: ps.setBlob(2, new InputStreamBlob(is,
62: length));
63: }
64: ps.setLong(3, length);
65: ps.setBoolean(4, false);
66: }
67: });
68: doAfterClose();
69: }
70:
71: protected abstract InputStream openInputStream() throws IOException;
72:
73: protected void doAfterClose() throws IOException {
74:
75: }
76:
77: protected void doBeforeClose() throws IOException {
78:
79: }
80: }
|