2010年10月29日金曜日

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

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

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

public class JCIFSGetBinaryFunction
{
static public InputStream jcifs_get_binary(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.");
}

InputStream ris = null;

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

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


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

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


4.以下のようなコマンドでWindows共有上のファイルをBLOBに格納できます。
drop table if exists blobtest;
create table blobtest (c1 numeric(4), c2 blob);
insert into blobtest values (1,
jcifs_get_binary('LIFERAY1',
'test1', 'test1', 'liferay1',
'share', 'SF.JPG')
);


※システム環境変数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に関する他の記事はこちらを参照してください。

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に関する他の記事はこちらを参照してください。

2010年10月27日水曜日

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

H2 DatabaseとJCIFSでWindows共有上のファイルを列挙する関数を作成するには、以下のスクリプトを実行します。

create alias if not exists jcifs_list_files as $$ 
import java.io.*;
import java.sql.*;
import java.util.*;
import jcifs.smb.*;
import org.h2.tools.*;
@CODE
ResultSet jcifs_list_files(String domain,
String user, String password, String server,
String path)
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.");
}

SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("NAME", Types.VARCHAR, 512, 0);

SmbFile sfile = new SmbFile(
"smb://" + domain + ";" + user + ":" + password +
"@" + server + "/" + path + "/");
//Windows共有上の ファイルの列挙
SmbFile files[] = sfile.listFiles();
for(int fi=0;fi<files.length;fi++){
rs.addRow(files[fi].getName());
}
return rs;
}
$$


実行例
call jcifs_list_files('LIFERAY1', 
'test1', 'test1', 'liferay1', 'share/jars');


※システム環境変数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に関する他の記事はこちらを参照してください。

2010年10月26日火曜日

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルの所有者のグループ(グループID)を返す関数を作成する

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルの所有者のグループ(グループID)を返す関数を作成するには、以下のスクリプトを実行します。

create alias if not exists ftp_get_group as $$ 
import java.io.*;
import java.sql.*;
import java.util.*;
import org.apache.commons.net.ftp.*;
@CODE
String ftp_get_group(String host, Integer port,
String user, String password, String encoding,
String path, String filename)
throws Exception
{
String result = null;
if( host == null ){
throw new Exception("host is not specified.");
}
int intPort = (port == null)?21:port.intValue();
user = (user == null)?"":user;
password = (password == null)?"":password;
encoding = (encoding == null)?"UTF-8":encoding;
path = (path == null)?".":path;
if( filename == null ){
throw new Exception("filename is not specified.");
}

FTPClient fc = new FTPClient();
try
{
// 接続
fc.connect(host, intPort);
if( !FTPReply.isPositiveCompletion(fc.getReplyCode()) ){
throw new Exception("failed to connect.");
}
// ログイン
if( fc.login(user, password) == false ){
throw new Exception("failed to login.");
}
// エンコーディングの設定
fc.setControlEncoding(encoding);

// グループ(グループID)の取得
FTPFile files[] = fc.listFiles(filename);
if( files.length > 1 ){
throw new Exception("too many files!");
}
result = files[0].getGroup();
// ログアウト
fc.logout();
}
finally
{
if( fc.isConnected() ){
fc.disconnect();
}
}

return result;
}
$$


実行例
call ftp_get_group('192.168.1.122', 21, 
'ftptest1', 'ftptest1',
'UTF-8', '.', 'SF.JPG');


※システム環境変数CLASSPATHにtools.jarとApache Commons Netのjar
(commons-net-2.0.jar, commons-net-ftp-2.0.jar)を追加しておくこと。

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

2010年10月25日月曜日

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルの所有者のユーザ(ユーザID)を返す関数を作成する

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルの所有者のユーザ(ユーザID)を返す関数を作成するには、以下のスクリプトを実行します。

create alias if not exists ftp_get_user as $$ 
import java.io.*;
import java.sql.*;
import java.util.*;
import org.apache.commons.net.ftp.*;
@CODE
String ftp_get_user(String host, Integer port,
String user, String password, String encoding,
String path, String filename)
throws Exception
{
String result = null;
if( host == null ){
throw new Exception("host is not specified.");
}
int intPort = (port == null)?21:port.intValue();
user = (user == null)?"":user;
password = (password == null)?"":password;
encoding = (encoding == null)?"UTF-8":encoding;
path = (path == null)?".":path;
if( filename == null ){
throw new Exception("filename is not specified.");
}

FTPClient fc = new FTPClient();
try
{
// 接続
fc.connect(host, intPort);
if( !FTPReply.isPositiveCompletion(fc.getReplyCode()) ){
throw new Exception("failed to connect.");
}
// ログイン
if( fc.login(user, password) == false ){
throw new Exception("failed to login.");
}
// エンコーディングの設定
fc.setControlEncoding(encoding);

// ユーザ(ユーザID)の取得
FTPFile files[] = fc.listFiles(filename);
if( files.length > 1 ){
throw new Exception("too many files!");
}
result = files[0].getUser();
// ログアウト
fc.logout();
}
finally
{
if( fc.isConnected() ){
fc.disconnect();
}
}

return result;
}
$$


