2010年11月28日日曜日

H2 DatabaseとsardineでWebDAVサーバ上のファイルをBLOBとして返す関数を作成する

H2 DatabaseとsardineでWebDAVサーバ上のファイルをBLOBとして返す関数を作成するには、以下の手順を実行します。

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

public class SardineGetBinaryFunction
{
static public InputStream sardine_get_binary(String url,
String user, String password)
throws Exception
{
if( url == null ){
throw new Exception("url is not specified.");
}
user = (user == null)?"":user;
password = (password == null)?"":password;

Sardine sardine = SardineFactory.begin(user, password);
InputStream ris = null;

if( sardine.exists(url) == false ){
return null;
}

InputStream is = sardine.getInputStream(url);

final File tf = File.createTempFile("lobtmp", null);
FileOutputStream fos = new FileOutputStream(tf.getAbsolutePath());

byte buf[] = new byte[8192];
int rs = -1;
try
{
while( (rs = is.read(buf)) != -1 ){
fos.write(buf, 0, rs);
}
}
finally
{
if( fos != null )fos.close();
if( is != null )is.close();
}

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.146.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/SardineGetBinary.jar"
basedir="build"/>
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>


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

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

4.以下のようなコマンドでWebDAVサーバ上のファイルをBLOBに格納できます。
drop table if exists blobtest;
create table blobtest (c1 numeric(4), c2 blob);
insert into blobtest values (1,
sardine_get_binary(
'http://192.168.1.208:8080/repository/default/SF.JPG',
'user', 'password')
);

※システム環境変数CLASSPATHにtools.jarとSardine関連のjar
(sardine.jar, httpcore-4.0.1.jar, httpclient-4.0.1.jar,
commons-codec-1.4.jar, commons-logging-1.1.1.jar)を追加しておくこと。

動作環境
JDK6 Update 22, H2 Database 1.2.146 (2010-11-08), sardine-129

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

0 件のコメント:

コメントを投稿