服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

精通ejb【五】


  为beans增加功能

ejb 上下文:通往容器的门户
存在如下信息:
1、关于bean的home对象和ejb对象的信息
2、bean的当前事务信息。
3、 对于客户授权的安全信息。bean可以通过查询环境决定客户执行操作所需要的安全层次。
4、 bean的环境属性。
容器将所有这些信息保存在一个称为ejb context object的对象里。ejb上下文作为容器的物理部分,可以被bean访问。这些访问可以让bean得到当前的状态和改变当前的状态。
上下文可以在bean的生命期中被更改。
ejb1.0 javax.ejb.ejbcontext接口:
public interface javax.ejb.ejbcontext
{
public javax.ejb.ejbhome getejbhome();
public java.util.properties getenvironment();
public java.security.identity getcalleridentity();
public boolean iscallerinrole(java.security.identity);
public javax.jts.usertransaction getusertransaction();
public void setrollbackonly();
public boolean getrollbackonly();
}
会话bean的上下文
上下文根据bean的不同分为:会话上下文和实体上下文。它们分别被用于会话bean和实体bean

javax.ejb.ejbcontext
public interface javax.ejb.sessioncontext
extends javax.ejb.ejbcontext
{
public javax.ejb.ejbobject getejbobject();
}
注意:sessioncontext接口继承了ejbcontext接口,在ejbcontext中定义的方法提供了对会话bean的存取路径。
对于会话bean,调用setsessioncontext,这个方法在javax.ejb.sessionbean接口中被定义。对于实体bean,调用setentitycontext。
sessioncontext.getejbobject()
在ejb中,beans可以作为其他bean的客户端。如果一个bean需要调用另外的bean,getejbobject()方法则是必需的。在java中,对象可以使用this关键字保存自身的参考。在ejb中,bean不能使用this关键字给其他bean传递对象,这是因为所有的客户调用bean上的方法都是间接调用bean的ejb对象。bean可以使用this关键字将自己传给ejb对象。
了解ejb的安全性
首先,客户端必须是可被鉴别的。
其次,客户端必须是已经授权的。
第一步:鉴别
不同的ejb容器拥有不同的鉴别客户端的方法。例如:bea的weblogic中,当不同客户端代码使用jndl定位home对象时,提供不同的用户名和密码。
properties props = system.getproperties();
props.put(context.security_principal, "employeea");
props.put(context.security_credentials, "mypassword1");
context ctx = new initialcontext(props);
// use the initial context to lookup home objects...
ejb没有制定如何鉴别的规范,因此这样就影响了可移植性。要了解这方面,查看各类容器的文档。
当运行这段代码时,应用服务器将验证你的用户名和密码,这是应用服务器规范。许多应用服务器允许在属性文件里设置用户名和密码。这个文件将在运行时由应用服务器读。
高级点的服务器支持已经存在的验证系统的整合。例如将用户名和密码列表存储在ldap服务器中。
第二步:授权
只有经过授权的客户端才可以调用bean中的方法。ejb中有两种验证授权的方法:declaratively和programmatically。即:由容器执行所有的授权检验、在程序中进行授权检查。
declarative授权检查时,要在配置描述符中声明bean的授权需要。例如使用bea的weblogic服务器的配置描述符的例子:
(accesscontrolentries
submitpurchaseorder [employees]
approvepurchaseorder [managers]
default [administrators]
); end accesscontrolentries
容器将在运行时自动的执行安全检查。抛会出java.lang.securityexception异常。
programmatic授权检查,必须查询ejb上下文得到当前客户端的授权信息。由两种方法调用callerinrole(identity role)和getcalleridentity()。
iscallerinrole()
import java.security.identity;
...
public class mybean implements sessionbean {
private sessioncontext ctx;
...
public void foo() {
identity id = new myidentity("administrators");
if (ctx.iscallerinrole(id)) {
system.out.println("an admin called me");
return;
}
system.out.println("a non-admin called me");
}
}

import java.security.identity;
public class myidentity extends identity {
public myidentity(string id) {
super(id);
}
}
getcalleridentity()
import java.security.identity;
...
public class mybean implements sessionbean {
private sessioncontext ctx;
...
public void bar() {
identity id = ctx.getcalleridentity();
string name = id.getname();
system.out.println("the caller´s name is " + name);
}
}
了解ejb对象的操作
许多ejb应用程序需要客户端有与bean断开的能力,还要有与bean重建连接的能力。ejb提供了ejb object handles。ejb对象操作对于ejb对象是一个长生命期的代理。可以用它来重建与ejb对象的连接,并保证会话状态不被丢失。下面是ejb对象操作的代码
// first, get the ejb object handle from the ejb object.
javax.ejb.handle myhandle = myejbobject.gethandle();
// next, serialize myhandle, and then save it in
// permanent storage.
objectoutputstream stream = ...;
stream.writeobject(myhandle);
// time passes...
// when we want to use the ejb object again,
// deserialize the ejb object handle
objectinputstream stream = ...;
handle myhandle = (handle) stream.readobject();
// convert the ejb object handle back into an ejb object
myremoteinterface myejbobject =
(myremoteinterface) myhandle.getejbobject();
// resume calling methods again
myejbobject.callmethod();
例子:the puzzle game “fazuul”(参考原文)

扫描关注微信公众号