jp.terasoluna.fw.web.jndi.DefaultJndiSupport.java Source code

Java tutorial

Introduction

Here is the source code for jp.terasoluna.fw.web.jndi.DefaultJndiSupport.java

Source

/*
 * Copyright (c) 2007 NTT DATA Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jp.terasoluna.fw.web.jndi;

import java.util.Map;
import java.util.Properties;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jndi.JndiLocatorSupport;

/**
 * <p>TERASOLUNA????JNDI?</p>
 *
 * <p>
 * WebAP?JNDI???<br>
 * JNDI????????Bean???
 * ?????initialize???
 * Bean????????
 * init-method?initialize???
 * <br>
 * <table border="1">
 * <caption><strong>Bean?</strong></caption>
 * <tr>
 *   <td><center><strong>???</strong></center></td>
 *   <td><center><strong></strong></center></td>
 *   <td><center><strong></strong></center></td>
 * </tr>
 * <tr>
 *   <td>factory</td>
 *   <td>JNDI???</td>
 *   <td>weblogic.jndi.WLInitialContextFactory</td>
 * </tr>
 * <tr>
 *   <td>url</td>
 *   <td>JNDI??????URI?</td>
 *   <td>t3://localhost:7001</td>
 * </tr>
 * <tr>
 *   <td>username</td>
 *   <td>JNDI?????</td>
 *   <td>weblogic</td>
 * </tr>
 * <tr>
 *   <td>password</td>
 *   <td>JNDI???</td>
 *   <td>password</td>
 * </tr>
 * </table>
 * </p>
 * <br>
 *
 * WebLogic???JNDI???java:comp/env/????
 * ??????jndiPrefix?false??<br>
 * ?false??<br>
 * <br>
 *
 * <strong>WebLogic?Bean</strong>
 * <code><pre>
 * &lt;bean id=&quot;jndiSupport&quot; scope="singleton"
 *       class=&quot;jp.terasoluna.fw.web.jndi.DefaultJndiSupport&quot;&gt;
 *       init-method="initialize"&gt;
 *   &lt;!-- ?? --&gt;
 *   &lt;property name="jndiEnvironmentMap"&gt;
 *     &lt;map&gt;
 *       &lt;entry key="factory"&gt;
 *         &lt;value&gt;weblogic.jndi.WLInitialContextFactory&lt;/value&gt;
 *       &lt;/entry&gt;
 *       &lt;entry key="url"&gt;
 *         &lt;value&gt;t3://localhost:7001&lt;/value&gt;
 *       &lt;/entry&gt;
 *       &lt;entry key="username"&gt;
 *         &lt;value&gt;weblogic&lt;/value&gt;
 *       &lt;/entry&gt;
 *       &lt;entry key="password"&gt;
 *         &lt;value&gt;password&lt;/value&gt;
 *       &lt;/entry&gt;
 *     &lt;/map&gt;
 *   &lt;/property&gt;
 *   &lt;!-- jndiPrefix? --&gt;
 *   &lt;property name="jndiPrefix"&gt;&lt;value&gt;<strong>false</strong>&lt;/value&gt;&lt;/property&gt;
 * &lt;/bean&gt;
 * </pre></code>
 * </p>
 * <strong>Tomcat?Bean</strong>
 * <code><pre>
 * &lt;bean id=&quot;jndiSupport&quot; scope="singleton"
 *       class=&quot;jp.terasoluna.fw.web.jndi.DefaultJndiSupport&quot; &gt;<br>
 *   &lt;!-- jndiPrefix??false --&gt;
 *   &lt;property name="jndiPrefix"&gt;&lt;value&gt;<strong>false</strong>&lt;/value&gt;&lt;/property&gt;
 * &lt;/bean&gt;
 * </pre></code>
 * </p>
 * <strong></strong>
 * <p>
 * Bean??????
 * <code><pre>
 * &lt;bean id="jndiLogic" scope="singleton"
 *   class="jp.sample.JndiLogic"&gt;
 *   &lt;property name=<strong>"jndiSupport"</strong>&gt;
 *     &lt;ref bean=<strong>"jndiSupport"</strong> /&gt;
 *   &lt;/property&gt;
 * &lt;/bean&gt;<br>
 * &lt;!-- JndiSupport --&gt;
 * &lt;bean id=<strong>"jndiSupport"</strong>  scope="singleton"
 *   class="jp.terasoluna.fw.web.jndi.DefaultJndiSupport" /&gt;
 * </code></pre>
 *
 * ????{@link DefaultJndiSupport}??<br>
 *
 * <code><pre>
 * public class JndiLogic {
 *   private JndiSupport <strong>jndiSupport</strong> = null;
 *
 *   public void setJndiSupport(jndiSupport) {
 *     this.jndiSupport = jndiSupport;
 *   }
 *
 *   public Object jndiLookup(String name) {
 *     return <strong>jndiSupport.lookup(name)</strong>;
 *   }
 * }
 * </code></pre>
 * </p>
 *
 */
