Tuesday 28 March 2017

Java Bean & JAR Files - Java Tutorials

Java Bean

What Is a Java Bean?

A Java Bean is a software component that has been designed to be reusable in a variety of different environments. There is no restriction on the capability of a Bean. It may perform a simple function, such as checking the spelling of a document, or a complex function, such as forecasting the performance of a stock portfolio. A Bean may be visible to an end user. One example of this is a button on a graphical user interface. A Bean may also be invisible to a user. Software to decode a stream of multimedia information in real time is an example of this type of building block. Finally, a Bean may be designed to work autonomously on a user’s workstation or to work in cooperation with a set of other distributed components. Software to generate a pie chart from a set of data points is an example of a Bean that can execute locally. However, a Bean that provides real-time price information from a stock or commodities exchange would need to work in cooperation with other distributed software to obtain its data.

You will see shortly what specific changes a software developer must make to a class so that it is usable as a Java Bean. However, one of the goals of the Java designers was to make it easy to use this technology. Therefore, the code changes are minimal.


Advantages of Java Beans

A software component architecture provides standard mechanisms to deal with software building blocks. The following list enumerates some of the specific benefits that Java technology provides for a component developer:
  • A Bean obtains all the benefits of Java’s “write-once, run-anywhere” paradigm.
  • The properties, events, and methods of a Bean that are exposed to an application builder tool can be controlled.
  • A Bean may be designed to operate correctly in different locales, which makes it useful in global markets.
  • Auxiliary software can be provided to help a person configure a Bean. This software is only needed when the design-time parameters for that component are being set. It does not need to be included in the run-time environment.
  • The configuration settings of a Bean can be saved in persistent storage and restored at a later time.
  • A Bean may register to receive events from other objects and can generate events that are sent to other objects.


Application Builder Tools

When working with Java Beans, most developers use an application builder tool, a utility that enables you to configure a set of Beans, connect them together, and produce a working application. In general, Bean builder tools have the following capabilities.
  • A palette is provided that lists all of the available Beans. As additional Beans are developed or purchased, they can be added to the palette.
  • A worksheet is displayed that allows the designer to lay out Beans in a graphical user interface. A designer may drag and drop a Bean from the palette to this worksheet.
  • Special editors and customizers allow a Bean to be configured. This is the mechanism by which the behavior of a Bean may be adapted for a particular environment.
  • Commands allow a designer to inquire about the state and behavior of a Bean. This information automatically becomes available when a Bean is added to the palette.
  • Capabilities exist to interconnect Beans. This means that events generated by one component are mapped to method invocations on other components.
  • When a collection of Beans has been configured and connected, it is possible to save all of this information in a persistent storage area. At a later time, this information can then be used to restore the state of the application.

Sun provides two Bean application builder tools. The first is the BeanBox, which is part of the Bean Developers Kit (BDK). The BDK is the original builder tool provided by Sun. The second is the new Bean Builder. Because Bean Builder is designed to supplant the BeanBox, Sun has stopped development of the BDK and all new Bean applications will be created using Bean Builder.

Although Bean Builder is the future of Bean development, it is not the sole focus of this chapter. Instead, both BeanBox and Bean Builder are discussed. The reason for this is that Bean Builder requires Java 2, version 1.4. It is incompatible with earlier versions of Java 2. This means that readers of this book using Java 2, version 1.2 or version 1.3 will not be able to use Bean Builder. Instead, they must continue to use the BDK. Further, readers using version 1.4 cannot use the BDK because it is not compatible with Java 2, version 1.4. So, if you are using version 1.4, then you must use Bean Builder. If you are using a version of Java prior to 1.4, you must use the BDK. Thus, both approaches are described here, beginning with the BDK. Keep in mind that the information about Beans, Bean architecture, JAR files, and so on, apply to either Bean development tool.

One other point: At the time of this writing, Java 2, version 1.4 is a released product, but Bean Builder is currently in beta testing. This means that the only way for a 1.4 user to create a Bean application is to do so using latest Bean Builder beta. For this reason, we will not examine its features in depth at this time. However, at the end of this chapter, a general overview is presented and a sample application is created.


Using the Bean Developer Kit (BDK)

The Bean Developer Kit (BDK), available from the JavaSoft site, is a simple example of a tool that enables you to create, configure, and connect a set of Beans. There is also a set of sample Beans with their source code. This section provides step-by-step instructions for installing and using this tool. Remember, the BDK is for use with versions of Java 2 prior to 1.4. For Java 2, v1.4 you must use the Bean Builder Tool described at the end of this chapter.

In this chapter, instructions are provided for a Windows environment. The procedures for a UNIX platform are similar, but some of the commands are different.

