小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Java技術(shù)回顧之JNDI(二):JNDI應(yīng)用實(shí)例,Java技術(shù)文章,Java系列教程,Java

 汲取者 2010-04-22

♦日志服務(wù)對(duì)象工廠類 SimpleLogServiceFactory.java

package xyz.serviceprovider;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.Name;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;

public class SimpleLogServiceFactory implements ObjectFactory {

 public Object getObjectInstance(Object obj, Name name, Context ctx,
   Hashtable<?, ?> env) throws Exception {
  if(obj instanceof Reference){
   return new SimpleLogService();
  }
  return null;
 }
}

 4、 JNDI容器和JNDI客戶端
最后,我們?cè)贘NDI應(yīng)用package(xyz.jndi)中實(shí)現(xiàn)一個(gè)JNDI容器 JNDIContainer和一個(gè)JNDI客戶端應(yīng)用JNDIClient。
JNDIContainer在內(nèi)部使用文件系統(tǒng)服務(wù)提供者 fscontext來(lái)提供命名和目錄服務(wù),配置文件JNDIContainer.properties是服務(wù)注入場(chǎng)所,供配置DBService和 LogService實(shí)現(xiàn)。

♦JNDI容器類 JNDIContainer.java

package xyz.jndi;

import java.io.InputStream;
import java.util.Hashtable;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import xyz.service.DBService;
import xyz.service.LogService;

public class JNDIContainer {

 private Context ctx=null;
 
 public void init() throws Exception{
     //初始化JNDI提供者。
     Hashtable env = new Hashtable();
     env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
     env.put(Context.PROVIDER_URL, "file:/c:/sample"); //fscontext的初始目錄,我們需要在c:\下創(chuàng)建sample目錄。
     ctx=new InitialContext(env);
     loadServices();
 }
 
 //從配置文件 JNDIContainer.properties中讀取DBService和LogService實(shí)現(xiàn),綁定到Context中。
 private void loadServices() throws Exception{
  InputStream in=getClass().getResourceAsStream("JNDIContainer.properties");
  Properties props=new Properties();
  props.load(in);
  
  //inject dbservice
  String s=props.getProperty("DBServiceClass");
  Object obj=Class.forName(s).newInstance();
  if(obj instanceof DBService){
   DBService db=(DBService)obj;
   String[] ss=props.getProperty("DBServiceProperty").split(";");
   for(int i=0;i<ss.length;i++)
    db.setProperty(i, ss[i]);
   ctx.rebind(props.getProperty("DBServiceName"), db);
  }
  
  //inject logservice
  s=props.getProperty("LogServiceClass");
  obj=Class.forName(s).newInstance();
  if(obj instanceof LogService){
   LogService log=(LogService)obj;
   ctx.rebind(props.getProperty("LogServiceName"), log);
  }
 }
 
 public void close() throws NamingException{
  ctx.close();
 }
 
 public Context getContext(){
  return ctx;
 }
}

♦JNDI 容器配置文件 JNDIContainer.properties

//和 JNDIContainer.java文件位于同一目錄
DBServiceName=DBService
DBServiceClass=xyz.serviceprovider.SimpleDBService
DBServiceProperty=mydb//192.168.1.2:8421/testdb;start

LogServiceName=LogService
LogServiceClass=xyz.serviceprovider.SimpleLogService

♦JNDI 客戶端 JNDIClient.java

package xyz.jndi;

import javax.naming.Context;

import xyz.service.DBService;
import xyz.service.LogService;

public class JNDIClient {
 
 public static void main(String[] args){
  try{
   JNDIContainer container=new JNDIContainer();
   container.init();
   
      //JNDI客戶端使用標(biāo)準(zhǔn)JNDI接口訪問(wèn)命名服務(wù)。
      Context ctx=container.getContext();
      DBService db=(DBService)ctx.lookup("DBService");
      System.out.println("db location is:"+db.getLocation()+",state is:"+db.getState());
      db.accessDB();
     
      LogService ls=(LogService)ctx.lookup("LogService");
      ls.log("this is a log message.");
     
      container.close();
  }
  catch(Exception e){
   e.printStackTrace();
  }
 }
}

至此,我們的整個(gè)Java SE應(yīng)用已經(jīng)完成。下面是整個(gè)應(yīng)用的源代碼結(jié)構(gòu)(Eclipse工程):

jndisample project 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多