Download Bean Soup: Using Components With Java

Document related concepts
no text concepts found
Transcript
Bean Soup: Using
Components With Java
Alexander Day Chaffee
[email protected]
jGuru Training by the Magelang
Institute
Copyright © 1998 Alex Chaffee
http://www.jguru.com/
Abstract

"Component-based software design" is the hottest
buzzword since "object-oriented programming." But
what does it mean? What's a component? What's a
JavaBean? An Enterprise JavaBean? What is COM
and ActiveX? Can I use COM objects from Java?
Can I use Java in an ASP? What about CORBA? Do
components really help you reuse code and increase
your team's productivity, or is it just another
meaningless technology? In this section we pose
these questions and more, and actually answer some
of them.
Copyright © 1998 Alex Chaffee
Introduction
 jGuru
Training by the Magelang Institute
 http://www.jguru.com/
 Java
 Alex
Training and Consulting
Chaffee
 Creator
of Gamelan
 Cool Java Dude
Copyright © 1998 Alex Chaffee
Overview
 Component Architectures
 Client-side
components
 Java
Beans
 Swing/JFC
 ActiveX
Copyright © 1998 Alex Chaffee
Overview (cont.)
 Server-side
components
 Servlets
 Remote
Method Invocation
 CORBA Objects
 Enterprise Java Beans
Copyright © 1998 Alex Chaffee
Components
Copyright © 1998 Alex Chaffee
What is an object?
 Encapsulation
 State
+ behavior
 Data + methods
 Polymorphism
 Inheritance
 Programming
construct
 Buried inside a program
 An object may be reusable
Copyright (c) 1997 Alex Chaffee
What is a component?
 Software
“part”
 Self-contained, independent
 Plug-and-play, drag-and-drop
 Interact and interoperate with other
components
 Assemble components to create an
application
 Ready for use by visual programming tools
 Marketable
 A component must be reusable
Copyright (c) 1997 Alex Chaffee
What a component is not
 an
application
Copyright (c) 1997 Alex Chaffee
Some Standard
Component Features
 Visual
representation
 Persistent state
 Event / message passing
 Distributed (network-aware)
Copyright (c) 1997 Alex Chaffee
Law and Order
 Components
must operate according to
a set of rules
 APIs (interfaces)
 Naming conventions
 Message-passing model
 method
invocation
 event model
Copyright (c) 1997 Alex Chaffee
The Architecture
 Dictates
rules
 Provides common set of services
 Example architectures
 JavaBeans
 CORBA
 OLE
/ ActiveX
 OpenDoc
 EJB
Copyright (c) 1997 Alex Chaffee
Core Architecture Services
 creation
 registration
 discovery
 disposal
Copyright (c) 1997 Alex Chaffee
More Architecture Services
 visual
representation
 visual tool integration
 persistence
 event passing and notification
 networking
Copyright (c) 1997 Alex Chaffee
Advanced services










Security
Licensing
Scripting
Introspection (meta)
Transactions
Naming
Universal ID
Version Management
Install/uninstall
Testing
Copyright (c) 1997 Alex Chaffee
Why Components?
 Quick
and easy GUI design
 Solve “bloatware”
 Scriptable
 Lower barrier to entry for small software
developers
 Easier to write, debug, mantain large
applications
 Assemble targeted applications
 e.g.

Word for Hospitals
(cont.)
Copyright (c) 1997 Alex Chaffee
Why Components? (cont.)
Document-centric computing
 Wrappers for legacy code

Copyright © 1998 Alex Chaffee
Java Beans
Copyright © 1998 Alex Chaffee
Why Java?
 Write
once, run anywhere
 Well-designed event model and APIs
 Full-featured language
Copyright (c) 1997 Alex Chaffee
Goals
 100%
Pure Java
 Keep it simple
 Not necessarily visual
 Leverages Java platform
 Usable inside builder tools
Copyright (c) 1997 Alex Chaffee
What is a bean?
 “A Java
Bean is a reusable software
component that can be manipulated
visually in a builder tool.”
 A Java class
 Any Java class
 that
follows certain naming conventions
Copyright (c) 1997 Alex Chaffee
Example Bean
class CoffeeBean implements Serializable
{
private double price;
public CoffeeBean() { this(100);}
public void setPrice(double price) {
this.price = price;
}
public double getPrice() {
return price;
}
}

