Java tutorial
/* * Copyright 2004-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.lucene.store.jdbc.handler; import java.io.IOException; import java.sql.*; import org.apache.lucene.store.IndexInput; import org.apache.lucene.store.IndexOutput; import org.apache.lucene.store.jdbc.JdbcDirectory; import org.apache.lucene.store.jdbc.JdbcFileEntrySettings; import org.apache.lucene.store.jdbc.JdbcStoreException; import org.apache.lucene.store.jdbc.index.JdbcIndexConfigurable; import org.apache.lucene.store.jdbc.support.JdbcTable; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.PreparedStatementCallback; import org.springframework.jdbc.core.PreparedStatementCreator; import org.springframework.jdbc.core.PreparedStatementSetter; /** * A base file entry handler that supports most of the file entry base operations. * <p/> * Supports the creation of configurable <code>IndexInput</code> and <code>IndexOutput</code>, * base on the {@link JdbcFileEntrySettings#INDEX_INPUT_TYPE_SETTING} and * {@link JdbcFileEntrySettings#INDEX_OUTPUT_TYPE_SETTING}. * <p/> * Does not implement the deletion of files. * * @author kimchy */ public abstract class AbstractFileEntryHandler implements FileEntryHandler { protected JdbcDirectory jdbcDirectory; protected JdbcTable table; protected JdbcTemplate jdbcTemplate; public void configure(JdbcDirectory jdbcDirectory) { this.jdbcDirectory = jdbcDirectory; this.jdbcTemplate = jdbcDirectory.getJdbcTemplate(); this.table = jdbcDirectory.getTable(); } public boolean fileExists(final String name) throws IOException { return ((Boolean) jdbcTemplate.execute(table.sqlSelectNameExists(), new PreparedStatementCallback() { @Override public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { ps.setFetchSize(1); ps.setString(1, name); return ps.execute(); } })).booleanValue(); } public long fileModified(final String name) throws IOException { return ((Long) jdbcTemplate.execute(table.sqlSelecltLastModifiedByName(), new PreparedStatementCallback() { @Override public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { ps.setFetchSize(1); ps.setString(1, name); return ps.execute(); } })).longValue(); } public void touchFile(final String name) throws IOException { jdbcTemplate.update(table.sqlUpdateLastModifiedByName(), new PreparedStatementCallback() { @Override public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { ps.setFetchSize(1); ps.setString(1, name); return ps.executeUpdate(); } }); } public void renameFile(final String from, final String to) throws IOException { // TODO find a way if it can be done in the same sql query deleteFile(to); jdbcTemplate.update(table.sqlUpdateNameByName(), new PreparedStatementCallback() { @Override public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { ps.setFetchSize(1); ps.setString(1, to); ps.setString(2, from); return ps.executeUpdate(); } }); } public long fileLength(final String name) throws IOException { return ((Long) jdbcTemplate.execute(table.sqlSelectSizeByName(), new PreparedStatementCallback() { @Override public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { ps.setFetchSize(1); ps.setString(1, name); ResultSet rs = ps.executeQuery(); if (rs.next()) { return new Long(rs.getLong(1)); } return new Long(0L); } })).longValue(); } public IndexInput openInput(String name) throws IOException { IndexInput indexInput; JdbcFileEntrySettings settings = jdbcDirectory.getSettings().getFileEntrySettings(name); try { Class inputClass = settings.getSettingAsClass(JdbcFileEntrySettings.INDEX_INPUT_TYPE_SETTING, null); indexInput = (IndexInput) inputClass.newInstance(); } catch (Exception e) { throw new JdbcStoreException("Failed to create indexInput instance [" + settings.getSetting(JdbcFileEntrySettings.INDEX_INPUT_TYPE_SETTING) + "]", e); } ((JdbcIndexConfigurable) indexInput).configure(name, jdbcDirectory, settings); return indexInput; } public IndexOutput createOutput(String name) throws IOException { IndexOutput indexOutput; JdbcFileEntrySettings settings = jdbcDirectory.getSettings().getFileEntrySettings(name); try { Class inputClass = settings.getSettingAsClass(JdbcFileEntrySettings.INDEX_OUTPUT_TYPE_SETTING, null); indexOutput = (IndexOutput) inputClass.newInstance(); } catch (Exception e) { throw new JdbcStoreException("Failed to create indexOutput instance [" + settings.getSetting(JdbcFileEntrySettings.INDEX_OUTPUT_TYPE_SETTING) + "]", e); } ((JdbcIndexConfigurable) indexOutput).configure(name, jdbcDirectory, settings); return indexOutput; } public void close() throws IOException { // do nothing } }