Chapter 8 Creating Enterprise JavaBean Components
In most cases it is easiest to define a Bean using one of the import methods described in "Importing Enterprise JavaBeans". However, if you prefer editing IDL to Java, you may follow the technique described here. Also, you can follow the IDL interface editing steps to edit a Bean's interfaces or primary key type for an EJB component that was created by the importer.
Creating a new EJB component from scratch:
Follow this procedure to create a new EJB component and define the home and remote interface.
Component type |
To indicate |
---|---|
EJB - Entity Bean |
An entity Bean |
EJB - Stateful Session Bean |
A stateful session Bean |
EJB - Stateless Session Bean |
A stateless session Bean |
The Home Interface Class, Remote Interface Class, and Primary Key Class fields cannot be edited. These fields are set automatically after the Bean's IDL interfaces and datatypes have been defined. You can change them by changing the component's IDL interfaces and types in subsequent steps.
If portability to other EJB servers is required, use only in parameters in remote interface methods.
Stubs generated automatically When you generate skeletons for your component, stub source is also generated under the same code base. You do not need to select the stubs option.
Define an entity Bean's primary key as one of the following:
An IDL structure The structure should reflect the primary key for the database relation that the entity Bean represents. In other words, add a field for each column in the primary key. Define the structure to match the intended Java package and class name. For example, if the Java class is to be foo.bar.PK1, define a new structure PK1 in module foo::bar. See Chapter 5, "Defining Component Interfaces" in the Jaguar CTS Programmer's Guide for more information.
The name of a serializable Java class Enter the name of a serializable Java class, for example: foo.bar.MyPK.
The IDL string type Use string if the key relation has only a string column. In Java, the mapped primary key is java.lang.String.
Interoperability and key types Define your entity Bean's primary key as an IDL structure or string if other types of clients besides Java will use the Bean.
You can add methods to a home interface using the techniques described in Chapter 5, "Defining Component Interfaces" in the Jaguar CTS Programmer's Guide. However, the method signatures in a home interface must follow the design patterns described here to ensure that the generated code works as intended.
Patterns for create methods All Beans can have create methods, which clients call to instantiate proxies for session Beans and insert new data for entity Beans.
Create methods must return the Bean's IDL remote interface type and raise CtsComponents::CreateException. Create methods can take any number of in parameters. To distinguish multiple overloaded create methods, append two underscores and a unique suffix. (This is the standard Java to IDL mapping for overloaded method names. When generating stubs for C++ and Java, Jaguar removes the underscores and suffix from the stub method name). The pattern is as shown below:
remote-interface create
(
in-parameters
) raises (CtsComponents::CreateException);
remote-interface create__suffix1
(
in-parameters
) raises (CtsComponents::CreateException);
Patterns for finder methods Only entity Beans can have finder methods. Clients call finder methods to look up entity instances for existing database rows. Names of finder methods typically have names beginning with find .
Every entity Bean must have a findByPrimaryKey method that matches the following pattern:
remote-interface findByPrimaryKeywhere remote-interface is the IDL remote interface, and pk-type is the IDL type of the primary key.
(
in pk-type primaryKey
) raises (CtsComponents::FinderException)
Entity Beans can have additional finder methods of two types:
remote-interface findSuffixwhere remote-interface is the IDL remote interface, Suffix is a name suffix other than ByPrimaryKey , and in-parameters is a valid parameter list composed solely of in parameters.
(
in-parameters
) raises (CtsComponents::FinderException)
componentList findSuffixwhere component is the component name, Suffix is a name suffix other than ByPrimaryKey , and in-parameters is a valid parameter list composed solely of in parameters.
(
in-parameters
) raises (CtsComponents::FinderException)
Sequence types are automatically generated Jaguar Manager creates IDL typedefs defining a sequence of remote interface methods and a sequence of primary keys when you set the Primary Key field on the Persistence tab of the Component Properties dialog box. The type for a sequence of remote interface instances is componentList and a sequence of primary keys is componentKeys, where component is the component name.
The IDL for your Enterprise Bean's remote interface must define a remove method and the business methods implemented by the Bean.
remove methods are called by clients to delete the database row associated with an entity Bean, and to release a reference to a session Bean instance. remove methods have the following signature:
void remove
(
)
raises (::CtsComponents::RemoveException);
You can define business methods graphically or using the IDL editor window. The procedure is the same as for any other IDL interface. See Chapter 5, "Defining Component Interfaces" in the Jaguar CTS Programmer's Guide for more information.
If portability to other EJB servers is required, use only in parameters in remote interface methods.
Your EJB can use EJB references to instantiate proxies for other EJBs. See Chapter 8, "Creating Enterprise JavaBean Clients," in the Jaguar CTS Programmer's Guide for more information.
You do not need to create references in order to invoke other EJBs from your code. However, doing so ensures that EJB references will be cataloged in the deployment descriptor if you export the EJB.
Stubs used for EJB references must be in the custom class list You must list stubs used for intercomponent calls in the custom class list for your component, as described in "The custom class list".
To add an EJB reference:
To edit an EJB reference:
ejb/
.
For example, if your code refers to java:comp/env/ejb/MyBean,
enter ejb/MyBean
.com.sybase.MyBeanHome
.com.sybase.MyBeanRemote
.Resource references are used to obtain database connections, JavaMail sessions, and URL factories.
To add a resource reference:
To edit a resource reference:
jdbc/MyDatabase
.http://www.sybase.com
or ftp://pub.sybase.com
.
To restrict access to an EJB 1.1 component, you must configure method permissions settings as described below, or you can call the isCallerInRole Java method to restrict access. If you call the isCallerInRole Java method, you must configure role references to map names used in isCallerInRole calls to J2EE role names that are configured in the package properties.
To restrict access in an EJB 1.0 component, use the Roles facility in Jaguar Manager. See Chapter 5, "Security Configuration" in the Jaguar CTS System Administration Guide for more information.
EJB 1.1 uses a different security model than other component types EJB 1.1 component security uses method level constraints rather than the package and component Role constraints used for other component models. The Roles folder does not display for EJB 1.1 components, or for packages that contain only EJB 1.1 components. If EJB 1.1 components are installed in a package that contains other component types, the package role folder has no effect on the EJB 1.1 components.
To configure EJB 1.1 method permissions:
To configure EJB 1.1 role references:
Environment properties allow you to specify read-only data for use by an EJB. For example, you might use environment properties to tune the size of a data cache used in your implementation, or to specify the name of a log file. Use environment properties for any constant value that might change when the EJB is deployed to another server.
When coding your EJB, use JNDI to retrieve environment properties,
using the prefix java:comp/env
in
JNDI lookups.
When you export your EJB, the deployment descriptor catalogs the environment properties used by your servlets and JSPs, as well as each property's Java datatype and default value. When the EJB is imported to another server, the deployer can override the default value for each environment property.
Environment properties for EJB 1.0 components
An EJB 1.0 component can only have environment properties
with datatype String, and these properties must
be configured in the All Properties window. Any property name that
does not begin with com.sybase.jaguar.component
is
considered an environment property. In source code, use the EJBContext.getEnvironment method
to retrieve property values. You cannot use the JNDI InitialContext.lookup
method to retrieve these values.
To add an EJB 1.1 environment property:
To edit EJB 1.1 environment properties:
If you are creating components from scratch in Jaguar Manager, you must follow the steps in this section to deploy the component class and other classes that it depends on. If you deploy from PowerJ, PowerJ performs these steps for you. If you are using another EJB development tool that can export EJB JAR files, import the EJB JAR file as described in "Importing Enterprise JavaBeans". If you import an EJB JAR file that calls Jaguar components that are not implemented in the same JAR file, you must list the stub classes for the called components in the custom class list as described below.
Jaguar supports hot refresh of components by using a Java class loader. This feature speeds the development process by allowing you to deploy new class versions without restarting Jaguar server. Repeat the steps below to deploy new versions of your implementation.
In a production environment, you may wish to disable refresh to improve performance. See "Disabling component Refresh" for details.
To deploy EJB component classes:
The preferred code base is java/classes For security reasons, it is preferable to deploy Java components to the java/classes subdirectory or some other directory that is not accessible to HTTP downloads. Deploying to this directory also allows your component to be refreshed, and allows you to deploy classes in JAR files without reconfiguring the server's CLASSPATH environment variable. If you deploy to another location, make sure it is listed in the server's CLASSPATH environment variable.
The custom class list To support component refresh, you must specify the custom class list to be loaded when a component is refreshed in the com.sybase.jaguar.component.java.classes component property. This property must be set on the All Properties tab in the Component Properties dialog box. "com.sybase.jaguar.component.java.classes" describes the syntax of this property.
The custom class list for an EJB component must contain these classes:
com.sybase.ejb.*;javax.naming.*;javax.naming.spi.*
Troubleshooting ClassCastException errors When calling javax.naming.InitialContext.lookup, if you see NamingContext exceptions with root-cause exception ClassCastException, check for the following errors:
Disabling component Refresh In a production server, you may wish to disable refresh for Java components to decrease memory use and increase performance. When refresh is enabled, duplicate copies of common Java classes can be loaded for components. When refresh is disabled, you must restart the Jaguar server in order for it to load a new version of your component class.
Refresh property and intercomponent calls If your component calls another Java component, both must have refresh enabled or both must have refresh disabled.
To disable refresh:
com.sybase.jaguar.component.refresh
to
false (the default is true).
Copyright © 2000 Sybase, Inc. All rights reserved. |