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

0 件のコメント:

コメントを投稿