2010年10月28日木曜日

H2 DatabaseとJCIFSでWindows共有上のファイルをCLOBで返す関数を作成する

H2 DatabaseとJCIFSでWindows共有上のファイルをCLOBで返す関数を作成するには、以下の手順を実行します。

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

public class JCIFSGetAsciiFunction
{
static public Reader jcifs_get_ascii(String domain,
String user, String password, String server,
String path, String filename)
throws Exception
{
if( domain == null ){
throw new Exception("domain is not specified.");
}
user = (user == null)?"":user;
password = (password == null)?"":password;
if( server == null ){
throw new Exception("server is not specified.");
}
if( path == null ){
throw new Exception("path is not specified.");
}
if( filename == null ){
throw new Exception("filename is not specified.");
}

Reader reader = null;

InputStreamReader isr = new InputStreamReader(
new SmbFileInputStream(
"smb://" + domain + ";" + user + ":" + password +
"@" + server + "/" + path + "/" + filename)
);

final File tf = File.createTempFile("lobtmp", null);
FileWriter fw = new FileWriter(tf.getAbsolutePath());

char buf[] = new char[8192];
int rs = -1;
try
{
while( (rs = isr.read(buf)) != -1 ){
fw.write(buf, 0, rs);
}
}
finally
{
fw.close();
isr.close();
}

reader = new Reader(){
private final Reader reader = new FileReader(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 ){
reader.close();
fn.delete();
deleted = true;
}
}
public int read(char buf[], int off, int len) throws IOException
{
return deleted?-1:autoDelete(reader.read(buf, off, len));
}

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

public int read() throws IOException
{
return deleted?-1:autoDelete(reader.read());
}
public int read(java.nio.CharBuffer target) throws IOException
{
return deleted?-1:autoDelete(reader.read(target));
}
};

return reader;
}
}


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


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

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


4.以下のようなコマンドでWindows共有上のファイルをCLOBに格納できます。
drop table if exists clobtest;
create table clobtest (c1 numeric(4), c2 clob);
insert into clobtest values (1,
jcifs_get_ascii('LIFERAY1',
'test1', 'test1', 'liferay1',
'share/h2', 'titles.txt')
);
select * from clobtest;


※システム環境変数CLASSPATHにtools.jarとJCIFSのjar
(jcifs-1.3.15.jar)を追加しておくこと。

動作環境
JDK6 Update 21, H2 Database 1.2.143 (2010-09-18), JCIFS 1.3.15
○関連情報
・H2 Databaseに関する他の記事はこちらを参照してください。

0 件のコメント:

コメントを投稿