EJB3.0 Development Guide: Transaction and Security

xiaoxiao2021-03-06  39

Not like in EJB2.x, you need to deploy transactions in the deployment file. EJB3.0 provides transaction support for the specified class or method by comment. In the preview specification of EJB3.0, specifying the use of TransactionAttribute as a comment, the statement of TransactionAttribute is as follows

@Target ({Method, Type}) @Retention (runtime)

Public @Interface TransactionAttribute

{

TransactionAttributeType Value () Default Required;

}

TransactionAttributeType is an enumeration type:

Public Enum TransactionAtTributeType {

Mandatory,

REQUIRED,

Requiresnew,

Supports,

NotSupported,

NEVER

}

It can be seen that TransactionAttribute can be annotated for classes and can be noted for methods.

Several enumeration values ​​of TransactionttributeType representative:

Mandatory

This property requires your transaction to be running, otherwise it will return an exception.

Required

If you want your EJB to always run in the transaction, you should use this mode. If the transaction is running, you will add your EJB components to your transaction. If there is no transaction, you will add a transaction.

Requiresnew

Always get a new transaction for your EJB component.

Supports

If the transaction exists, add your EJB to the transaction, if there is no transaction, it will not run in the transaction.

NotSupported

Your EJB component will not use transactions. Suppose your component B sets this property, component A uses a transaction, when A calls B, a transaction will be suspended, and the transaction operation will not perform transaction operation. When B is completed, a transaction is activated.

NEVER

Your EJB component will not use transactions. Suppose your component B sets this property, when the client calls B in the transaction, the container will throw an exception.

In JBOSS, this note is simplified, and TransactionAttribute is simplified to TX, TransactionAttributeType is simplified to TXTYPE.

MethodPermissions and Unchecked are secure comments. JSR-181 is simplified. These two comments can be used in classes and methods.

MethodPermissions is used to specify a list of roles that can be allowed to use such or this method:

@MethodPermissions

(

{"Role1", "role2", "role3"}

)

Unchecked specified method and class will not do permission check.

In Eclipse, the example provided this article is imported.

This example is similar to the example of the stateless session bean, but transaction or permissions are added to the business method.

This example mainly has 7 files:

Counter.java: Business Interface.

CounterBean.java: Business implementation class. The EJB we develop in the future is also named (adding beans on the interface name).

Client.java: Test EJB client class.

JNDI.Properties :jndi property file provides basic configuration properties for accessing JDNI.

Build.xml: Ant profile, to compile, release, test, and clear EJB.

Roles.properties: Role settings

Users.properties: User Settings

Here is a description of the content of each file.

Counter.java:

Package com.kuaff.ejb3.security;

Import javax.ejb.remote;

@Remote

Public Interface Counter

{

Public Intd (INT I);

Public int getnumber ();

Public Int getValue ();

}

This interface is very simple, and the stateless session bean is the same, JBoss defaults to use the full name of the interface as its JNDI name. In the above example, its full name can be obtained by counter.class.getname ().

CounterBean.java:

Package com.kuaff.ejb3.security;

Import org.jboss.ejb3.security.securitydomain

Import javax.ejb.methodpermissions;

Import javax.ejb.stateless;

Import javax.ejb.tx;

Import javax.ejb.txtype;

Import javax.ejb.unchecked;

@StateLess

@Securitydomain ("kuaff")

Public Class CounterBean Implements Counter

{

Private int numBer = 0;

@Unchecked

@TX (TXTYPE.REQUIRESNEW)

Public Int Add (INT i)

{

Number = i;

Return Number;

}

@MethodperMissions ({"manager"})

Public int getNumber ()

{

Return Number;

}

@MethodPermissions ({"User"})

Public Int getValue ()

{

Return Number;

}

}

This is the implementation class of the counter. In this class, SecurityDomain is a comment added by JBoss to specify a JaAs's warehouse. Set to Other, verify the properties file. In our example, you can see two new files.prperties and roles.properties, store users and role information separately. Add methods support transactions and do not do permissions. GetNumber needs to have a Manager role to access, GetValue needs to have users who have user roles to access.

Client.java

Package com.kuaff.ejb3.security;

Import javax.naming.initialcontext;

Import javax.naming.namingexception;

Import org.jboss.security.securityassociation;

Import org.jboss.security.simpleprincipal;

Public Class Client

{

Public static void main (string [] args)

{

InitialContext CTX;

Try

{

CTX = New InitialContext ();

Counter counter = (counter) ctx.lookup (counter.class.getname ());

SecurityAssociation.SetPrincipal (New SimplePrincipal ("Smallnest");

System.out.println ("Use Manager Role Access"); Securityassociation.Setcredential ("ASD # $ 45GAS5" .tocharaRray ());

Try

{

System.out.println ("Current Number:" Counter.getNumber ());

}

Catch (SecurityException EX)

{

System.out.println ("Safety Abnormal 001");

}

System.out.Println ("Accessing other roles to access methods);

Try

{

System.out.Println ("Current Number:" Counter.getValue ());

}

Catch (SecurityException EX)

{

System.out.println ("Security Abnormal 002");

}

}

Catch (Namingexception E)

{

E.PrintStackTrace ();

}

}

}

This class is used to test the counter EJB we publish. First pass

CTX = new initialContext (); get the context, then use the lookup counter, then set the user and password, the counter method is executed, and the exception will be thrown once it is not accessed.

Please run the run.bat: Run? C all under the directory of {$ jboss_home} / bin, start JBoss.

Execute the EJBJAR TARGET in the ANT view of Eclipse. Or in the command line, enter this project directory, perform Ant Ejbjar, publish this EJB.

Execute Run Target in the ANT view of Eclipse. Or on the command line, enter this project directory, perform Ant Run, test this EJB.

转载请注明原文地址:https://www.9cbs.com/read-76247.html

New Post(0)