Site Search :
Standard Enterprise XML Methodology Pattern Setting Tunning Other
Article Contributors
GuestBook
Javapattern Maven
XSourceGen Dev
JetSpeed Test
JLook Image
jLook Family Site


InvokeEJB Custom Tag Library implementation
 
JSP °³¹ß½Ã EBJ tierÀÇ stateless session beanÀÇ business method È£Ãâ¿¡´ëÇÑ code logicÀ» encapsulationÇÏ´Â Custom Tag Library ¼Ò°³¸¦ Çϸç Web tier¿Í EJB tier°£ °³¹ßÀÚ roleÀ» ¸íÈ®È÷ ºÐ¸®ÇÒ¼ö ÀÖ´Â ÀåÁ¡ Á¦°øÇÏ´Â tag lib¸¦ ¼Ò°³ÇÑ´Ù. ( 2003/03/10 ) 316
Written by specular - ÀüÈ«¼º
2 of 3
 



  invokeEjb Custom Tag´Â EJBÀÇ Business method¸¦ È£ÃâÇϱâ À§ÇØ param tag·Î ºÎÅÍ passµÈ parameter Á¤º¸¸¦ ÀúÀåÇϱâ À§ÇÑ CollectionÀ» °®°í ÀÖ¾î¾ß °Ú°í, dynamic invocationÀ» Çϱâ À§ÇØ EJBMetaData interface¸¦ ÀÌ¿ëÇÑ´Ù. EJBMetaData¿¡ ´ëÇÑ »çÇ×µµ Áß¿äÇÏÁö¸¸, invokeEjb Tag¿¡¼­´Â Business method¸¦ pageContext·Î scripting variable·Î ³Ñ±â´Â ¹æ¹ýÀ» ¾Ë¾Æ¾ß °ÚÁÒ
±¸Çö ÄÚµå´Â ´ÙÀ½°ú °°½À´Ï´Ù.

package com.javapattern.servlet.jsp;

import java.io.*;
import java.util.*;
import java.lang.reflect.*;
import java.rmi.*;
import javax.rmi.*;
import javax.ejb.*;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
/** ¢Æ¢Æ¢Æ¢Æ EJB invocation Tag Handler ¢Æ¢Æ¢Æ¢Æ
 * Attribute jndiName	Home objectÀÇ jndi name
 *		context	JNDI CONTEXT FACTORY class name
 *		url	JNDI namespace location
 *		method	business method name
 *		id	return valueÀÇ scripting variable
 *		type	return valueÀÇ class type
 * <web:invokeEjb	jndiName="" context="" url="" method="" id="" type="">
 *	<web:param name="java.lang.String" value="<%= name%>"/>
 *	<web:param name="int.class" value="10"/>
 * </web:invokeEjb>
 * 		written by JeonHongSeong
 */
  
public class InvokeEJBTag extends BodyTagSupport 
{
  private String jndiName;
  private String ctx;
  private String url;
  private String method;
  private String type;
	
  private Vector argType; 
  private Vector argValue;
  private boolean flag;
  private EJBObject ejbObj;
  private Class cRemote;
	
  public InvokeEJBTag()
  {
	argType = new Vector(1,1);	
	argValue=new Vector(1,1);
  }
	
  public void setJndiName(String jndiName) 
  {
	this.jndiName=jndiName;
  }
	
  public void setContext(String ctx) 
  {
	this.ctx = ctx;	
  }
	
  public void setURL(String url) 
  {
	this.url = url;
  }
	
  public void setMethod(String method) 
  {
	this.method = method;
  }
	
  public void setType(String type)
  {
	this.type=type;
  }
	
  public int doStartTag() 
	throws JspTagException
  {
	return EVAL_BODY_BUFFERED;
  }
	
  public int doAfterBody()
	throws JspException
  {
	return SKIP_BODY;
  }