Installing the BDK
The Java 2 SDK must be installed on your machine for the BDK to work. Confirm that the SDK tools are accessible from your environment. The BDK can then be downloaded from the JavaSoft site (http://java.sun.com). It is packaged as one file that is a self-extracting archive. Follow the instructions to install it on your machine. The discussion that follows assumes that the BDK is installed in a directory called bdk. If this is not the case with your system, substitute the proper directory.

Starting the BDK
To start the BDK, follow these steps:
  1. Change to the directory c:\bdk\beanbox.
  2. Execute the batch file called run.bat. This causes the BDK to display the three windows s. ToolBox lists all of the different Beans that have been included with the BDK. BeanBox provides an area to lay out and connect the Beans selected from the ToolBox. Properties provides the ability to configure a selected Bean. You may also see a window called Method Tracer, but we won’t be using it.


Using the BDK
This section describes how to create an application by using some of the Beans provided with the BDK. First, the Molecule Bean displays a three-dimensional view of a molecule. It may be configured to present one of the following molecules: hyaluronic acid, benzene, buckminsterfullerine, cyclohexane, ethane, or water. This component also has methods that allow the molecule to be rotated in space along its X or Y axis. Second, the OurButton Bean provides a push-button functionality. We will have one button labeled “Rotate X” to rotate the molecule along its X axis and another button labeled “Rotate Y” to rotate the molecule along its Y axis.

Create and Configure an Instance of the Molecule Bean
Follow these steps to create and configure an instance of the Molecule Bean:
  1. Position the cursor on the ToolBox entry labeled Molecule and click the left mouse button. You should see the cursor change to a cross.
  2. Move the cursor to the BeanBox display area and click the left mouse button in approximately the area where you wish the Bean to be displayed. You should see a rectangular region appear that contains a 3-D display of a molecule. This area is surrounded by a hatched border, indicating that it is currently selected.
  3. You can reposition the Molecule Bean by positioning the cursor over one of the hatched borders and dragging the Bean.
  4. You can change the molecule that is displayed by changing the selection in the Properties window. Notice that the Bean display changes immediately when you change the selected molecule.


Create and Configure an Instance of the OurButton Bean
Follow these steps to create and configure an instance of the OurButton Bean and connect it to the Molecule Bean:
  1. Position the cursor on the ToolBox entry labeled OurButton and click the left mouse button. You should see the cursor change to a cross.
  2. Move the cursor to the BeanBox display area and click the left mouse button in approximately the area where you wish the Bean to be displayed. You should see a rectangular region appear that contains a button. This area is surrounded by a hatched border indicating that it is currently selected.
  3. You may reposition the OurButton Bean by positioning the cursor over one of the hatched borders and dragging the Bean.
  4. Go to the Properties window and change the label of the Bean to “Rotate X”. The button appearance changes immediately when this property is changed.
  5. Go to the menu bar of the BeanBox and select Edit | Events | action | actionPerformed. You should now see a line extending from the button to the cursor. Notice that one end of the line moves as the cursor moves. However, the other end of the line remains fixed at the button.
  6. Move the cursor so that it is inside the Molecule Bean display area, and click the left mouse button. You should see the Event Target Dialog dialog box.
  7. The dialog box allows you to choose a method that should be invoked when this button is clicked. Select the entry labeled “rotateOnX” and click the OK button. You should see a message box appear very briefly, stating that the tool is “Generating and compiling adaptor class.”

Test the application. Each time you press the button, the molecule should move a few degrees around one of its axes. Now create another instance of the OurButton Bean. Label it “Rotate Y” and map its action event to the “rotateY” method of the Molecule Bean. The steps to do this are very similar to those just described for the button labeled “Rotate X”. Test the application by clicking these buttons and observing how the molecule moves.




JAR Files

Before developing your own Bean, it is necessary for you to understand JAR (Java Archive) files, because tools such as the BDK expect Beans to be packaged within JAR files. A JAR file allows you to efficiently deploy a set of classes and their associated resources. For example, a developer may build a multimedia application that uses various sound and image files. A set of Beans can control how and when this information is presented. All of these pieces can be placed into one JAR file.

JAR technology makes it much easier to deliver and install software. Also, the elements in a JAR file are compressed, which makes downloading a JAR file much faster than separately downloading several uncompressed files. Digital signatures may also be associated with the individual elements in a JAR file. This allows a consumer to be sure that these elements were produced by a specific organization or individual.

The package java.util.zip contains classes that read and write JAR files.


Manifest Files

A developer must provide a manifest file to indicate which of the components in a JAR file are Java Beans. An example of a manifest file is provided in the following listing. It defines a JAR file that contains four .gif files and one .class file. The last entry is a Bean.

  Name: sunw/demo/slides/slide0.gif
  Name: sunw/demo/slides/slide1.gif
  Name: sunw/demo/slides/slide2.gif
  Name: sunw/demo/slides/slide3.gif
  Name: sunw/demo/slides/Slides.class
  Java-Bean: True

A manifest file may reference several .class files. If a .class file is a Java Bean, its entry must be immediately followed by the line “Java-Bean: True”.

The JAR Utility
A utility is used to generate a JAR file. Its syntax is shown here:

      jar options files

The following examples show how to use this utility.

Creating a JAR File
The following command creates a JAR file named Xyz.jar that contains all of the .class
and .gif files in the current directory:

  jar cf Xyz.jar *.class *.gif

If a manifest file such as Yxz.mf is available, it can be used with the following command:

  jar cfm Xyz.jar Yxz.mf *.class *.gif


JAR Command Options

c      -   A new archive is to be created.

C     -   Change directories during command execution.

f      -   The first element in the file list is the name of the archive that is to be created or accessed.

i      -   Index information should be provided.

m    -   The second element in the file list is the name of the external manifest file.

M    -   Manifest file not created.

t      -   The archive contents should be tabulated.

u     -   Update existing JAR file.

v     -   Verbose output should be provided by the utility as it executes.

x    -   Files are to be extracted from the archive. (If there is only one file, that is the name of the archive, and all files in it are extracted. Otherwise, the first element in the file list is the name of the archive, and the remaining elements in the list are the files that should be extracted from the archive.)

0     -   Do not use compression.


Tabulating the Contents of a JAR File
The following command lists the contents of Xyz.jar:

  jar tf Xyz.jar

Extracting Files from a JAR File
The following command extracts the contents of Xyz.jar and places those files in the current directory:

  jar xf Xyz.jar

Updating an Existing JAR File
The following command adds the file file1.class to Xyz.jar:

  jar -uf Xyz.jar file1.class

Recursing Directories
The following command adds all files below directoryX to Xyz.jar:

  jar -uf Xyz.jar -C directoryX *

No comments:

Post a Comment