1 The JNDI ENC and Injection Every EJB container that is deployed in an application server has its own personal internal registry called the Enterprise.

Slides:



Advertisements
Similar presentations
Communicating in J2EE.
Advertisements

11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.
Basic Object-Oriented concepts. Concept: An object has behaviors In old style programming, you had: –data, which was completely passive –functions, which.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
ITEC200 – Week03 Inheritance and Class Hierarchies.
JBoss Seam: Contextual Components Jason Bechtel
Module 7 Security and Transactions. Security and Transactions Topics to be Covered: Security and the Enterprise Transactions.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
EJB Development and Support Services. EJB Development and Support Services Topics to be Covered: EJB Design Bean/Container Interaction Java Naming and.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Chapter 10 Classes Continued
Programming Languages and Paradigms Object-Oriented Programming.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.
1 Security Most Java EE applications need to provide identity to users who access them and security for that access. Applications may want to prevent hostile.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Lecture 9 Polymorphism Richard Gesick.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Module 6 Message-Driven Beans. History Introduced in EJB 2.0 –Supports processing of asynchronous messages from a JMS provider Definition expanded in.
Session Beans Overview EJB container Remote Invocation Represent client's access to app Local / Remote speed / flexibility trade-off when to choose local.
Basic Object- Oriented Concepts Presented By: George Pefanis 21-Sep-15.
EJB Framework.  As we know, EJB is the center of the J2EE architecture that provides a sturdy framework for building enterprise applications. The major.
Java Server Pages A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format,
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Component frameworks Roy Kensmil. Historical trens in software development. ABSTRACT INTERACTIONS COMPONENT BUS COMPONENT GLUE THIRD-PARTY BINDING.
Ch 2 – Application Assembly and Deployment COSC 617 Jeff Schmitt September 14, 2006.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
ASP.NET Web Services.  A unit of managed code installed under IIS that can be remotely invoked using HTTP.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Session Beans Based on: Patel, Brose, Silverman, Mastering Enterprise JavaBeans 3.0.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Java Programming: Advanced Topics 1 Enterprise JavaBeans Chapter 14.
Library Example February 2010 – August 2010
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
FIT Objectives By the end of this lecture, students should: understand the role of constructors understand how non-default constructors are.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Enterprise Java v JNDI Enterprise Naming Conext (ENC) and Injection JNDI ENC.
EJB Enterprise Java Beans JAVA Enterprise Edition
1 Entity Callbacks and Listeners When you execute EntityManager methods like persist( ), merge( ), remove( ), and find( ), or when you execute EJB QL queries,
17 Copyright © 2004, Oracle. All rights reserved. Integrating J2EE Components.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Enterprise Java Beans. Contents  Understanding EJBs  Practice Section.
Callbacks and Interceptors. Contents  Session Beans Life Cycle  Interceptors.
Object-Oriented Concepts
Java Programming Language
Understanding and Designing with EJB
Objectives In this lesson, you will learn to:
Java Inheritance.
Java Programming Language
Knowledge Byte In this section, you will learn about:
Presentation transcript:

1 The JNDI ENC and Injection Every EJB container that is deployed in an application server has its own personal internal registry called the Enterprise Naming Context (ENC). This ENC is implemented by JNDI and is a sandbox where the EJB container can hold specific references to its environment. Think of it as the EJB container's personal address book, where it writes down addresses to various Java EE services that it wants to look up and use within its business logic.

2 The JNDI ENC The ENC has been around in the EJB specification since the early 1.0 days. It began as a local JNDI namespace that was specific to an EJB container. Developers could define aliases to resources, EJBs, and environment entries in the JNDI ENC through EJB XML deployment descriptors. These aliases could then be looked up directly in JNDI within business logic. In EJB 3.0, this mechanism was enhanced so that JNDI ENC references could be injected directly into the fields of a bean class. Annotations are the primary mechanism for doing this, but XML deployment descriptor support is available for those who wish to use that abstraction.

3 What Can Be Registered in the JNDI ENC? Many different items can be bound to the ENC: references to any EJB interface, a JMS queue or topic destination, JMS connection factories, data sources, any JCA resource, and even primitive values. Java EE services such as javax.transaction.UserTransaction, javax.ejb.TimerService, and org.omg.CORBA.ORB are also available in the ENC.

4 How Is the JNDI ENC Populated? The ENC's JNDI namespace is populated in two separate ways: via XML or via annotations. Any reference that you declare in XML to a service or resource automatically populates the JNDI ENC with the reference's name. Any environment annotation that you use in your bean class also causes the ENC to be populated. Once an item is bound to the JNDI ENC of the EJB container, it can be referenced by a JNDI lookup.

