天天看点

一个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