3.12.3 Implementing the JavaBeans resources

This subsection uses examples to describe the procedure for implementing the JavaBeans resources.

Organization of this subsection
(1) Import settings
(2) Creating the implementation class of the JavaBeans resources
(3) Application settings
(4) Starting and terminating an application

(1) Import settings

This section describes the required settings and notes when you use the server management commands (cjimportjb command) to import the JavaBeans resources.

(a) Settings for the JavaBeans resource property file

Create the JavaBeans resource property file keeping the following points in mind:

An example of settings for the JavaBeans resource property file is as follows:

<!DOCTYPE hitachi-javabeans-resource-property PUBLIC '-//Hitachi, Ltd.//DTD JavaBeans Resource Property 7.0//EN'
'http://localhost/hitachi-javabeans-resource-property_7_0.dtd'>

<hitachi-javabeans-resource-property>
<description></description>
<display-name>JavaBean_resource</display-name>
<class-name>com.mycompany.mypackage.MyJavaBean</class-name>
<runtime>
<property>
<property-name>UserName</property-name>
<property-type>java.lang.String</property-type>
<property-value>Hitachi</property-value>
</property>
<property>
<property-name>UserID</property-name>
<property-type>java.lang.String</property-type>
<property-value>01234567​</property-value>
</property>
</runtime>
</hitachi-javabeans-resource-property>

The template file (jb_template.xml) of the JavaBeans resource property file is stored in the following directory:

Cosminexus-installation-directory\CC\admin\templates

(b) How to use the -d option

By using the -d option during an import operation, the JavaBeans resources can be imported with the same directory configuration and without creating an archive. Specify the top directory to be imported as the directory to be specified in -d.

An example of specifying the -d option when you import a JavaBeans resource is as follows. In this example, the "MyJavaBean" class with package name "com.mycompany.mypackage" is imported.

Directory-specified-in--d\
+ com\
+ mycompany
+ mypackage
+ MyJavaBean.class

The -d option imports all the files existing beneath the specified directory, so do not include unnecessary files in the directory.

Notes on importing multiple JavaBeans resources
You cannot import a JavaBeans resource with the same implementation class name as that of an imported JavaBeans resource. Delete the JavaBeans resource that was imported earlier and then import the target JavaBeans resource, or change and re-create the implementation class name, and then import the JavaBeans resource.

(2) Creating the implementation class of the JavaBeans resources

You declare the method for operating the data (property) managed by JavaBeans. To register the data, specify the set method (set + property-name). To reference the data, specify the get method (get + property-name).

An example implementation of the class that registers and references the JavaBeans resource is as follows:

package com.mycompany.mypackage;
public class MyJavaBean {
private String username;
private String userid;

public void setUserName(String user_name) {
this.username = user_name;
}
public void setUserID(String user_id) {
this.userid = user_id;
}
public String getUserName() {
return this.username;
}
public int getUserID() {
return this.userid;
}
}

(3) Application settings

This section describes the implementation and definitions required in an application when you use the JavaBeans resources.

(a) Implementing lookup (JavaBeans resources)

You obtain a JavaBeans resource by using lookup or DI. This section describes how to look up with the java:comp/env format.

Context initCtx = new InitialContext();
MyJavaBean jb = (MyJavaBean) initCtx.lookup("java:comp/env/bean/myJB");

Like other resources, the range of JavaBeans resource lookup is the application in the same J2EE server process.

(b) Contents defined in the DD (JavaBeans resources)

To use lookup to obtain a JavaBeans resource, you define the information on the name to be looked up and the implementation class name in the DD (ejb-jar.xml or web.xml). The tags to be specified are as follows:

Also, when you deploy a created application on the J2EE server, bind the reference name in lookup and the actual name with linked-to. To execute this operation, use the server management commands (cjsetappprop command).

The following is an example of the <resource-env-ref> tag in the property file passed by cjsetappprop:

<resource-env-ref>
<resource-env-ref-name>bean/myJB</resource-env-ref-name>
<resource-env-ref-type>com.mycompany.mypackage.MyJavaBean</resource-env-ref-type>
<linked-to>JavaBean_resource</linked-to>
</resource-env-ref>

(4) Starting and terminating an application

You start or terminate an application that uses a JavaBeans resource with the server management commands or Management Server. For details on how to start an application, see 10.2.1 Starting J2EE applications in the uCosminexus Application Server Application Setup Guide. For details on how to terminate an application, see 10.2.2 Stopping J2EE applications in the uCosminexus Application Server Application Setup Guide.

Application Server provides a sample program for JavaBeans resources. For an overview and the execution method of the sample program, see Appendix M.6 Sample program for JavaBeans resources in the uCosminexus Application Server System Setup and Operation Guide.