Note: no explicit “is a bean” syntax
Copyright (c) 1997 Alex Chaffee
Classes vs. Beans
 If
any class can be a bean, what makes
a class a bean?
 Properties
 Events
 Persistence
 Customization
 Naming
conventions
Copyright (c) 1997 Alex Chaffee
Naming Conventions
 used
to mark design patterns
 very important for JavaBeans
 simply by naming methods correctly,
your class becomes a bean
 Not required, but strongly
recommended
 can
use BeanInfo class instead
Copyright (c) 1997 Alex Chaffee
Introspection
 metabean
 1)
low-level reflection (JDK1.1)
 2) analyze wrt design patterns
 or, use BeanInfo
Copyright (c) 1997 Alex Chaffee
Properties
 Exposed
data values
 Named attributes
 Accessor / mutator methods
 Design pattern:
 public
void setColor(Color c)
 public Color getColor()
Copyright (c) 1997 Alex Chaffee
Methods
 Exposed
public methods
 Design pattern:
 public
void scratchAndSniff()
Copyright (c) 1997 Alex Chaffee
Events
 Methods
called when something
interesting happens
 Event source / event listener design
pattern
 Design pattern:
 public
void addSniffListener(SniffListener
listener)
 public void
removeSniffListener(SniffListener listener)
Copyright (c) 1997 Alex Chaffee
Design-time vs. Run-time
 Beans
can run in two environments
 inside
a BeanBox
 inside an application
 Different
requirements
 Different overhead
Copyright (c) 1997 Alex Chaffee
Customization
 default
UI for editing properties
 a bean can provide its own UI
 per
property
 per entire bean
 property
a
sheet
window that allows a user to edit beans
Copyright (c) 1997 Alex Chaffee
Persistence
 all
beans must support either
Serializable or Externalizable
Copyright (c) 1997 Alex Chaffee
Beans and Java 1.1
 Beans
rely on many features added to
JDK 1.1
 Realistically, you need a 1.1 VM
 JDK
1.1, JRE 1.1 (Sun)
 Microsoft IE4.0
 Netscape Navigator 4.03 + patch
Copyright (c) 1997 Alex Chaffee
Applets vs. Beans
 Applets
live in browsers, beans live in
applications
 Beans need much more functionality
 Beans are more strict in their naming
conventions
 You can make a class that is both an
applet and a bean
Copyright (c) 1997 Alex Chaffee
Beans vs. ActiveX
 Beans
have OO advantages of Java
 ActiveX controls have performance
advantages and access to Win32 API
 Interoperable
 Sun’s
Bean-ActiveX Bridge
 MS’s JVM transparently integrates the two
Copyright (c) 1997 Alex Chaffee
Limitations of JavaBeans
 Java-centric
(pro and con)
Copyright (c) 1997 Alex Chaffee
Related Technologies
 ActiveX/OLE
 CORBA
 JavaIDL
 RMI
 JDBC
Copyright (c) 1997 Alex Chaffee
Bean Technology: Glasgow
(JDK 1.2)

Bean Contexts (aka “Runtime Containment
and Services”)
 This
provides a containment hierarchy for
JavaBeans and provides ways for beans to find
context-specific information such as design-mode
versus run-mode.

Native Drag-and-Drop support
Copyright (c) 1997 Alex Chaffee
Glasgow (cont.)

JavaBeans Activation Framework (Standard
Extension)
 Using
beans to view different media types
 Identify media type (e.g. MPEG)
 Locate viewer for that type
Copyright © 1998 Alex Chaffee
Bean Technology: InfoBus
 enables
dynamic exchange of data
between JavaBeans components
 protocols based on a notion of an
information bus
 based on Lotus’ InfoBus technology
 only works inside a single VM
Copyright (c) 1997 Alex Chaffee
Demo
Copyright (c) 1997 Alex Chaffee
Some cool beans
 HotJava
 HTML
Bean
Renderer
 DoqView
 Word/Excel/Powerpoint
viewers
 Alphaworks
 image
filters, POP3/SMTP/FTP protocols,
spinners, progress bars, XML parsers
Copyright (c) 1997 Alex Chaffee
Bean Builders (IDEs)
 BeanBox
