Transcript 072908

Self-executing Java, J2EE
James Atlas
July 29, 2008
Review
• Software Testing
 Design process
July 29, 2008
James Atlas - CISC370
2
Announcements
• Final Exam
 08/15 at 3:30PM to 5:30PM in 107 Sharp Lab
(SHL107)
 We will have a review on 08/12, no class on
08/14
• Remaining topics
 Java Security
 3D graphics API + Sound
July 29, 2008
James Atlas - CISC370
3
Today
• Self-executing Java
• J2EE Intro
July 29, 2008
James Atlas - CISC370
4
Self-executing Java
• "How do I make an .EXE file from my Java
•
application?“
Many different approaches:




Executable Jars
Java Web Start
Custom launcher/wrapper
Ahead-Of-Time Compilers
July 29, 2008
James Atlas - CISC370
5
Executable Jars
• “zip” archive containing application code
•
and resources
contains a META-INF/MANIFEST.MF file
 Main-Class: MyAppMain
 Class-Path: mylib.jar
• automatically associated in Windows with
•
your installed JRE (javaw.exe)
requires a working, installed JRE
July 29, 2008
James Atlas - CISC370
6
Java Web Start
• From a web-link it automatically downloads
application to a local cache and launches it
• Less secure than Applets (with user
confirmation)
• Internet connectivity required
• Similar to:
java -classpath http://www.mysite.com/app/MyApp.jar
com.mysite.app.Main
• Example:
http://www.calcexp.com/weblaunch.htm
July 29, 2008
James Atlas - CISC370
7
Custom launcher/wrapper
• Creates a small native program that calls the
appropriate java commands
 Allows custom taskbar icons and splashscreens
 Can be configured to use a bundled JRE
• Launch4J - http://launch4j.sourceforge.net/
July 29, 2008
James Atlas - CISC370
8
Ahead-Of-Time Compilers
• Produces a conventional native executable for the
•
target platform
Pros:
 performance
 Intellectual Property
 No “warm-up” time
• Cons:
 limited tools depending on what version of Java
 very hard to do dynamic applications (custom
classloaders like web servers with JSP content)
 produces executable that is platform specific but
somewhat hardware agnostic
July 29, 2008
James Atlas - CISC370
9
Programming Assignment 5
July 29, 2008
James Atlas - CISC370
10
J2EE
July 29, 2008
James Atlas - CISC370
11
History
• Initially two tier architecture (client server
applications)
• Client is responsible for data access applying
business logic and presentation of data
• Only service provided by Server was that of
database server.
July 29, 2008
James Atlas - CISC370
12
Two Tier Application Architecture
Client
July 29, 2008
Server
James Atlas - CISC370
13
Two Tier Application Architecture
• Drawbacks
- Easy to deploy but difficult to enchance or
upgrade.
- It makes reuse of business and presentation
logic difficult
- Not scalable and not suited for internet
July 29, 2008
James Atlas - CISC370
14
J2EE
• To develop N tier application
• It supports the development of a
variety of application types
 small client server systems
 Systems running on Intranets
 Systems on large scale internet ecommerce site
July 29, 2008
James Atlas - CISC370
15
J2EE Features
•
•
•
•
•
Component based model
Container provided services
Highly Scaleable
Simplified Architecture
Flexible security model
July 29, 2008
James Atlas - CISC370
16
Key J2EE APIs
• Component Technologies
 Servlets
 Java Server Pages (JSP)
 Enterprise Java Beans (EJB)
• Standard Services
 Java Database Connectivity (JDBC API)
 Java Naming and Directory Interface
(JNDI)
 Java Transaction API (JTA)
• Other Services
 HTTP, HTTPS, RMI-IIOP, JMS, JavaMail
July 29, 2008
James Atlas - CISC370
17
Containers in the N-Tier J2EE
Architecture
July 29, 2008
James Atlas - CISC370
18
J2EE Tiers
• Client Presentation
 HTML or Java applets deployed in Browser
 XML documentations transmitted through HTTP
 Java clients running in Client Java Virtual Machine
(JVM)
• Presentation Logic
 Servlets or JavaServer Pages running in web
server
• Application Logic
 Enterprise JavaBeans running in Server