5 XML Population To illustrate how XML population works, let's define a reference to the stateless session bean we wrote in Lecture 11. Here we define a local interface reference to the ProcessPayment EJB for the TravelAgent EJB: TravelAgentBean ejb/ProcessPayment Session com.titan.processpayment.ProcessPaymentLocal ProcessPaymentBean

6 XML Population The element tells the EJB container that the TravelAgentBean wants a reference to the ProcessPayment EJB. A reference to this bean is registered in the TravelAgentBean's JNDI ENC under the name ejb/ProcessPayment. This is defined by the element. Other referenceable things, like resources and JMS destinations, have similar XML elements such as to specify how and where the reference will be bound into their JNDI ENCs. Each service type in Java EE has its own reference syntax.

7 Annotation Population Each referenceable type also has a corresponding annotation that can be used as an alternative to XML. If you specify these annotations on the bean class, they will cause the JNDI ENC to be populated with the information defined in the annotation: beanInterface=ProcessPaymentLocal.class, beanName="ProcessPaymentBean") public class TravelAgentBean implements TravelAgentRemote {... }

8 Annotation Population In this example, we are registering a reference to the ProcessPayment EJB under the ejb/ProcessPayment name. Business logic running inside the TravelAgentBean is able to do JNDI lookups to find this reference. Each environment annotation, such has a name( ) attribute that specifies the JNDI ENC name to which you want the service reference to be bound.

9 How Are Things Referenced from the ENC? Anything registered in the JNDI ENC can be looked up by name under the java:comp/env context. The comp part of this name corresponds to component. The JNDI name resolves to a different context depending on where you invoke the lookup. For example, if you invoke jndi.lookup("java:comp/env") within the TravelAgentBean, you will get that EJB container's ENC. If you do the same within ProcessPaymentBean, you will get a different ENC registry specific to that bean. The application server knows what ENC is active when you perform beanInterface=ProcessPaymentLocal.class, beanName="ProcessPaymentBean")

10 How Are Things Referenced from the ENC? public class TravelAgentBean implements TravelAgentRemote { public TicketDO bookPassage(CreditCardDO card, double amount) { ProcessPaymentLocal payment = null; try { javax.naming.InitialContext ctx = new InitialContext( ); payment = (ProcessPaymentLocal) ctx.lookup("java:comp/env/ejb/ProcessPayment"); } catch (javax.naming.NamingException ne) { throw new EJBException(ne); } payment.process(card, customer, amount);... }

11 How Are Things Referenced from the ENC? In this example, the bookPassage( ) method from our TravelAgent EJB needs a reference to the ProcessPayment EJB so that it can bill the customer for the reservation. A reference to the ProcessPayment EJB was created in the TravelAgent's ENC by annotating the bean class with annotation. The preceding code does a JNDI lookup to find this reference. While the ProcessPayment.process( ) method is invoked, the java:comp/env/ejb/ProcessPayment reference is no longer available because the ProcessPayment's ENC is active instead of the TravelAgent's ENC.

12 Annotation injection Instead of an ENC lookup, the ProcessPayment EJB reference can be injected directly into a member variable of the TravelAgent EJB. This injection can be done through environment annotations or an XML deployment descriptor public class TravelAgentBean implements TravelAgentRemote private ProcessPaymentLocal payment;... } By using annotation on the payment field of the TravelAgentBean class, the EJB container will automatically inject a reference to the ProcessPayment EJB directly into the payment field when the TravelAgent bean instance is created.