(Sun)
 Visual Café (Symantec)
 VisualAge for Java (IBM)
 JBuilder (Borland)
 Cosmo Code (SGI)
 Super Mojo (Penumbra)* (R.I.P.)
* written in 100% Pure Java
Copyright (c) 1997 Alex Chaffee
JFC and Swing
 JFC
= Swing + Java2D + Drag-anddrop + a few other things
 Swing = New GUI widget set
 All Swing widgets are JavaBeans
 Standard (core) with JDK 1.2 release
Copyright © 1998 Alex Chaffee
Basic Widgets
 JButton
 JLabel
 JPanel
 JMenu
 JFrame
(window)
Copyright © 1998 Alex Chaffee
JTree and JTable
 JTable
 spreadsheet-like
 JTree
 hierarchical
display
Copyright © 1998 Alex Chaffee
JEditorPane
 Full
HTML renderer component
Copyright © 1998 Alex Chaffee
ActiveX and COM
Integration
Copyright © 1998 Alex Chaffee
Definitions
 COM
 ActiveX
 DCOM
Copyright © 1998 Alex Chaffee
COM Interfaces
 Base:
IUnknown
Copyright © 1998 Alex Chaffee
The Registry
 Mongo
database
 Register all COM objects
 Can also register Java objects as COM
objects
 JavaReg
/register /class:com.foo.Tommy
/progid:Tommy
Copyright © 1998 Alex Chaffee
COM Integration with the
Microsoft VM
 Transparent
integration
 COM objects are accessible from Java
as Java objects
 Note:
huge security hole
 Java
objects are accessible from COM
as COM objects
 Must
register using JavaReg
Copyright © 1998 Alex Chaffee
COM Integration with the
Sun VM
 Need
to use “ActiveX Bridge”
 Straightforward but not as seamless
Copyright © 1998 Alex Chaffee
Server-side components
 Servlets
 CORBA
 EJB
Copyright © 1998 Alex Chaffee
Servlets
Copyright © 1998 Alex Chaffee
What Is A Servlet
 A Java
object
 Plug-in for a web server
 Replacement for CGI scripts
 Can
also be used to extend server as a
plug-in
 Full
power of Java
 Platform-independent
 Database
access
 Fun to write
Copyright © 1998 Alex Chaffee
Server/Service/Servlet
 server
- a process running on a host
machine
 Apache,
 service
- a protocol running on a port
 HTTP,
 servlet
Java Web Server
FTP
- a module running inside a
service
 PhoneServlet
Copyright © 1998 Alex Chaffee
Servlets vs. Applets
 Servlets
have no GUI
 Server-side, not client-side
 Different security model
 Installed, not downloaded
 But
you can download remote servlets too
 Consistent
 Much
server-side VM
easier to test
Copyright © 1998 Alex Chaffee
Servlets vs. CGI
 "performance,
flexibility, portability, and
security" (whitepaper)
 Faster and Leaner
 No
fork-process like Perl
 No need to initialize for each request
 Only lightweight thread context switching
 Built-in multithreading
Copyright © 1998 Alex Chaffee
Servlets vs. CGI (Cont.)
 Easy
to manage state
 share
data across successive requests
 share data between concurrent requests
 use hidden fields, cookies, or sessions
 Write
once, run anywhere
 It's
easy to write unportable Perl
 Servlets have standard API
 Supports
 GET,
all methods
POST, PUT, DELETE, et al.
Copyright © 1998 Alex Chaffee
Servlets vs. FastCGI

FastCGI sends multiple requests to a single
separate process
 requires
process context switch
 Servlets
send multiple requests to multiple
threads in same process
 requires
lightweight thread context switch
(Also applies to ISAPI)
 Nice diagram in White Paper
 Servlets also automatically take advantage of
multiprocessors
Copyright © 1998 Alex Chaffee

Supported Servers
 Java
Web Server
 Apache
 Netscape
 Many others (see web site)
 Servlet Engines
 IBM's
ServletExpress
 Live Software’s JRun
Copyright © 1998 Alex Chaffee
Servlet Security
 Trusted
Servlets (full access)
 JWS
Internal
 Local (in the "servlets" directory)
 Servlet
