天天看點

一個ServiceLocator模式的實作

一個ServiceLocator模式的實作

import javax.naming.*;

一個ServiceLocator模式的實作

import javax.naming.namingexception;

一個ServiceLocator模式的實作

import javax.rmi.portableremoteobject;

一個ServiceLocator模式的實作

import javax.ejb.ejbhome;

一個ServiceLocator模式的實作

import javax.ejb.ejblocalhome;

一個ServiceLocator模式的實作

import javax.sql.datasource;

一個ServiceLocator模式的實作

import java.util.*;

一個ServiceLocator模式的實作

import java.sql.*;

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

/**

一個ServiceLocator模式的實作

 *  實作 service locater 模式,用于由用戶端來調用以通過jndi查

一個ServiceLocator模式的實作

 *  找相關的 ejb或是其它服務的入口.

一個ServiceLocator模式的實作

 * */

一個ServiceLocator模式的實作

public final class servicelocater {

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

  protected static servicelocater inst = new servicelocater();

一個ServiceLocator模式的實作

  private initialcontext ic = null;

一個ServiceLocator模式的實作

  private map ejbhomecache = null;

一個ServiceLocator模式的實作

  private map datasourcecache = null;

一個ServiceLocator模式的實作

  protected servicelocater() {

一個ServiceLocator模式的實作

    try {

一個ServiceLocator模式的實作

      datasourcecache = collections.synchronizedmap(new hashmap());

一個ServiceLocator模式的實作

      ejbhomecache = collections.synchronizedmap(new hashmap());

一個ServiceLocator模式的實作

      ic = new initialcontext();

一個ServiceLocator模式的實作

    }

一個ServiceLocator模式的實作

    catch (exception e) {

一個ServiceLocator模式的實作

      e.printstacktrace();

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

  }

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

  /**

一個ServiceLocator模式的實作

   * 取得 servicelocater的單子執行個體.

一個ServiceLocator模式的實作

   * */

一個ServiceLocator模式的實作

  synchronized public static servicelocater getinstance() {

一個ServiceLocator模式的實作

    return inst;

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

   *查找并傳回一個資料源

一個ServiceLocator模式的實作

   * @param name string 資料源名稱

一個ServiceLocator模式的實作

   * @return datasource ,查找不到則抛出異常.

一個ServiceLocator模式的實作

   * @throws namingexception ,查找不到或是類型不對。

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

  private datasource lookupdatasource(string name) throws namingexception {

一個ServiceLocator模式的實作

    datasource tmpds = (datasource)this.datasourcecache.get(name);

一個ServiceLocator模式的實作

    if (tmpds == null) {

一個ServiceLocator模式的實作

      try {

一個ServiceLocator模式的實作

        tmpds = (datasource)this.ic.lookup(name);

一個ServiceLocator模式的實作

        this.datasourcecache.put(name, tmpds);

一個ServiceLocator模式的實作

      }

一個ServiceLocator模式的實作

      catch (namingexception name) {

一個ServiceLocator模式的實作

        throw name;

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

      catch (exception othere) {

一個ServiceLocator模式的實作

        throw new namingexception(othere.getmessage());

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

    return tmpds;

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

   * 查找并傳回一個遠端接口

一個ServiceLocator模式的實作

   * @param jndihomename ebj名字

一個ServiceLocator模式的實作

   * @param classname  ejb類名字

一個ServiceLocator模式的實作

   * @return

一個ServiceLocator模式的實作

   * @throws servicelocatorexception

一個ServiceLocator模式的實作

   */

一個ServiceLocator模式的實作

  public ejbhome getremotehome(string jndihomename, class classname) throws

一個ServiceLocator模式的實作

      servicelocatorexception {

一個ServiceLocator模式的實作

    ejbhome home = (ejbhome)this.ejbhomecache.get(jndihomename);

一個ServiceLocator模式的實作

    if (home == null) {

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

        object objref = ic.lookup(jndihomename);

一個ServiceLocator模式的實作

        object obj = portableremoteobject.narrow(objref, classname);

一個ServiceLocator模式的實作

        home = (ejbhome) obj;

一個ServiceLocator模式的實作

        this.ejbhomecache.put(jndihomename, home);

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

      catch (namingexception ne) {

一個ServiceLocator模式的實作

        throw new servicelocatorexception(ne);

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

      catch (exception e) {

一個ServiceLocator模式的實作

        throw new servicelocatorexception(e);

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

    return home;

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

   * 查找并傳回一個本地接口

一個ServiceLocator模式的實作

   * @param jndihomename  jndihomename名字

一個ServiceLocator模式的實作

   * @return 一個本地接口

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

  public ejblocalhome getlocalhome(string jndihomename) throws

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

    ejblocalhome home = null;

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

      home = (ejblocalhome) ic.lookup(jndihomename);

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

    catch (namingexception ne) {

一個ServiceLocator模式的實作

      throw new servicelocatorexception(ne);

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

      throw new servicelocatorexception(e);

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

   *查找一個資料源,并取得一個連接配接.

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

  public connection getconnection(string datasourcejndiname) throws

一個ServiceLocator模式的實作

      sqlexception {

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

      connection conn = this.lookupdatasource(datasourcejndiname).getconnection();

一個ServiceLocator模式的實作

      conn.setautocommit(false);

一個ServiceLocator模式的實作

      //conn.settransactionisolation(connection.transaction_read_committed);

一個ServiceLocator模式的實作

      return conn;

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

      throw new sqlexception(e.getmessage());

一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作
一個ServiceLocator模式的實作

}

文章轉自莊周夢蝶  ,原文釋出5.16