leodutra
5/13/2015 - 10:18 PM

JNDI URL Lookup TagLib para WAS6

JNDI URL Lookup TagLib para WAS6

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
	<tlibversion>1.0</tlibversion>
	<jspversion>1.1</jspversion>
	<shortname>jndi-utils</shortname>
	<uri>jndi-utils</uri>
	<tag>
		<name>getURL</name>
		<tagclass>com.github.leodutra.util.tag.JNDIURLAccessTag</tagclass>
		<bodycontent>empty</bodycontent>
		<attribute>
			<name>jndi</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>removeTrailingSlash</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
	</tag>	
</taglib>
<%@ taglib uri="/WEB-INF/tld/jndi-utils.tld"   prefix="jndi-utils" %>

...

<jndi-utils:getURL removeTrailingSlash="true" jndi="WAS7IntranetRef" />

...
package com.github.leodutra.util.tag.JNDIURLAccessTag;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;



/**
 * Classe utilitária para ser utilizada nas JSP's.
 * @author Leonardo Dutra
 * @see <a href="http://gist.github.com/LeoDutra">LeoDutra's Gist @ GitHub</a>
 */
public class JNDIURLAccessTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -537522358344964789L;
	private static final String KEY_FOR_CACHED_LOOKUP = JNDIURLAccessTag.class.getCanonicalName() + serialVersionUID;
	private static final Logger LOGGER = Logger.getLogger(JNDIURLAccessTag.class);

	private boolean removeTrailingSlash;
	private String jndi;

	/**
	 * Método release
	 */
	public void release() {
		super.release();

		jndi = null;
	}

	/**
	 * Construtor da classe UtilTag.
	 */
	public JNDIURLAccessTag() {
		super();
	}

	/**
	 * Método de finalização da tag.
	 * 
	 * @throws JspException
	 *             Qualquer exceção (erro) que ocorra na chamada do método.
	 * @return SKIP_BODY para não avaliar o corpo da tag.
	 */
	public int doEndTag() throws JspException {
		return SKIP_BODY;
	}

	/**
	 * método de inicialização da tag.
	 * 
	 * @throws JspException
	 *             Qualquer exceção (erro) que ocorra na chamada do método.
	 * @return EVAL_PAGE para avaliar o restante da página.
	 */
	@SuppressWarnings("unchecked")
	public int doStartTag() throws JspException {

		try {
			
			final Object stored = pageContext.getServletContext().getAttribute(
					KEY_FOR_CACHED_LOOKUP);
			
			final Map<String, Object> cachedLookup;
			
			if (stored != null) {
				cachedLookup = (Map<String, Object>) stored;
			}
			else {
				cachedLookup = new HashMap<String, Object>();
			}
			
			String url;
			
			if (cachedLookup.containsKey(jndi)) {
				
				url = (String) cachedLookup.get( jndi);
				
			} else {
				
				url = getURL(jndi).toString();
				
				if (Boolean.TRUE.equals(removeTrailingSlash)) {
					if (url.toString().endsWith("/")){
						url = url.substring(0, url.length() - 1);
						
					}
					
				}
				
				cachedLookup.put(jndi, url);
			}
			
			pageContext.getOut().print(url);

		} catch (NamingException e) {
			LOGGER.fatal(e);
			return EVAL_PAGE;
		} catch (IOException e) {
			LOGGER.fatal(e);
			return EVAL_PAGE;
		} catch (Exception e) {
			LOGGER.fatal(e);
			return EVAL_PAGE;
		}

		return SKIP_BODY;
	}
	
	private static URL getURL(final String jndi) throws NamingException {
		return (URL) new InitialContext().lookup("java:comp/env/" + jndi);
	}



	/**
	 * @return the jndi
	 */
	public String getJndi() {
		return jndi;
	}

	/**
	 * @param jndi the jndi to set
	 */
	public void setJndi(String jndi) {
		this.jndi = jndi;
	}

	/**
	 * @return the removeTrailingSlash
	 */
	public String getRemoveTrailingSlash() {
		return String.valueOf(removeTrailingSlash);
	}

	/**
	 * @param removeTrailingSlash the removeTrailingSlash to set
	 */
	public void setRemoveTrailingSlash(String removeTrailingSlash) {
		this.removeTrailingSlash = Boolean.valueOf(removeTrailingSlash);
	}
}