Sandbox
 Signed
Network Servlets (full access)
 Unsigned Network Servlets (limited access)
Copyright © 1998 Alex Chaffee
Servlet Security:
Implications
 IT
managers can sign servlets for use in
their organization
 ISPs can allow users to run servlets
 less
of a security hole than CGI scripts,
since Java is safe and secure (at least more
so than C or Perl)
 still allows denial-of-service attacks
 Network
servlets are possible
 chaining / proxying
 allows agents
 common servlet repository
servers
for multiple
Copyright © 1998 Alex Chaffee
Servlet Security: Problems
 Too
simplistic
 All
or nothing
 Should
 They
 Should
allow ACLs for particular signers
claim it will in a future version
get better with 1.2 security model
 Finer-grained
access control
Copyright © 1998 Alex Chaffee
Servlet Client Security
 Java
Web Server
 Allows
Access Control Lists for clients
 Supports HTTP authentication
 Supports Digest Authentication
 Other
Web Servers
 Usually
support HTTP authentication
 May have other security features
Copyright © 1998 Alex Chaffee
API Availability
 Standard
Java Extension API
 From
white paper: "This means that while it
is not part of the core Java framework which
must always be part of all products bearing
the Java brand, it will be made available
with such products by their vendors as an
add-on package."
 package
javax.servlet.*,
javax.servlet.http.*
Copyright © 1998 Alex Chaffee
Servlet Architectures:Three-tier
system
 Tier
1: Client
 HTML
browser
 Java client
 Tier
2: Servlets
 embody
business logic
 secure, robust
 Tier
3: Data Sources
 Java
can talk to SQL, CORBA, OODB, File
system, etc. etc.
Copyright © 1998 Alex Chaffee
Servlet Architectures: N-tier
system
 Tier
1: HTML Browser
 Tier 2: Servlet
 User
 Tier
interface
3: EJB/CORBA/RMI Objects
 Business
 Tier
logic
4: Other Servers (e.g. RDBMS)
 Data
storage
Copyright © 1998 Alex Chaffee
Servlet Architectures: Web
Publishing
 SSI
Servlets
 JSP Servlets
 Best
to keep business logic inside Java
objects
 Keep the JSP light so designers don’t get
scared
 Chaining
servlets
 Multiple servers
 data
gathering, collecting, serving, load
Copyright © 1998 Alex Chaffee
balancing, etc.
Server-side Includes (SSI)
 Must
 can
be in a file named .shtml or .jsp
change this with Admin Tool
 Normal
SSI
 <!--#include
 Servlet
file="foo.txt" -->
SSI
 <servlet
code=DateServlet.class>
 </servlet>
Copyright © 1998 Alex Chaffee
SSI Details
 pass
init parameters in servlet tag
 pass servlet parameters in param tags
 can specify codebase in servlet tag
 e.g.
<servlet code=DateServlet.class
codebase=http://servlets.foo.com/
initParam1=val1 initParam2=val2>
<param name=serviceParam1 value=val3>
<param name=serviceParam2 value=val4>
</servlet>
Copyright © 1998 Alex Chaffee
URL invocation
 Directly
from browser as URL
 http://www.myserver.com/servlet/MyServlet
 From
inside FORM tag as script
 From
inside JHTML or JSP page
<FORM METHOD=POST
ACTION=”/servlet/MyServlet”>
...
</FORM>
 Uses
“Page Compilation”
 Compiles the jsp file into a servlet on the fly,
then executes it
Copyright © 1998 Alex Chaffee
Page Compilation (JSP)
 Embed
Java code in static HTML pages
then compile those pages into individual
Java servlets to create a dynamic web
site
 Based on JHTML technology from Art