  public int doEndTag() 
	throws JspTagException
  {
	if(argType.size()!=argValue.size()) 
		throw new JspTagException("Invalid argument");
		
	// parameter list ±¸¼º
	Class[] argsType=new Class[argType.size()];
	Object[] argsValue=new Object[argValue.size()];
	for(int i=0;i<argType.size();i++) {
		argsType[i] = (Class)argType.elementAt(i);
		argsValue[i] = (Object)argValue.elementAt(i);
	}
			
	try {
		
	  Context ctx = getInitialContext();
	  Object tmp = ctx.lookup(jndiName);
	  EJBHome home = 
	  	(EJBHome)PortableRemoteObject.narrow(tmp,EJBHome.class);
		
	  EJBMetaData md = home.getEJBMetaData();
	  Class cHome = md.getHomeInterfaceClass();
	  cRemote = md.getRemoteInterfaceClass(); 
	  if(md.isStatelessSession()) {
		Class[] args1 = new Class[0];
		Method mHome = cHome.getDeclaredMethod("create",args1);
			
		//	remote object »ý¼º
		Object[] argValue1=new Object[0];
		ejbObj = (EJBObject)mHome.invoke(home,argValue1);
	  } else {
		throw new JspTagException(
				"Target EJB is not a stateless SessionBean"
				);	
	  }
	  Method bizMethod = cRemote.getDeclaredMethod(method,argsType);
	  Object result=bizMethod.invoke(ejbObj,argsValue);
	  if(result!=null) {
		pageContext.setAttribute(id,result,PageContext.PAGE_SCOPE);
	  }
	} catch(IllegalAccessException e) {
		throw new JspTagException(e.getMessage());
	} catch(InvocationTargetException e) {
		Throwable th = e.getTargetException();
		if(th instanceof RemoteException) {
		  RemoteException re = (RemoteException)th;
		  throw new JspTagException(re.detail.getMessage());
		} else {
		  throw new JspTagException(e.getMessage());	
		}					
	} catch(NamingException e) {
		throw new JspTagException(e.getMessage());
	} catch(RemoteException e) {
		Throwable th = e.detail;
		throw new JspTagException(th.getMessage());		
	} catch(NoSuchMethodException e) {
		throw new JspTagException(e.getMessage());	
	} 
	return EVAL_PAGE;
  }
	
  // JNDI InitialContext °´Ã¼¸¦ ¹Ýȯ.
  private Context getInitialContext() 
	throws NamingException
  {
	if(url==null) url="t3://127.0.0.1:7001/";
	if(ctx==null) ctx = "weblogic.jndi.WLInitialContextFactory";
	
	Properties p = new Properties();
	p.put(Context.INITIAL_CONTEXT_FACTORY, ctx);
	p.put(Context.PROVIDER_URL, url);
		return new InitialContext(p);
  }
	
  // parameter data typeÀ» ÀúÀå
  public void addArgType(Class arg) 
  {
	argType.addElement(arg);	
  }
	
  // parameter value¸¦ ÀúÀå
  public void addArgValue(Object obj) 
  {
	argValue.addElement(obj);
  }
}

  invokeEjb Custom Tag¿¡ TEI class´Â ´ÙÀ½°ú °°´Ù.

            
package com.javapattern.servlet.jsp;

import javax.servlet.jsp.tagext.TagExtraInfo;
import javax.servlet.jsp.tagext.VariableInfo;
import javax.servlet.jsp.tagext.TagData;

public class InvokeEJBTEI extends TagExtraInfo {

public VariableInfo[] getVariableInfo(TagData data) {
	String varName = data.getId();
	String className = data.getAttributeString("type");
	className = (className == null) ? "java.lang.Object" : className;
	
	VariableInfo info = 
		new VariableInfo(varName, className, true, VariableInfo.AT_BEGIN);
	VariableInfo[] result = { info };
	
	return result;
}

public boolean isValid(TagData data) {
	return true;
}
}

2001.08.03 written by Jeon HongSeong

 
 
1 2 3
References
 
http://java.sun.com/taglib
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD