You are on page 1of 355

Sun Educational Services

JAVA Development Kit Software


1.1–1.2 Enhancements

SL-277

JAVA Development Kit Software 1.1–1.2 Enhancements November 1998


Sun Educational Services

Module 1

Overview of JDK 1.2 Enhancements

JAVA Development Kit Software 1.1–1.2 Enhancements November 1998


Sun Educational Services

Objectives
• Identify differences between JDK™ 1.1 and JDK 1.2

JAVA Development Kit Software 1.1–1.2 Enhancements -2 of 13


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Security Enhancements
• External policy files

• Granular access control

• Applications confined to the sandbox (treated like an


applet)

• Permissions, policies, and protection domains

JAVA Development Kit Software 1.1–1.2 Enhancements -3 of 13


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Java Foundation Classes


• New GUI model, Swing
• New GUI libraries with greater flexibility than AWT
• Multiple, configurable, and dynamic look and feel
implementations
• Based on the model view controller (MVC)
paradigm, granular access control
• Java 2-D API
• Enhanced graphics and imaging
• Works with existing AWT architectures

JAVA Development Kit Software 1.1–1.2 Enhancements -4 of 13


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Java Foundation Classes


• Drag and drop
• Between Java™ technology applications and native
applications
• Accessiblity
• Accessible content
• Support for special accesibility tools
• AWT
• Multithreaded event queue
• Cursors
• Keyboard navigation
JAVA Development Kit Software 1.1–1.2 Enhancements -5 of 13
Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

The Collections API


• New collection types
• Linked lists
• Maps
• Sets
• Generic collections
• Support for "legacy" collections like Vector and
arrays
• Backward compatibility

JAVA Development Kit Software 1.1–1.2 Enhancements -6 of 13


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Remote Method Invocation


• Remote object activation
• Custom sockets
• Support for SSL
• Basic improvements
• Interfaces and exceptions
• Stubs and skeletons
• Object exporting

JAVA Development Kit Software 1.1–1.2 Enhancements -7 of 13


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JavaBeans Components
• Overview of JavaBeans™ Component Model
• The JavaBeans Activation Framework
• The InfoBus™
• Enterprise JavaBeans

JAVA Development Kit Software 1.1–1.2 Enhancements -8 of 13


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

System Enhancements
• Improved multithreading
• More efficient garbage collector
• Improved memory usage
• Just-in-time compilers
• Rewritten native libraries

JAVA Development Kit Software 1.1–1.2 Enhancements -9 of 13


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

System Enhancements
• Serialization enhancement
• Input method framework
• Java Debugger API
• Reference objects

JAVA Development Kit Software 1.1–1.2 Enhancements -10 of 13


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Miscellaneous Enhancements
• JDBC 2.0
• Support for SQL 3.0
• Audio enhancements
• Package version control
• JAR improvements

JAVA Development Kit Software 1.1–1.2 Enhancements -11 of 13


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Miscellaneous Enhancements
• Java IDL support (CORBA)
• Java standard extensions
• Reflection enhancements

JAVA Development Kit Software 1.1–1.2 Enhancements -12 of 13


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Cutting Edge Java Technology


• JavaSpaces™
• Jini™
• HotSpot™ VM
• JavaBlend™

JAVA Development Kit Software 1.1–1.2 Enhancements -13 of 13


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Module 2

Java Security Enhancements

JAVA Development Kit Software 1.1–1.2 Enhancements November 1998


Sun Educational Services

Objectives
• Discuss the goals of security
• Compare JDK 1.1 and JDK 1.2 software security
• Identify new classes and concepts
• Policy objects
• Permission and permissions objects
• Access controller
• Protection domain

JAVA Development Kit Software 1.1–1.2 Enhancements -2 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Security Goals
• Protection from harmful code
• Viruses
• Trojan horse programs
• Protection from system intrusion
• Hidden private data
• Authentication of entities
• User IDs and passwords
• Digital signatures

JAVA Development Kit Software 1.1–1.2 Enhancements -3 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Security Goals
• Authorization
• Permitted activities for some authenticated entity
• Containment
• Enforceable boundaries
• The sandbox security model
• Non-repudiation
• No denying involvement in a transaction

JAVA Development Kit Software 1.1–1.2 Enhancements -4 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JDK 1.1 Software Security Review


• The sandbox
• Provides an environment where applets can safely
execute
• Protects system resources
• With digital signatures, allows applets a "roomier"
sandbox
• Elements of the sandbox
• Has bytecode verifier
• Has class loaders
• Has security managers

JAVA Development Kit Software 1.1–1.2 Enhancements -5 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JDK 1.1 Software Security Review


• The SecurityManager

• Called by the Java API, checks access rights to system


• Enforces the sandbox constraints

JAVA Development Kit Software 1.1–1.2 Enhancements -6 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

A Sample Security Manager,


ABCSecurityManager.java
1 public class ABCSecurityManager extends SecurityManager
2 {
3 public void checkRead(String file) throws
4 SecurityException{
5 if(file.endsWith(".txt")){
6 return;
7 }
8 throw new SecurityException("No permission to read
9 file: " + file);
10 }
11 } /* end ABCSecurityManager.java */

JAVA Development Kit Software 1.1–1.2 Enhancements -7 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JDK 1.1 Software Security Review


• Advantages of this model
• Simple to create and use
• The Java API does all of the standard security checks
• Digital signatures can free applets from the
restrictive sandbox
• Disadvantages of this model
• Difficult to add new security checks beyond the
standard methods
• Signed applet security is binary: all or nothing

JAVA Development Kit Software 1.1–1.2 Enhancements -8 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

New Security Enhancements


• Granular access control
• Configurable policy files
• End-users can control their own policies without
programming
• Java code queries these policies dynamically at
runtime
• Applications treated like applets
• The only code always trusted is JDK system code

JAVA Development Kit Software 1.1–1.2 Enhancements -9 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

New Security Enhancements


• Improved support for X.509 certificates
• X.509 v1, v2, and v3 are now supported
• New security tools
• keytool
• jarsigner
• policytool

JAVA Development Kit Software 1.1–1.2 Enhancements -10 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Granular Access Control


• Every loaded class belongs to a protection domain
• A CodeSource URL and digital signature
• The set of Permissions granted the CodeSource

../images/chapter2/class1.jpg

JAVA Development Kit Software 1.1–1.2 Enhancements -11 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Permissions
• The Permission class
• Represents access to a particular resource
• Creates subclasses for specific types
• Included permissions
• java.io.FilePermission
• java.lang.RuntimePermission
• java.net.SocketPermission

JAVA Development Kit Software 1.1–1.2 Enhancements -12 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Permissions
• Permission format
• All permissions have some target, the resource being
controlled
• Many also have a list of actions
• Examples
FilePermission fp = new
FilePermission("/tmp/-","read, write");
RuntimePermission rp = new
RuntimePermission("exit");
SocketPermission sp = new
SocketPermission("www.foo.com","accept");

JAVA Development Kit Software 1.1–1.2 Enhancements -13 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Permissions
• The implies(Permission p) method
• All permissions must implement this method
• It determines logical implications
• Example
FilePermission abc = new
FilePermission("*.txt","read");
FilePermission xyz = new
Permissions
FilePermission("cool.txt","read");

abc.implies(xyz) == true
xyz.implies(abc) == false

JAVA Development Kit Software 1.1–1.2 Enhancements -14 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Policies
• Policy objects
• They are queried at runtime for the permissions
granted a particular CodeSource
• There can only be one in existence at a time, but it can
change during runtime
Policy currentPolicy = Policy.getPolicy();
Permissions granted =
currentPolicy.getPermissions(someCodeSource);

JAVA Development Kit Software 1.1–1.2 Enhancements -15 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Policies
• Policy files
• User configurable series of grant statements
• No involvement in making decisions
• No limit on the number of policy files; they are all
read to create the system policy
• Grant statements
grant [codebase "<URL>"][signedBy <alias>"]{
permission [permission
class]["target"],["actions"];
};

JAVA Development Kit Software 1.1–1.2 Enhancements -16 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Policies
• Two default policy files
• The system policy file, java.policy
• The user policy file, .java.policy
• Property expansion in policy files
• All ${XXX} tokens are expanded to match some
system property
grant signedby "Godzilla" {
permission java.io.FilePermission
${user.home}${/}"*", "read";
};

JAVA Development Kit Software 1.1–1.2 Enhancements -17 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services
The System Policy File,
java.policy
1 // Standard extensions get all permissions by default
2
3 grant codeBase "file:${java.home}/lib/ext/" {
4 permission java.security.AllPermission;
5 };
6
7 // default permissions granted to all domains
8
9 grant {
10 // allows anyone to listen on un-privileged ports
11 permission java.net.SocketPermission "localhost:1024-",
12 "listen";
13
14 // "standard" properies that can be read by anyone
15
16 permission java.util.PropertyPermission "java.version",
"read";
17 permission java.util.PropertyPermission "java.vendor",
18 "read";
19 permission java.util.PropertyPermission "java.vendor.url",
20 "read";
21 permission java.util.PropertyPermission
22 "java.class.version", "read";
23 permission java.util.PropertyPermission "os.name", "read";
24 permission java.util.PropertyPermission "os.version",
25 "read";
26 permission java.util.PropertyPermission "os.arch", "read";
27 permission java.util.PropertyPermission "file.separator",
28 "read";
29 permission java.util.PropertyPermission "path.separator",
30 "read";
31 permission java.util.PropertyPermission "line.separator",
32 "read";

JAVA Development Kit Software 1.1–1.2 Enhancements -18 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services
The System Policy File,
java.policy
33 permission java.util.PropertyPermission
34 "java.specification.version", "read";
35 permission java.util.PropertyPermission
36 "java.specification.vendor", "read";
37 permission java.util.PropertyPermission
38 "java.specification.name", "read";
39
40 permission java.util.PropertyPermission
41 "java.vm.specification.version", "read";
42 permission java.util.PropertyPermission
43 "java.vm.specification.vendor", "read";
44 permission java.util.PropertyPermission
45 "java.vm.specification.name", "read";
46 permission java.util.PropertyPermission "java.vm.version",
47 "read";
48 permission java.util.PropertyPermission "java.vm.vendor",
49 "read";
50 permission java.util.PropertyPermission "java.vm.name",
51 "read";
52 };

JAVA Development Kit Software 1.1–1.2 Enhancements -19 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

The User Policy File, .java.policy


1 // user policy file
2
3 grant {
4 permission java.io.FilePermission("*.dat","read");
5 };
6
7 grant codeBase "http://www.foo.com" {
8 permission acme.fax.FaxPermission("send, receive");
9 };
10
11 grant codeBase "http://www.bar.com" signedBy "Ziggy"{
12 permission
13 java.io.FilePermission("-","read,write,execute");
14 };

JAVA Development Kit Software 1.1–1.2 Enhancements -20 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Security Management
• The AccessController entity
• Asks current policy for permission to access some
controlled resource
• Is used by the security manager to check system
resources
• A file access example
FilePermission fp = new
FilePermission("/data/money.dat","read");
try{
AccessController.checkPermission(fp);
}catch(AccessControlException){...}

JAVA Development Kit Software 1.1–1.2 Enhancements -21 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Security Management
• How the AccessController works
• Starting with the caller's domain, checks each class
domain on the stack
• Makes sure all class domains are granted permission
• Is successful if all domains have no exceptions or a
privileged block
for(i = firstDomain; i >=0; i++){
if(domain i does not have permission)
throw newAccessControlException("Denied");
if(domain i is priveleged)
return;
}
return;

JAVA Development Kit Software 1.1–1.2 Enhancements -22 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Security Management
• Privileged blocks
• Indicates a stopping point for the
AccessController stack trace
• Allows invoked, privileged code to perform some
action on behalf of calling unprivileged code
...
AccessController.doPrivileged(new
PrivilegedAction() {
public Object run() {
System.loadLibrary("awt");
return null; // could return a property value
}
});

JAVA Development Kit Software 1.1–1.2 Enhancements -23 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Security Management
• The SecurityManager
• No longer an abstract class
• Applications are subject to sandbox
• Load the system security manager automatically
and/or specify policy files
• New class path for untrusted, but local, code
java -Djava.security.manager MyApp
java -Djava.security.policy = my.policy MyApp
java -Djava.security.policy == my.policy MyApp
java -Djava.class.path=\somePath MyApp

JAVA Development Kit Software 1.1–1.2 Enhancements -24 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

NewABCSecurityManager.java
Example
import java.security.*;
public class NewABCSecurityManager extends
SecurityManager {

public void checkRead(String file)throws


SecurityException{
FilePermission fp = new FilePermission(file,
"read");
AccessController.checkPermission(fp);
} /* end NewABCSecurityManager.java */

JAVA Development Kit Software 1.1–1.2 Enhancements -25 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Digital Signatures and Certificates


• Support for digital signatures
• Part of core Java technology since JDK 1.1
• Is used for authentication and non-repudiation
• Review of the Signature class
• Is used to sign and verify data
• Default algorithm provided is DSA
• Uses private key to sign and public key to verify
(asymmetric)

JAVA Development Kit Software 1.1–1.2 Enhancements -26 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Digital Signatures and Certificates


Signing example
Signature sig = Signature.getInstance("DSA");
PrivateKey pk = ... ;
String message = "Nobody will know this!";
byte [] signature = message.getBytes();
sig.initSign(pk);
sig.update(message.getBytes());
byte [] signature = sig.sign();

JAVA Development Kit Software 1.1–1.2 Enhancements -27 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

The Flow of Signing Data

../images/chapter2/signing.gif/

JAVA Development Kit Software 1.1–1.2 Enhancements -28 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services
Signing.java
1 import java.security.*;
2 import java.io.*;
3 public class Signing{
4 public static void main(String [] args) throws Exception{
5 if(args.length < 2){
6 System.err.println("Usage: java Signing <alias>
7 <password>");
8 System.exit(0);
9 }
10 String alias = args[0];
11 String password = args[1];
12 char [] passArray = password.toCharArray();
13 Signature sig = Signature.getInstance("DSA");
14 KeyStore ks = KeyStore.getInstance("JKS");
15 FileInputStream keyStream = new
16 FileInputStream("\\windows\\.keystore");
17 ks.load(keyStream, passArray);
18 String message = "Nobody will know this!";
19 PrivateKey pk = (PrivateKey) ks.getKey(alias, passArray);
20 keyStream.close();
21 sig.initSign(pk);
22 sig.update(message.getBytes());
23 byte [] signature = sig.sign();
24 FileOutputStream fos = new
25 FileOutputStream("Signed.dat");
26 ObjectOutputStream out = new ObjectOutputStream(fos);
27 out.writeObject(message);
28 out.writeObject(signature);
29 out.close();
30 }
31 } /* end Signing.java */

JAVA Development Kit Software 1.1–1.2 Enhancements -29 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Digital Signatures and Certificates


• What is a certificate?
• A digitally signed statement from some trusted
entity saying the public key of some other entity is
valid
• How do I get one?
• You can create your own (self-signed)
• Someone acting as a Certificate Authority (CA) can
issue one
The CA should be some trusted entity, perhaps
verified by yet another CA

JAVA Development Kit Software 1.1–1.2 Enhancements -30 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Digital Signatures and Certificates


• X.509 certificates in Java technology
• java.security.Certificate interface is
deprecated
• Replaced with
java.security.cert.Certificate
abstract class
• Flexible system allows for different implementations
of certificates
• Support for X.509 v1, v2, and v3

JAVA Development Kit Software 1.1–1.2 Enhancements -31 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Digital Signatures and Certificates


• Contents of an X.509 Certificate
• Version (v1, v2, v3)
• Distinguished name
CN=Todd Greanier OU=Education O=Lake Systems,
Inc L=Rochester S=New York C=US
• Public key
• Certificate issuer name
• Serial number
• Validity period

JAVA Development Kit Software 1.1–1.2 Enhancements -32 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Digital Signatures and Certificates


A verifying example
Signature sig = Signature.getInstance("DSA");

PublicKey pk = ...;
sig.initVerify(pk);
sig.update(message.getBytes());
byte [] signature = ...;
if(sig.verify(signature)){...}

JAVA Development Kit Software 1.1–1.2 Enhancements -33 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

The Flow of Verifying Data

../images/chapter2/verifying.gif

JAVA Development Kit Software 1.1–1.2 Enhancements -34 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services
Verifying.java
1 import java.security.*;
2 import java.io.*;
3 public class Verifying{
4 public static void main(String [] args) throws Exception{
5 if(args.length < 2){
6 System.err.println("Usage: java Signing <alias>
7 <password>");
8 System.exit(0);
9 }
10 String alias = args[0];
11 String password = args[1];
12 char [] passArray = password.toCharArray();
13 Signature sig = Signature.getInstance("DSA");
14 KeyStore ks = KeyStore.getInstance("JKS");
15 FileInputStream keyStream = new
16 FileInputStream(".keystore");
17 ks.load(keyStream, passArray);
18 PublicKey pk = ks.getCertificate(alias).getPublicKey();
19 keyStream.close();
20 sig.initVerify(pk);
21 FileInputStream fis = new FileInputStream("Signed.dat");
22 ObjectInputStream in = new ObjectInputStream(fis);
23 String message = (String) in.readObject();
24 byte [] signature = (byte []) in.readObject();
25 in.close();
26 sig.update(message.getBytes());
27 if(sig.verify(signature)){
28 System.out.println("Message is secure");
29 System.out.println("Message: " + message);
30 System.exit(0);
31 }
32 System.out.println("Message is corrupted");
33 }
34 } /* end Verifying.java */

JAVA Development Kit Software 1.1–1.2 Enhancements -35 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Introduction to the JCE


• What is the JCE?
• Java Cryptography Extension classes
• Provided encryption algorithms and API
• Standard Java extension (javax.crypto)
• 100% Pure Java™ solution
• Binary form is not exportable

JAVA Development Kit Software 1.1–1.2 Enhancements -36 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Introduction to the JCE


• Some provided algorithms
• DES
• Triple DES
• Diffie-Hellman
• Export control
• Cryptographic algorithms are considered weapons.
• JCE binaries are controlled by U.S. law.
• It can only be distributed within U.S./Canada
borders.
• There is a drive to lessen these restrictions.

JAVA Development Kit Software 1.1–1.2 Enhancements -37 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Introduction to the JCE


• Encryption example
• Generate secret key using the KeyGenerator class
• Use the same key for both parties (symmetric)
KeyGenerator kg =
KeyGenerator.getInstance("DES");
SecretKey secret = kg.generateKey();

• Create a cipher and initialize it


Cipher cipher = Cipher.getInstance("DES");
cipher.init (Cipher.ENCRYPT_MODE, secret);

JAVA Development Kit Software 1.1–1.2 Enhancements -38 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Introduction to the JCE


Encryption example

• Encrypt the data


String data = "This is a test.";
byte [] clearText = data.getBytes();
byte [] cipherText = cipher.doFinal(clearText);

JAVA Development Kit Software 1.1–1.2 Enhancements -39 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

New Security Tools


The keytool program
• It is used to create, store, and manage private keys
and certificates.
• Private keys are always password protected.
• The keystore itself can be password protected as well
• The keystore is written out as a flat file (.keystore)
and stored in the user's home directory by default.

JAVA Development Kit Software 1.1–1.2 Enhancements -40 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services
Example of Generating the
Key Pairs
1 C:\ keytool - genkey
2
3 Enter keystore password: froglegs
4
5 What is your first and last name?
6 [Unknown]: Todd Greanier
7
8 What is the name of your organizational unit?
9 [Unknown]: Education
10
11 What is the name of your organization?
12 [Unknown]: Lake Systems, Inc.
13
14 What is the name of your City or Locality?
15 [Unknown]: Rochester
16
17 What is the name of your State or Province?
18 [Unknown]: New York
19
20 What is the two-letter country code for this unit?
21 [Unknown]: US
22
23 Is <CN=Todd Greanier, OU=Education, O="Lake Systems, Inc",
24 L=Rochester, S=New York, C=US> correct?
25 [no]: Yes
26
27 Enter key password for <mykey>
28 <RETURN if same as keystore password>: aa22bb33

JAVA Development Kit Software 1.1–1.2 Enhancements -41 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

New Security Tools


• jarsigner

• Signs and verifies JAR files


• Compares a certificate stored inside the JAR file with
one stored in the local keystore to verify it
• Signs a private key stored in the local keystore

JAVA Development Kit Software 1.1–1.2 Enhancements -42 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services
Example of Signing and
Verifying a JAR File
Signing a JAR file
1. Create a JAR file that contains the
data to sign.
jar cvf myJar.jar *.class *.gif

2. Run the jarsigner utility, passing


the JAR file name and key alias.
jarsigner myJar.jar mykey
Enter Passphrase for keystore:
froglegs

Verifying a JAR file.


1. Run the jarsigner utility passing
the -verify flag
jarsigner -verify myJar.jar
jar verified

JAVA Development Kit Software 1.1–1.2 Enhancements -43 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

New Security Tools


policytool
• A GUI assists users in creating and editing a security
policy.
• No Java coding is necessary.

JAVA Development Kit Software 1.1–1.2 Enhancements -44 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services
Example of Using the
policytool

../chapter2/ptmain.gif
arrow_d.gif
ptnext.gif
arrow_d.gif
ptadd.gif

JAVA Development Kit Software 1.1–1.2 Enhancements -45 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Module Summary
• JDK 1.2 offers fine-grained access control with
permissions, policies, and protection domains.
• Protection domains are composed of a CodeSource (a
URL and an array of public keys) and the set of
Permissions granted.
• Access control is handled by the AccessController.
• Applications can be constrained in the same sandbox as
applets.
• New security tools enhance security control.

JAVA Development Kit Software 1.1–1.2 Enhancements -46 of 46


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Module 3

Java Foundation Classes

JAVA Development Kit Software 1.1–1.2 Enhancements November 1998


Sun Educational Services

Objectives
• Explain motivations for JFC
• Discuss simple Swing components

JAVA Development Kit Software 1.1–1.2 Enhancements -2 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Overview of JFC
• Motivations for JFC's creation
• Limited widget set in AWT
• Increase flexibility of UI code
• Configurable look and feel of components
• Increase graphics and rendering capabilities
• Need for accessibility support – Screen magnifiers
and Braille support
• Support for drag and drop

JAVA Development Kit Software 1.1–1.2 Enhancements -3 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Swing GUI Components


• Has an abstract Windowing Toolkit
• Is designed for platform-independent GUIs
• Automatically assumes the native look and feel of the
environment it ran in
• Is based on the peer model
• Has native components which were created for each
platform
• Creates heavyweight components

JAVA Development Kit Software 1.1–1.2 Enhancements -4 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Swing GUI Components


• Developed by Netscape
• Created to bring modern UI controls to Java technology
• Has a set of APIs running on top of AWT
• Is not really useful outside of the Netscape browser

JAVA Development Kit Software 1.1–1.2 Enhancements -5 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Swing GUI Components


• Has modern components like tables and trees
• Contains flexible versions of AWT components
• Uses MVC to allow for configurable components
• Is 100% Pure Java™
• Makes sure all Swing components are JavaBeans
compliant

JAVA Development Kit Software 1.1–1.2 Enhancements -6 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Swing GUI Components


• Has a lightweight UI framework
• Is introduced in AWT for the JDK 1.1 software
• Allows components to be created without native
widgets
• Is more flexible and has configurable components
• Directly extends java.awt.Component

JAVA Development Kit Software 1.1–1.2 Enhancements -7 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


• JComponent
• Is the parent of most Swing components
• Extends java.awt.Container
• Adds support for Swing concepts – borders,
ToolTips, and pluggable look and feel

JAVA Development Kit Software 1.1–1.2 Enhancements -8 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


• JPanel
• Is the same as AWT counterpart
• Has built-in support for double buffering
JPanel panel = new JPanel();
panel.setBackground(Color.red);
panel.setBorder(new LineBorder(Color.gray, 5);

JAVA Development Kit Software 1.1–1.2 Enhancements -9 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


• Icon and ImageIcon
• Are not components
• Represent glyphs to add to components
• Implement the Icon interface
• Have ImageIcon which is used to represent icons
from image files (like GIF and JPEG)
• Are exemplified in JButton and JLabel

JAVA Development Kit Software 1.1–1.2 Enhancements -10 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


• JLabel
• Adds to normal java.awt.Label functionality
such as icons and positioning of text and icon relative
to each other
JLabel textLabel = new JLabel("Swing!");
JLabel imageLabel = new JLabel("Duke!");
ImageIcon icon = new ImageIcon("duke.gif");
imageLabel.setIcon(icon);
imageLabel.setHorizontalTextAlignment
(JLabel.CENTER);

JAVA Development Kit Software 1.1–1.2 Enhancements -11 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


AbstractButton
• Parent of all Java "button" types including
• Clickable buttons (JButton)
• Check boxes (JCheckBox)
• Radio buttons (JRadioButton)
• Contains common methods
doClick()
setMnemonic()
setRolloverIcon()
setDisabledIcon()

JAVA Development Kit Software 1.1–1.2 Enhancements -12 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


• JButton
• Acts like java.awt.Button
• Can contain icons to make image buttons
JButton button = new JButton("Press Me!");
ImageIcon icon = new ImageIcon("duke.gif");
JButton imageButton = new JButton(icon);
ImageIcon icon2 = new ImageIcon("java.gif");
JButton both = new JButton("Java!", icon2);

JAVA Development Kit Software 1.1–1.2 Enhancements -13 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


• JCheckBox
• Represents true or false state
• Allows view to be changed from default using icons
JCheckBox box1 = new JCheckBox("Cola");
JCheckBox box2 = new JCheckBox("Milk");
box2.setHorizontalTextPosition(JCheckBox.LEFT);
JCheckBox box3 = new JCheckBox();
box3.setIcon(new ImageIcon("java.gif"));
box3.setSelectedIcon(new ImageIcon("java2.gif"));
box3.setRolloverIcon(new ImageIcon("java3.gif"));

JAVA Development Kit Software 1.1–1.2 Enhancements -14 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


• JRadioButton
• Represents mutually exclusive true/false state
• Must be placed in a ButtonGroup to function

JAVA Development Kit Software 1.1–1.2 Enhancements -15 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


JRadioButton b1 = new JRadioButton("High");
JRadioButton b2 = new JRadioButton("Medium");
JRadioButton b3 = new JRadioButton("Low", true);
b1.setMnemonic('H');
b2.setMnemonic('M');
b3.setMnemonic('L');
ButtonGroup bg = new ButtonGroup();
bg.add(b1); bg.add(b2); bg.add(b3);

JAVA Development Kit Software 1.1–1.2 Enhancements -16 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


• JToggleButton
• Parent class of both JRadioButton and
JCheckBox
• Can be used as a component by itself
• ButtonGroup
• Can be used to group any buttons together in a
mutex relationship

JAVA Development Kit Software 1.1–1.2 Enhancements -17 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


• JTextField and JTextArea
• Behave like AWT counterparts
• Must have the JTextArea in a JScrollPane to gain
scrollbars
JTextField jtf = new JTextField("And let slip
the dogs of war!");
JTextArea jta = new JTextArea("Cry Havoc!",10,60);
JScrollPane jsp = new JScrollPane();
jsp.add(jta);

JAVA Development Kit Software 1.1–1.2 Enhancements -18 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


• JPasswordField
• A text field that will not display the contained text
• The echo character is controllable
JPasswordField pf1 = new JPasswordField();
JPasswordField pf2 = new JPasswordField();
pf2.setEchoChar('%');

JAVA Development Kit Software 1.1–1.2 Enhancements -19 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


• JScrollBar
• A Swing version of the AWT counterpart
JScrollBar bar =
new JScrollBar(JScrollBar.HORIZONTAL, 0, 10, 0,
255);

JAVA Development Kit Software 1.1–1.2 Enhancements -20 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


• JSlider
• Works like a scroll bar, but adds tick marks, both
major and minor
• Supports optional labels
JSlider slider =
new JSlider(JSlider.HORIZONTAL, 0, 0, 100);
slider.setMajorTickSpacing(10);
slider.setMinorTickSpacing(5);
slider.setPaintLabels(true);
slider.setPaintTicks(true);

JAVA Development Kit Software 1.1–1.2 Enhancements -21 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


• JProgressBar
• Allows for a GUI display of some operation's status
• Has no AWT counterpart
JProgressBar pb = new JProgressBar();
pb.setMinimum(minimumValue);
pb.setMaximum(maximumValue);
for (int i = 0; i < maximumValue + 1; i++){
pb.setValue(i);
}

JAVA Development Kit Software 1.1–1.2 Enhancements -22 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


• JList
• Can add elements via constructor call
• Can add objects, not just strings
• Has no automatic scrolling; needs a JScrollPane
String [] data =
{"Hamlet","Macbeth","King Lear",
"Othello","Romeo &Juliet"};
JList list = new JList(data);
JScrollPane pane = new
JScrollPane(list);

JAVA Development Kit Software 1.1–1.2 Enhancements -23 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


• JComboBox
• Is similar to AWT Choice button
• Can hold any object, like a JList
• Can have a ListCellRenderer for display control
String data = {"Bill", "Joe", "Jim", "Sally"};
JComboBox box = new JComboBox(data);
box.setRenderer(new MyCellRenderer());

JAVA Development Kit Software 1.1–1.2 Enhancements -24 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

ListCellRenderer
1 public class MyCellRenderer extends JLabel implement ListCellRenderer{
2
3 public Component getListCellRendererComponent(JList list, Object o
4 int index, boolean selected, boolean focus){
5
6 setFont(new Font("serif",Font.BOLD,24));
7 setBackground(selected ? Color.blue : Color.white);
8 setForeground(selected ? Color.white : Color.blue);
9 return this;
10 }
11 }
12

• list – Current JList or JComboBox component


• o – Current item
• index – Current index
• selected – The cell selected
• focus– The cell focus

JAVA Development Kit Software 1.1–1.2 Enhancements -25 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


• JScrollPane
• Works like the AWT ScrollPane
• Adds components to the viewport
JScrollPane pane = new JScrollPane();
ImageIcon icon = new ImageIcon("sun.gif");
JLabel label = new JLabel(icon);
pane.getViewport().add(label);

JAVA Development Kit Software 1.1–1.2 Enhancements -26 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


• JApplet
• Extends java.applet.Applet (not lightweight)
• Must get a reference to the content pane to add
components
• Has a default content pane layout, BorderLayout
• JFrame
• Extends java.awt.Frame (not lightweight)
• Must get a reference to the content pane to add
components
Container c = this.getContentPane();

JAVA Development Kit Software 1.1–1.2 Enhancements -27 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


JMenu
• Basically the same as the AWT menuing model except:
• Menu classes are first-class components (not
subclasses of MenuComponent as in AWT).
• Icons can be associated with menus.
• The JCheckBoxMenuItem class is available for
Boolean selections.
• The JRadioButtonMenuItem class is used for a
mutex on a menu.
• Menus can be put in any component, including
applets.

JAVA Development Kit Software 1.1–1.2 Enhancements -28 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


JMenu on a JApplet
JMenuBar mb = new JMenuBar();
JMenu menu = new JMenu("Edit");
JMenuItem cut = new
JMenuItem("Cut",cutIcon);
JMenuItem copy = new
JMenuItem("Copy", copyIcon);
JMenuItem paste = new
JMenuItem("Paste", pasteIcon);
JMenuItem delete = new
JMenuItem("Delete",deleteIcon);
menu.add(cut); menu.add(copy);
menu.add(paste);
menu.addSeparator();
menu.add(delete);
mb.add(menu);
setJMenuBar(mb);

JAVA Development Kit Software 1.1–1.2 Enhancements -29 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


• JPopupMenu
• Can be associated with any component
JPopupMenu pm = new
JPopupMenu();
pm.add(new JMenuItem("Cut", cutIcon));
pm.add(new JMenuItem("Copy", copyIcon));
pm.add(new JMenuItem("Paste", pasteIcon));
pm.addSeparator();
pm.add(new JMenuItem("Delete", deleteIcon));

JAVA Development Kit Software 1.1–1.2 Enhancements -30 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


• JToolBar

• It is a container which gives the toolbar an


appearance and behavior.
• It can be floatable if underlying UI manager allows it.
• Any component can be added, though JButton is the
most common.

JAVA Development Kit Software 1.1–1.2 Enhancements -31 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


JToolBar
JToolBar tb = new
JToolBar();
JButton cut = new
JButton(cutIcon);
JButton copy = new
JButton(copyIcon);
JButton paste = new
JButton(pasteIcon);
JButton delete = new
JButton(deleteIcon);

JAVA Development Kit Software 1.1–1.2 Enhancements -32 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


JToolBar
tb.add(cut);
tb.add(copy);
tb.add(paste);
tb.addSeparator();
tb.add(delete);

JAVA Development Kit Software 1.1–1.2 Enhancements -33 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


• JTabbedPane
• Container that finally makes CardLayout useful
• Tabs can be text, icons, or both
• Any component can be added to the pane
JTabbedPane pane = new JTabbedPane();
pane.addTab("1", labelOne);
pane.addTab("2", labelTwo);
pane.addTab("3", labelThree);

JAVA Development Kit Software 1.1–1.2 Enhancements -34 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Swing Components


• JOptionPane
• Creating pop-up standard dialogs is now extremely
easy.
• Calls to static methods do all the work.
• All of the dialogs are modal
JOptionPane.showMessageDialog(this, "I’m Duke!",
"Important Message From Duke!",
JOptionPane.PLAIN_MESSAGE,
new ImageIcon("duke.gif"));
JOptionPane.showInputDialog
("What is your shoe size?");

JAVA Development Kit Software 1.1–1.2 Enhancements -35 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Introduction to MVC
What is MVC?
• Has a design pattern with three cooperating objects
• Model
• View
• Controller
• Is often used in GUI design
• Allows control over appearance, data sources, and
event responses in a flexible fashion

JAVA Development Kit Software 1.1–1.2 Enhancements -36 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Introduction to MVC
The model
• Design pattern with three cooperating objects
• Underlying representation
• Definition of how something works
• For example, a checkbox model might indicate a true
or false state regardless of how it appears to the user
• Encapsulated in Swing by interfaces (ButtonModel,
ListModel,and so on)
• Found only in components using data

JAVA Development Kit Software 1.1–1.2 Enhancements -37 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Introduction to MVC
The view
• Visual representation
• Definition of how something appears
• Separate entity from the model

JAVA Development Kit Software 1.1–1.2 Enhancements -38 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Introduction to MVC
• The controller
• Specifies how to handle events and user input
• Acts as a conduit between the model and view when
changes occur in either
• Uses event handlers for the controller role
• The delegate
• Coordinates between the view and controller
• Combines these two into one unit called the delegate
• Is encapsulated in the XXXUI classes

JAVA Development Kit Software 1.1–1.2 Enhancements -39 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Introduction to MVC
• MVC demonstration
• Create one model for a JList and JComboBox to
share
• Use the DefaultListModel for each

JAVA Development Kit Software 1.1–1.2 Enhancements -40 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Introduction to MVC
Create one model for a JList and JComboBox to share
1 class FontListModel extends DefaultListModel implements
2 ComboBoxModel{
3 private String [] list =
4 Toolkit.getToolkit().getFontList();
5 private Object cv;
6 public int getSize(){
7 return list.length;}
8 public Object getElementAt(int index){
9 return list[index];}
10 public void setSelectedItem(Object o){
11 cv = o; }
12 public Object getSelectedItem(){
13 return cv;}
14 }
15 FontListModel model = new FontListModel();
16 JList list = new JList(model);
17 JComboBox box = new JComboBox(model);

JAVA Development Kit Software 1.1–1.2 Enhancements -41 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Advanced Swing Components


JInternalFrame and JDesktopPane
• They are used to create Multiple Document Interface
(MDI) windows.
• Internal frames can overlap each other.
• The JDesktopPane manages the internal frame
behavior.
• Four states can be set – Resizable, closeable,
maximizable, and iconifiable.

JAVA Development Kit Software 1.1–1.2 Enhancements -42 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Advanced Swing Components


JInternalFrame and JDesktopPane
JDesktopPane dp = new JDesktopPane();
JInternalFrame frame1 = new JInternalFrame
("Frame One", true, false, true, true);
JInternalFrame frame2 = new JInternalFrame
("Frame Two", false, false, false, true);
dp.add(frame1, 5);
dp.add(frame2, 10);

JAVA Development Kit Software 1.1–1.2 Enhancements -43 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Advanced Swing Components


JTable
• Used to display one or more columns of data
• Can optionally allow editing
• Has a super-charged listbox
• Uses models represented by the TableModel interface

JAVA Development Kit Software 1.1–1.2 Enhancements -44 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Advanced Swing Components


The TableModel interface
• Must implement
java.awt.swing.table.TableModel
• Has one concrete class, AbstractTableModel, that
makes this simple to do
• Must implement three methods if you subclass
AbstractTableModel
• getRowCount()
• getColumnCount()
• getValueAt(int row, int column)

JAVA Development Kit Software 1.1–1.2 Enhancements -45 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Advanced Swing Components


The TableModel interface
TableModel model = new SeasonModel();
JTable table = new JTable(model);

JAVA Development Kit Software 1.1–1.2 Enhancements -46 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Advanced Swing Components


Expanding the model – Creating a custom editor
JComboBox box1 =
new JComboBox(moreOriginalMembers);
JComboBox box2 = new JComboBox(moreNewMembers);
TableColumn columnOne =
table.getColumnModel().getColumn(0);
TableColumn columnTwo =
table.getColumnModel().getColumn(1);
columnOne.setCellEditor
(new DefaultCellEditor(box1));
columnTwo.setCellEditor
(new DefaultCellEditor(box2));

JAVA Development Kit Software 1.1–1.2 Enhancements -47 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Advanced Swing Components


JTree
• Displays hierarchical data vertically
• Has a separate model,
com.sun.java.swing.tree.TreeModel
• Makes sure each row contains one item, called a node
• Has a root node from which all other nodes descend

JAVA Development Kit Software 1.1–1.2 Enhancements -48 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Advanced Swing Components


• Each node can contain other nodes called leaf nodes.
• Only leaf nodes cannot have children nodes.
• Each node is represented by an implementation of
TreeNode.
• Basic implementation is via DefaultMutableTreeNode

JAVA Development Kit Software 1.1–1.2 Enhancements -49 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Advanced Swing Components


JTree
DefaultMutableTreeNode root = new
DefaultMutableTreeNode("Seasons");
DefaultMutableTreeNode summer = new
DefaultMutableTreeNode("Summer");
DefaultMutableTreeNode winter = new
DefaultMutableTreeNode("Winter");
DefaultMutableTreeNode swimsuit = new
DefaultMutableTreeNode("Swimsuit");
DefaultMutableTreeNode baseball = new
DefaultMutableTreeNode("Baseball");
DefaultMutableTreeNode warm = new
DefaultMutableTreeNode("Warm")
...

JAVA Development Kit Software 1.1–1.2 Enhancements -50 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Advanced Swing Components


JTree
original.add(swimsuit);
original.add(baseball);
original.add(warm);
root.add(summer);
JTree tree = new JTree(root);

JAVA Development Kit Software 1.1–1.2 Enhancements -51 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Advanced Swing Components


JEditorPane
• Is a subclass of JTextPane
• Displays HTML and RTF files
JEditorPane pane = new JEditorPane();
...
URL url = someURL
pane.setPage(url);
...

JAVA Development Kit Software 1.1–1.2 Enhancements -52 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Pluggable Look and Feel


Why a cross-platform look and feel?
• Peer-based structure of AWT offered little flexibility
• System-specific behavior impossible to reliably handle
• Mouse buttons
• Mnemonics
• Colors, borders, fonts, shapes, and so forth

JAVA Development Kit Software 1.1–1.2 Enhancements -53 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Pluggable Look and Feel


• Design goals for look and feel
• Improve the overall appearance
• Make it work on any GUI-based system
• Allow creation of specific "themes"
• Set the look and feel
try{
UIManager.setLookAndFeel
("MotifLookAndFeel");
}catch(UnsupportedLookAndFeelException ex){}

JAVA Development Kit Software 1.1–1.2 Enhancements -54 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

2-D Graphics API Overview


What is the 2-D Graphics API?
• Comprehensive set of utilities for rendering drawings,
manipulating fonts, and managing colors
• Three new packages
• java.awt.geom
• java.awt.font
• java.awt.color
• Additions to java.awt and java.awt.image

JAVA Development Kit Software 1.1–1.2 Enhancements -55 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

2-D Graphics API Overview


• Features of the API
• Enhances imaging and graphics model
• Blends in with existing AWT architecture
• Provides a uniform mechanism for performing
• Transforms (like scaling and rotation)
• Hooks into various graphics devices like printers and
screens
• Is completely compatible with previous AWT code
since the 2-D API extends the original

JAVA Development Kit Software 1.1–1.2 Enhancements -56 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

2-D Graphics API Overview


A 2-D Graphics curve example
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D)g;
GeneralPath path = new GeneralPath();
path.moveTo(10.0f,10.0f);
path.curveTo(100.0f,100.0f,150.0f,50.0f,
225.0f,0.0f);

path.quadTo(100.0f,150.0f,200.0f,200.0f);
path.closePath();
g2d.setColor(Color.blue);
g2d.fill(path);
}

JAVA Development Kit Software 1.1–1.2 Enhancements -57 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

2-D Graphics API Overview


A 2-D Graphics curve example

images/chapter3/bezier.gif

JAVA Development Kit Software 1.1–1.2 Enhancements -58 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

2-D Graphics API Overview


A 2-D Graphics transparency example
...
Image im = getImage(getCodeBase(),
"duke_waving.gif");
AlphaComposite alpha =
AlphaComposite.getInstance
(AlphaComposite.SRC_OVER);

g2d.setComposite(alpha);
g2d.drawImage(im, 75, 25, this);
...

JAVA Development Kit Software 1.1–1.2 Enhancements -59 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

2-D Graphics API Overview


A 2-D Graphics transparency example

images/chapter3/alpha1.gif

JAVA Development Kit Software 1.1–1.2 Enhancements -60 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

2-D Graphics API Overview


Another 2-D Graphics transparency example
...
Image im = getImage(getCodeBase(),
"duke_waving.gif");
AlphaComposite alpha =
AlphaComposite.getInstance
(AlphaComposite.SRC_OVER,0.5);
g2d.setComposite(alpha);
g2d.drawImage(im, 75, 25, this);
...

JAVA Development Kit Software 1.1–1.2 Enhancements -61 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

2-D Graphics API Overview


Another 2-D Graphics transparency example

images/chapter3/alpha2.gif

JAVA Development Kit Software 1.1–1.2 Enhancements -62 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Drag and Drop Technology


• What is drag and drop?
• User initiated data transfer reflected in a GUI
environment.
• In Java technology, this is between Java
programming applications and potentially between
Java programming applications and native
applications.
• It is a copy and paste shortcut.

JAVA Development Kit Software 1.1–1.2 Enhancements -63 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Drag and Drop Technology


• Design goals
• Enable drag and drop in Java technology
• Appear between VMs
• Keep pace with industry
• Ensure success with applications on PCs, NCs, the
web, and so forth

JAVA Development Kit Software 1.1–1.2 Enhancements -64 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Drag and Drop Technology


• Drag and drop concepts
• The location where the dragging begins is the drag
source
• The location where the object is dropped is the drag
target
• The icon displayed during the transfer is called the
hot spot

JAVA Development Kit Software 1.1–1.2 Enhancements -65 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Drag and Drop Technology

images/chapter3/dnd.gif

JAVA Development Kit Software 1.1–1.2 Enhancements -66 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Drag and Drop Technology


• Java technology and drag and drop
• Logic is in the java.awt.dnd package.
• The dragging functionality is in DragSource.
• The dropping functionality is in DropTarget.
• The object being transfered implements the
Transferable interface.

JAVA Development Kit Software 1.1–1.2 Enhancements -67 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Drag and Drop Technology


• DragGestureRecognizer
• Recognizes drag and drop initation for each platform
• Fires events to DragGestureListener
implementations
• DragGestureListener
public void
dragGestureRecognized
(DragGestureEvent dge);

JAVA Development Kit Software 1.1–1.2 Enhancements -68 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Drag and Drop Technology


• DragSource
• Creates a DragGestureRecognizer that identifies
platform-specific drag and drop initiations
• Calls the startDrag() method to start things
DragSource source =
DragSource.getDefaultDragSource();

DragGestureRecognizer dgr =
source.createDefaultDragGestureRecognizer
(component, DnDConstants.ACTION_COPY,
dragSourceListener);

JAVA Development Kit Software 1.1–1.2 Enhancements -69 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Drag and Drop Technology


• DragSourceListener
• Provides source-side callbacks to a DragSource for the
following behaviors:
public void dragExit(DragSourceEvent e)
public void dragEnter(DragSourceDragEvent e)
public void dragOver(DragSourceDragEvent e)
public void dropActionChanged(DragSourceDragEvent e)
public void dragDropEnd(DragSourceDropEvent e)

JAVA Development Kit Software 1.1–1.2 Enhancements -70 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Drag and Drop Technology


• DropTarget
• Is associated with some component that wants to receive
drops
• Talks to a DropTargetListener
DropTarget target =
new DropTarget(component,
DnDConstants.ACTION_COPY,
droptargetListener);

JAVA Development Kit Software 1.1–1.2 Enhancements -71 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Drag and Drop Technology


• DropTargetListener
• Provides target-side callbacks to a drop target for the
following behaviors:
public void dragExit(DropTargetEvent e)
public void dragEnter(DropTargetDragEvent e)
public void dragOver(DropTargetDragEvent e)
public void dropActionChanged
(DropTargetDragEvent e)
public void drop(DropTargetDropEvent e)

JAVA Development Kit Software 1.1–1.2 Enhancements -72 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Drag and Drop Technology


Other drag and drop classes
• Transferable
Defines behavior of objects so target can get data sent by
source
• DataFlavor
Associates MIME type with Java class
• FlavorMap
Maps native strings to MIME types and DataFlavors

JAVA Development Kit Software 1.1–1.2 Enhancements -73 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Drag and Drop Technology


A drag and drop example
public void drop(DropTargetDropEvent dtde){
dtde.acceptDrop(dtde.getSourceActions());
Transferable select =
dtde.getContents(this);
Object o =
select.getTransferData
(DataFlavor.stringFlavor);
String s = (String)o;
dtde.dropComplete(true);
}

JAVA Development Kit Software 1.1–1.2 Enhancements -74 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Accessibility and Assistive


Technologies
What are assistive technologies?
• Help people with disabilities use computers and
software
• Use synthesized speech recognition tools and programs
• Use screen magnifiers, renderers, and readers
• Have helpful user interfaces and designs

JAVA Development Kit Software 1.1–1.2 Enhancements -75 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Accessibility and Assistive


Technologies
Why are they important ?
• Over 40 million Americans have a disability of some
kind
• Over 7 million Americans cannot use a computer
without some form of assistive technology aid
• Americans With Disabilites Act of 1992

JAVA Development Kit Software 1.1–1.2 Enhancements -76 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Accessibility and Assistive


Technologies
Accessibility in Java technology
• Most Swing components implement Accessible which
defines one method, getAccessibleContext().
• The AccessibleContext of a component represents
the minimum information needed to return.
• This information can be used by assistive technology
developers to enhance systems.

JAVA Development Kit Software 1.1–1.2 Enhancements -77 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Accessibility and Assistive


Technologies
AccessibleContext
• Contains information about the name, description, role,
and actions available from some accessible component
• If supported, has one or more of the following
interfaces:
• AccessibleAction – Discover what actions can be
performed and how to perform them
• AccessibleComponent – Sets the graphical
representation of an object

JAVA Development Kit Software 1.1–1.2 Enhancements -78 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Accessibility and Assistive


Technologies
AccessibleContext
• AccessibleSelection – Select the children of an
accessible object
• AccessibleText – Access text via content,
attributes, and location
• AccessibleValue – Determine and set a numeric
value

JAVA Development Kit Software 1.1–1.2 Enhancements -79 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Accessibility and Assistive


Technologies
Demonstration
private void go(Accessible acc){
AccessibleContext context =
acc.getAccessibleContext();
context.getAccessibleAction().
doAccessibleAction(0);
}

JAVA Development Kit Software 1.1–1.2 Enhancements -80 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Accessibility and Assistive


Technologies

Component Action

JButton Click

JCheckBox Click

JRadioButton Click

JComboBox Toggle Box

JAVA Development Kit Software 1.1–1.2 Enhancements -81 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

AWT Enhancements
Multithreaded event queue
• The event-dispatching thread handles events
(actionPerformed()) and rendering (paint()).
• Operations can interfere with the event-dispatch thread
and vice-versa.
• invokeLater(Runnable r) and
invokeAndWait(Runnable r) solve this interference.
• Developers can have specific code run inside the event-
dispatching thread.

JAVA Development Kit Software 1.1–1.2 Enhancements -82 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

AWT Enhancements
class ABC implements Runnable{
public void run(){ // some intensive work }
}

//invokeLater(Runnable r)
Runnable r = new ABC();
SwingUtilities.invokeLater(r); // don't wait
//around
//invokeAndWait(Runnable r)
Runnable r = new ABC();
SwingUtilities.invokeAndWait(r); // wait until
//done

JAVA Development Kit Software 1.1–1.2 Enhancements -83 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

AWT Enhancements
• Cursors
• All the basic cursors are provided with JDK software
• Cursors are customarily 16x16 or 32x32 pixels in size
• A custom cursor
Toolkit t = Toolkit.getDefaultToolkit();
Image im = t.getImage("copy.gif");
Cursor copyCursor = t.createCustomCursor(im,
new Point(0,0), "copy");
this.setCursor(copyCursor);

JAVA Development Kit Software 1.1–1.2 Enhancements -84 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

AWT Enhancements
• Keyboard navigation
• Mnemonics are included with Swing
• AbstractButton defines the get/setMnemonic
methods
• The platform-specific accelerator key (Alt, Ctrl, Meta
and so forth) is used

JAVA Development Kit Software 1.1–1.2 Enhancements -85 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

AWT Enhancements
• KeyStroke
• Represents a key being typed on the keyboard
• Can contain a modifier if needed (Alt, Ctrl, Meta, and
so on)
• Defines high-level action events
• Does not trap every keystroke
• KeyStroke example
KeyStroke ks = KeyStroke.getKeyStroke('g');
rb1.registerKeyboardAction(actionListener, ks,
JComponent.WHEN_IN_FOCUSED_WINDOW);

JAVA Development Kit Software 1.1–1.2 Enhancements -86 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Module Summary
• Swing is the future of Java GUIs.
• Swing provides modern, configurable, and accessible
controls.
• 2-D Graphics greatly improve the imaging and
rendering capabilities of Java technology.
• Drag and drop brings Java technology into the modern
UI world.
• Java accessibility is essential for continued success with
user with disabilities.

JAVA Development Kit Software 1.1–1.2 Enhancements -87 of 87


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Module 4

Collections

JAVA Development Kit Software 1.1–1.2 Enhancements November 1998


Sun Educational Services

Objectives
• Define collections
• List advantages of the Collections API
• Explain design goals

JAVA Development Kit Software 1.1–1.2 Enhancements -2 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Introduction to Collections
• What are collections?
• Individual objects that represent a group of objects
• Containers
• Examples of collections
• Arrays
• Vector
• Hashtable

JAVA Development Kit Software 1.1–1.2 Enhancements -3 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Java Collections API


The collection framework
• A unified architecture
• Interfaces which provide implementation
independence
• Implementations which offer reuse of data structures
• Algorithms that offer reuse of functionality

JAVA Development Kit Software 1.1–1.2 Enhancements -4 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Advantages of Collections
• Little effort to learn new API
• Little effort to implement API
• Leads to reusability
• Interoperability between different APIs

JAVA Development Kit Software 1.1–1.2 Enhancements -5 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Design Goals
• Small API
• Easy to learn
• Reasonably powerful
• Works with earlier collections
• Easily extensible

JAVA Development Kit Software 1.1–1.2 Enhancements -6 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Core Collection Interfaces


Collection
• Has a root interface of core heirarchy
• Represents a group of objects called elements
• Allows for manipulation of collections in an
independent fashion
• Contains many optional methods, throwing an
UnsupportedOperationException

JAVA Development Kit Software 1.1–1.2 Enhancements -7 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Core Collection Interfaces


Some Collection methods
int size();
boolean isEmpty();
boolean contains(Object element);
boolean add(Object element); // Optional
boolean remove(Object element); // Optional
Object[] toArray();
boolean containsAll(Collection c);
boolean addAll(Collection c); // Optional
boolean removeAll(Collection c); // Optional
boolean retainAll(Collection c); // Optional
void clear(); // Optional

JAVA Development Kit Software 1.1–1.2 Enhancements -8 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Core Collection Interfaces


Set
• Extends Collection
• Stipulates no duplicate elements
• Returns false if element already is contained
• At most, allows one null element
• Adds no methods to Collection

JAVA Development Kit Software 1.1–1.2 Enhancements -9 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Core Collection Interfaces


List
• An ordered collection
• Sometimes called a sequence
• Offers control over positioning elements
• Usually allows duplicate elements, including null
elements

JAVA Development Kit Software 1.1–1.2 Enhancements -10 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Core Collection Interfaces


Some List methods
Object get(int index);
Object set(int index, Object element); // Optional
void add(int index, Object element); // Optional
Object remove(int index); // Optional
void removeRange(int start, int end); // Opt
boolean addAll(int index, Collection c); // Opt
int indexOf(Object o);
int lastIndexOf(Object o);

JAVA Development Kit Software 1.1–1.2 Enhancements -11 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Core Collection Interfaces


Map
• Extends Collection
• Maps keys to values
• Does not allow duplicate keys
• Replaces the Dictionary class

JAVA Development Kit Software 1.1–1.2 Enhancements -12 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Core Collection Interfaces


Map methods
int size();
boolean isEmpty();
boolean containsKey(Object key);
boolean containsValue(Object value);
Object get(Object key);
Object put(Object key, Object value); // Optional
Object remove(Object key); // Optional
void putAll(Map map); // Optional
void clear(); // Optional
Set keySet();
Set entrySet();
Collection values();

JAVA Development Kit Software 1.1–1.2 Enhancements -13 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Implementations
Abstract base classes
• Implements corresponding interface
• Extends to create implementations
• Provides minimal implementation
• AbstractCollection
• AbstractSet
• AbstractMap
• AbstractList
• AbstractSequentialList

JAVA Development Kit Software 1.1–1.2 Enhancements -14 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Implementations
HashSet
• Extends AbstractSet
• Does not allow duplicate elements
• Has no guarantee of order
• Is not thread-safe
HashSet hs = new HashSet();
Integer x = new Integer(1);
Integer y = new Integer(2);
hs.add(x);
hs.add(y);
hs.add(x); // not added
int size = hs.size(); // 2
Object [] stuff = hs.toArray();

JAVA Development Kit Software 1.1–1.2 Enhancements -15 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Implementations
HashMap
• Extends AbstractMap
• Does not allow duplicate keys
• Has no guarantee of order and is not thread-safe
HashMap hm = new HashMap();
Integer x = new Integer(1);
Integer y = new Integer(2);
hm.put("First", x);
hm.put("Second", y);
hm.put("First", x); // not added
hm.put("Third", x); //added
int size = hs.size(); // 3
hs.containsKey("First"); //true

JAVA Development Kit Software 1.1–1.2 Enhancements -16 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Implementations
ArrayList
• Extends AbstractList
• Is resizable dynamically
• Acts like Vector, but unsynchronized
• Provides random access and not thread-safe
HashSet set = new HashSet();
set.add("Bluejay");
set.add("Oriole");
set.add("Sparrow");
ArrayList list = new ArrayList(set);
int size = list.size(); //3
list.contains("Bluejay"); // true
list.ensureCapacity(25);

JAVA Development Kit Software 1.1–1.2 Enhancements -17 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Basic Implementations
LinkedList
• Extends AbstractSequentialList
• Is resizable dynamically
• Provides sequential access
• Is not thread-safe
LinkedList list = new LinkedList();
list.add("Bluejay");
list.addFirst("Oriole");
list.addLast("Sparrow");
String s = list.getFirst(); // returns "Bluejay"
String t = list.getLast(); // returns "Sparrow"

JAVA Development Kit Software 1.1–1.2 Enhancements -18 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Iterators
Iterator
• Represents an iteration over a collection
• Replaces Enumeration for collections
• Differs from Enumeration
• Allows removal of elements from collection
• Has improved method names
hasNext() // true if there is a nextelement
next() // returns next element
remove() // removes last element retrieved
(optional)

JAVA Development Kit Software 1.1–1.2 Enhancements -19 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Iterators
ListIterator
• Extends Iterator
• Allows traversal in either direction
• Can optionally support adding/removing
• Adds some new methods:
asPrevious() // true if there is a
//previous element
previous() // returns previous element
set(Object o) // replaces last element
//retrieved - optional
add(Object o) //adds new element - optional

JAVA Development Kit Software 1.1–1.2 Enhancements -20 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Iterators
ListIterator example
LinkedList list = new LinkedList();
list.add("Bluejay");
list.add("Oriole");
ListIterator iterator = list.listIterator();
iterator.add("Sparrow");
while(iterator.hasNext()){
System.out.println(iterator.next());
}
• The output
• Bluejay
• Sparrow
• Oriole

JAVA Development Kit Software 1.1–1.2 Enhancements -21 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Anonymous Implementations
The Collections class
• Consists only of static methods
• Contains algorithms for general operations
• Has manipulations
• binarySearch(List list, Object element)
• copy(List listOne, List listTwo)
• max(Collection coll)
• min(Collection coll)
• sort(List list)

JAVA Development Kit Software 1.1–1.2 Enhancements -22 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Anonymous Implementations
• Unmodifiable collections
unmodifiableCollection(Collection coll)
unmodifiableXXX(XXX xxx)
• Synchronized wrappers
• Arrays
• Static methods for copying, sorting, and searching
arrays
• Methods that convert arrays to lists

JAVA Development Kit Software 1.1–1.2 Enhancements -23 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Anonymous Implementations
• Collections examples

Collections.sort(list); // required before search


int index = Collections.binarySearch
(list,"Bill");
List frozenList =
Collections.unmodifiableList(list);
frozenList.add("Johnnie"); // not allowed

• Arrays examples
int [] array = {1,4,2,5,3};
Arrays.sort(array); // now 1,2,3,4,5
int index = Arrays.binarySearch(array, 2) // 1

JAVA Development Kit Software 1.1–1.2 Enhancements -24 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Synchronized Wrappers
Details of synchronized wrappers
• Gives collections thread safety
• Has iterations which must be done in a synchronized
block
• Must access through the new version

JAVA Development Kit Software 1.1–1.2 Enhancements -25 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Synchronized Wrappers
Example
LinkedList list = new LinkedList(); // not
thread-safe
list.add("Bluejay");
list.add("Oriole");
list.add("Sparrow");
List safeList =
Collections.synchronizedList(list);
synchronized(safeList){
ListIterator iterator = safeList.listIterator();
...
}
list.addFirst("Magpie"); // NO!
...

JAVA Development Kit Software 1.1–1.2 Enhancements -26 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Comparing Collections
Comparable
• Has an interface implemented by specific classes
• Provides natural ordering
• Has its functionality in the compareTo(Object o)
method
• Supports some implementing classes: String, URL,
and Date

JAVA Development Kit Software 1.1–1.2 Enhancements -27 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Comparing Collections
Comparator
• Interface implemented by specific classes
• Overrides any natural ordering
• Two methods
compare(Object one,Object two)
-1 = less than
0 = equals
1 = greater than
equals(Object o)

JAVA Development Kit Software 1.1–1.2 Enhancements -28 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Comparing Collections
Comparator example
class AnyString implements Comparator{
public int compare(Object one, Object two){
String s = (String)one;
String t = (String)two;
return s.compareToIgnoreCase(t);
}
}
...
LinkedList list = new LinkedList();
list.add("Bluejay");
list.add("Oriole");
list.add("Sparrow");
Collections.sort(list, new AnyString());

JAVA Development Kit Software 1.1–1.2 Enhancements -29 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Compatibility
• Changes to existing classes
• Vector implements List
• Hashtable implements Map
• Arrays.asList(someArray)
• Has backward compatibility
• someCollection.toArray()
• new Vector(someCollection)
• new Hashtable(someMap)

JAVA Development Kit Software 1.1–1.2 Enhancements -30 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Compatibility
• Cross compatibility
• new LinkedList(Collection c)
• new HashSet(Collection c)
• new HashMap(Map m)

JAVA Development Kit Software 1.1–1.2 Enhancements -31 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Module Summary
• New Collections API improve on basic types.
• Simple API allows for short learning curve.
• Built-in algorithms meet common needs.
• Extensible implementations meet advanced needs.
• There is wide-ranged compatibility.

JAVA Development Kit Software 1.1–1.2 Enhancements -32 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Module 5

Remote Method Invocation

JAVA Development Kit Software 1.1–1.2 Enhancements November 1998


Sun Educational Services

Objectives
• Describe RMI
• Build an RMI system
• Introduce activation
• Build an activation system
• Describe custom socket factories
• Introduce SSL support

JAVA Development Kit Software 1.1–1.2 Enhancements -2 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Overview of RMI
What is RMI?
• Remote method invocation
• Distributed OO programming in Java technology
• Similar to RPC
• Different than CORBA and DCOM
• 100% Pure Java solution

JAVA Development Kit Software 1.1–1.2 Enhancements -3 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Overview of RMI
What are the benefits?
• Simple to implement
• Transparent access to remote objects
• Distributed garbage collection
• No extra language to learn
• Ships with standard JDK distribution

JAVA Development Kit Software 1.1–1.2 Enhancements -4 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Overview of RMI
Are there drawbacks?
• Static binding of objects
• Not as flexible as CORBA
• Not persistent by default
• Latency introduced to the system

JAVA Development Kit Software 1.1–1.2 Enhancements -5 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Building an RMI Solution


1. Define a remote interface.
• Defines the "services" of the remote object
• Generally extends Remote
• Has all methods throw RemoteException
public interface Bird extends Remote{
public String[] getBird()
throws RemoteException;
}

JAVA Development Kit Software 1.1–1.2 Enhancements -6 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Building an RMI Solution


2. Implement the interface.
• Defines the remote object
• Extends UnicastRemoteObject
• Adds "local" methods
• Has a constructor throw RemoteException

JAVA Development Kit Software 1.1–1.2 Enhancements -7 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Building an RMI Solution


public class BirdImpl extends UnicastRemoteObject
implements Bird{

private String[] birds =


{"Bluejay","Oriole","Sparrow"};
public BirdImpl() throws RemoteException{}
public String[] getBird()
throws RemoteException{
return birds;
}
public void supportMethod(){...}
}

JAVA Development Kit Software 1.1–1.2 Enhancements -8 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Building an RMI Solution


3. Compile the code.
javac Bird.java BirdImpl.java
4. Create the stubs and skeletons.
• Create proxy objects
• Use stubs for the client
• Use skeletons for the server
rmic BirdImpl
produces
BirdImpl_Stub.class
BirdImpl_Skel.class

JAVA Development Kit Software 1.1–1.2 Enhancements -9 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Building an RMI Solution


5. Create a server class.
• Instantiates the implementation (BirdImpl)
• Binds the object to the registry
class Server{
public static void main(String [] args){
try{
BirdImpl impl = new BirdImpl();
Naming.rebind("Bird", impl);
}catch(Exception e){
// RemoteException
// MalformedURLException
// UnknownHostException
}
}

JAVA Development Kit Software 1.1–1.2 Enhancements -10 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Building an RMI Solution


6. Start the registry.
• Has a simple naming service
• Is not persistent
• Binds objects with specified name (for example, Bird)
• Uses the default port 1099
• Produces no output unless there is an error
rmiregistry

JAVA Development Kit Software 1.1–1.2 Enhancements -11 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Building an RMI Solution


7. Create a client.
• Looks up object on server using URL string
• Deals only with interface, never implementation
• Receives a remote reference
• Makes method calls appear as being local
class Client{
...
String [] birds = null;
Bird bird = null;
String url="rmi://serverName:1099/Bird";
try{
birds = (Bird)Naming.lookup(url);
bird = birds.getCrew();
}catch(Exception e){ }

JAVA Development Kit Software 1.1–1.2 Enhancements -12 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Building an RMI Solution


RMI semantics
• Remote objects are represented by proxy objects
• Sockets are created and maintained by RMI subsystem
• Remote objects are passed by reference
• Non-remote objects are passed by value (as parameters
or return types)
• Non-remote objects must implement Serializable

JAVA Development Kit Software 1.1–1.2 Enhancements -13 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Remote Object Activation


What is activation?
• Offers a persistent remote object state
• Has objects which can be created "on demand"
• Uses a new tool, rmid,which is a virtual machine that
can create other virtual machines
• Introduces a new interface, Activatable
• Has implementations which deactivate themselves

JAVA Development Kit Software 1.1–1.2 Enhancements -14 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Remote Object Activation


Obtain a reference to object

images/chapter5/proto_1.gif

JAVA Development Kit Software 1.1–1.2 Enhancements -15 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Remote Object Activation


Perform normal method invocation

images/chapter5/proto_3.jpg

JAVA Development Kit Software 1.1–1.2 Enhancements -16 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

New Activatable Classes


Activator
• Facilitates remote object activation
• Contains the activate() method
• Calls to retrieve a "live" reference
• Uses an ActivationID to find object
ActivationID
• Denotes activatable remote objects
• Has a remote reference to an object Activator
• Contains a unique object ID
JAVA Development Kit Software 1.1–1.2 Enhancements -17 of 32
Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

New Activatable Classes


MarshalledObject
• Contains a serialized object in a byte stream
• Has the get() method return a copy of the original
object
• Stores objects
ActivationDesc
• Contains information necessary to do activation
• Has a group identifier
• Has a class name
• Has a class file location (codebase)
• Contains the MarshalledObject
JAVA Development Kit Software 1.1–1.2 Enhancements -18 of 32
Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Building an Activatable System


1. Create the remote interface.
public interface Active extends Remote{

public String getData()


throws RemoteException;

JAVA Development Kit Software 1.1–1.2 Enhancements -19 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Building an Activatable System


2. Create the implementation.
• Extend Activatable
• Provide constructor with two parameters:
ActivationID and MarshalledObject
public class ActiveImpl extends Activatable
implements Active{
public ActiveImpl(ActivationID id,
MarshalledObject obj)
throws RemoteException{
super(id, 0); // id and port (any)
}
public String getData() throws RemoteException{
return "It's alive!!!; } }

JAVA Development Kit Software 1.1–1.2 Enhancements -20 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Building an Activatable System


3. Create stubs and skeletons.
rmic ActiveImpl
4. Create the server.
RMISecurityManager rsm = new RMISecurityManager();
System.setSecurityManager(rsm); // required
String url = "/home/code/"; // location
try{
ActivationDesc desc =
new ActivationDesc
("ActiveImpl",url,null);
active = (Active)Activatable.register(desc);
Naming.rebind("Active",active);
}catch(Exception e){...}
System.exit(0); // see ya!

JAVA Development Kit Software 1.1–1.2 Enhancements -21 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Building an Activatable System


5. Start rmiregistry and rmid.
rmiregistry
rmid
6. Create client as normal.
String url = "rmi://serverName:1099/Active";
Active active = (Active)Naming.lookup(url);
System.out.println(active.getMessage());

JAVA Development Kit Software 1.1–1.2 Enhancements -22 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Custom Socket Factories


Why custom sockets?
• Control over socket communication
• Encryption
• Compression
• Allows use of different socket types

JAVA Development Kit Software 1.1–1.2 Enhancements -23 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Custom Socket Factories


JDK 1.1 software support
• It was possible to create custom sockets.
• Sockets were per RMI transport.
• Registry had to communicate the socket protocol.
JDK 1.2 software enhancements
• There are custom sockets per object.
• Default registry is used.

JAVA Development Kit Software 1.1–1.2 Enhancements -24 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Custom Socket Factories


How to use the factories
• Define RMISocketFactory to create sockets
• Decide on custom socket
• One for client (RMIClientSocketFactory)– Define
createSocket(String host, int port)
• One for server(RMIServerSocketFactory)–
Define createServerSocket(int port)

JAVA Development Kit Software 1.1–1.2 Enhancements -25 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Custom Socket Factories


Defining the factories
public class ABCClientFactory implements
RMIClientSocketFactory{
public Socket createSocket
(String host, int port){
return new ABCSocket(host, port);
}
}
}
public class ABCServerFactory implements
RMIServerSocketFactory{
public ServerSocket createServerSocket
(int port){
return new ABCServerSocket(port);
}
} }

JAVA Development Kit Software 1.1–1.2 Enhancements -26 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Custom Socket Factories


Installing the factory
• Write a constructor that takes the factories
• Use UnicastRemoteObject or Activatable for server
implementation
public class MyImpl extends UnicastRemoteObject
implements MyInterface{
public MyImpl(String host, int port) throws
RemoteException{
super(0,
new ABCClientFactory(host,port),
new ABCServerFactory(port));
}
...
}

JAVA Development Kit Software 1.1–1.2 Enhancements -27 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

RMI and SSL


Now supported in JDK 1.2 software
• Uses custom sockets and the socket factories
• Contains some objects which can be sent over SSL while
others are not
Java Standard Extension
• API provided by Sun™ in javax.net.ssl
• Some third-party vendors implement this API

JAVA Development Kit Software 1.1–1.2 Enhancements -28 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

RMI and SSL


Third-party vendors
• Phaos Technology – SSLava Toolkit
• Baltimore Technologies – J/SSL
• JCP Computer Service – SSL - Pro
• IAIK – iSaSiLk

JAVA Development Kit Software 1.1–1.2 Enhancements -29 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

RMI and SSL


javax.net.ssl
• SSLSockets and SSLServerSockets
• Socket factories
• Session capabilities, including authentication
• Handshaking and notification of completion
• SSL-specific exceptions
• Cipher suites
• Options for RC4, DES, 3-DES, and CBC mode
(cipherblock chaining) and null encryption
(authentication only)
JAVA Development Kit Software 1.1–1.2 Enhancements -30 of 32
Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

RMI and SSL


The URL class and HTTPS
• Use the normal java.net.URL class
• Provide support for common SSL access

JAVA Development Kit Software 1.1–1.2 Enhancements -31 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Module Summary
• RMI provides a distributed, Java technology solution.
• Activation is now provided, increasing performance.
• Building an RMI solution is fairly simple.
• Socket factories provide customized control.
• SSL support is provided by a standard interface.
• Third-party vendors provide SSL implementations.

JAVA Development Kit Software 1.1–1.2 Enhancements -32 of 32


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Module 6

JavaBeans Components

JAVA Development Kit Software 1.1–1.2 Enhancements November 1998


Sun Educational Services

Objectives
• Define the JavaBean component model
• Introduce model characteristics
• Explain the JavaBeans Activation Framework
• Describe the InfoBus
• Review Enterprise JavaBeans

JAVA Development Kit Software 1.1–1.2 Enhancements -2 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JavaBeans Defined
• What is a JavaBean?
• A reusable software component that can be
manipulated visually in a builder tool
• A cross-platform, cross-builder tool, flexible,
component model for Java technology
• Why use JavaBeans components?
• They offer true reusability in a dynamically
introspective fashion.
• They give Java technology a modern component
architecture for flexible solutions.

JAVA Development Kit Software 1.1–1.2 Enhancements -3 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JavaBeans Defined
• What does the model define?
• Java technology software components
• How the components interact
• Standardized "hooks" for builder tool compatibility

JAVA Development Kit Software 1.1–1.2 Enhancements -4 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JavaBeans Defined
• Design goals
• Simple to learn, design, and use
• Leverages existing Java technology features
• Can be built with builder tools and manually
• Extensible enough to build complex solutions

JAVA Development Kit Software 1.1–1.2 Enhancements -5 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JavaBeans Characteristics
• Introspection
• Is based on Java Reflection API
• Has automatic discovery of a Bean
• Learns the interface
• Has standard "design patterns" that make this simple
• Enables more complex control via a BeanInfo

JAVA Development Kit Software 1.1–1.2 Enhancements -6 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JavaBeans Characteristics
• Properties
• Represent a component's state
• Are defined with "getter" and "setter" methods
• Have three styles of properties – Simple, Bound, and
Constrained

JAVA Development Kit Software 1.1–1.2 Enhancements -7 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JavaBeans Characteristics
• Events
• Are based on standard event model
• Allow for registration of listeners
• Fire methods in a standard fashion
• Offer Bean communication
• Provide an extremely flexible solution

JAVA Development Kit Software 1.1–1.2 Enhancements -8 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JavaBeans Characteristics
• Customization
• Allows for simple control of a Bean's properties
• Has two GUI mechanisms for customization –
Property Sheets and Customizers
• Is completely configurable

JAVA Development Kit Software 1.1–1.2 Enhancements -9 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JavaBeans Characteristics
• Persistence
• Uses Java Serialization API
• Allows for a customized bean to be stored
• Has a mechanism that is customizable
• Distributed capabilities
• Is fully capable of working with distributed APIs –
RMI, JDBC, and CORBA; for example, Enterprise
JavaBeans
• Component delivery
• Packages Beans in JAR files

JAVA Development Kit Software 1.1–1.2 Enhancements -10 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Samples of Characteristics
• Properties
public void setMonster(Monster m){...}
public Monster getMonster(){...}
• Event sources
public void addMonsterListener(MonsterListener
m){...}
public void
removeMonsterListener(MonsterListener m({...}
...

JAVA Development Kit Software 1.1–1.2 Enhancements -11 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Samples of Characteristics
• Other characteristics
• Must be public classes
• Must have a no-arg constructor
• Must contain private data and public methods

JAVA Development Kit Software 1.1–1.2 Enhancements -12 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JavaBeans Activation Framework


• What is the JAF™?
• A data typing and registry technology
• A technology based on MIME types
• A technology distributed as a standard extension
(javax)
• Motivation for the JAF
• Binding data types to a component
• Having methods for discovering component data
types
• Modernizes JavaBeans architecture
JAVA Development Kit Software 1.1–1.2 Enhancements -13 of 56
Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JavaBeans Activation Framework


• Design goals
• Mechanisms determine arbitrary data types
• Encapsulate data access
• Discover operations available on data
• Instantiate components associated with data

JAVA Development Kit Software 1.1–1.2 Enhancements -14 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JAF Architecture
• DataSource interface
• Abstracts an object containing data
• Provides streams to access data
• Stores a string defining the MIME type
• Provides two implementations
FileDataSource
URLDataSource

JAVA Development Kit Software 1.1–1.2 Enhancements -15 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JAF Architecture
• Implementing DataSource
• Is responsible for typing the data
• Provides various types associations

JAVA Development Kit Software 1.1–1.2 Enhancements -16 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JAF Architecture
• DataHandler class
• Provides an interface to data in various formats and
sources
• Manages stream-to-String conversions
• Provides access to command associated with data

JAVA Development Kit Software 1.1–1.2 Enhancements -17 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JAF Architecture
• DataHandler implements Transferable
• Allows data to be used in AWT data transfer
operations
• Uses a DataContentHandler associated with the
MIME types
• Has DataHandler encapsulate this access

JAVA Development Kit Software 1.1–1.2 Enhancements -18 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JAF Architecture
• CommandMap class
• Retrieves the correct command object to operate on
the data
• Stores some or all of the mapping in a .mailcap file
• Enables the .mailcap fileto work with a mime.types
file

JAVA Development Kit Software 1.1–1.2 Enhancements -19 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Building A JAF Solution


1. Write the JAF enabled code.
List list = new List(10, false);
String [] files = new File(".").list();
//add items to list
public void actionPerformed(ActionEvent e){
DataSource ds = new FileDataSource(
new File(index));
DataHandler dh = new DataHandler(ds);
CommandInfo [] info =
handler.getPreferredCommands();
for(int i = 0; i < info.length; i++){
Object cmd = info[i].
getCommandObject(handler,
this.getClass().getClassLoader());
frame.add((Component)cmd);}

JAVA Development Kit Software 1.1–1.2 Enhancements -20 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Building A JAF Solution


2. Write a .mailcap file.
• Holds a list of the MIME types and user-visible
commands
image/gif;; x-java-view=ImageViewer
text/*;; x-java-view=TextViewer
text/*;; x-java-edit=TextEditor
3. Write a mime.types file.
text/html html htm
text/plain txt text java
image/gif gif

JAVA Development Kit Software 1.1–1.2 Enhancements -21 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

The InfoBus
What is the InfoBus?
• Standardized communication for Java technology
solutions
• Finding the data
• Representing the data
• Exchanging the data
• Doing all at runtime
• A technology that works with applets, JavaBean
components, servlets, and so on

JAVA Development Kit Software 1.1–1.2 Enhancements -22 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

The InfoBus
• Better solution than using scripting to "bind" objects
• Developed by Lotus, licensed by Sun for the JDK
software
• Distributed as a Java Standard Extension

JAVA Development Kit Software 1.1–1.2 Enhancements -23 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

The InfoBus
• Characteristics of the InfoBus
• Works within one VM
• Provides hooks for intra-VM communication
• Has a standard interface to access data
• Dynamically informs interested code of new data
• Is 100% Pure Java
• Is vendor-independent
• Is free

JAVA Development Kit Software 1.1–1.2 Enhancements -24 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

The InfoBus
• How does the InfoBus work?
• Finds the data
• Defines interfaces for representing data
• Defines interfaces for accessing data
• Transfers the actual data directly between the
components

JAVA Development Kit Software 1.1–1.2 Enhancements -25 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

The InfoBus
Diagram of the InfoBus

images/chapter6/model.gif

JAVA Development Kit Software 1.1–1.2 Enhancements -26 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Building an InfoBus Solution


1. Become a member of the InfoBus.
• Any component can connect to an InfoBus
• It must implement InfoBusMember interface
• Delegate the interface methods to
InfoBusMemberSupport.
public class BusApplet extends JApplet
implements InfoBusMember{
private InfoBusMemberSupport support;
private InfoBus bus;
public void init(){
support = new
InfoBusMemberSupport(this);
bus = support.joinInfoBus("MonsterBus");}}

JAVA Development Kit Software 1.1–1.2 Enhancements -27 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Building an InfoBus Solution


2. Register as a listener for data.
• The producer listens for data requests.
bus.addDataProducer(this);
...
}
public void dataItemRequested
(InfoBusItemRequestedEvent e){...}

JAVA Development Kit Software 1.1–1.2 Enhancements -28 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Building an InfoBus Solution


• The consumer listens for data.
bus.addDataConsumer(this);
...
}
public void dataItemAvailable
(InfoBusItemAvailableEvent e){...}
public void dataItemRevoked
(InfoBusItemRevokedEvent e){...}

JAVA Development Kit Software 1.1–1.2 Enhancements -29 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Building an InfoBus Solution


3. Producers announce available data.
...
DataFlavor [] flavors =
{DataFlavor.stringFlavor};
bus.fireItemAvailable("Data",flavors,this);
...

4. Consumers request data.


...
DataFlavor [] flavors =
{DataFlavor.stringFlavor};
Object o =
bus.findDataItem("Data",flavors,this);

JAVA Development Kit Software 1.1–1.2 Enhancements -30 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Enterprise JavaBeans
• What are Enterprise JavaBeans?
• Defines a component model for distributed
components
• Supports server-side components

JAVA Development Kit Software 1.1–1.2 Enhancements -31 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Enterprise JavaBeans
• Server components
• Runs application components on a server
• Simplifies moving the logic to the server
• Implements a set of automatic services
• Is non-visual
• Is CORBA compatible

JAVA Development Kit Software 1.1–1.2 Enhancements -32 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Enterprise JavaBeans
• Benefits of multitier solutions
• Increases scalability
• Improves preformance
• Heightens reliability
• Increases flexibility
• Eases system load
• Encapsulates business rules

JAVA Development Kit Software 1.1–1.2 Enhancements -33 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Enterprise JavaBeans
• Component execution systems
• Provides runtime services for EJB components –
Threading, Transactions, State, and Sharing
resources
• Is designed to be implemented by vendors
• Is often an Object Transaction Monitor (OTM)
• Has EJB Servers

JAVA Development Kit Software 1.1–1.2 Enhancements -34 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Enterprise JavaBeans
• Goals of EJB
• Become the standard enterprise component model
for Java technology
• Have toolable server-side components
• Be easy to develop as complexities are abstracted
• "Write Once, Run Anywhere"™
• Have the life cycle of EJB components addressed by
architecture
• Be CORBA compatible, which offers interoperability

JAVA Development Kit Software 1.1–1.2 Enhancements -35 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

EJB Roles
• EJB provider
• Is usually an application domain expert
• Is not expert in system-level programing –
Transactions, concurrency, security, and distribution
• Defines business rule architecture

JAVA Development Kit Software 1.1–1.2 Enhancements -36 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

EJB Roles
• Application assembler
• Composes applications using EJB
• Only recognizes interfaces, not implementations
• Often designs client-side GUIs as well
• Deployer
• Is responsible for EJB components and their
containers
• Adapts EJB components to specific logical
environements
• Modifies the attributes of an EJB component
JAVA Development Kit Software 1.1–1.2 Enhancements -37 of 56
Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

EJB Roles
• Server provider
• Specialist in distributed object management
• Usually an OS, database, or middleware vendor
• Creates the containers for EJB components
• Allowed to create specialized containers
• Bridges to existing application environments
• Has object–relational data mapping

JAVA Development Kit Software 1.1–1.2 Enhancements -38 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

EJB Roles
• System administrator
• Monitors a complete, running system
• Uses management tools to maintain the system

JAVA Development Kit Software 1.1–1.2 Enhancements -39 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

EJB Component Details


• Created and managed by a container
• Container provides an interface to components
• Is customizable by changing properties
• Has external metadata which allows manipulation
• Deployment descriptors
• Security attributes
• Method definitions

JAVA Development Kit Software 1.1–1.2 Enhancements -40 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

EJB Component Details


• Can be deployed in any EJB container
• Standard interfaces allow this
• Applications can be built without writing code
• The client view is defined by EJB developer
• Separate from internal view (container's view)
• Allows "Write Once, Run Anywhere"

JAVA Development Kit Software 1.1–1.2 Enhancements -41 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

EJB Component Details


• Session Beans
• Transient state
• Extension of the client
• Transaction-aware, if desired
• Usually a short lifetime

JAVA Development Kit Software 1.1–1.2 Enhancements -42 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

EJB Component Details


• Entity Beans
• Persistent state
• Persistent object reference that holds a primary key
• Long lifetime
• Multiple users

JAVA Development Kit Software 1.1–1.2 Enhancements -43 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

EJB Container Details


• Distributed infrastructure abstraction
• Assumes an underlying ORB
• Packaging and deployment
• Packaging based on JAR files
• Includes deployment descriptors
• Container extracts the JAR
• Runs EJB based on manifest and descriptors in JAR

JAVA Development Kit Software 1.1–1.2 Enhancements -44 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

EJB Container Details


• Implicit transactions
• No need for clients to make explicit transaction
demarcations
• Container manages start, commit, and rollback
• Transaction rules defined in deployment descriptor

JAVA Development Kit Software 1.1–1.2 Enhancements -45 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

EJB Container Details


• Supports factories
• Containers manage component life cycles.
• The definition of an interface extends EJBHome.
• create(),find() and remove()are defined.
• Factories are registered with the container.

JAVA Development Kit Software 1.1–1.2 Enhancements -46 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

EJB Container Details


• Activation and passivation
• Container calls component when it is loaded
• Component is called when it is unloaded
• Container metadata
• Information about contained Beans – Class name and
methods
• Concurrency automatically handled

JAVA Development Kit Software 1.1–1.2 Enhancements -47 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Example EJB Systems

images/chapter6/runtime.gif

JAVA Development Kit Software 1.1–1.2 Enhancements -48 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Example EJB Systems

images/chapter6/app_serv.gif

JAVA Development Kit Software 1.1–1.2 Enhancements -49 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

EJB Provider Responsibilites


1. Define the component's remote interface.
• Is based on RMI
• Extends javax.ejb.EJBObject
• Defines specific business logic methods
public interface EBean extends EJBObject{
public String [] getData() throws
RemoteException;
}

JAVA Development Kit Software 1.1–1.2 Enhancements -50 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

EJB Provider Responsibilites


2. Provide the EJB component class.
• Implements the appropriate Bbean-type interface
javax.ejb.SessionBean
javax.ejb.EntityBean
• Do not implement the Bean interface (delegation is
used)
• Provide implementations of methods in Bean
interface

JAVA Development Kit Software 1.1–1.2 Enhancements -51 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

EJB Provider Responsibilites


• Optionally provide
ejbCreate() -- zero or more
ejbFind() -- for entity beans

JAVA Development Kit Software 1.1–1.2 Enhancements -52 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

EJB Provider Responsibilites


3. Provide a home interface.
• Define the client-side view
• Extend javax.ejb.EJBHome
• Configure each method to be one or more life cycle
methods
create()
find() // for EntityBean

• Match each defined method to an ejbXXX() method

JAVA Development Kit Software 1.1–1.2 Enhancements -53 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

EJB Provider Responsibilites


4. Provide deployment information.
• Set a serialized version of the appropriate descriptor.

javax.ejb.deployment.SessionDescriptor
javax.ejb.deployment.EntityDescriptor

• Control how the Bean is managed


STATEFUL_SESSION
STATELESS_SESSION
• Include in the deployment JAR file

JAVA Development Kit Software 1.1–1.2 Enhancements -54 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

EJB Provider Responsibilites


5. Package components in a JAR file.
• Provide a manifest file
• Have one entry for each Bean
Name = EBeanImpl.ser
Enterprise-Bean: True

• Include a deployment descriptor

JAVA Development Kit Software 1.1–1.2 Enhancements -55 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Module Summary
• JavaBeans are 100% Pure Java components.
• Components provide true reusability.
• The JAF automates MIME type binding to commands.
• The InfoBus allows transparent communication
between components and applets.
• EJB changes server applications radically.
• EJB is the future of distributed Java technology
solutions.

JAVA Development Kit Software 1.1–1.2 Enhancements -56 of 56


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Module 7

System Enhancements

JAVA Development Kit Software 1.1–1.2 Enhancements November 1998


Sun Educational Services

Objectives
• Discuss improvements in threading model
• Explain improvements in memory usage
• Talk about just-in-time compilers
• Explain the rewritten native libraries
• Describe serialization enhancements

JAVA Development Kit Software 1.1–1.2 Enhancements -2 of 18


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Threading Enhancements
• Solaris native threads option
• In JDK 1.1, Solaris threading was user-level (green
threads)
• In JDK 1.2, native Solaris threads were used
• Native threads enable the use of multiple CPUs and
better performance

JAVA Development Kit Software 1.1–1.2 Enhancements -3 of 18


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Threading Enhancements
• Faster synchronized methods
• Improved monitor design
• Run almost as fast as normal methods now

JAVA Development Kit Software 1.1–1.2 Enhancements -4 of 18


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Memory Management
• The heap
• One heap per VM
• Created on VM start-up
• Runtime memory area for objects and arrays
• Might expand, never contracts
• Improved heap performance
• Java heap is managed automatically by the GC
• Fewer "heap locks" are required in VM
• Decreases time of garbage collection process

JAVA Development Kit Software 1.1–1.2 Enhancements -5 of 18


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Memory Management
• The contant pool and strings
• One per class or interface
• Created when a class or interface is loaded
• In JDK 1.2, strings are shared among classes
• Reduces memory consumption

JAVA Development Kit Software 1.1–1.2 Enhancements -6 of 18


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Just-in-Time Compilers
• What is a JIT?
• Converts bytecode to machine code
• Stores machine code in a runtime cache
• Does not create executable files
• Forces code to run much faster

JAVA Development Kit Software 1.1–1.2 Enhancements -7 of 18


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Just-In-Time Compilers
• JIT is now included with JDK software
• Has default runtime environment
• Can be turned off or replaced with another JIT
java -Djava.compiler=NONE
java -Djava.compiler=OtherJIT

JAVA Development Kit Software 1.1–1.2 Enhancements -8 of 18


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Serialization Enhancements
• Added optional replace and resolve methods
• Object writeReplace()
• Object readResolve()
• Both methods can have any access modifier
• If provided in class, methods are called automatically
• The objects returned are the "new" objects

JAVA Development Kit Software 1.1–1.2 Enhancements -9 of 18


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Input Method Framework


• What is the input method framework?
• Allows text components to receive different
languages – Chinese, Japanese, and Korean
• Works with a regular keyboard
• Enables text to be entered using character sequences
• Converts sequences automatically
• Is supported only by AWT TextField and TextArea
• Includes direct Swing text component support

JAVA Development Kit Software 1.1–1.2 Enhancements -10 of 18


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Input Method Framework


• What is required?
• Text components that are already enabled
• Two interfaces
java.awt.event.InputMethodListener
java.awt.im.InputMethodRequests

• Specific fonts must be installed

JAVA Development Kit Software 1.1–1.2 Enhancements -11 of 18


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Reference Objects
• What is a reference object?
• Encapsulates a normal Java reference
• Gives user-control over the garbage collector
• Keeps objects eligible for collection "alive"
• Why are they useful?
• References objects with different strengths
• Allows flexible control over memory management

JAVA Development Kit Software 1.1–1.2 Enhancements -12 of 18


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Reference Objects
• Has reachability strengths
• Strongly reachable (normal)
• Softly reachable; might be garbage collected if
memory is low
• Weakly reachable
• Will be collected as garbage

JAVA Development Kit Software 1.1–1.2 Enhancements -13 of 18


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Reference Objects
• Phantomly reachable
• Will be finalized, but not removed
• Performs specific clean-up
• Informs the program
• Must create a reference queue

JAVA Development Kit Software 1.1–1.2 Enhancements -14 of 18


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Reference Objects
• Reference queues
• Data structure where reference objects can be stored
• Programs used by the garbage collector
• Program which places reference objects into the
queue
• Program which can poll queue for reclamation time

JAVA Development Kit Software 1.1–1.2 Enhancements -15 of 18


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Reference Object Example


• No reference queue
public class RefApplet extends Applet{
SoftReference ref;
public void paint(){
if(ref == null){
im = getImage(getCodeBase(),
"duke.gif");
ref = new SoftReference(im);
}else{
im = (Image)ref.get();
}
}
}

JAVA Development Kit Software 1.1–1.2 Enhancements -16 of 18


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services
Reference Object Example
• With reference queue
public class RefApplet2 extends
Applet{
SoftReference ref;
ReferenceQueue queue = new
ReferenceQueue();
public void paint(){
if(ref == null){
im = getImage(getCodeBase(),
"duke.gif");
ref = new SoftReference(im, queue);
}else{
im = (Image)ref.get();
im = null; // strong reference gone!
}
}
}
...
Reference r = (Reference)ref;
if((r = queue.poll()) == null){
ref.clear();
}
...
JAVA Development Kit Software 1.1–1.2 Enhancements -17 of 18
Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Module Summary
• Threading has improved, especially on Solaris™
platforms.
• Memory is better managed.
• Strings are shared between different classes.
• JIT compilers increase performance.
• Serialized objects can be replaced in streams.
• The IMF internationalizes Java technology better.
• Reference objects allow user control of memory.

JAVA Development Kit Software 1.1–1.2 Enhancements -18 of 18


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Module 8

Miscellaneous Enhancements

JAVA Development Kit Software 1.1–1.2 Enhancements November 1998


Sun Educational Services

Objectives
• List JDBC 2.0 additions
• Introduce audio enhancements
• Describe package version control
• Explain JAR improvements
• Introduce the Java IDL classes
• Describe Java Standard extensions
• Describe reflection enhancements

JAVA Development Kit Software 1.1–1.2 Enhancements -2 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Introducing JDBC
• What is JDBC?
• Java Database Connectivity
• Transparent access to various databases
• Implementations provided by vendors – Oracle,
Sybase, and any ODBC database

JAVA Development Kit Software 1.1–1.2 Enhancements -3 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Introducing JDBC
• How does JDBC work?
• Is based on interfaces, providing consistent types
• Maps calls to database by a driver
• Has vendors write the drivers
• Is used by developers to communicate with the
drivers via JDBC

JAVA Development Kit Software 1.1–1.2 Enhancements -4 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Introducing JDBC
• The DriverManager
• Class providing access to drivers
• Drivers which register with DriverManager when
loaded
• Developer’s call to getConnection() to connect
• Statement objects which wrap communication to
DB
• ResultSet objects which hold results of queries

JAVA Development Kit Software 1.1–1.2 Enhancements -5 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JDBC Step-by-Step
1. Load the vendor's driver.
Class.forName("com.zippy.CoolDBDriver");
2. Create a URL string to the driver.
String url = "jdbc:zippy://hostname:1500/CoolDB";
3. Get a connection to the database.
Connection c = DriverManager.getConnection(url);

JAVA Development Kit Software 1.1–1.2 Enhancements -6 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JDBC Step-by-Step
4. Create a statement.
Statement s = c.createStatement();
5. Send SQL command.
String command = "SELECT * FROM Names";
ResultSet rs = s.executeQuery(command);

JAVA Development Kit Software 1.1–1.2 Enhancements -7 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JDBC Step-by-Step
6. Process the results.
while(rs.next()){
System.out.println(rs.getString(1));
System.out.println(rs.getInt(2));
}
7. When done, close connection.
c.close();

JAVA Development Kit Software 1.1–1.2 Enhancements -8 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JDBC 2.0 Enhancements


• Scrollable result sets
• JDBC 1.0 only allowed forward scrolling
• JDBC 2.0 offered backward scrolling
• Dependent on vendor driver
• Added the previous() method

JAVA Development Kit Software 1.1–1.2 Enhancements -9 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JDBC 2.0 Enhancements


• An example
ResultSet rs = s.executeQuery(command);
rs.next();
String one = rs.getString(1);
rs.next();
String two = rs.getString(1);
rs.previous();
String three = rs.getString(1);

JAVA Development Kit Software 1.1–1.2 Enhancements -10 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JDBC 2.0 Enhancements


• Sensitive result sets
• Scroll-insensitive
A static view of the data
• Scroll-sensitive
Changes to data are seen

JAVA Development Kit Software 1.1–1.2 Enhancements -11 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JDBC 2.0 Enhancements


• Fetching rows
• Can now set the number of rows to retrieve at a time
• Heightens performance
ResultSet rs = s.executeQuery(command);
rs.setFetchSize(25);

JAVA Development Kit Software 1.1–1.2 Enhancements -12 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JDBC 2.0 Enhancements


• Result set concurrency
• Read-only
No updates to viewed data allowed

• Updatable

• Updates to data are allowed


• Updates are reflected in database

JAVA Development Kit Software 1.1–1.2 Enhancements -13 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JDBC 2.0 Enhancements


ResultSet rs = s.executeQuery(command);
rs.next();
rs.updateString("James");
rs.updateString("Cagney");
rs.updateRow();

JAVA Development Kit Software 1.1–1.2 Enhancements -14 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JDBC 2.0 Enhancements


• Flexible cursor movement
• A cursor indicates the current row of a result set
• The cursor can be moved flexibly
• There is specific positioning
• There is relative positioning

JAVA Development Kit Software 1.1–1.2 Enhancements -15 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JDBC 2.0 Enhancements


ResultSet rs = s.executeQuery(command);
rs.afterLast();
rs.previous();
rs.beforeFirst();
rs.next();
rs.relative(1);

JAVA Development Kit Software 1.1–1.2 Enhancements -16 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JDBC 2.0 Enhancements


• Batch updates
• Allows sending of commands in a batch
• Turns auto commit off
• Provides basic transactional control
Connection c = DriverManager.getConnection(url);
c.setAutoCommit(false);
Statement s = c.createStatement();
s.addBatch("DELETE FROM Names WHERE LastName='Jones'");
s.addBatch("DELETE FROM Names WHERE LastName='Wolf'");
s.addBatch("INSERT INTO Names VALUES
('Pavel','Chekov')");
s.executeBatch();

JAVA Development Kit Software 1.1–1.2 Enhancements -17 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JDBC 2.0 Enhancements


• Support for SQL 3 types
• Binary large objects (blobs)
• Character large objects (clobs)
• Arrays
Blob blob = rs.getBlob();
Clob clob = rs.getClob();
Array array = rs.getArray()

JAVA Development Kit Software 1.1–1.2 Enhancements -18 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Audio Enhancments
• New formats supported – WAV, AIFF, and MIDI
• New sound engine
• Better sound quality
• Static newAudioClip(URL) method in Applet
• Allows applications to easily use an AudioClip

JAVA Development Kit Software 1.1–1.2 Enhancements -19 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Version Control
• Runtime environment discovery
• Applications can now determine the version of JDK,
JRE, and packages
• Discovery is done via system properties
• New class, Package, was added to support discovery

JAVA Development Kit Software 1.1–1.2 Enhancements -20 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Version Control Information


• Virtual machine information
Retrieved with System.getProperty(String
property)
java.vm.specification.version
java.vm.version
java.vm.specification.vendor
java.vm.vendor
java.vm.specification.name
java.vm.name

JAVA Development Kit Software 1.1–1.2 Enhancements -21 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Version Control Information


• Package information
Retrieved via static methods in the Package class:
getSpecificationVersion()
getImplementationVersion()
getSpecificationVendor()
getImplemntationVendor()
getSpecificationTitle()
getImplemntationTitle()

JAVA Development Kit Software 1.1–1.2 Enhancements -22 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Version Control Information


• Defining version information
• Classes are placed in a JAR file
• The manifest file contains package version
information

JAVA Development Kit Software 1.1–1.2 Enhancements -23 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Jar Enhancements
• JAR tool support for updating archives
jar cvf MyJar.jar First.class

jar uvf MyJar.jar Forgotten.class

• Classes for reading and writing JAR files


java.util.jar.JarInputStream
java.util.jar.JarOutputStream

JAVA Development Kit Software 1.1–1.2 Enhancements -24 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JavaIDL
• CORBA classes part of core JDK software
• org.omg packages
org.omg.CORBA
org.omg.CosNaming
org.omg.portable
• Simple ORB included in JDK software
• Only provides a naming service
• Compliant with CORBA 2.0 portability specs

JAVA Development Kit Software 1.1–1.2 Enhancements -25 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JavaIDL
• The transient name service daemon
• Is tnameserv
• IDL compiler provided as separate download
• Is idltojava
• Uses official OMG Java-to-IDL mapping
• Supports the TIE protocol (delegation)

JAVA Development Kit Software 1.1–1.2 Enhancements -26 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Java Standard Extensions


• What are Java Standard Extensions?
• Optional additions made to the JDK software
• Shipped in packages with a root of javax
• Distributed in JAR files
• Once installed, treated like "core" classes
• Trusted like system classes
• Is not added to classpath

JAVA Development Kit Software 1.1–1.2 Enhancements -27 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Adding Extensions
• Installed extensions
• Extracted to $JAVA_HOME/lib/ext
• Installed native libraries to
Solaris: $JAVA_HOME/lib/<arch>
Windows: $JAVA_HOME/bin
• Searched right after system classes

JAVA Development Kit Software 1.1–1.2 Enhancements -28 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Adding Extensions
• Downloaded extensions
• Referenced via manifest files in another JAR file
• Specified by Class-Path field in manifest
Classpath: someJar.jar stuff/cool.jar
• Have "temporary" extensions
• Have no fixed location; loaded in memory
• Do not become installed extensions

JAVA Development Kit Software 1.1–1.2 Enhancements -29 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Some Extensions
• JNDI
• JavaMail
• Enterprise JavaBeans
• Java Commerce
• JavaBeans Activation Framework
• InfoBus
• Java Help

JAVA Development Kit Software 1.1–1.2 Enhancements -30 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Reflection Enhancements
• What is reflection?
• Allows runtime discovery of loaded class
information
• Class
• Superclass
• Methods
• Fields
• Constructors

JAVA Development Kit Software 1.1–1.2 Enhancements -31 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Reflection Enhancements
• When is it used?
• JavaBeans
• RMI
• Any application needing runtime flexibility

JAVA Development Kit Software 1.1–1.2 Enhancements -32 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Reflection Enhancements
• Security and reflection

• In JDK 1.2 software, member access is controlled in


policies.
• Sometimes, an object will allow normally denied
access.
• There are provisions for developers to set this option.

JAVA Development Kit Software 1.1–1.2 Enhancements -33 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Reflection Enhancements
• AccessibleObject
• Is a new class in JDK 1.2
• Is a superclass of all reflection classes
• Contains three methods to control and determine
access level

JAVA Development Kit Software 1.1–1.2 Enhancements -34 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Reflection Enhancements
• ReflectPermission
• If set, allows setting of the accessible flag
• Gives the right to ystem code automatically
• Allows any method to be called (including private)
• Be careful!

JAVA Development Kit Software 1.1–1.2 Enhancements -35 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Reflection Enhancements
• An access example
class CallMe{
private void method(){
// some senstive operations
}
}
class WatchThis{
...
Class clazz = Class.forName("CallMe");
Method m = clazz.getMethod("method", null);
m.setAccessible(true);
m.invoke(new CallMe(), null); // WORKS!
...
}

JAVA Development Kit Software 1.1–1.2 Enhancements -36 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Module Summary
• JDBC is radically improved for flexibility.
• Result sets are scrollable and updatable.
• Batch processing of SQL commands was added.
• Audio has been expanded and improved.
• Packages can now be version controlled.
• JAR files can be updated.
• JAR files can be read and written within applications.

JAVA Development Kit Software 1.1–1.2 Enhancements -37 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Module Summary
• Java IDL is now part of JDK software.
• Java Standard Extensions provide optional additions to
the JDK software.
• Access can now be controlled during reflection.

JAVA Development Kit Software 1.1–1.2 Enhancements -38 of 38


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Module 9

Topics

JAVA Development Kit Software 1.1–1.2 Enhancements November 1998


Sun Educational Services

Topics
• JavaSpaces
• Jini
• HotSpot VM
• Java Blend

JAVA Development Kit Software 1.1–1.2 Enhancements -2 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Introducing JDBC
• What is JDBC?
• Java Database Connectivity
• Transparent access to various databases
• Implementations provided by vendors – Oracle,
Sybase, and any ODBC database

JAVA Development Kit Software 1.1–1.2 Enhancements -3 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JavaSpaces
• Where will it be used?
• Workflow systems
• Trading services
• Auction-style systems
• Publish and subscribe systems
• Data distribution systems

JAVA Development Kit Software 1.1–1.2 Enhancements -4 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Goals of JavaSpaces
• Only six major methods
• read()
• take()
• write()
• notify()
• readIfExists()
• takeIfExists()
• Most server code written by JavaSpace™ for you

JAVA Development Kit Software 1.1–1.2 Enhancements -5 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Goals of JavaSpaces
• Unified interface
• Sharing, cooperating, and communicating objects all
via one simple interface
• Support for legacy code
• Works with CORBA, EJB, JDBC, and so on
• Utilize Java semantics
• Find objects via type

JAVA Development Kit Software 1.1–1.2 Enhancements -6 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Goals of JavaSpaces
• Uses live objects, not just data
• Behavior and state can be stored
• Asynchronous
• No blocking during writing or reading
• Is handled for you by JavaSpaces
• Extensibility
• Can store data in RDBMS or ODBMS

JAVA Development Kit Software 1.1–1.2 Enhancements -7 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Goals of JavaSpaces
• Transparent implementation
• Providers and requestors need not know details
• 100% Pure Java
• No native code is required

JAVA Development Kit Software 1.1–1.2 Enhancements -8 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JavaSpaces Concepts
• The JavaSpace
• Holds entries
• Is accessible for writing and reading
• Entries
• Have typed objects implementing space.entry

JAVA Development Kit Software 1.1–1.2 Enhancements -9 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JavaSpaces Concepts
• Manipulating entries
• Write creates a copy in the space
• Read returns a copy
• Take removes a copy
• Notify informs users when an entry is available
• Templates
• Use objects that have specific values set
• Involve only matching entries in read, take, or notify

JAVA Development Kit Software 1.1–1.2 Enhancements -10 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JavaSpaces Concepts
• Distributed events
• Basis for notify operations
• Transactions
• JavaSpaces provides atomic control
• Either all operations in a transaction succeed, or none
do

JAVA Development Kit Software 1.1–1.2 Enhancements -11 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Jini
What is Jini?
• Was inspired by Bill Joy
• Enables "spontaneous networking" of anything
• Uses hardware and software in a sort of "network
communion"
• Makes using networks and devices easy
• Has dynamic connections that allow networks to
automatically change

JAVA Development Kit Software 1.1–1.2 Enhancements -12 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

The Jini Vision


The services are
• Software
• Hardware
• Digital cameras
• DVD players
• Anything that passes digital streams
The services compose the system, not one computer

JAVA Development Kit Software 1.1–1.2 Enhancements -13 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

The Jini Vision


• Bill Joy's proposal
• A programming language that runs on any platform
(Java technology)
• A virtual machine for that language (Java VM)
• A network system allowing distributed VMs to work
as one (JINI)
• Open systems
• To gain rapid acceptance, source code will be
released

JAVA Development Kit Software 1.1–1.2 Enhancements -14 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Jini Infrastructure
Discovery
• Defines how a service registers itself with a network
• Requires no prior knowledge of network
• When a device is plugged-in, sends a byte packet to
some port
• Announces its presence on a network which supports
Jini

JAVA Development Kit Software 1.1–1.2 Enhancements -15 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Jini Infrastructure
• Lookup
• Is a "bulletin board" for network services
• Changes dynamically as services are added and
removed
• Listens on some port for new additions to the
systems
• Join
• Occurs after a successful discovery-join transaction
• Enables a device to upload information about itself to
network

JAVA Development Kit Software 1.1–1.2 Enhancements -16 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Distributed Jini
• Leasing
• A device registers for a specific lease time
• Once this time expires, the device must renegotiate
• This allows for distributed garbage collection
• Distributed events
• Events carry an event number allowing for sequencing
• Allows learning if events were recieved out of order
• If events are lost, it can be discovered

JAVA Development Kit Software 1.1–1.2 Enhancements -17 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Distributed Jini
• Transactions
• A transaction manager can be started by objects
• All participating objects register with the manager
• If any part fails, the manager rolls back all objects
• Maintains well-known state
• Network services
• JavaSpaces will play a large role in Jini

JAVA Development Kit Software 1.1–1.2 Enhancements -18 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

HotSpot VM
What is the HotSpot?
• Is the next-generation virtual machine
• Maintains the same rules as current VMs
• Was totally re-engineered with new technologies
• Provides much higher performance than current VMs

JAVA Development Kit Software 1.1–1.2 Enhancements -19 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

HotSpot Benefits
High performance
• References implemented as direct pointers
• Significantly faster thread synchronization
• Reduced code space and start-up time
• Faster method invocations
• Native-code speed
• Method-inlining when possible
• HotSpot "learns" how to make improvements
dynamically

JAVA Development Kit Software 1.1–1.2 Enhancements -20 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

HotSpot Benefits
• Improved garbage collection
• Faster object allocation
• More accurate object reclamation
• Radically decreases collection pauses
• New algorithm which provides less memory
fragmentation
• HotSpot detection
• Optimization focused on performance-critical code

JAVA Development Kit Software 1.1–1.2 Enhancements -21 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JavaBlend
What is JavaBlend?
• Provides mapping between objects and database tables
• Enables automatic mapping, reducing complexity

images/chapter9/jbapps.gif

JAVA Development Kit Software 1.1–1.2 Enhancements -22 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Benefits of Java Blend


• There is automatic access to RDBMS data.
• Only need to know Java programming language
• No SQL or JDBC knowledge needed
• Development time is decreased.

JAVA Development Kit Software 1.1–1.2 Enhancements -23 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Using Java Blend


• GUI tool provided to aid mapping
• Can generate Java classes automatically
• Can generate DB schema from Java classes
• Is completely customizable
CREATE TABLE NAMES{
FIRST CHAR (20) LAST CHAR (30));
public class Names
implements PersistenceCapable{
private String first;
private String last;
...
}
}
JAVA Development Kit Software 1.1–1.2 Enhancements -24 of 27
Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Using Java Blend


• Adding business logic
• Changes made to object are made in database
• All automatic
public class Names implements PersistenceCapable{
private String first;
private String last;
public void changeName(String fst, String lst){
first = fst; // change made to database
last = lst; // change made to database
}
}

JAVA Development Kit Software 1.1–1.2 Enhancements -25 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Using Java Blend


Support for OQL
• Object Query Language
• Queries based on classes, not tables
• Supported precompiled or dynamic queries

JAVA Development Kit Software 1.1–1.2 Enhancements -26 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Module Summary
• JavaSpaces provides dynamic distrubution.
• Jini allows distributed VMs to work as one.
• Jini revolutionizes distributed systems.
• HotSpot solves Java technology performance problem.
• JavaBlend eases mapping objects to databases.

JAVA Development Kit Software 1.1–1.2 Enhancements -27 of 27


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Overview of JDK 1.2 Enhancements ....................................................................................1-1


Objectives ................................................................................................................................................. 1-2
Security Enhancements .......................................................................................................................... 1-3
Java Foundation Classes ........................................................................................................................ 1-4
The Collections API ................................................................................................................................ 1-6
Remote Method Invocation ................................................................................................................... 1-7
JavaBeans Components .......................................................................................................................... 1-8
System Enhancements ............................................................................................................................ 1-9
Miscellaneous Enhancements ............................................................................................................. 1-11
Cutting Edge Java Technology ............................................................................................................ 1-13

Java Security Enhancements .................................................................................................2-1


Objectives ................................................................................................................................................. 2-2
Security Goals .......................................................................................................................................... 2-3
JDK 1.1 Software Security Review ........................................................................................................ 2-5
A Sample Security Manager, ABCSecurityManager.java ..........................2-7
JDK 1.1 Software Security Review ........................................................................................................ 2-8
New Security Enhancements ................................................................................................................. 2-9
Granular Access Control ...................................................................................................................... 2-11
Permissions ............................................................................................................................................ 2-12
Policies .................................................................................................................................................... 2-15
The System Policy File, java.policy ..................................................................................................... 2-18
The User Policy File, .java.policy ........................................................................................................ 2-20
Security Management ........................................................................................................................... 2-21
NewABCSecurityManager.java Example ........................................................................................ 2-25
Digital Signatures and Certificates ..................................................................................................... 2-26

JAVA Development Kit Software 1.1–1.2 Enhancements November 1998


Sun Educational Services

The Flow of Signing Data .................................................................................................................... 2-28


Signing.java ............................................................2-29
Digital Signatures and Certificates ..................................................................................................... 2-30
The Flow of Verifying Data ................................................................................................................. 2-34
Verifying.java ..........................................................2-35
Introduction to the JCE ........................................................................................................................ 2-36
New Security Tools ............................................................................................................................... 2-40
Example of Generating the Key Pairs ................................................................................................ 2-41
Example of Signing and Verifying a JAR File ................................................................................... 2-43
Example of Using the policytool ............................................2-45
Module Summary ................................................................................................................................. 2-46

Java Foundation Classes .......................................................................................................3-1


Objectives ................................................................................................................................................. 3-2
Overview of JFC ...................................................................................................................................... 3-3
Swing GUI Components ........................................................................................................................ 3-4
Basic Swing Components ....................................................................................................................... 3-8
Basic Swing Components ....................................................................................................................... 3-9
ListCellRenderer ........................................................3-25
Basic Swing Components ..................................................................................................................... 3-26
Introduction to MVC ............................................................................................................................ 3-36
Advanced Swing Components ........................................................................................................... 3-42
Pluggable Look and Feel ...................................................................................................................... 3-53
2-D Graphics API Overview ................................................................................................................ 3-55
Drag and Drop Technology ................................................................................................................. 3-63
Accessibility and Assistive
Technologies ....................................................................................................................................... 3-75
AWT Enhancements ............................................................................................................................. 3-82
Module Summary ................................................................................................................................. 3-87

JAVA Development Kit Software 1.1–1.2 Enhancements Table of Contents, 2 of 5


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Collections ...............................................................................................................................4-1
Objectives ................................................................................................................................................. 4-2
Introduction to Collections .................................................................................................................... 4-3
Java Collections API ............................................................................................................................... 4-4
Advantages of Collections ..................................................................................................................... 4-5
Design Goals ............................................................................................................................................ 4-6
Core Collection Interfaces ...................................................................................................................... 4-7
Basic Implementations ......................................................................................................................... 4-14
Iterators ................................................................................................................................................... 4-19
Anonymous Implementations ............................................................................................................ 4-22
Synchronized Wrappers ....................................................................................................................... 4-25
Comparing Collections ......................................................................................................................... 4-27
Compatibility ......................................................................................................................................... 4-30
Module Summary ................................................................................................................................. 4-32

Remote Method Invocation ..................................................................................................5-1


Objectives ................................................................................................................................................. 5-2
Overview of RMI ..................................................................................................................................... 5-3
Building an RMI Solution ...................................................................................................................... 5-6
Remote Object Activation .................................................................................................................... 5-14
New Activatable Classes ...................................................................................................................... 5-17
Building an Activatable System .......................................................................................................... 5-19
Custom Socket Factories ...................................................................................................................... 5-23
RMI and SSL .......................................................................................................................................... 5-28
Module Summary ................................................................................................................................. 5-32

JavaBeans Components .........................................................................................................6-1


Objectives ................................................................................................................................................. 6-2
JavaBeans Defined .................................................................................................................................. 6-3
JavaBeans Characteristics ...................................................................................................................... 6-6

JAVA Development Kit Software 1.1–1.2 Enhancements Table of Contents, 3 of 5


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

Samples of Characteristics ................................................................................................................... 6-11


JavaBeans Activation Framework ...................................................................................................... 6-13
JAF Architecture .................................................................................................................................... 6-15
Building A JAF Solution ....................................................................................................................... 6-20
The InfoBus ............................................................................................................................................ 6-22
Building an InfoBus Solution .............................................................................................................. 6-27
Enterprise JavaBeans ............................................................................................................................ 6-31
EJB Roles ................................................................................................................................................. 6-36
EJB Component Details ........................................................................................................................ 6-40
EJB Container Details ........................................................................................................................... 6-44
EJB Container Details ........................................................................................................................... 6-45
Example EJB Systems ........................................................................................................................... 6-48
EJB Provider Responsibilites ............................................................................................................... 6-50
Module Summary ................................................................................................................................. 6-56

System Enhancements ............................................................................................................7-1


Objectives ................................................................................................................................................. 7-2
Threading Enhancements ...................................................................................................................... 7-3
Memory Management ............................................................................................................................ 7-5
Just-in-Time Compilers .......................................................................................................................... 7-7
Serialization Enhancements ................................................................................................................... 7-9
Input Method Framework ................................................................................................................... 7-10
Reference Objects .................................................................................................................................. 7-12
Reference Object Example ................................................................................................................... 7-16
Module Summary ................................................................................................................................. 7-18

Miscellaneous Enhancements ...............................................................................................8-1


Objectives ................................................................................................................................................. 8-2
Introducing JDBC .................................................................................................................................... 8-3
JDBC Step-by-Step .................................................................................................................................. 8-6

JAVA Development Kit Software 1.1–1.2 Enhancements Table of Contents, 4 of 5


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998
Sun Educational Services

JDBC 2.0 Enhancements ......................................................................................................................... 8-9


Audio Enhancments ............................................................................................................................. 8-19
Version Control ..................................................................................................................................... 8-20
Version Control Information ............................................................................................................... 8-21
Jar Enhancements .................................................................................................................................. 8-24
JavaIDL ................................................................................................................................................... 8-25
Java Standard Extensions ..................................................................................................................... 8-27
Adding Extensions ................................................................................................................................ 8-28
Some Extensions .................................................................................................................................... 8-30
Reflection Enhancements ..................................................................................................................... 8-31
Module Summary ................................................................................................................................. 8-37

Topics .......................................................................................................................................9-1
Topics ........................................................................................................................................................ 9-2
Introducing JDBC .................................................................................................................................... 9-3
JavaSpaces ................................................................................................................................................ 9-4
Goals of JavaSpaces ................................................................................................................................ 9-5
JavaSpaces Concepts ............................................................................................................................... 9-9
Jini ............................................................................................................................................................ 9-12
The Jini Vision ....................................................................................................................................... 9-13
Jini Infrastructure .................................................................................................................................. 9-15
Distributed Jini ...................................................................................................................................... 9-17
HotSpot VM ........................................................................................................................................... 9-19
HotSpot Benefits .................................................................................................................................... 9-20
JavaBlend ................................................................................................................................................ 9-22
Benefits of Java Blend ........................................................................................................................... 9-23
Using Java Blend ................................................................................................................................... 9-24
Module Summary ................................................................................................................................. 9-28

JAVA Development Kit Software 1.1–1.2 Enhancements Table of Contents, 5 of 5


Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services November 1998

You might also like