Technology Group (http://www.atg.com/)
 Product:
Dynamo, a Java Web Application
Server
Copyright © 1998 Alex Chaffee
HelloHttpServlet
public class HelloHttpServlet extends HttpServlet
{
public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws IOException,
ServletException
{
String name = req.getParameter("name");
if (name == null) name = "Joe";
res.setContentType("text/plain");
ServletOutputStream out = res.getOutputStream();
out.println("Hello, " + name + "!");
}
}
Copyright © 1998 Alex Chaffee
Saving State: Why
 Shopping
Cart
 User Preferences
 “Wizard” interfaces
 i.e.,
successive linked dialog boxes / form
entry pages
Copyright © 1998 Alex Chaffee
Saving State: How
 Client-side
storage
 Hidden
fields
 URL Rewriting
 Cookies
 Server-side
storage
 Instance
variables
 Database Access
 JWS
Session Management
 Best
possible solution (but still flawed)
Copyright © 1998 Alex Chaffee
Remote Method Invocation
(RMI)
Copyright © 1998 Alex Chaffee
What Is RMI?
 Access
to Remote Objects
 Java-to-Java only
 Client-Server Protocol
 High-level API
 Transparent
 Lightweight
Copyright © 1997 Alex Chaffee
Remote Objects (Diagram)
Java Virtual Machine
Java Virtual Machine
Client
Object
Remote
Object
TCP
Copyright © 1997 Alex Chaffee
RMI Layers
Java Virtual Machine
Java Virtual Machine
Client
Object
Remote
Object
Stub
Skeleton
Remote Reference Layer
Remote Reference Layer
Transport Layer
TCP
Transport Layer
Copyright © 1997 Alex Chaffee
Stubs and Skeletons
 Stub
 lives
on client
 pretends to be remote object
 Skeleton
 lives
on server
 receives requests from stub
 talks to true remote object
 delivers response to stub
Copyright © 1997 Alex Chaffee
Remote Interfaces and
Stubs
Remote Interface
implements
Client
Stub
implements
Skeleton
Remote Object
(Server)
Copyright © 1997 Alex Chaffee
Creating Remote Objects
 Define
a Remote Interface
 extends
java.rmi.Remote
 Define
a class that implements the
Remote Interface
 extends
java.rmi.RemoteObject
 or java.rmi.UnicastRemoteObject
Copyright © 1997 Alex Chaffee
CORBA Objects
Copyright © 1998 Alex Chaffee
What is CORBA?
 Common
Object Request Broker
Architecture
 Communication infrastructure for
distributed objects
 Allows a heterogeneous, distributed
collection of objects to collaborate
transparently
Copyright © 1998 Alex Chaffee
What is CORBA good for?
 Developing
distributed applications
 Locating remote objects on a network
 Sending messages to those objects
 Common interface for transactions,
security, etc.
 CORBA
Services (more later)
Copyright © 1998 Alex Chaffee
What is the OMG?
 Designers
of CORBA
 Consortium of 700+ companies
 Not
including Microsoft
 Members:
 platform
vendors
 database
 software
tool developers
 corporate
 software
vendors
developers
application vendors
Copyright © 1998 Alex Chaffee
Basic CORBA Architecture
Server
Client
response
request
ORB
ORB
“Object Bus”
Copyright © 1998 Alex Chaffee
CORBA Objects

Examples
 Service
 Client
 Component
 Business

object
CORBA objects approach universal
accessibility
 Any
Language
 Any Host on network
 Any Platform
Copyright © 1998 Alex Chaffee
ORB
 Object
Request Broker
 “Object
 Handles
Bus”
all communication among
objects
 Each host (machine) has its own ORB
 ORBs know how to talk to each other
 ORB also provides basic services to
client
Copyright © 1998 Alex Chaffee
ORB Responsibilities
 Find
the object implementation for the
request
 Prepare the object implementation to
receive the request
 Communicate the data making up the
request
 Retrieve results of request
Copyright © 1998 Alex Chaffee
Network of ORBs
 There’s
an ORB on the server too
 ORB receives request
Copyright © 1998 Alex Chaffee
IIOP
 Internet
Inter-Orb Protocol
 Network or “wire” protocol
 Works across TCP/IP (the Internet
protocol)
Copyright © 1998 Alex Chaffee
Stubs and Skeletons
 Stub
 lives
on client
 pretends to be remote object
 Skeleton
 lives
on server
 receives requests from stub
 talks to true remote object
 delivers response to stub
Copyright © 1997 Alex Chaffee
Stubs and Skeletons (Fig.)
Client Host Machine
Server Host Machine
Client
Object
Remote
Object
Stub
Skeleton
ORB
IIOP
ORB
Copyright © 1997 Alex Chaffee
Client vs. Server
 in
CORBA, a client is a client relative to
a particular object
 i.e. an object with a reference to a
“server” object
 A client may also act as a server
 If
it has an IDL and stubs and skeletons
 Technically,
a CORBA server contains
one or more CORBA objects
Copyright © 1998 Alex Chaffee
The Java ORB
 100%
Java
 Generic
 Allows Java IDL applications to run
either as stand-alone Java applications,
or as applets within Java-enabled
browsers
 Uses IIOP
Copyright © 1998 Alex Chaffee
RMI over IIOP
 In
Development
 Uses IIOP as transport protocol
 Uses Value Objects to pass Java
serialized objects
Copyright © 1998 Alex Chaffee
CORBA via RMI
 Possible
using middleware server
 RMI from client to middleware
 IIOP from middleware to other servers
 Can use Servlet as middleware
Copyright © 1998 Alex Chaffee
Enterprise Java Beans
Copyright © 1998 Alex Chaffee
What is an EJB
 NOT
a JavaBean
 Server-side business component
 Transactional
 Distributed
 Secure
 Platform-independent
 Vendor -independent
Copyright © 1998 Alex Chaffee
The EJB Spec
 Defines
the role of an EJB server
 Levels the playing field
 Removes
 Created
competitive “lock-in” advantage
by many competitors
 Testament
to power of having a common
enemy
 Allows
EJBs to run in many different
vendors’ products
Copyright © 1998 Alex Chaffee
Distributed Transactions
 One
transaction
 Many operations
 Many servers
 Many databases
 Distributed two-phase commit
Copyright © 1998 Alex Chaffee
Two Types of Enterprise
Beans
 Session
Bean
 Represents
a particular client during a
single session
 Entity
Bean
 Represents
a data object
 E.g. a row in a table
 Often,
a session bean will invoke one or
more entity beans
Copyright © 1998 Alex Chaffee
EJB Containers
 Implement
services used by EJBs
 Specialized containers
 Extend
EJB spec
 110% Pure
Copyright © 1998 Alex Chaffee
CORBA and EJB
 Transport
 EJB
uses RMI interface, RMI uses IIOP
 CORBA 3.0
promises object
compatibility with EJB
 Not
quite sure what that means
 Some
 All
EJB Servers contain an ORB
EJB Objects are also CORBA objects
Copyright © 1998 Alex Chaffee
CORBA and EJB (Cont.)
 All
EJB Servers use CORBA
Transactions (via JTS)
 That
means that any client can make a
distributed transaction that includes both
CORBA and EJB Objects
 Not
an either-or decision
 You
can have both EJB and CORBA
working together in a single system
Copyright © 1998 Alex Chaffee
Java Transactions
 Java
Transaction Service (JTS)
 A standard
Java mapping of the OMG
Object Transaction Service (OTS)
 packages
org.omg.CosTransaction and
org.omg.CosTSPortability
 Java
Transaction API (JTA)
 High-level
transaction management
specification
 package
javax.transaction
Copyright © 1998 Alex Chaffee
Object Transaction Service
(OTS)
 Distributed
 Part
transaction specification
of CORBA, but also standalone
 Transactions
can span
 multiple
queries
 multiple
objects
 multiple
servers
 multiple
databases
 multiple
vendors’ products
Copyright © 1998 Alex Chaffee
OTS Features
 Distributed
 Flat
transactions
or nested transactions
 Nested
not supported in Java
 Two-phase
 Vendor
commit
interoperability (?)
Copyright © 1998 Alex Chaffee
EJB + Servlets
 A great
architecture
 Wider platform for clients
 HTML
runs on most clients, even without
Java
 Secure
 No
need to put password on client
 Fast
 Data
access happens inside your LAN
Copyright © 1998 Alex Chaffee
Conclusion
Copyright © 1998 Alex Chaffee
Credits
 Thanks
to Christof Sieberath
(@swisscom.com) for may/must
distinction
Copyright © 1998 Alex Chaffee
Where to Get More
Information:

Web sites
 http://www.Developer.com/
(Gamelan)
 http://www.JavaWorld.com/ (magazine)
 http://www.Purpletech.com/ (author’s site)
 http://www.alphaworks.ibm.com/alphaBeans
Copyright © 1998 Alex Chaffee