Table of Contents

preface xv
acknowledgments xvii
about this book xviii
author online xxii
about the cover illustration xxiii

Part I Appetizers 1

1 Client code 3
1.1 Invoking a local EJB from another EJB 4
1.2 Invoking a remote EJB from another EJB 6
1.3 Accessing EJBs from a servlet 8
1.4 Invoking an EJB from a JavaServer Page 12
1.5 Invoking EJB business logic from a JMS system 15
1.6 Persisting a reference to an EJB instance 18
1.7 Retrieving and using a persisted EJB reference 20
1.8 Persisting a home object reference 21
1.9 Comparing two EJB references for equality 23
1.10 Using reflection with an EJB 25
1.11 Invoking an EJB from an applet 27
1.12 Improving your client-side EJB lookup code 31
2 Code generation with XDoclet 33
An XDoclet appetizer 35
2.1 Generating home, remote, local, and local home interfaces 37
2.2 Adding and customizing the JNDI name for the home interface 43
2.3 Keeping your EJB deployment descriptor current 45
2.4 Creating value objects for your entity beans 47
2.5 Generating a primary key class 53
2.6 Avoiding hardcoded XDoclet tag values 56
2.7 Facilitating bean lookup with a utility object 58
2.8 Generating vendor-specific deployment descriptors 62
2.9 Specifying security roles in the bean source 63
2.10 Generating and maintaining method permissions 64
2.11 Generating finder methods for entity home interfaces 66
2.12 Generating the ejbSelect method XML 67
2.13 Adding a home method to generated home interfaces 68
2.14 Adding entity relation XML to the deployment descriptor 70
2.15 Adding the destination type to a message-driven bean deployment descriptor 71
2.16 Adding message selectors to a message-driven bean deployment descriptor 73

Part II Main courses 75

3 Working with data 77
3.1 Using a data source 78
3.2 Creating EJB 2.0 container-managed persistence 81
3.3 Using different data sources for different users 85
3.4 Using a database sequence to generate primary key values for entity beans 88
3.5 Using a compound primary key for your entity beans 92
3.6 Retrieving multiple entity beans in a single step 95
3.7 Modeling one-to-one entity data relationships 97
3.8 Creating a one-to-many relationship for entity beans 101
3.9 Using entity relationships to create a cascading delete 104
3.10 Developing noncreatable, read-only entity beans 107
3.11 Invoking a stored procedure from an EJB 109
3.12 Using EJB-QL to create custom finder methods 111
3.13 Persisting entity data into a database view 115
3.14 Sending notifications upon entity data changes 117
3.15 Creating an interface to your entity data 120
3.16 Retrieving information about entity data sets 122
3.17 Decreasing the number of calls to an entity bean 124
3.18 Paging through large result sets 126
4 EJB activities 133
4.1 Retrieving an environment variable 134
4.2 Implementing toString() functionality for an EJB 136
4.3 Providing common methods for all your EJBs 137
4.4 Reducing the clutter of unimplemented bean methods 139
4.5 Sending an email from an EJB 144
4.6 Using the EJB 2.1 timer service 145
Sending a JMS message from an EJB 147
4.7 Using an EJB as a web service 149
4.8 Creating asynchronous behavior for an EJB client 151
4.9 Creating asynchronous behavior without message-driven beans 156
4.10 Insulating an EJB from service class implementations 157
4.11 Creating a batch process mechanism 159
5 Transactions 163
A transaction appetizer 165
5.1 Tuning the container transaction control for your EJB 166
5.2 Handling transaction management without the container 169
5.3 Rolling back the current transaction 170
5.4 Attempting error recovery to avoid a rollback 172
5.5 Forcing rollbacks before method completion 175
5.6 Imposing time limits on transactions 176
5.7 Combining entity updates into a single transaction 177
5.8 Managing EJB state at transaction boundaries 179
5.9 Using more than one transaction in a method 181
5.10 Managing EJB state after a rollback 183
5.11 Throwing exceptions without causing a rollback 184
5.12 Propagating a transaction to another EJB business method 186
5.13 Propagating a transaction to a nonEJB class 188
5.14 Starting a transaction in the client layer 190
5.15 Holding a transaction across multiple JavaServer Pages 191
5.16 Updating multiple databases in one transaction 193
6 Messaging 197
6.1 Sending a publish/subscribe JMS message 198
6.2 Sending a point-to-point JMS message 200
6.3 Creating a message-driven Enterprise JavaBean 202
6.4 Processing messages in a FIFO manner from a message queue 205
6.5 Insulating message-driven beans from business logic changes 209
6.6 Streaming data to a message-driven EJB 210
6.7 Triggering two or more message-driven beans with a single JMS message 213
6.8 Speeding up message delivery to a message-driven bean 216
6.9 Filtering messages for a message-driven EJB 219
6.10 Encapsulating error-handling code in a message-driven EJB 221
6.11 Sending an email message asynchronously 223
6.12 Handling rollbacks in a message-driven bean 225
7 Security 229
7.1 Finding the identity and role of the caller inside an EJB method 231
7.2Assigning and determining EJB client security roles 232
7.3 Passing client credentials to the EJB container 234
7.4 Disabling methods for certain users 235
7.5 Assigning a role to an EJB 238
7.6 Preventing access to entity data 239
7.7 Using EJBs to handle simple authentication with an LDAP source 241
7.8 Securing a message-driven bean 242

part III Desserts 245

8 Logging 247
A log4j appetizer 248
8,1 Formatting log messages 251
8.2 Improving logging performance 254
8.3 Using logging to generate reports 257
8.4 Sending log messages to a JMS topic 258
8.5 Logging to an XML file 259
8.6 Creating log file views for the web browser 261
8.7 Creating a centralized log file in a clustered environment 263
8.8 Tracking the lifecycle of an EJB 265
8.9 Using a different configuration at runtime 267
8.19 Sorting log messages by client 269
9 Deploying and unit testing 273
A deployment and testing appetizer 274
9.1 Compiling Enterprise JavaBeans 278
9.2 Building the ejb.jar file 280
9.3 Building Enterprise JavaBean stub classes 283
9.4 Creating a stateless session bean unit test 286
9.5 Creating a stateful session bean unit test 290
9.6 Creating an entity bean unit test 292
9.7 Automating test case execution 294
9.8 Executing test cases using a UI 298
 
appendix A Mixing it up: related recipes 303
appendix B Second helpings: additional resources 315
index 317