July 29, 2008
James Atlas - CISC370
19
J2EE Application Model
• Browser is able to process HTML and
applets pages.
• It forwards requests to the web server, which
has JSPs and Servlets
• Servlets and JSPs may access EJB server.
• Java Standalone runs on java client, which
access EJB server using RMI.
July 29, 2008
James Atlas - CISC370
20
J2EE Application Model
July 29, 2008
James Atlas - CISC370
21
EJB – Enterprise Java Beans
• Enterprise Java Beans are components that are
•
deployed into containers
The container provides services
 Loading / Initialization
 Transactions
 Persistence
 Communication with EJB clients
 Enterprise Naming Context (JNDI name space)
July 29, 2008
James Atlas - CISC370
22
Anatomy of an EJB
• Remote Interface
 Methods that can be accessed by the outside world.
 Extends javax.ejb.EJBObject
• Remote Home Interface
 Life-cycle methods (create, findByPrimaryKey)
 Extends javax.ejb.EJBHome which extends
java.rmi.Remote
• Bean class
 The class performing the actual business process
 Implements an interface based on type of bean
July 29, 2008
James Atlas - CISC370
23
EJB – Enterprise Java Beans
• Entity Beans
• Session Beans
• Message Beans
 New in EJB 2.0
July 29, 2008
James Atlas - CISC370
24
Client / EJB Relationship
• How does a client application (Java class) utilize EJBs?
 Lookup - JNDI ENC
 Network protocol - RMI
 EJB container creates object with RemoteHome and Home
interfaces – this object passes calls to the bean class
July 29, 2008
James Atlas - CISC370
25
EJB – Enterprise Java Beans
• Entity Beans
• Session Beans
• Message Beans
 New in EJB 2.0
July 29, 2008
James Atlas - CISC370
26
EJB – Entity Beans
• Entity beans are classes that map to individual
entities – typically, an Entity bean references a row
in a database table, providing an object
representation of that database object.
 For example, an entity bean could represent a customer,
and changing the values in that entity bean would cause
updates to that database row
• Entity beans provide an abstraction layer so that
working with the entity is not specific to the storage
mechanism for that entity.
July 29, 2008
James Atlas - CISC370
27
Entity Beans - Persistence
• Container Managed Persistence (CMP)
 The EJB container automatically persists the EJB
objects, usually to a relational database where each type
of object is represented as a table, and each instance of
the object is a row in that table
• Bean Managed Persistence (BMP)
 The EJB container calls bean methods when it is
appropriate for the bean to load, save or update data,
enforcing transactions without transaction code written by
the bean developer
July 29, 2008
James Atlas - CISC370
28
EJB – Session Beans
• Session beans perform work for a client
application
 For example, a session bean could charge a
