550 likes | 628 Views
Chapter 15. Advanced Features of Java. Objectives. Recognize the importance of component-based development. Use the BeanBox to display and modify existing JavaBeans. Add events to connect multiple beans. Create your own JavaBean components and run them in the BeanBox.
E N D
Chapter 15 Advanced Features of Java
Objectives • Recognize the importance of component-based development. • Use the BeanBox to display and modify existing JavaBeans. • Add events to connect multiple beans. • Create your own JavaBean components and run them in the BeanBox. • Recognize the importance of RMI and CORBA to client/server applications.
Objectives Continued • Understand how the Java Native Interface (JNI) allows Java programs to use methods written in other languages. • Be aware of issues affecting the internationalization of programs. • Know the options available to make software more accessible to handicapped individuals. • Explain the parts of the java.security package and how applets can be signed.
Introspection, BeanBoxes and the BeanInfo Class • Java uses a technique called introspection to look at a component and determine the properties, methods, and events that are available to other components or applications. • The java.beans.Introspector class handles the introspection process. • If you use the proper naming conventions for methods that retrieve and assign property values, the Introspector class can determine the exposed properties. • Several vendors supply a tool, called a BeanBox tool,which you can use to test JavaBeans.
Introspection, BeanBoxes and the BeanInfo Class Continued • You can add your bean into the bean box container, along with other beans with which your bean must interact. • When you add your bean to the bean box, the tool handles the introspection by identifying the exposed properties, methods, and events. • If you don't follow naming conventions for your bean, introspection cannot determine the exposed properties and methods. • You must create a BeanInfo class to specify the interface information.
Introspection, BeanBoxes and the BeanInfo Class Continued • The BeanInfo class contains FeatureDescriptor objects and subclasses for each feature including a BeanDescriptor, PropertyDescriptor, MethodDescriptor, and an EventSetDescriptor. • Another application that is sometimes used to pass data between components is InfoBus. • You can use InfoBus to define the interface of components and pass data, including arrays and rows of a dataset from one bean to another.
Using the BeanBox Tool • You can use any one of several BeanBox tools to test beans. • The examples in this chapter use Sun's BDK (Bean Development Kit), which you can download and install on your system. • The BDK is available in versions for Windows, NT, and Solaris.
Downloading and Installing the BDK • Download the correct version of BDK for your system. • For Windows, make sure to change the default location to a folder to beneath the root. • For Windows, make sure to change the default location to a folder to beneath the root.
Running the BDK • Once you have installed the BDK, you can run the program in Windows by double-clicking on the run.bat file in the BDK1.1\beanbox folder.
The BDK User Interface • The BeanBox has several windows including the ToolBox, the BeanBox, and the Properties Sheet. • The ToolBox contains the beans that are stored in the Bdk1.1\jars folder. • When you create your own beans, you place their JAR files in this folder and the beans automatically appear in the ToolBox. • Use the BeanBox to create instances of beans and test their operation.
Properties • You can change the values in the Property Sheet (also called the Properties window or the Properties palette). • For text values, just type a new value. If the property is a color, you can double-clicking on the color and an input box appears.
Designing Beans • When you design a new JavaBean, you follow a procedure similar to that for any other class. • List the properties. • Determine what events the component must respond to or must fire. • Identify which properties, methods, and events are public. • Decide if there are any initial property values that should be set.
Inheritance in Beans • Each new graphical bean that you design is based on some other component. • You can choose to create a bean that inherits from a Panel or a Canvas, or use a more specialized component such as a Label or a TextArea. • You can base a bean on AWT components, Swing components, or any other components that you have available. • The new bean inherits all properties and methods of the super class.
Properties • To create a new property for a JavaBean, you usually declare a private variable to hold the value and a pair of public "get" and "set" methods. • If you follow this pattern, the introspection process can identify the bean's properties.
Properties Continued • It's good practice to declare the methods as synchronized, in case the bean is used in a multithreaded application. • private double dblMiles; • public synchronized double getMiles() • { • //Retrieve the current value of Miles property • return dblMiles; • } • public synchronized setMiles (double dblMilesNew) • { • //Set value for Miles property • dblMiles = dblMilesNew; • }
Properties Continued • This example that the property name is "Miles." • The Introspector determines the name from your get and set methods, not from the name of the private variable used to hold the property value.
Saving Property Values • Each time you add an object to the BeanBox (or to the interface of an applet or application), you declare a new instance of the class. • You can change the properties of each object independently. • For example, each button has its own label property. • Each of those button objects must somehow save the set of changed properties, so that next time it is displayed the correct values appear. • JavaBeans use Object Serialization to store the property values of an object.
Saving Property Values Continued • Each time the bean is displayed, the saved property values are restored. • And each time the bean disappears from view, such as when the window is covered by another window, the property values are saved. • When you create a new bean, you must import java.io.Serializable and implement the Serializable interface • All that is handled by the Serializable interface.
Indexed Properties • Properties may contain a single value or multiple values. • A property with multiple values is called an indexed property. • Indexed properties are similar to an array; you can get and set individual elements using an integer subscript.
Bound Properties • A property of a bean may be bound to a property of another component. • If a change is made to the value of a bound property, the component to which it is bound is notified. • Bound properties are implemented similarly to action listeners. • You must add a PropertyChangeListener to the component to be notified. • In the bean's class, when the value of a bound property changes, you must initiate the action to notify all components that have registered a listener.
Constrained Properties • A constrained property is a property that cannot be changed without first checking with another component. • You must add a VetoableChangeListener and a vetoableChange method in the component that gives permission for the change. • The code in the bean's class must notify the component of any changes in a property, which can be overridden by the component.
Methods • When you create a new bean class, you should always create a constructor method with no arguments. • Although this practice is not mandatory, it is accepted as good programming style and is necessary for the BeanBox to instantiate an object of the class from the Toolbox. • To create other methods, you will need property accessor methods (the set and get methods), along with any other methods needed to implement the functions of the bean.
Events • The objects that you create from your new classes can generate events, which the applet or application can respond to or ignore. • For example, if a condition exists in an object and the user should be notified, your object should not display a message to the user; the user interface must display the message. • Your object must either fire an event or throw an exception, to which the applet or application can respond. • An object that generates or raises an event is called the eventsource. • The object that responds to an event is called an eventlistener.
Creating a JavaBean • When you create a bean, keep in mind the elements that allow a class to be a JavaBean: • 1. You must be able to instantiate the class. This means that the bean cannot be an abstract class or an interface. • 2. You should include a constructor with no parameters. You can also include additional constructors that do have parameters, if necessary. • 3. The class must import java.io.Serializable and implement the Serializable interface. • 4. You used this interface in Chapter 13 for object serialization. Here you need it for persistence. • 5. You should follow the naming patterns to provide for introspection (get and set methods). Otherwise, you must create a BeanInfo class to expose the properties and methods.
Creating a JavaBean Continued • Compile the code • Write the Manifest file • Create the Archive file • Move the Archive file
Client/Server Applications • Today's computing environment requires applications to run on multiple systems. • Often the application and/or library files reside on one computer, called the server. • The system from which the user is working is called the client. • Next we will describe several Java features that facilitate client/server computing.
RMI • Remote Method Invocation (RMI) allows you to use an object that exists on a different computer. • The remote system can even call the methods of the object, just as if the object existed on the local machine. • This technique is used extensively in client/server applications. • The java.rmi package contains the classes to implement RMI.
CORBA • Many client/server systems rely on Common Object Request Broker Architecture (CORBA) for implementation. • Java can work with CORBA. • The basis of CORBA relies on an ORB (Object Request Broker). • The ORB can call methods from objects in any language that have an Interface Design Language (IDL). • The IDL is not a programming language but is used to define the interface for objects. • The Java IDL defines the remote methods and attributes for the interface.
CORBA Continued • With ORB, the programmer does not need to know the actual location of the class declaration or the language used to create the class. • CORBA has several services to expedite client/server applications.
CORBA Continued • Transaction processing in CORBA controls the operation over multiple objects using such techniques as locking, commit, and rollback.
DCOM • Distributed Component Object Model (DCOM) is Microsoft's component architecture. • At times you may need use CORBA to work with a system that uses DCOM. • In that situation, you can connect a DCOM client to communicate with a CORBA server by using the Microsoft's DCOM-to-CORBA bridge.
JNI • Java Native Interface (JNI) allows Java programs to use methods that were created in another programming language. • Prior to the introduction of JNI into Java, both Netscape and Microsoft generated their own techniques for incorporating "native" code into a Java application. • The current JNI most closely resembles the Netscape version. • There are many obvious advantages to having JNI. • It allows a program to use existing code (often called legacycode) regardless of the format in which it exists. • The ability to interface with other systems is also enhanced.
JNI Continued • There is also a downside to JNI: • If the original program is an applet, the security on a browser may stop the program from running with native methods. • In addition, the incorporation of JNI into an application eliminates the cross-platform advantages of Java.
JNI Continued • What exactly does JNI allow a programmer to do? • Pass primitive data or objects to a native method. • Return data from the method. • The native method can access, create, and update Java objects. • Allow the native method to throw and catch exceptions. • Incorporate runtime type checking in the native method. • Provide synchronization for multi-threading in the native method.
Java Servlets • A Java servlet provides a way to create interactive web applications written in Java. • A servlet is like an applet that runs on the server side and is platform-independent. • The servlet can allow a web developer to access existing business applications. • In addition to the JDBC API, servlets have access to libraries of HTTP-specific calls. • Third-party servlet containers are available for Microsoft IIS, Apache Web Server, and iPlanet Web Server (Netscape Enterprise Server).
XML • In addition to HTML, another markup language, XML is gaining popularity. • XML is designed for portability and cross-platform handling of data over the Internet. • Sun has announced plans to develop a Java standard extension for XML.
JINI • Jini technology is both a hardware and software concept, covered here because it is used for connectivity. • Basically the Jini concept is that devices should be easy to connect without a concern about drivers, operating system problems or unusual cables.
Internationalization • Internationalization issues include formatting, language, and even alphabets. • The Locale object allows a program to produce different results when executed in different countries. • Java uses Unicode to store characters, rather than ASCII. Unicode provides flexibility and the ability to store large character sets needed for some languages. • The text prompts and messages should display in the local language. • Rather than "hard code" the text strings for captions, labels, and messages, into the program, you can keep the text separate from the program and retrieve it at run time. • This is accomplished with the ResourceBundle class, which is contained in the java.util package.
Accessibility • Java contains support for people with disabilities. • These "assistive technologies" include screen readers, screen magnifiers, and speech recognition. • The Accessibility API works with JFC and AWT components. • The assistive features allow programs to meet federal regulations and the needs of a large market.
Security • One of the advantages of Java is the security provided with the language. • Security includes the validity of classes as well as the voluminous exception handling incorporated into the code. • Another area of security is the resource restrictions placed on applets running in a browser, such as access to local files and printers. • The Java compiler enforces many of the security rules. • One important difference between Java and C++ is that Java does not allow pointer arithmetic. • A pointer references a memory address; this restriction is to protect data in a system's memory.
SecurityContinued • Unlike C++, Java checks the boundaries of arrays. • If you try to access an area of memory outside of defined areas, Java creates an exception (error condition). • And network connections are limited to the host machine where an applet came from. • Security is also provided by the JVM, which contains a verifier and a class loader. • The class loader locates all classes used by an applet. • A verifier tests classes so that only appropriate classes are loaded. • The goal is to determine that a hostile compiler cannot generate dangerous bytecode.
SecurityContinued • There are several steps in verification: • The general format. • Java conventions. • bytecode verification. • The existence of classes. • Tests for syntactic accuracy avoid such problems as overflow. • Any untrusted classes are not allowed to execute or use system resources. • The security package in Java allows a program to check a digital signature from the developer. • This theoretically determines that the program has not been altered and does not contain any viruses or malicious operations.
SecurityContinued • However, if the application has complete control over the client machine and something does go wrong, the only thing you have is the id from the signature of the perpetrator. • Finally, the Java API contains a Security Manager class for defining the tasks that an application can perform.