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