⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jndiutil.java

📁 struts+spring+hibernate自创框架
💻 JAVA
字号:
/*********************************************************************************
 FILE:      JNDIUtil.java
 PACKAGE:   net.ema.crms.common.util

 Copyright(C)2004 E-MA Logistics Systems (Shanghai) Limited.

 History:
 2003-11-7:Arthur Chen
 Create.
 *********************************************************************************/
package com.pegasus.framework.util;


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

/**
 * JNDI utilities.
 *
 * @author Arthur Chen
 * @version $Id: JNDIUtil.java,v 1.1 2005/07/29 08:25:02 rcen Exp $
 */
public class JNDIUtil {
    /**
     * Private constructor to prevent instances of this class.
     */
    private JNDIUtil() {
    }

    /**
     * Bind the object in the JNDITree. Will create needed subcontext's if missing.
     * If the object already exists it will be rebinded.
     *
     * @param jndiName the jndi name where the object should be bound.
     * @param obj      the object to bind.
     */
    public static void bindContext(String jndiName, Object obj) {
        try {
            Context ctx = new InitialContext();


            //create subcontexts
            int count = StringUtil.countDelim(jndiName, "/");
            for (int i = 0; i < count; i++) {
                String prefix = StringUtil.getStringAt(jndiName, "/", i);
                try {
                    ctx.lookup(prefix);
                } catch (NamingException e) {
                    ctx.createSubcontext(prefix);
                }
            }

            // bind object
            try {
                ctx.lookup(jndiName);
                // already exists, so rebind
                ctx.rebind(jndiName, obj);
            } catch (NamingException e) {
                // none exists so bind a new
                ctx.bind(jndiName, obj);
            }

            ctx.close();

        } catch (NamingException ne) {
            throw  new RuntimeException("Error binding object to " + jndiName, ne);
        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -