Weblogic中Struts2与EJB3.0调整

Weblogic中Struts2与EJB3.0整合

  

           struts2-ejb3-plugin 该plugin提供struts2的Action及Interceptor对EJB组件及Resource的无侵入式依赖注入(DI)。实现Struts2与EJB3的整合。需要注意的是,在Action的execute(或者自定义的名称)方法中同时使用struts2的Interceptor和@Interceptors时,@Interceptors会在Interceptor之前开始,在Interceptor之后结束。

目前的版本暂时不支持@PreDestroy。 现阶段只有 jboss与glassfish2.x 应用服务器的实现,在以后的版本中会陆续增加如 weblogic 等应用服务器的实现。如果现在需要 jboss与glassfish2.x之外的实现.

 

    由于  struts2-ejb3-plugin 不提供最weblogic的EJB3的支持,可以通过 自定义注释,通过在EJB3客户端对象添加自定义注释,在通过拦截器进行查找注入.

自定义注释如下:

/**
 * 
 * <p>EJB注入的注解类<p>
 *
 * 创建日期2013-1-9<br>
 * @author  longgangbai<br>
 * @version $Revision: 37 $ 2013-1-9
 * @since   3.0.0
 */
@Retention(RetentionPolicy.RUNTIME)
@Target( { ElementType.FIELD })
public @interface EJBRef
{
	/**
	 * 解析EJB的JNDI的名字
	 * 
	 * @return 解析EJB的名字
	 */
	String mappedName();
}

 

/**
 * 
 * <p>通过Struts2的拦截器诸如相关的EJB stub对象<p>
 *
 * 创建日期2013-1-9<br>
 * @author  longgangbai<br>
 * @version $Revision: 144 $ 2013-1-9
 * @since   3.0.0
 */
public class InjectorManagerInterceptor extends AbstractInterceptor {
    //
    private static final long serialVersionUID = -4065712251841048146L;
    private static Context context = null;
    /**
     * 服务类客户端存根
     */
    private static Map<String, Object> icache = new ConcurrentHashMap<String, Object>();


    static {
        try {
            if (context == null) {
                PropUtils prop = new PropUtils();
                // Properties p = new Properties();
                // p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
                // p.put(Context.PROVIDER_URL, "t3://localhost:7001");
                // p.put(Context.SECURITY_PRINCIPAL, "weblogic");
                // p.put(Context.SECURITY_CREDENTIALS, "weblogic");
                // context=new InitialContext(prop.getProperties());
                Properties p = new Properties();
                p.put(Context.INITIAL_CONTEXT_FACTORY, prop.getString(Context.INITIAL_CONTEXT_FACTORY));
                p.put(Context.URL_PKG_PREFIXES,  prop.getString(Context.URL_PKG_PREFIXES));
                p.put(Context.PROVIDER_URL,  prop.getString(Context.PROVIDER_URL));
//                p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
//                p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
//                p.put(Context.PROVIDER_URL, "jnp://10.120.1.251:1099");
                context = new InitialContext(p);
            }
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public String intercept(ActionInvocation actionInvocation) throws Exception {
        // log.info("调用注解管理,注入EJB Stub);
        Object fromObject = actionInvocation.getAction();
        Class<? extends Object> fromClass = fromObject.getClass();
        // System.out.println(fromClass);
        try {
            Field[] fields = fromClass.getDeclaredFields();
            // 获得基类的成员变
            if (fields.length == 0) {
                fields = fromClass.getSuperclass().getDeclaredFields();
            }
            for (Field field : fields) {
                // 处理EJBRef注解
                if (field.isAnnotationPresent(EJBRef.class)) {
                    boolean access = field.isAccessible();
                    field.setAccessible(true);
                    setEJBRef(fromObject, field);
                    field.setAccessible(access);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return actionInvocation.invoke();
    }

    /**
     * 
     * @param fromObject
     * @param field
     * @throws IllegalAccessException
     * @throws NamingException 
     */
    private void setEJBRef(Object fromObject, Field field) throws IllegalAccessException, NamingException {
        String ejbrefJndiName = determineJndiName(field);
        // 如果不存在EJB引用的Stub则注入相关的EJB对象
        Object ejbref = field.get(fromObject);
        if (ejbref == null) {
            // 通过反射注入相关的EJB对象
            Object ejbRefStub = lookupEJBRef(ejbrefJndiName);
            if (ejbRefStub != null) {
                field.set(fromObject, ejbRefStub);
            }
        }
    }

    /**
     * 查询相关的EJB对象的stub对象
     * @param ejbRefJndiName
     * @return
     * @throws NamingException
     */
    public Object lookupEJBRef(String ejbRefJndiName) throws NamingException {
        Object ejbref=null;
        if(icache.containsKey(ejbRefJndiName)){
            ejbref=icache.get(ejbRefJndiName);
        }else{
            ejbref=context.lookup(ejbRefJndiName);
            icache.put(ejbRefJndiName, ejbref);
        }
        return ejbref;
    }

    /**
     * 
     * @param field
     * @return
     */
    private String determineJndiName(Field field) {
        EJBRef ejbAnnotation = field.getAnnotation(EJBRef.class);
        Class<?> ejbClass = field.getClass();
        if (ejbAnnotation.mappedName().equals("")) {
            return ejbClass.getSimpleName();
        }
        return ejbAnnotation.mappedName();
    }
}

 

strut2的配置:

		<!-- 设置自定义拦截器栈  -->
			<interceptor-stack name="teamwareStack">
				<interceptor-ref name="ejbInjectInterceptor"/>
				<!--
					<interceptor-ref name="encodingInterceptor"/>
					-->
				<interceptor-ref name="crudStack"/>
			</interceptor-stack>