2010年11月5日金曜日

H2 DatabaseとJCIFSでWindows共有上の空きスペースを返す関数を作成する

H2 DatabaseとJCIFSでWindows共有上の空きスペースを返す関数を作成するには、以下のコードを実行します。

create alias if not exists jcifs_get_freesize as $$ 
import java.io.*;
import java.sql.*;
import org.h2.tools.*;
import jcifs.smb.*;
@CODE
long jcifs_get_freesize(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.");
}

SmbFile file = new SmbFile(
"smb://" + domain + ";" + user + ":" + password +
"@" + server + "/" + path
);

return file.getDiskFreeSpace();
}
$$


実行例
select jcifs_get_freesize(
'LIFERAY1',
'test1', 'test1', 'liferay1',
'share');


※システム環境変数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 件のコメント:

コメントを投稿