2011年2月13日日曜日

H2 DatabaseとOrion SSH2を使用してSFTPでファイルをダウンロードしてBLOBとして返す関数を作成する

H2 DatabaseとOrion SSH2を使用してSFTPでファイルをダウンロードしてBLOBとして返す関数を作成するには、以下の手順を実行します。

1.以下のソースファイルをantなどでコンパイルします。
Orionssh2GetBinaryFunction.java
package com.serverarekore.h2;
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import com.trilead.ssh2.*;

public class Orionssh2GetBinaryFunction
{
static public InputStream orionssh2_get_binary(String host,
String user, String password, String path)
throws Exception
{
if( host == null ){
throw new Exception("server is not specified.");
}
if( user == null ){
throw new Exception("user is not specified.");
}
if( password == null )password = "";
if( path == null ){
throw new Exception("path is not specified.");
}

com.trilead.ssh2.Connection conn = null;
SFTPv3Client sftpc = null;

// サーバに接続
conn = new com.trilead.ssh2.Connection(host);
conn.connect();
if( !conn.authenticateWithPassword(user, password) ){
throw new Exception("authentication failed.");
}

// SFTP client作成
sftpc = new SFTPv3Client(conn);
sftpc.setCharset("UTF-8");

// ファイルをダウンロード
final File tf = File.createTempFile("lobtmp", null);
SFTPv3FileHandle fh = sftpc.openFileRO(path);

BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(tf));
byte buf[] = new byte[8192];
long fo = 0;
int sz = 0;
while((sz = sftpc.read(fh, fo, buf, 0, 8192)) != -1){
bos.write(buf, 0, sz);
fo += sz;
}
sftpc.closeFile(fh);
bos.flush();
bos.close();

InputStream ris = new InputStream(){
private final InputStream fis = new FileInputStream(tf);
private boolean deleted = false;
private final File fn = tf;
public int autoDelete(int sz) throws IOException
{
if(sz < 0 ){
close();
}
return sz;
}
public void close() throws IOException
{
if( !deleted ){
fis.close();
fn.delete();
deleted = true;
}
}
public int read(byte buf[], int off, int len) throws IOException
{
return deleted?-1:autoDelete(fis.read(buf, off, len));
}

public int read(byte buf[]) throws IOException
{
return deleted?-1:autoDelete(fis.read(buf));
}

public int read() throws IOException
{
return deleted?-1:autoDelete(fis.read());
}
};

return ris;
}
}

build.xmlのサンプル
<project name="H2Functions" default="compile" basedir=".">
<path id="lib.classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
<pathelement location="c:/Program Files/H2/bin/h2-1.3.149.jar"/>
</path>
<target name="compile">
<echo message="project: ${ant.project.name}"/>
<mkdir dir="build/" />
<javac srcdir="src/" destdir="build/"
deprecation="on" debug="on">
<classpath>
<path refid="lib.classpath"/>
</classpath>
</javac>
<jar destfile="c:/share/jars/Orionssh2GetBinary.jar"
basedir="build"/>
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>


2.システム環境変数CLASSPATHにコンパイルしてできたOrionssh2GetBinary.jarを追加し、
H2 Databaseのサービスを再起動。

3.H2 Consoleから以下のコマンドを実行して、ファンクション作成。
create alias orionssh2_get_binary for 
"com.serverarekore.h2.Orionssh2GetBinaryFunction.orionssh2_get_binary";

4.以下のようなSQLでSFTP上のファイルをBLOBに格納できます。
drop table if exists blobtest;
create table blobtest (c1 numeric(4), c2 blob);
insert into blobtest values (1,
orionssh2_get_binary(
'192.168.1.25',
'user', 'password',
'sf.png')
);


※以下のjarをCLASSPATH環境変数に追加
orion-ssh2-214.jar

○動作環境
JDK6 Update23, H2 Database 1.2.149 (2011-01-07)

○関連情報
H2 Database上でOrion SSH2を使用するユーザー定義関数のまとめ
・H2 Databaseに関する他の記事はこちらを参照してください。

0 件のコメント:

コメントを投稿