2009-03-10
シングルトンでキャッシュ処理
シングルトン化することにより、システム内で共通のキャッシュデータを参照させることができる。
マルチスレッドの場合は、パフォーマンスの低下を考慮しつつ、getInstanceにsynchronizedをつけたほうがいいかも。
・キャッシュを保持するクラス
public class TestCache {
// このクラスに唯一のインスタンス
private static TestCache instance = new TestCache();
private TestCache() {
}
// MacAddressキャッシュ
private String macAddress = null;
// インスタンス取得メソッド
public static TestCache getInstance() {
return instance;
}
// MacAddressキャッシュ取得
public String getMacAddress () {
return macAddress;
}
// MacAddressキャッシュ設定
public void setMacAddress (String macAddress) {
this.macAddress = macAddress;
}
}
・キャッシュを取り出す処理
//キャッシュ保持クラス
TestCache testCache = TestCache.getInstance();
MacAddress = testCache.getMacAddress();
if (MacAddress == null) {
/* MACアドレスを取得 */
WinConfig wcfg = new WinConfig();
MacAddress = wcfg.getMacAddress();
// キャッシュにMacAddressを登録
certCache.setMacAddress(MacAddress);
}
参考URL:
http://www.nulab.co.jp/designPatterns/designPatterns2/designPatterns2-1.html
2009-02-06
2009-02-05
q4e(maven2プラグイン)はすごい!&pom.xml
pom.xmlもメモ。
q4eは使いやすい。
maven2の設定のわずらわしさがかなり軽減された感がある。
eclipse公認のプラグインになるらしいから、来年くらいから本格的にmaven2が普及するのかな?
jarファイルを作成するだけだったら、プロジェクトを右クリック⇒ポップアップからMaven2にカーソルを合わせる⇒「アーティファクトのパッケージ」でよい。
「アーティファクトのデプロイ」だとスナップショットをアップロードするサーバを指定&用意しないといちいちおこられる。
⇒http://d.hatena.ne.jp/uriyuri/20081111/1226361500
Maven2でStruts環境がすぐにつくれるpom.xmlとか、
⇒http://hondou.homedns.org/pukiwiki/pukiwiki.php?JavaEE%20Struts#e63ac6e9
すげ〜な〜。
Maven2使ってる人のHPはどこ見ても「慣れるまで大変だけど1度使ったらAntに戻れない」って言ってるけど、わかるわぁ・・・
・pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Maven2</groupId>
<artifactId>Maven2</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Maven2</name>
<url>http://maven.apache.org</url>
<!-- ディペンデンシー -->
<dependencies>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- H2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.1.107</version>
</dependency>
</dependencies>
<!-- Manifestの設定 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>Maven2.JDBCTest</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- 依存ライブラリを指定ディレクトリにコピー -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies-lib</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>lib</outputDirectory>
</configuration>
</execution>
<execution>
<id>copy-dependencies-target-lib</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>target/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!-- スナップショットの設定 -->
<distributionManagement>
<repository>
<uniqueVersion>false</uniqueVersion>
<id>http-basic-repository</id>
<name>Hoge repo</name>
<url>http://localhost/maven2</url>
</repository>
<snapshotRepository>
<uniqueVersion>true</uniqueVersion>
<id>http-basic-repository</id>
<name>Hoge repo snapshot</name>
<url>http://localhost/maven2/maven2-snapshot</url>
</snapshotRepository>
</distributionManagement>
</project>
MANIFEST.MFとbuild.xml
恥ずかしながら、MANIFEST.MFとbuild.xmlを1から書いたことが無かったのでメモ
・MANIFEST.MF
Manifest-Version: 1.0 Main-Class: Maven2.JDBCTest Class-Path: ./lib/h2-1.1.107.jar
<?xml version="1.0" encoding="Shift_JIS"?>
<project name="TEST_H2" default="jar" basedir=".">
<!--
******************************************************************************
プロパティ
******************************************************************************
-->
<!-- ソースファイルがあるディレクトリ -->
<property name="src.dir" value="src"/>
<!-- ライブラリがあるディレクトリ -->
<property name="lib.dir" value="lib"/>
<!-- MP3があるディレクトリ -->
<property name="sound.dir" value="sound"/>
<!-- 画像ファイルがあるディレクトリ -->
<property name="image.dir" value="image"/>
<!-- ビルド時に使用するワーク・ディレクトリ -->
<property name="build.dir" value="build"/>
<!-- クラスファイルを出力するディレクトリ -->
<property name="classes.dir" value="classes"/>
<!-- jarファイル名 -->
<property name="jar.name" value="H2Test.jar"/>
<!-- META-INFディレクトリ -->
<property name="meta-inf.dir" value="META-INF"/>
<!-- MFディレクトリ -->
<property name="mf.dir" value="./"/>
<!-- deploy -->
<property name="deploy.dir" value="C:\Users\t-fujisaki\Desktop\H2Test"/>
<!-- debug -->
<property name="debug" value="on"/>
<!-- クラスパス -->
<path id="project.class.path">
<pathelement path="${lib.dir}/h2-1.1.107.jar"/>
<pathelement path="${lib.dir}/junit-3.8.1.jar"/>
</path>
<!--
******************************************************************************
ターゲット定義
******************************************************************************
-->
<!-- ワークディレクトリの作成 -->
<target name="setup">
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.dir}/${meta-inf.dir}"/>
</target>
<!-- コンパイル -->
<target name="compile" depends="setup">
<javac srcdir="${src.dir}" destdir="${build.dir}" debug="${debug}">
<classpath refid="project.class.path"/>
<include name="**/**.java"/>
</javac>
</target>
<!-- jarファイルの作成 -->
<target name="jar" depends="compile">
<jar jarfile="${jar.name}" basedir="${build.dir}" manifest="${mf.dir}manifest.mf"></jar>
</target>
<!-- デプロイ -->
<target name="deploy" depends="jar">
<copy todir="${deploy.dir}/${sound.dir}">
<fileset dir="${sound.dir}" includes="*.*"/>
</copy>
<copy todir="${deploy.dir}/${image.dir}">
<fileset dir="${image.dir}" includes="*.*"/>
</copy>
<copy todir="${deploy.dir}/${lib.dir}">
<fileset dir="${lib.dir}" includes="*.*"/>
</copy>
<copy file="${jar.name}" todir="${deploy.dir}" overwrite="yes"/>
</target>
<!-- 生成したファイルやフォルダの削除 -->
<target name="clean">
<delete dir="${build.dir}"/>
<delete file="${jar.name}" />
</target>
</project>
2009-02-04
h2Databaseを使ったJDBCのサンプルコード
公開する程のものではないが貼り付けてみる。
・JDBCTest.java
package Maven2;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.h2.tools.Server;
public class JDBCTest {
private static String installPath = "/h2_1_1_107_beta/bin";
public static void main(String[] args) {
Connection con = null;
Server s = null;
try {
/* 起動方法をコマンドプロンプト方式より変更した */
s = Server.createTcpServer( new String[] { "-tcpPort", "9092", "-baseDir ", installPath});
// データベース起動
s.start();
// DB接続
Class.forName("org.h2.Driver");
con = DriverManager.getConnection("jdbc:h2:./data/database/H2Test;TRACE_LEVEL_FILE=0", "sa", null);
// オートコミットオフ
con.setAutoCommit(false);
PreparedStatement preState = null;
try {
// SQL(DROP TABLE)発行
preState = con.prepareStatement("drop table test;");
preState.executeUpdate();
preState = con.prepareStatement("drop table blob_test;");
preState.executeUpdate();
} catch (SQLException se) {
// 何もしない
System.out.println("テーブルは作られていなかった模様");
}
// SQL(CREATE TABLE)発行
preState = null;
preState = con.prepareStatement(
"create table test(" +
"id numeric not null primary key," +
"name varchar2(30) not null);");
preState.executeUpdate();
// SQL(CREATE TABLE(バイナリテスト用))発行
preState = con.prepareStatement(
"create table blob_test(" +
"key int primary key," +
"binary BLOB);");
preState.executeUpdate();
// SQL(INSERT)発行
for (int i = 0; i < 1000; i ++) {
preState = con.prepareStatement("insert into test values (" + i + "" + ",'red_" + i + "');");
preState.executeUpdate();
}
con.commit();
// SQL(SELECT)発行
preState = con.prepareStatement("select * from test;");
ResultSet rs = preState.executeQuery();
while (rs.next()) {
String result = rs.getString(1);
String result2 = rs.getString(2);
System.out.println("Result = " + result + ", " + result2);
}
con.commit();
// バイナリをDBに保存
preState = con.prepareStatement("insert into blob_test values(1 ,?);");
File file = new File("/test.jpg");
FileInputStream input = new FileInputStream(file);
preState.setBinaryStream(1,input,(int)file.length());
preState.executeUpdate();
con.commit();
// バイナリをDBからファイルに出力
preState = con.prepareStatement("select binary from blob_test where key=1");
rs = preState.executeQuery();
while(rs.next()){
File fileOut = new File("/test_out.jpg");
Blob content = rs.getBlob("binary");
InputStream in = content.getBinaryStream();
OutputStream out = new FileOutputStream(fileOut);
byte[] buffer = new byte[4096];
while(true){
int len = in.read(buffer);
if(len < 0) break;
out.write(buffer, 0, len);
}
in.close();
out.close();
}
rs.close();
preState.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
// DBを接続解除
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
// データベース停止
s.stop();
}
}
}
2009-02-02
h2とaxis2とPHPで、
何かできないか?
と、画策してみる。
h2(2009/2/2現在:最新Ver⇒1.1.107beta)
⇒http://www.h2database.com/html/main.html
AXIS2(2009/2/2現在:最新Ver⇒1.4.1)
⇒http://ws.apache.org/axis2/download/1_4_1/download.cgi
