Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
2.0 vs. 3.0 2.0. vs 3.0
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
EJB Architecture Clients (typical use cases)
Java
Client application
EJB EJB EJB
EJB
Client
HTML- Servlet
EJB EJB client or JSP EJB EJB
Client Database Database
EJB Container Web server EJB Container
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
Distributed Objects Distributed Objects
Request
Network Network Interceptor
Remote
interface
Distributed Distributed
Skeleton Skeleton
Remote object Remote object
interface interface
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
Skeleton Bean
Remote
interface
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
EJB 2.0 Enterprise JavaBeans
! Home interface
Client Stub
• Defines the life cycle methods of the bean
- Create
- Destroy
! Remote interface Network EJB
Object Bean
• Defines the business methods of the bean
! Bean class
• Business logic Skeleton
EJB Container
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
! Extends javax.ejb.EJBObject
Client Stub
! Defines business methods clients call
! Acts as a proxy
Network EJB
Object Bean
package ejbExample.interfaces
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
EJB Home Object (Home Interface) EJB Home Object Characteristics
! Extends javax.ejb.EJBHome
Client Stub
! Acts as a factory to create EJB instances
! Allows clients to create/remove/find EJBs
Network EJB
Object Bean
package ejbExample.interfaces
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
Deployment How clients find the Home object
! EJB deployment descriptor (XML) ! Java Naming and Directory Interface (JNDI)
! ejb-jar.xml - Similar to CORBA naming service
- Mapping between resource names and physical locations
! Attributes of the beans specified declaratively
! No machine address to home object hard
! Deployment descriptor language is a coded
composition language - Address to JNDI server is needed
! EJB-jar file is verified by container - Kept in the initial context
- Use initial context factory to acquire an initial context (is
! Container generates stubs and skeletons the JNDI driver)
- Vendor specific, bound to J2EE server implementation
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
! Session beans
• Stateless
3 EJB • Stateful
Object Bean
Client ! Entity beans
2 EJB ! Message-Driven beans
1 Home
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
So, what does the container do? How can container vendors compete?
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
XDoclet
! Deployment descriptor
! Generate from declarative specification Demonstration
• Remote interface
• home interface
• local interface
• local home interface
• primary key class CUGS
Mikhail Chalabine
! Specification as comments in the mikch@ida.liu.se
Bean class
! When beans call beans locally ! Represent business data stored in database
! Optimization ! Database types converted to Java types
! Call by value/reference problem ! Change of values in the Entity Bean is
propagated to the DB
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
EJB Container
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
Entity Beans: Security
! Authentication - JAAS
! Authorization Demonstration
! Deployment descriptor
• Roles
• Roles and methods
No instance level based security
!
CUGS
Mikhail Chalabine
mikch@ida.liu.se
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
Final thoughts Possible problem sources
! Is it object-oriented? ! Remotability
• Separation of data and operations (entity beans and • Pramatics: stay away from large distributed systems
session beans) ! Security
• No inheritance between beans in 2.0!
! Persistence
- 3.0 Standard: beans are POJOs
! Suitable for which tasks? ! Caching
• One architecture. Anomalies if trying to do anything ! Scalability
else ! Messaging
! Component marketplace? ! Transactions
• Not today!
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
Create a New Project EJB
! File > New > Project > JBoss-IDE > J2EE ! File > New > Other > JBoss-IDE > EJB
Projects > J2EE Project Components > Session Bean
• Name: CUGS-EJB ! Set package to cugs-ejb.ejb and the class to
• Create folder src MyBean.
• Set default output to /EJB-CUGS/bin ! Make sure ejbCreate() is selected
! Click create, note all the method stubs are
generated with the default ejbCreate() method
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
! Right click on the MyBean class under the ! Project properties (right click on the project)
MyBean Java file ! Select the XDoclet configurations
! J2EE > Add Business Method ! Enable XDoclet
! Enter getName as the method name and
String for the return type ! EJB Configuration: right-click in the upper area
! Add the implementation to the method to pop-up a menu and choose Add.
• Type EJB
! /**
! * Business method
• Click Ok
! * @ejb.interface-method view-type = "remote"
! */
! public String getName(String input_str) {
! ! // TODO Auto-generated method stub
! ! return input_str;
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
Generate EJB related files (2) Generate EJB related files (3)
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
Generate EJB related files (4) Generate EJB related files (5)
! All of the standard EJB deployment descriptors ! All of the JBoss-specific deployment
will now be placed in the src/META-INF descriptors will now be placed in the src/
directory META-INF directory.
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
Generate EJB related files (6) Generate EJB related files (7)
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
The Servlet and the Web-App (1) The Servlet and the Web-App (2)
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
The Servlet and the Web-App (3) The Servlet and the Web-App (4)
! Complete the doPost() method
protected void doPost(HttpServletRequest request, HttpServletResponse response)
! ! throws ServletException,
! Insert the missing XDoclet tags in the class MyServlet
! ! IOException {
! ! // TODO Auto-generated method stub
! ! response.setContentType("text/html"); /**
! ! PrintWriter out = response.getWriter();
* Servlet Class
! ! out.println("<html><head><title>");
*
! ! out.println("Name Service");
* @web.servlet name="MyServlet"
! ! out.println("</title></head>");
* display-name="My servlet"
! ! out.println("<body>");
! ! out.println("<h1>"); * description="Servlet that returns a name"
! ! out.println("Getting the name"); * @web.servlet-mapping url-pattern="/MyServlet"
! ! out.println("</h1>"); * @web.servlet-init-param name="A parameter"
! ! try { * value="A value"
! ! My bean = home.create(); * @web.ejb-ref
! ! String result = bean.getName(); * name= "ejb/My"
! ! bean.remove(); * type= "Session"
! ! out.println("<p>"); * home= "tddc18.interfaces.MyHome"
! ! out.print("The name is: "); * remote= "tddc18.interfaces.My"
! ! out.println(result); *
! ! out.println("</p>"); * @jboss.ejb-ref-jndi
! ! } catch (Exception e) {
* ref-name= "ejb/My"
! ! out.println(e.getMessage()); e.printStackTrace(out);
* jndi-name= "ejb/My"
! ! } finally {
! ! out.println("</body></html>"); */
! ! out.close();
! ! } }
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
Generate Servlet -related files (1) Generate Servlet -related files (2)
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
Remains to be done J2EE Application (1)
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
! Add the following to the application.xml ! The EJB JAR. It will contain the EJB classes
and interfaces, as well as the ejb-jar.xml and
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE application PUBLIC
jboss.xml deployment descriptors
!
!
"-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN"
"http://java.sun.com/dtd/application_1_3.dtd">
! The EJB Client Jar. It will contain the EJB
<application>
! <display-name>Get Name Application</display-name>
interfaces.
! <module>
! ! <ejb>MyEJB.jar</ejb> ! The Web Application WAR. It will contain the
!
!
</module>
<module> Servlet class, the EJB client Jar, as well as the
!
!
!
!
<web>
! <web-uri>MyWeb.war</web-uri>
web.xml deployment descriptors
! ! ! <context-root>/My</context-root>
! ! </web> ! The J2EE Application EAR. It will contain the
! </module>
</application> EJB Jar and the Web Application War, as well
as the application.xml deployment descriptor.
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
Packaging (2) Packaging (3): MyEJB.jar
! Enable Packaging
! Create EJB JAR
• Right-click in the area to pop-up the menu and
• Right-click project properties
choose Add Archive. Type MyEJB.jar
• Select Packaging Configurations
• Enable Packaging
! We want to add the EJB classes and
interfaces.
• Select the MyEJB.jar and right-click in the area to
pop-up the menu and select Add Folder.
• A folder chooser dialog appears
- Click Project Folder
- Select /CUGS-EJB/bin. Click Ok.
• Set includes to: cugs-ejb/ejb/*.class,cugs-ejb/
interfaces/*.class and click Ok.
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
! Add the standard EJB deployment descriptor ! Add the specific EJB deployment descriptor
• Select the MyEJB.jar (JBoss)
• Right-click in the area and select Add File from the • Select the MyEJB.jar
pop-up menu • Right-click in the area and select Add File from the
pop-up menu
• The dialog allows to select which file to include in the - Click Project File
package and to set a prefix which will be appended - Choose CUGS-EJB/src/META-INF/jboss.xml
when building the package - Set Prefix to META-INF. The jboss.xml should be located in
the META-INF directory.
- Click Project File
- Select CUGS-EJB/src/META-INF/ejb-jar.xml and click Ok.
- Set the Prefix to META-INF. The ejb-jar.xml should be
located in the META-INF directory. ! The package configuration for MyEJB.jar is
complete now
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
Packaging (6): MyEJB-client.jar Packaging (7): MyWeb.jar
! Add the standard web deployment descriptor ! Add the specific web deployment descriptor
• Select the MyWeb.war, right-click in the area and (JBoss)
select Add File from the pop-up menu. • Select the MyWeb.war, right-click in the area and
• Click Project File in the folder chooser dialog and select Add File from the pop-up menu.
select CUGS-EJB/src/WEB-INF/web.xml. Click Ok. • Click Project File in the folder chooser dialog and
• The web.xml should be located in the WEB-INF select CUGS-EJB/src/WEB-INF/jboss-web.xml.
folder according to the container specification. Set Click Ok.
the Prefix to WEB-INF • The web.xml should be located in the WEB-INF
folder according to the container specification. Set
the Prefix to WEB-INF
Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB) Mikhail Chalabine mikch@ida.liu.se Enterprise Java Beans (EJB)
Packaging (10): MyWeb.jar Packaging (11): MyApp.ear
! Add the EJB module to the application ! Add the WEB module to the application
• Select the MyApp.ear, right-click in the area and • Select the MyApp.ear, right-click in the area and
select Add File from the pop-up menu. select Add File from the pop-up menu.
• Click Project File in the folder chooser dialog • Click Project File in the folder chooser dialog
- The file to be selected is CUGS-EJB/MyEJB.jar but it does - The file to be selected is CUGS-EJB/MyWeb.jar but it does
not exist yet, so set File to CUGS-EJB/MyEJB.jar manually not exist yet, so set File to CUGS-EJB/MyWeb.jar
(type it in) manually (type it in)
• Click Ok. • Click Ok.