credit card for a specific transaction.
July 29, 2008
James Atlas - CISC370
29
Session Beans – State
• Stateful – A stateful bean maintains a
•
conversational state with a client. The client
perceives that it is only talking to one bean, and
that bean maintains information between calls
Stateless – A stateless bean maintains no client
information between method calls – the container
can substitute beans as necessary between
method calls
July 29, 2008
James Atlas - CISC370
30
EJB – Session Bean Example
package org.jboss.docs.interest;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
/** This interface defines the `Remote' interface for the `Interest' EJB. Its single method is the only
method exposed to the outside world. The class InterestBean implements the method. */
public interface Interest extends EJBObject
{
/** Calculates the compound interest on the sum `principle', with interest rate per period `rate'
over `periods' time periods. This method also prints a message to standard output; this is
picked up by the EJB server and logged. In this way we can demonstrate that the method is
actually being executed on the server, rather than the client. */
public double calculateCompoundInterest(double principle, double rate, double periods)
throws RemoteException;
}
July 29, 2008
James Atlas - CISC370
31
EJB – Session Bean Example
package org.jboss.docs.interest;
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
/** This interface defines the 'home' interface for the 'Interest' EJB. */
public interface InterestHome extends EJBHome
{
/** Creates an instance of the `InterestBean' class on the server, and returns a remote
reference to an Interest interface on the client. */
Interest create() throws RemoteException, CreateException;
}
July 29, 2008
James Atlas - CISC370
32
EJB – Session Bean Example
package org.jboss.docs.interest;
import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
/** This class contains the implementation for the 'calculateCompoundInterest' method exposed by
this Bean. It includes empty method bodies for the methods prescribe by the SessionBean
interface; these don't need to do anything in this simple example. */
public class InterestBean implements SessionBean
{
public double calculateCompoundInterest(double principle, double rate, double periods)
{
System.out.println("Someone called `calculateCompoundInterest!'");
return principle * Math.pow(1+rate, periods) - principle;
}
public void ejbCreate() {}
public void ejbPostCreate() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sc) {}
July 29, 2008
James Atlas - CISC370
}
33
EJB – Session Bean Example
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar>
<description>JBoss Interest Sample Application</description>
<display-name>Interest EJB</display-name>
<enterprise-beans>
<session>
<ejb-name>Interest</ejb-name>
<home>org.jboss.docs.interest.InterestHome</home>
<remote>org.jboss.docs.interest.Interest</remote>
<ejb-class>org.jboss.docs.interest.InterestBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
July 29, 2008
James Atlas - CISC370
34
EJB – Session Bean Example
package org.jboss.docs.interest;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
class InterestClient
{
/** This method does all the work. It creates an instance of the Interest EJB on the EJB server,
and calls its `calculateCompoundInterest()' method, then prints the result of the calculation. */
public static void main(String[] args)
{
try {
InitialContext jndiContext = new InitialContext();
ref = jndiContext.lookup("interest/Interest");
InterestHome home = (InterestHome) PortableRemoteObject.narrow(ref, InterestHome.class);
Interest interest = home.create(); //Create an Interest object from the Home interface
System.out.println(interest.calculateCompoundInterest(1000, 0.10, 2));
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
July 29, 2008
James Atlas - CISC370
}
35
EJB – Message Beans
• Message beans are classes that receive
asynchronous notification from a Java
Message Service server
 For example, a message bean could be
activated when vendor sends a purchase order
to a JMS queue.
July 29, 2008
James Atlas - CISC370
36
JMS – Java Message Service
JMS Queue
July 29, 2008
James Atlas - CISC370
37
JMS – Java Message Service
JMS Topic
July 29, 2008
James Atlas - CISC370
38
JMS – Java Message Service
• Why should I use JMS?
 Loosely-coupled systems
• Connectionless
• Removes dependence on client and server platform /
programming language / version
 Publish / Subscribe metaphor
• Send / receive information with many, unknown clients
 Integration with other messaging systems
• IBM MQ-Series
• Microsoft Message Queue
July 29, 2008
James Atlas - CISC370
39
Example EJB Application
July 29, 2008
James Atlas - CISC370
40
Servlets
• Are container managed web components
• Replace Common Gateway Interface(CGI) or
•
•
•
Active Server Pages (ASP)
Generate dynamic response to requests from web
based clients
Synchronize multiple concurrent client request
Serve as client proxies
July 29, 2008
James Atlas - CISC370
41
JavaServer Pages (JSP)
• Text based documents describe how to
process a request and create a response
• Contains HTML or XML and other JSP
elements defined by JSP specification.
• Installed on web server
• Web components that sit on top of java
servlets
July 29, 2008
James Atlas - CISC370
42
J2EE Application Packaging
July 29, 2008
James Atlas - CISC370
43
JBoss
July 29, 2008
James Atlas - CISC370
44
What is JBoss
 Created in 1999, JBoss is the product of an
OpenSource developer community dedicated to
developing the best J2EE-compliant application
server in the market
 With 1000 developers worldwide and a steadily
growing number of downloads per month, reaching
72,000 for October ’01 (per independent
www.sourceforge.net), JBoss is arguably the most
downloaded application server in the world today
 Distributed under an LGPL license, JBoss is
absolutely FREE for use. No cost. Period.
July 29, 2008
James Atlas - CISC370
45
What is Application Server
 Application servers enable the development of
multi-tiered distributed applications. They are
also called “middleware”
 An application server acts as the interface
between the database(s), the web servers and
the client browsers
July 29, 2008
James Atlas - CISC370
46
JBoss- Application Server
July 29, 2008
James Atlas - CISC370
47
JBoss - example
July 29, 2008
James Atlas - CISC370
48
Project 2
• Need to setup group meeting time
July 29, 2008
James Atlas - CISC370
49