13 Annotation injection Alternatively, if you do not like this form of injection, the specification also supports injecting via a bean setter public class TravelAgentBean implements TravelAgentRemote { private ProcessPaymentLocal public void setProcessPayment(ProcessPaymentLocal payment) { this.payment = payment; } Unlike the previous example, when the TravelAgentBean instance is allocated, the EJB container will instead invoke the setProcessPayment( ) method, passing in the EJB reference as a parameter.

14 XML injection If you prefer not to use annotations to initialize the fields of your bean class, then the element is available to you in your ejb-jar.xml deployment descriptor: TravelAgentBean ProcessPayment Session com.titan.processpayment.ProcessPaymentLocal ProcessPaymentBean com.titan.travelagent.TravelAgentBean payment

15 XML injection Each XML environment element such as can use to populate a field or call a setter method with the referenced item. The element is the class where your field or method is declared. This may seem unnecessarily verbose, but this becomes important when there are inheritance hierarchies.

16 Injection and inheritance It is possible for a bean class to be part of a class hierarchy. If any fields or methods have injection annotations on them, they will still be populated, but certain injection rules are followed: public class BaseClass DataSource public void setProcessPayment(ProcessPaymentLocal pp) {... public class MySessionBean extends BaseClass implements MySessionRemote {... }

17 Injection and inheritance In this example, we have a stateless session bean class that inherits from a base class. All instances of MySessionBean would have the appropriate resource injected into the base class's data field as well as the setProcessPayment( ) method. It is possible to change what is injected into the setProcessPayment( ) method by reimplementing and overriding it in the public class MySessionBean extends BaseClass implements MySessionRemote public void setProcessPayment(ProcessPaymentLocal pp) {... }... }

18 Injection and inheritance The ProcessPaymentBean would no longer be injected into the setProcessPayment( ) method. Instead, the new overridden reference, AcmeProcessPayment, would be injected. There is one exception to this rule. If the setProcessPayment( ) method in the BaseClass was a private method rather than a protected or public method, then the base class would still be injected with the old reference: public class BaseClass DataSource private void setProcessPayment(ProcessPaymentLocal pp) {... }

19 Injection and public class MySessionBean extends BaseClass implements MySessionRemote public void setProcessPayment(ProcessPaymentLocal pp) {... }... } So, in the previous example, both setProcessPayment( ) methods would be invoked and set with a different ProcessPaymentLocal reference. The BaseClass 's setProcessPayment( ) method would get a reference to the ProcessPaymentBean, and the setProcessPayment( ) method of MySessionBean would get AcmeProcessPayment.

annotation can be used on your bean class's setter methods, on member fields, or directly on the class itself: package METHOD, EJB { String name( ) default ""; Class beanInterface( ) default Object.class; String beanName( ) default ""; String mappedName( ) default ""; }

21 XML-based remote EJB references The element defines a reference to remote EJBs. It contains the subelements (optional), (required), (required), (required), (optional), (optional), and (optional), as well as the element (optional) described in the first part of this chapter. Here is a remote reference to the ProcessPayment EJB: TravelAgentBean ejb/ProcessPaymentRemote Session com.titan.processpayment.ProcessPaymentRemote

22 XML-based local EJB references The element defines a reference to remote EJBs. It contains the subelements (optional), (required), (required), (required), (optional), (optional), and (optional), as well as the element (optional) described in the first part of this chapter. Here is a local reference to the ProcessPayment EJB: TravelAgentBean ejb/ProcessPaymentRemote Session com.titan.processpayment.ProcessPaymentLocal

annotation can be used on your bean class's setter methods or member fields, or directly on the class itself: package METHOD, PersistenceUnit { String name( ) default ""; String unitName( ) default ""; }

annotation can be used on your bean class's setter methods or member fields, or directly on the class itself: package javax.persistence; public enum PersistenceContextType { TRANSACTION, EXTENDED }

PersistenceProperty { String name( ); String value( ); METHOD, PersistenceContext { String name( ) default ""; String unitName( ) default ""; PersistenceContextType type( ) default TRANSACTION; PersistenceProperty[] properties( ) default {}; }

annotation is used to reference an external resource. It can be applied to your bean class's setter methods or member fields, or directly on the class itself. This annotation is highly overloaded and overused in the Java EE specification in that in addition to external resources, it is also used to reference JMS message destinations, environment entries, EJBContext s, and Java EE core services. For now, we'll focus solely on using this annotation to access external resources:

package METHOD, Resource { public enum AuthenticationType { CONTAINER, APPLICATION } String name( ) default ""; Class type( ) default Object.class; AuthenticationType authenticationType( ) default v AuthenticationType.CONTAINER; boolean shareable( ) default true; String description( ) default ""; String mappedName( ) default ""; }

28 Shareable resources When several enterprise beans in a transaction use the same resource, you will want to configure your EJB server to share that resource. Sharing a resource means that each EJB will use the same connection to access the resource (e.g., database or JMS provider), a strategy that is more efficient than using separate resource connections. In terms of a database, EJBs that are referencing the same database will probably want to use the same database connection during a transaction so that all CRUD operations return consistent results.

29 Shareable resources EJB containers share resources by default, but resource sharing can be turned on or off explicitly with the shareable( ) attribute of annotation. Occasionally, advanced developers may run into situations where resource sharing is not desirable, and having the option to turn off resource sharing is beneficial. Unless you have a good reason for turning off resource sharing, we recommend that you set the shareable( ) attribute to true.

30 XML-based resource references The element defines a reference to a given resource. It contains the subelements (optional), (required), (required), (required), (optional), and (optional), as well as the element (optional) described in the first section of this chapter. Here is an example of a reference to a data source of TitanDB: TravelAgentBean jdbc/OracleDB javax.sql.DataSource Container java:/DefaultDS

31 Environment Entries In Lecture 11, the ProcessPayment EJB had a configurable property for minimum check number. These types of configurable properties are called environment entries. The bean can use environment entries to customize its behavior. Although they can be defined using annotations, environment entries are almost always configured via XML, as they really are configuration values and not metadata. The element is used to define them. This element contains the subelements (optional), (required), (required), and (optional), as well as the element (optional). Here is a typical declaration:

32 Environment Entries ProcessPaymentBean minCheckNumber java.lang.Integer 2000