4.ServiceLocatorスタブ
XMLによるデータが必要ヒツヨウ場合バアイは、RMIやEJBのJNDIルックアップによる
リモートインターフェースの取得シュトクによるところがオオい。
ListTest というリモートインターフェースが必要ヒツヨウ場合バアイ、RMIでは以下イカのように
クライアントをく。サービスメイをパッケージ抜きのクラスメイとする。
String location = "rmi://" + hostname + "/ListTest" ;
ListTest obj=(ListTest)Naming.lookup(location); 
ホンDigesterによるXMLみクラスListTestは、
String className = "sample.util.digester." + "/ListTest" ;
ListTest obj = (ListTest)Class.forName(className).newInstanse();
RMIのサービスメイとXMLみクラスメイさえ対応タイオウがつけば、RMIをカイさずに
XMLみクラスを取得シュトクするServiceLocatorがツクれる。
それが以下イカである。
package sample.util.servicelocator;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import sample.util.digester.*;
import sample.util.db.SoftwareBean;
/**
 * This class is an implementation of the Service Locator pattern. It is used to looukup resources such as EJBHomes,
 * JMS Destinations, etc. This implementation uses the "singleton" strategy and also the "caching" strategy. This
 * implementation is intended to be used on the web tier and not on the ejb tier.
 */
public class RMIServiceLocator {
    /**  DOCUMENT ME!  */
    private static RMIServiceLocator me;
    static {
        try {
            me = new RMIServiceLocator();
        } catch(ServiceLocatorException se) {
            System.err.println(se);
            se.printStackTrace(System.err);
        }
    }
    /**  DOCUMENT ME!  */
    private Map cache; //used to hold references to EJBHomes/JMS Resources for re-use
    /**
     * Creates a new RMIServiceLocator 
object.
     *
     * @throws ServiceLocatorException DOCUMENT 
ME!
     */
    private RMIServiceLocator() throws ServiceLocatorException {
        try {
            cache = Collections.synchronizedMap(new HashMap());
        } catch(Exception e) {
            throw new ServiceLocatorException(e);
        }
    }
    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    static public RMIServiceLocator getInstance() {
        return me;
    }
    /**
     * will get the ejb Local home factory. If this ejb home factory has already been clients need to cast to the type
     * of EJBHome they desire
     *
     * @return the EJB Home corresponding to the homeName
     */
    public Remote getRemote(String remoteName) throws ServiceLocatorException {
     String className = "sample.util.digester." + remoteName;
        Remote remote = null;
        try {
            if(cache.containsKey(remoteName)) {
                remote = (Remote)cache.get(remoteName);
            } else {
                remote = (Remote)Class.forName(className).newInstance();
                cache.put(remoteName, remote);
            }
        } catch(Exception e) {
            throw new ServiceLocatorException(e);
        }
        return remote;
    }
    
    public static void main(String[] args) throws ServiceLocatorException , RemoteException{
     RMIServiceLocator rsl = new RMIServiceLocator();
    
     /* ListTest */
System.out.println( "<<ListTest>>");
long t1 = System.currentTimeMillis();
     ListTest lt = (ListTest)rsl.getRemote("ListTest");  
long t2 = System.currentTimeMillis();
System.out.println("ListTest1 " + Long.toString(t2 - t1) + " ms.");  
     List list = lt.getSoftwareBeanList();
for(int i = 0; i < list.size(); i++) {
SoftwareBean sb = (SoftwareBean)list.get(i);
System.out.println(sb);
}
/* ListTest2 */
System.out.println( "<<ListTest2>>");
t1 = System.currentTimeMillis();
lt = (ListTest)rsl.getRemote("ListTest");  
t2 = System.currentTimeMillis();
System.out.println("ListTest2 " + Long.toString(t2 - t1) + " ms.");  
list = lt.getSoftwareBeanList();
for(int i = 0; i < list.size(); i++) {
SoftwareBean sb = (SoftwareBean)list.get(i);
System.out.println(sb);
}
/* ListBeanTest */
System.out.println( "<<ListBeanTest>>");
t1 = System.currentTimeMillis();
ListBeanTest lbt = (ListBeanTest)rsl.getRemote("ListBeanTest"); 
t2 = System.currentTimeMillis();
System.out.println("ListBeanTest2 " + Long.toString(t2 - t1) + " ms."); 
ListBean lb = lbt.getListBean();
System.out.println(lb.toString() );
/* CascadeListBeanTest */
System.out.println( "<<CascadeListBeanTest>>");
t1 = System.currentTimeMillis();
CascadeListBeanTest clbt = (CascadeListBeanTest)rsl.getRemote("CascadeListBeanTest"); 
t2 = System.currentTimeMillis();
System.out.println("CascadeListBeanTest2 " + Long.toString(t2 - t1) + " ms.");
CascadeListBean clb = clbt.getCascadeListBean();
System.out.println(clb.toString() );
    
    }
}
RMIServiceLocator実行ジッコウ結果ケッカ
<<ListTest>>
ListTest1 20 ms.
PC2,FujitsuDeskPower,500,256,16,,,,,,,
PC3,FujitsuDeskPower,500,192,12,,漢字テスト,,,,あぱっち,
<<ListTest2>>
ListTest2 0 ms.
PC2,FujitsuDeskPower,500,256,16,,,,,,,
PC3,FujitsuDeskPower,500,192,12,,漢字テスト,,,,あぱっち,
<<ListBeanTest>>
ListBeanTest2 0 ms.
東京|高輪
    PC2,FujitsuDeskPower,500,256,16,,,,,,,
    PC3,FujitsuDeskPower,500,192,12,,漢字テスト,,,,あぱっち,
<<CascadeListBeanTest>>
CascadeListBeanTest2 0 ms.
<日本|Japan>
  東京|高輪
    PC2,FujitsuDeskPower,500,256,16,,,,,,,
    PC3,FujitsuDeskPower,500,192,12,,漢字テスト,,,,あぱっち,
  千葉|東金
    PC4,FujitsuDeskPower,500,256,16,,,,,,,
    PC5,FujitsuDeskPower,500,192,12,,漢字テスト,,,,あぱっち,
<まとめ>
データのXMLファイルとルールのXMLファイルがあれば、2ギョウでJavaのインスタンスにめて簡単カンタンであった。
JavaBeanのプロパティとXMLの属性ゾクセイチガったトキもマッピングできるなど、柔軟性ジュウナンセイタカい。
JAXBやRelaxerはJDBCコードやFactoryなどオオくのコードを出力シュツリョクできるので、それが必要ヒツヨウトキ以外イガイはdigesterでいであろう。
参考サンコウURL>
http://jakarta.apache.org/commons/digester/
http://www.ad.cyberhome.ne.jp/~milmil/java/digester-description.html
http://www.teria.com/~koseki/emacswiki/CommonsDigester.html
http://sapporo.cool.ne.jp/oppama/jaco/commons-digester.html