JBoss 7: How to inject some meta data into EJBContext on EJB client side? How to controll concurrent access?
ybxiang.china Sep 3, 2012 8:04 AMDear all,
In JBoss 5,
(a) we can inject some meta data on client side:
import java.io.Serializable;
import org.jboss.aop.advice.Interceptor;
import org.jboss.aop.joinpoint.Invocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* It will be downloaded from server to client, so must implement Serializable.
* @author yxiang
*
*/
public class SessionTokenInjectInterceptor implements Interceptor, Serializable{
private static final long serialVersionUID = 1L;
public final static Logger logger = LoggerFactory.getLogger(SessionTokenInjectInterceptor.class);
public String getName() {
return SessionTokenInjectInterceptor.class.getSimpleName();
}
public Object invoke(Invocation invocation) throws Throwable {
String sessionId = "ybxiang."+String.valueOf(System.currentTimeMillis());
logger.info("=========SessionTokenInjectInterceptor set metadata[GroupName_Session/KEY_SESSION_ID]:"+sessionId);
invocation.getMetaData().addMetaData("GroupName_Session","KEY_SESSION_ID",sessionId);
//
return invocation.invokeNext();
}
}
(b) we can retrieve the meta data on server side:
import org.jboss.aop.advice.Interceptor;
import org.jboss.aop.joinpoint.Invocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SessionTokenRetriveInterceptor implements Interceptor{
public final static Logger logger = LoggerFactory.getLogger(SessionTokenRetriveInterceptor.class);
public String getName() {
return SessionTokenRetriveInterceptor.class.getSimpleName();
}
public Object invoke(Invocation invocation) throws Throwable {
try{
Object metaData = invocation.getMetaData().getMetaData("GroupName_Session","KEY_SESSION_ID");
logger.warn("======SessionTokenRetriveInterceptor,metaData="+metaData);
System.out.println(Thread.currentThread().getStackTrace());
return invocation.invokeNext();
}finally{
}
}
}
(c) ejb3-interceptors-aop.xml
<?xml version="1.0" encoding="UTF-8"?>
<aop xmlns="urn:jboss:aop-beans:1.0">
...
<!-- added by ybxiang -->
<interceptor class="com.ybxiang.jbossaop.ejb3metadata.common.SessionTokenInjectInterceptor" scope="PER_VM"/>
<interceptor class="com.ybxiang.jbossaop.ejb3metadata.server.SessionTokenRetriveInterceptor" scope="PER_VM"/>
...
<stack name="StatelessSessionClientInterceptors">
<interceptor-ref name="com.ybxiang.jbossaop.ejb3metadata.common.SessionTokenInjectInterceptor"/>
...
</stack>
...
<stack name="ClusteredStatelessSessionClientInterceptors">
<interceptor-ref name="com.ybxiang.jbossaop.ejb3metadata.common.SessionTokenInjectInterceptor"/>
<interceptor-ref name="org.jboss.ejb3.remoting.ClusteredIsLocalInterceptor"/>
<interceptor-ref name="org.jboss.ejb3.security.client.SecurityClientInterceptor"/>
<interceptor-ref name="org.jboss.aspects.tx.ClientTxPropagationInterceptor"/>
<interceptor-ref name="org.jboss.aspects.remoting.ClusterChooserInterceptor"/>
<interceptor-ref name="org.jboss.aspects.remoting.InvokeRemoteInterceptor"/>
</stack>
...
<domain name="Stateless Bean" extends="Intercepted Bean" inheritBindings="true">
<!-- added by ybxiang -->
<bind pointcut="execution(public * @javax.ejb.Remote->*(..)) AND execution(public * $instanceof{com.ybxiang.jbossaop.ejb3metadata.server.EJB3Hello}->*(..))">
<interceptor-ref name="com.ybxiang.jbossaop.ejb3metadata.server.SessionTokenRetriveInterceptor"/>
</bind>
...
</domain>
...
</aop>
( It is really complicated in JBoss 5! )
But those jboss-sepecified annotations(org.jboss.aop.advice.*) are NOT available in jboss 7.
How to implement the same function in jboss 7?
My case introduction:
1. Each EJB client must call LoginInEjb.login(String username, String password) method to get the session token(a long string). In this method, we will generate one session token and save it into DB.
2.One username has at most 5 different session tokens.
If the 6th EJB client tries to call LoginInEjb.login(String username, String password) with same username, exception will be thrown.
3. Business logic bean is SecuredRemoteSession.java.
4. SessionTokenInterceptor.java is an interceptor. It is used to retrieve the session token like above SecuredRemoteSession.
getNameFree() does.
If the session token does NOT exist in DB, one secure exception is thrown.
5. If one evil ejb client has connected to the jboss server, we can delete the session token to force it log out.
Now, my questioin:
During calling ISecuredRemoteSession's method i
n my EJB client, how to access to save session token into the EJB Context?