public class DefaultJndiSupport extends JndiLocatorSupport implements JndiSupport {

    /**
     * 
     */
    private static Log log = LogFactory.getLog(DefaultJndiSupport.class);

    /**
     * JNDI??jndiEnvironmentMap????????
     */
    private static final String JNDI_FACTORY_KEY = "factory";

    /**
     * JNDI??URLjndiEnvironmentMap????????
     */
    private static final String JNDI_URL_KEY = "url";

    /**
     * JNDI??jndiEnvironmentMap????????
     */
    private static final String JNDI_USERNAME_KEY = "username";

    /**
     * JNDI??jndiEnvironmentMap????????
     */
    private static final String JNDI_PASSWORD_KEY = "password";

    /**
     * JNDI???<code>Map</code>
     */
    private Map<String, String> jndiEnvironmentMap = null;

    /**
     * ????
     * ?resourceRef??
     * ???true???"java:comp/env/"?????
     *
     * @param jndiPrefix ???
     */
    public void setJndiPrefix(boolean jndiPrefix) {
        super.setResourceRef(jndiPrefix);
    }

    /**
     * ????
     * ?resourceRef???
     * ???true???"java:comp/env/"?????
     *
     * @return jndiPrefix ???
     */
    public boolean isJndiPrefix() {
        return super.isResourceRef();
    }

    /**
     * jndiEnvironmentMap??
     * @return JNDI???<code>Map</code>
     */
    public Map<String, String> getJndiEnvironmentMap() {
        return jndiEnvironmentMap;
    }

    /**
     * jndiEnvironmentMap?
     * @param jndiEnvironmentMap JNDI???<code>Map</code>
     */
    public void setJndiEnvironmentMap(Map<String, String> jndiEnvironmentMap) {
        this.jndiEnvironmentMap = jndiEnvironmentMap;
    }

    /**
     * 
     */
    public DefaultJndiSupport() {
    }

    /**
     * JndiTemplate??
     */
    public void initialize() {

        // JNDI???????Weblogic??
        if (jndiEnvironmentMap != null) {

            // jndiEnvironmentMap???
            String factory = jndiEnvironmentMap.get(JNDI_FACTORY_KEY);
            String url = jndiEnvironmentMap.get(JNDI_URL_KEY);
            String username = jndiEnvironmentMap.get(JNDI_USERNAME_KEY);
            String password = jndiEnvironmentMap.get(JNDI_PASSWORD_KEY);

            Properties environment = new Properties();
            environment.put(Context.INITIAL_CONTEXT_FACTORY, factory);
            environment.put(Context.PROVIDER_URL, url);

            if (!"".equals(username) && username != null) {
                environment.put(Context.SECURITY_PRINCIPAL, username);
                if (password == null) {
                    password = "";
                }
                environment.put(Context.SECURITY_CREDENTIALS, password);
            }

            // ??
            getJndiTemplate().setEnvironment(environment);

            // 
            if (log.isInfoEnabled()) {
                log.info("Initialize Weblogic JNDI Resource");
                log.info(Context.INITIAL_CONTEXT_FACTORY + " = " + factory);
                log.info(Context.PROVIDER_URL + " = " + url);
                log.info(Context.SECURITY_PRINCIPAL + " = " + username);
                log.info(Context.SECURITY_CREDENTIALS + " = " + password);
            }
        }
    }

    /**
     * ????????
     * ?????
     *
     * @param name ??
     * @param obj ??
     */
    @Override
    public void rebind(String name, Object obj) {
        if (name == null || obj == null) {
            log.error("Illegal arguments error : name=" + name + ", obj=" + obj);
            throw new IllegalArgumentException();
        }
        // ???
        String jndiNameToUse = convertJndiName(name);
        try {
            getJndiTemplate().rebind(jndiNameToUse, obj);
        } catch (NamingException e) {
            log.error("Illegal JNDI context name.");
            throw new JndiException(e);
        }
    }

    /**
     * ????
     *
     * @param name ??
     */
    @Override
    public void unbind(String name) {
        if (name == null) {
            log.error("Illegal arguments error : name=" + name);
            throw new IllegalArgumentException();
        }
        // ???
        String jndiNameToUse = convertJndiName(name);
        try {
            getJndiTemplate().unbind(jndiNameToUse);
        } catch (NamingException e) {
            log.error("Illegal JNDI context name.");
            throw new JndiException(e);
        }
    }

    /**
     * ????
     *
     * @param name ??
     * @return 
     */
    @Override
    public Object lookup(String name) {
        if (name == null) {
            log.error("Illegal arguments error : name=" + name);
            throw new IllegalArgumentException();
        }
        // ???
        String jndiNameToUse = convertJndiName(name);
        try {
            return getJndiTemplate().lookup(jndiNameToUse);
        } catch (NamingException e) {
            log.error("Illegal JNDI context name.");
            throw new JndiException(e);
        }
    }
}