Java Beans Joe Komar 2112022 Komar Associates 1
Java. Beans Joe Komar 2/11/2022 Komar Associates 1
Java. Beans Features n Discrete -- small and combinable n Reusable n Configured visually with IDE n Changeable properties n Communicate with other component models such as Active X 2/11/2022 Komar Associates 2
Java. Beans Services n Property Management n Introspection n Event Handling n Persistence n Application Builder Support 2/11/2022 Komar Associates 3
Property Management n n Properties are the data part of the Bean Changed via: u Accessor methods at run time -- setters and getters u Visually in an IDE u Through persistence (storage and retrieval) u Via scripts such as VBScript or Java. Script 2/11/2022 Komar Associates 4
Properties n n n Simple -- single value (color, location, etc. ) Indexed -- array of values Bound -- notifies interested parties when the property value has changed u Interested party -- application, applet, another Bean n Constrained -- interested party validates a new value and can “veto” that value 2/11/2022 Komar Associates 5
Simple Property import java. awt. *; public class Junk 1 extends Canvas { String my. String = "Junker"; public Junk 1 (){ set. Background(Color. red); set. Foreground(Color. blue); } public void set. Name(String new. Value){ my. String = new. Value; } public String get. Name(){ return my. String; } public Dimension get. Minimum. Size(){ return new Dimension(50, 50); } } 2/11/2022 Komar Associates 6
Indexed Properties n n n Represents an array of values (primitive or object) getters and setters take an integer index value May also support getting and setting the entire array with overloaded getters and setters 2/11/2022 Komar Associates 7
Bound Properties import java. awt. *; import java. beans. *; public class Junk 1 extends Canvas { String my. String = "Junker"; private Property. Change. Support changes = new Property. Change. Support(this); public Junk 1 (){ set. Background(Color. red); set. Foreground(Color. blue); } public void set. Name(String new. Value){ String old. String = new. Value; my. String = new. Value; changes. fire. Property. Change("name", old. String, new. Value); } 2/11/2022 Komar Associates 8
Bound Properties public void add. Property. Change. Listener (Property. Change. Listener listener){ changes. add. Property. Change. Listener(listener); } public void remove. Property. Change. Listener (Property. Change. Listener listener){ changes. remove. Property. Change. Listener(listener); } 2/11/2022 Komar Associates 9
Constrained Properties public class Junk 1 extends Canvas { String my. String = "Junker"; private Property. Change. Support changes = new Property. Change. Support(this); private Vetoable. Change. Support vetos = new Vetoable. Change. Support(this); 2/11/2022 Komar Associates 10
Constrained Properties public void set. Name(String new. Value) throws Property. Veto. Exception { String old. String = new. Value; vetos. fire. Vetoable. Change("name", old. String, new. Value); my. String = new. Value; changes. fire. Property. Change("name", old. String, new. Value); } 2/11/2022 Komar Associates 11
Constrained Properties public void add. Vetoable. Change. Listener (Vetoable. Change. Listener listener){ vetos. add. Vetoable. Change. Listener(listener); } public void remove. Vetoable. Change. Listener (Vetoable. Change. Listener listener){ vetos. remove. Vetoable. Change. Listener(listener); } 2/11/2022 Komar Associates 12
Introspection n n Ability to make public data, methods, and properties visible Uses the Reflection API and the Serialization API Uses low level design patterns (e. g. “set…”) Intended primarily for IDE use 2/11/2022 Komar Associates 13
Bean Design Patterns n Simple Properties: public <Property. Type> get<Property. Name>(); public void set<Property. Name>(<Property. Type> t); Example: public Color get. Highlight. Color(); public void set. Highlight. Color(Color c); n Boolean properties: public boolean is<Property. Name>(); e. g. public boolean is. Plant(); 2/11/2022 Komar Associates 14
Bean Design Patterns n Indexed Properties: public <Property. Element> get<Property. Name>(int a); public void set<Property. Name>(int a, <Property. Element> b); Examples: public Member get. Member(int a); public void set. Member(int a, Member b); n Events: public void add<Event. Listener. Type>(<Event. Listener. Type a); public void remove<Event. Listener. Type>(<Event. Listener. Type a); e. g. public void add. The. Listener(The. Listener l); public void remove. The. Listener(The. Listener l); 2/11/2022 Komar Associates 15
Bean. Info n n n <Bean. Name>Bean. Info class implements the Bean. Info interface Provides methods for learning about the Bean’s events, properties, and methods. Bean. Info class can choose what to expose explicitly and what to leave to introspection (java. beans. Introspector) 2/11/2022 Komar Associates 16
Event Handling n n Event Source -- generates events Event Listener -- responds to events Event Listeners are “registered” with the event sources When the event occurs in the source, a specific method (with appropriate state variables) is called in all registered listeners 2/11/2022 Komar Associates 17
“Event State” Objects n n Subclass of java. util. Event. Object End with the word Event Public class Mouse. Moved. Event extends java. util. Event. Object Moved. Event(java. awt. Component source, Point location){ super(source); x = location. x; y = location. y; } public Point get. Location() { return new Point(x , y); } 2/11/2022 Komar Associates 18
Event Handling methods n n n Even. Listener interfaces Inherit from java. util. Event. Listener End with the word listener interface Mouse. Moved. Listener extends java. util. Event. Listener { // defines listener methods that any event listeners for // Mouse. Moved events must support void mouse. Moved(Mouse. Moved. Event mme); } class Any. Class implements Mouse. Moved. Listener { public void mouse. Moved(Mouse. Moved. Event mme) {…} } 2/11/2022 Komar Associates 19
Event Listener Registration n Event source classes provide methods for registering and de-registering listeners Interface Mouse. Moved. Listener extends java. util. Event. Listener { void mouse. Moved(Mouse. Moved. Event mme); } public class Junk { private Vector listeners = new Vector(); public synchronized void add. Junk. Mouse. Listener(Mouse. Moved. Listener mml){ listeners. add. Element(mml); } public synchronized void remove. Junk. Mouse. Listener(Mouse. Moved. Listener mml){ listeners. remove. Element(mml) 2/11/2022} Komar Associates 20
Event Listener Registration protected void notify. Mouse. Moved() { Vector l; Event. Object e = new Event. Object(this); synchronized(this) {l = (Vector)listeners. clone(); } for (int ii = 0: ii < l. size(); ii++) { ((Mouse. Moved. Listener)l. element. At(ii). mouse. Moved(e); } } } 2/11/2022 Komar Associates 21
Event Handling n n n Unicast event source -- generates event for single listener Multicast event source -- can have multiple listeners Event adapters -- objects that go between the source and listener and provide additional functionality 2/11/2022 Komar Associates 22
Persistence n n Persistence is the ability to store state information “permanently” Serialization: class Junk 1 implements java. io. Serializable u All variables except transient or static will be saved for each object in. ser files n n Implement Externalizable for other file formats Beans and related files (images, . ser files, etc. ) are kept in Java ARchive (JAR) files 2/11/2022 Komar Associates 23
Persistence Example import java. io. *; import java. beans. *; class Ser. Example { public static void main (String[] args) { if (args[0]. equals("save")) { Stuff c = new Stuff(); c. set. Value(13); try { File. Output. Stream f = new File. Output. Stream("tester. ser"); Object. Output. Stream s = new Object. Output. Stream(f); s. write. Object(c); s. flush(); } 2/11/2022 Komar Associates 24
Persistence Example catch (Exception e) { System. out. println(e); } c. print(); } 2/11/2022 Komar Associates 25
Persistence Example else if (args[0]. equals("restore")){ try { File. Input. Stream f = new File. Input. Stream("tester. ser"); Object. Input. Stream s = new Object. Input. Stream(f); Stuff c = (Stuff)s. read. Object(); c. print(); } catch (Exception e) { System. out. println(e); } } 2/11/2022 Komar Associates 26
Persistence Example else if (args[0]. equals("instantiate")) { try { Stuff c = (Stuff)Beans. instantiate(null, "tester"); c. print(); } catch (Exception e) { System. out. println(e); } } 2/11/2022 Komar Associates 27
Persistence Example class Stuff implements Serializable { private int value; public Stuff() { } public void set. Value(int v){ value = v; } public void print(){ System. out. println(value); } } 2/11/2022 Komar Associates 28
Persistence Example C: qm 490Files>java Ser. Example save Symantec Java! Just. In. Time Compiler Version 3. 00. 029(i) for JDK 1. 1. x Copyright (C) 1996 -98 Symantec Corporation 13 C: qm 490Files>java Ser. Example restore Symantec Java! Just. In. Time Compiler Version 3. 00. 029(i) for JDK 1. 1. x Copyright (C) 1996 -98 Symantec Corporation 13 C: qm 490Files>java Ser. Example instantiate Symantec Java! Just. In. Time Compiler Version 3. 00. 029(i) for JDK 1. 1. x Copyright (C) 1996 -98 Symantec Corporation 2/11/2022 13 Komar Associates 29
JAR Files n n n File format based on the ZIP file format One JAR file downloaded in a single HTTP transaction rather than each item individually Compressed items Items can be digitally signed Cross platform standard format 2/11/2022 Komar Associates 30
JAR use with Applets n HTML code for JAR files: <applet code =“class_file. class” archive = “class_file. jar” width = 460 height = 200> </applet> n Can name multiple JAR files, separated by comma 2/11/2022 Komar Associates 31
Java Archive Tool n Command: jar [options] [manifest] destination input-files n Options: uc - create a new or empty archive file u t - list the table of contents in a JAR file u x file - extracts all files or just the named files u f - the second argument specifies a jar file to process (create, xtract, or table) u v - generates verbose output on stderr 2/11/2022 Komar Associates 32
Java Archive Tool n Options (continued): um - includes manifest information from a specified manifest file u o - store only, without using ZIP compression u M - do not create a manifest file for entries 2/11/2022 Komar Associates 33
Java Archive Tool Examples jar cvf all_stuff. jar *. * jar cvf selected_stuff. jar. class. au. jpg. gif jar cvf subdirectory_stuff. jar sub 1 sub 2 sub 3 jar tf whats_in_it. jar tvf really_whats_in_it. jar NOTE: You can also build JAR files in Visual Café starting with the “Project” menu 2/11/2022 Komar Associates 34
Application Builder Support n Support for third party vendor IDE n Allows building of Beans with little or no programming effort n Visual Café has such functionality 2/11/2022 Komar Associates 35
Beans at UST n Find directories for Beans stuff at: u Network Neighborhood, Entire Network, CSLAB_B, Nt 431, Public, Komar OR u Start, Run, “\Nt 431PublicKomar” n n “Jars” directory contains JAR files for some sample Beans “Demo” directory is top directory for source code directories 2/11/2022 Komar Associates 36
Using the Example Beans n n Copy the “jars” directory to your diskette In Visual Café, do the following: u Open or create a new project u Choose the Insert menu and choose “Component into Library” u A file open dialogue box appears and you then need to open the appropriate. jar file on your diskette (Jelly. jar or juggler. jar or one of the “button” Beans) 2/11/2022 Komar Associates 37
Using Example Beans n n n Once added to the Component Library, you can open a view of the Component Library through the View menu Drag the bean to the form from the Component Library as you would any other component When you add interaction with the Bean, you will see specific Bean actions at the bottom of the list 2/11/2022 Komar Associates 38
Beans Assignment n n Study the Explicit. Button. Bean. Info. java file under directory Demo, Buttons. You will need to be able to explain what this code does Copy the Transitional. java file from Demo, transitional and add explanatory comments to indicate what the code is doing at each stage -- turn this in one week from today 2/11/2022 Komar Associates 39
Beans Assignment n n Use the Jelly. Bean and create an Applet that does the following: u Has the Jelly. Bean, three buttons, and a text field u The first button when pressed changes the Jelly. Bean to red, adds three cents to the price. In. Cents, and displays the price in the text field u The second button changes the color to blue and reports the current price in the text field u The third button changes the color to green, subtracts three cents from the price and reports the new price Turn in the program one week from today 2/11/2022 Komar Associates 40
Beans Assignment Extra Credit n n Use Visual Café to create a simple, visual Bean of your choice Prepare a short demo for the class on what you’ve created and how you went about it 2/11/2022 Komar Associates 41
Enterprise Java. Beans n The “Bean” concept on the server side u customizable through properties and customization methods u can be assembled with other Beans and custom code to form an application n Sun has provided a detailed specification for how the Enterprise Java. Beans will interact with an enterprise bean container system 2/11/2022 Komar Associates 42
Enterprise Java. Beans n Application execution systems can provide EJB services: u TP Monitors (CICS) u Component Transaction Servers (Sybase Jaguar) u CORBA platforms u Data base management systems (Oracle, Sybase, etc. ) u Web Servers (Netscape Enterprise Server, etc. ) 2/11/2022 Komar Associates 43
Enterprise Java. Beans n n EJB Server -- provides access to distributed transaction management services EJB Container -- manages a specific class of objects u life-cycle management u implicit transaction control u persistence management u transparent distribution services u security 2/11/2022 Komar Associates 44
Enterprise Java. Beans 2/11/2022 Komar Associates 45
- Slides: 45