copyright © 1998 alex chaffee bean soup: using components with java alexander day chaffee...
TRANSCRIPT
Copyright © 1998 Alex Chaffee
Bean Soup: Using Bean Soup: Using Components With JavaComponents With Java
Alexander Day Chaffee
jGuru Training by the Magelang Institutehttp://www.jguru.com/
Copyright © 1998 Alex Chaffee
AbstractAbstract
"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
IntroductionIntroduction
jGuru Training by the Magelang Institute http://www.jguru.com/
Java Training and Consulting Alex Chaffee
Creator of Gamelan Cool Java Dude
Copyright © 1998 Alex Chaffee
OverviewOverview
Component Architectures Client-side components
Java Beans Swing/JFC ActiveX
Copyright © 1998 Alex Chaffee
Overview (cont.)Overview (cont.)
Server-side components Servlets Remote Method Invocation CORBA Objects Enterprise Java Beans
Copyright © 1998 Alex Chaffee
ComponentsComponents
Copyright (c) 1997 Alex Chaffee
What is an object?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?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 notWhat a component is not
an application
Copyright (c) 1997 Alex Chaffee
Some Standard Some Standard Component FeaturesComponent Features
Visual representation Persistent state Event / message passing Distributed (network-aware)
Copyright (c) 1997 Alex Chaffee
Law and OrderLaw 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 ArchitectureThe Architecture
Dictates rules Provides common set of services Example architectures
JavaBeans CORBA OLE / ActiveX OpenDoc EJB
Copyright (c) 1997 Alex Chaffee
Core Architecture ServicesCore Architecture Services
creation registration discovery disposal
Copyright (c) 1997 Alex Chaffee
More Architecture ServicesMore Architecture Services
visual representation visual tool integration persistence event passing and notification networking
Copyright (c) 1997 Alex Chaffee
Advanced servicesAdvanced services
Security Licensing Scripting Introspection (meta) Transactions Naming Universal ID Version Management Install/uninstall Testing
Copyright (c) 1997 Alex Chaffee
Why Components?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 © 1998 Alex Chaffee
Why Components? (cont.)Why Components? (cont.)
Document-centric computing Wrappers for legacy code
Copyright © 1998 Alex Chaffee
Java BeansJava Beans
Copyright (c) 1997 Alex Chaffee
Why Java?Why Java?
Write once, run anywhere Well-designed event model and APIs Full-featured language
Copyright (c) 1997 Alex Chaffee
GoalsGoals
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?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 BeanExample Beanclass 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. BeansClasses 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 ConventionsNaming 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
IntrospectionIntrospection
metabean 1) low-level reflection (JDK1.1) 2) analyze wrt design patterns or, use BeanInfo
Copyright (c) 1997 Alex Chaffee
PropertiesProperties
Exposed data values Named attributes Accessor / mutator methods Design pattern:
public void setColor(Color c) public Color getColor()
Copyright (c) 1997 Alex Chaffee
MethodsMethods
Exposed public methods Design pattern:
public void scratchAndSniff()
Copyright (c) 1997 Alex Chaffee
EventsEvents
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-timeDesign-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
CustomizationCustomization
default UI for editing properties a bean can provide its own UI
per property per entire bean
property sheet a window that allows a user to edit beans
Copyright (c) 1997 Alex Chaffee
PersistencePersistence
all beans must support either Serializable or Externalizable
Copyright (c) 1997 Alex Chaffee
Beans and Java 1.1Beans 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. BeansApplets 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. ActiveXBeans 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 JavaBeansLimitations of JavaBeans
Java-centric (pro and con)
Copyright (c) 1997 Alex Chaffee
Related TechnologiesRelated Technologies
ActiveX/OLE CORBA JavaIDL RMI JDBC
Copyright (c) 1997 Alex Chaffee
Bean Technology: Glasgow Bean Technology: Glasgow (JDK 1.2)(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 © 1998 Alex Chaffee
Glasgow (cont.)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 (c) 1997 Alex Chaffee
Bean Technology: InfoBusBean 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
DemoDemo
Copyright (c) 1997 Alex Chaffee
Some cool beansSome cool beans
HotJava Bean HTML 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)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 © 1998 Alex Chaffee
JFC and SwingJFC and Swing
JFC = Swing + Java2D + Drag-and-drop + 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 WidgetsBasic Widgets
JButton JLabel JPanel JMenu JFrame (window)
Copyright © 1998 Alex Chaffee
JTree and JTableJTree and JTable
JTable spreadsheet-like
JTree hierarchical display
Copyright © 1998 Alex Chaffee
JEditorPaneJEditorPane
Full HTML renderer component
Copyright © 1998 Alex Chaffee
ActiveX and COM ActiveX and COM IntegrationIntegration
Copyright © 1998 Alex Chaffee
DefinitionsDefinitions
COM ActiveX DCOM
Copyright © 1998 Alex Chaffee
COM InterfacesCOM Interfaces
Base: IUnknown
Copyright © 1998 Alex Chaffee
The RegistryThe 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 COM Integration with the Microsoft VMMicrosoft 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 COM Integration with the Sun VMSun VM
Need to use “ActiveX Bridge” Straightforward but not as seamless
Copyright © 1998 Alex Chaffee
Server-side componentsServer-side components
Servlets CORBA EJB
Copyright © 1998 Alex Chaffee
ServletsServlets
Copyright © 1998 Alex Chaffee
What Is A ServletWhat 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/ServletServer/Service/Servlet
server - a process running on a host machine Apache, Java Web Server
service - a protocol running on a port HTTP, FTP
servlet - a module running inside a service PhoneServlet
Copyright © 1998 Alex Chaffee
Servlets vs. AppletsServlets 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 server-side VM
Much easier to test
Copyright © 1998 Alex Chaffee
Servlets vs. CGIServlets 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.)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 all methods GET, POST, PUT, DELETE, et al.
Copyright © 1998 Alex Chaffee
Servlets vs. FastCGIServlets 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 if the underlying JVM does
Copyright © 1998 Alex Chaffee
Supported ServersSupported 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 SecurityServlet 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: Servlet Security: ImplicationsImplications
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 for multiple servers
one place to install updates
Copyright © 1998 Alex Chaffee
Servlet Security: ProblemsServlet Security: Problems
Too simplistic All or nothing
Should allow ACLs for particular signers They claim it will in a future version
Should get better with 1.2 security model Finer-grained access control
Copyright © 1998 Alex Chaffee
Servlet Client SecurityServlet 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 AvailabilityAPI 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 Servlet Architectures:Three-tier systemsystem
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 Servlet Architectures: N-tier systemsystem
Tier 1: HTML Browser Tier 2: Servlet
User interface Tier 3: EJB/CORBA/RMI Objects
Business logic Tier 4: Other Servers (e.g. RDBMS)
Data storage
Copyright © 1998 Alex Chaffee
Servlet Architectures: Web Servlet Architectures: Web PublishingPublishing
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 balancing, etc.
Copyright © 1998 Alex Chaffee
Server-side Includes (SSI)Server-side Includes (SSI)
Must be in a file named .shtml or .jsp can change this with Admin Tool
Normal SSI <!--#include file="foo.txt" -->
Servlet SSI <servlet code=DateServlet.class> </servlet>
Copyright © 1998 Alex Chaffee
SSI DetailsSSI 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 invocationURL invocation Directly from browser as URL
http://www.myserver.com/servlet/MyServlet From inside FORM tag as script
<FORM METHOD=POST ACTION=”/servlet/MyServlet”>
...</FORM>
From inside JHTML or JSP page Uses “Page Compilation” Compiles the jsp file into a servlet on the fly,
then executes it
Copyright © 1998 Alex Chaffee
Page Compilation (JSP)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
HelloHttpServletHelloHttpServlet
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: WhySaving State: Why
Shopping Cart User Preferences “Wizard” interfaces
i.e., successive linked dialog boxes / form entry pages
Copyright © 1998 Alex Chaffee
Saving State: HowSaving 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 Remote Method Invocation (RMI)(RMI)
Copyright © 1997 Alex Chaffee
What Is RMI?What Is RMI?
Access to Remote Objects Java-to-Java only Client-Server Protocol High-level API Transparent Lightweight
Copyright © 1997 Alex Chaffee
TCP
Remote Objects (Diagram)Remote Objects (Diagram)Java Virtual Machine
Client Object
Java Virtual Machine
Remote Object
Copyright © 1997 Alex Chaffee
RMI LayersRMI Layers
TCPRemote Reference Layer
Transport Layer
Java Virtual Machine
Client Object
Remote Reference Layer
Transport Layer
Java Virtual Machine
Stub
Remote Object
Skeleton
Copyright © 1997 Alex Chaffee
Stubs and SkeletonsStubs 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 Remote Interfaces and StubsStubs
Remote Interface
StubRemote Object
(Server)Client Skeleton
implements implements
Copyright © 1997 Alex Chaffee
Creating Remote ObjectsCreating 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 © 1998 Alex Chaffee
CORBA ObjectsCORBA Objects
Copyright © 1998 Alex Chaffee
What is CORBA?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?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?What is the OMG?
Designers of CORBA Consortium of 700+ companies
Not including Microsoft Members:
platform vendors database vendors software tool developers corporate developers software application vendors
Copyright © 1998 Alex Chaffee
Basic CORBA ArchitectureBasic CORBA Architecture
Client Server
ORB ORB
request response
“Object Bus”
Copyright © 1998 Alex Chaffee
CORBA ObjectsCORBA Objects
Examples Service Client Component Business object
CORBA objects approach universal accessibility Any Language Any Host on network Any Platform
Copyright © 1998 Alex Chaffee
ORBORB
Object Request Broker “Object Bus”
Handles 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 ResponsibilitiesORB 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 ORBsNetwork of ORBs
There’s an ORB on the server too ORB receives request
Copyright © 1998 Alex Chaffee
IIOPIIOP
Internet Inter-Orb Protocol Network or “wire” protocol Works across TCP/IP (the Internet
protocol)
Copyright © 1997 Alex Chaffee
Stubs and SkeletonsStubs 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.)Stubs and Skeletons (Fig.)
IIOPORB
Client Host Machine
Client Object
ORB
Server Host Machine
Stub
Remote Object
Skeleton
Copyright © 1998 Alex Chaffee
Client vs. ServerClient 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 ORBThe 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 IIOPRMI over IIOP
In Development Uses IIOP as transport protocol Uses Value Objects to pass Java
serialized objects
Copyright © 1998 Alex Chaffee
CORBA via RMICORBA 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 BeansEnterprise Java Beans
Copyright © 1998 Alex Chaffee
What is an EJBWhat is an EJB
NOT a JavaBean Server-side business component Transactional Distributed Secure Platform-independent Vendor -independent
Copyright © 1998 Alex Chaffee
The EJB SpecThe EJB Spec
Defines the role of an EJB server Levels the playing field
Removes competitive “lock-in” advantage Created 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 TransactionsDistributed Transactions
One transaction Many operations Many servers Many databases Distributed two-phase commit
Copyright © 1998 Alex Chaffee
Two Types of Enterprise Two Types of Enterprise BeansBeans
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 ContainersEJB Containers
Implement services used by EJBs Specialized containers
Extend EJB spec 110% Pure
Copyright © 1998 Alex Chaffee
CORBA and EJBCORBA 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 EJB Servers contain an ORB All EJB Objects are also CORBA objects
Copyright © 1998 Alex Chaffee
CORBA and EJB (Cont.)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 TransactionsJava 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 class UserTransaction
Copyright © 1998 Alex Chaffee
Object Transaction Service Object Transaction Service (OTS)(OTS)
Distributed transaction specification Part of CORBA, but also standalone Transactions can span
multiple queries multiple objects multiple servers multiple databases multiple vendors’ products
Copyright © 1998 Alex Chaffee
OTS FeaturesOTS Features
Distributed transactions Flat or nested transactions
Nested not supported in Java
Two-phase commit Vendor interoperability (?)
Copyright © 1998 Alex Chaffee
EJB + ServletsEJB + 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
ConclusionConclusion
Copyright © 1998 Alex Chaffee
CreditsCredits
Thanks to Christof Sieberath (@swisscom.com) for may/must distinction
Copyright © 1998 Alex Chaffee
Where to Get More Where to Get More Information: 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