実行例
call ftp_get_user('192.168.1.122', 21, 
'ftptest1', 'ftptest1',
'UTF-8', '.', 'SF.JPG');


※システム環境変数CLASSPATHにtools.jarとApache Commons Netのjar
(commons-net-2.0.jar, commons-net-ftp-2.0.jar)を追加しておくこと。

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

2010年10月24日日曜日

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルのタイムスタンプを返す関数を作成する

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルのタイムスタンプを返す関数を作成するには、以下のスクリプトを実行します。

create alias if not exists ftp_get_timestamp as $$ 
import java.io.*;
import java.sql.*;
import java.util.*;
import org.apache.commons.net.ftp.*;
@CODE
Timestamp ftp_get_timestamp(String host, Integer port,
String user, String password, String encoding,
String path, String filename)
throws Exception
{
Timestamp result = null;
if( host == null ){
throw new Exception("host is not specified.");
}
int intPort = (port == null)?21:port.intValue();
user = (user == null)?"":user;
password = (password == null)?"":password;
encoding = (encoding == null)?"UTF-8":encoding;
path = (path == null)?".":path;
if( filename == null ){
throw new Exception("filename is not specified.");
}

FTPClient fc = new FTPClient();
try
{
// 接続
fc.connect(host, intPort);
if( !FTPReply.isPositiveCompletion(fc.getReplyCode()) ){
throw new Exception("failed to connect.");
}
// ログイン
if( fc.login(user, password) == false ){
throw new Exception("failed to login.");
}
// エンコーディングの設定
fc.setControlEncoding(encoding);

// タイムスタンプの取得
FTPFile files[] = fc.listFiles(filename);
if( files.length > 1 ){
throw new Exception("too many files!");
}
result = new Timestamp(
files[0].getTimestamp().getTime().getTime() +
TimeZone.getDefault().getOffset(new java.util.Date().getTime()));
// ログアウト
fc.logout();
}
finally
{
if( fc.isConnected() ){
fc.disconnect();
}
}

return result;
}
$$


実行例
call ftp_get_timestamp('192.168.1.122', 21, 
'ftptest1', 'ftptest1',
'UTF-8', '.', 'SF.JPG');


※システム環境変数CLASSPATHにtools.jarとApache Commons Netのjar
(commons-net-2.0.jar, commons-net-ftp-2.0.jar)を追加しておくこと。

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

2010年10月23日土曜日

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルのサイズを返す関数を作成する

H2 DatabaseとApache Commons NetでFTPサーバ上のファイルのサイズを返す関数を作成するには、以下のスクリプトを実行します。

create alias if not exists ftp_get_filesize as $$ 
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import org.h2.value.*;
import org.apache.commons.net.ftp.*;
@CODE
long ftp_get_filesize(String host, Integer port,
String user, String password, String encoding,
String path, String filename)
throws Exception
{
long result = -1;
if( host == null ){
throw new Exception("host is not specified.");
}
int intPort = (port == null)?21:port.intValue();
user = (user == null)?"":user;
password = (password == null)?"":password;
encoding = (encoding == null)?"UTF-8":encoding;
path = (path == null)?".":path;
if( filename == null ){
throw new Exception("filename is not specified.");
}

FTPClient fc = new FTPClient();
try
{
// 接続
fc.connect(host, intPort);
if( !FTPReply.isPositiveCompletion(fc.getReplyCode()) ){
throw new Exception("failed to connect.");
}
// ログイン
if( fc.login(user, password) == false ){
throw new Exception("failed to login.");
}
// エンコーディングの設定
fc.setControlEncoding(encoding);

// ファイルサイズの取得
FTPFile files[] = fc.listFiles(filename);
if( files.length > 1 ){
throw new Exception("too many files!");
}
result = files[0].getSize();
// ログアウト
fc.logout();
}
finally
{
if( fc.isConnected() ){
fc.disconnect();
}
}

return result;
}
$$


実行例
call ftp_get_filesize('192.168.1.122', 21, 
'ftptest1', 'ftptest1',
'UTF-8', '.', 'SF.JPG');


※システム環境変数CLASSPATHにtools.jarとApache Commons Netのjar
(commons-net-2.0.jar, commons-net-ftp-2.0.jar)を追加しておくこと。

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