CS483_Wk1_Slides - Regis University: Academic Web Server

advertisement
CS 483 Enterprise and Web
Application Programming
Week 1
1
Welcome and Introduction




Introductions
Syllabus
Introduction to the class
Week 1 Topics





Overview of Distributed Systems
Distributed Systems Architectures
Middleware
Data Persistence Tier
JDBC Java/MySQL API
2
Distributed Systems Overview

What is a distributed system?


Your textbook authors define a distributed
system as “one in which components
located at networked computers
communicate and coordinate their actions
by passing messages”
Most enterprise and web applications fall
under the umbrella of distributed systems

For example, you use them when you use a
Regis Online course

WebAdvisor, Forum, Email
3
Distributed Systems
Consequences

Concurrency


No global clock


all computers on the network do NOT share the
same clock or time – no notion of correct time
Independent failures



work is happening at the same time on different
computers and share resources
faults in a network result in isolation of the
computers that are connected to it
each component can fail independently
Motivation to construct a distributed system

desire to share resources
4
Examples of Distributed
Systems



Internet – WWW, email, file transfer
Intranet – firewalls, routers, servers
Mobile and ubiquitous computing


Laptops
Handheld devices (PDAs, mobile phones,
smart watches, smart appliances)
5
A Typical Portion of the Internet
intranet
ISP
%
%
%
%
backbone
satellite link
desktop computer:
server:
network link:
6
A Typical Intranet
email s erv er
Desktop
computers
print and other servers
Web server
Local area
netw ork
email s erv er
File s erv er
print
other servers
the rest of
the Internet
router/firew all
Portable and Handheld Devices in
a Distributed System
Internet
Host intranet
WAP
gatew ay
Wireles s LAN
Mobile
phone
Laptop
Printer
Camera
Home intranet
Host site
8
Web Servers and Web
Browsers
www.google.com
http://www.google.comlsearch?q=kindberg
Browsers
Web servers
Internet
www.cdk3.net
http://www.cdk3.net/
www.w3c.org
File system of
www.w3c.org
http://www.w3c.org/Protocols/Activity.html
Protocols
Activity.html
9
Challenges of Distributed Systems

Heterogeneity

Variety and difference as applied to:


networks, computer hardware, operating systems,
programming languages, implementations by different
developers
Middleware

Software layer that provides a programming abstraction
as well as masking the heterogeneity of the underlying
networks, hardware, operating systems, and
programming languages



CORBA (Common Object Request Broker) (www.omg.org)
RMI (Java Remote Invocation) – covered in Week 2 – only
supports single programming language
Mobile code

Code sent from one computer to another to run at the
destination (Java applets)
10
Challenges of Distributed Systems

Openness

Determines whether the system can be extended
and re-implemented in various ways


Degree to which a new resource-sharing service can be
added and be made available for use by a variety of
client programs
Open distributed systems:



have key interfaces published
have a uniform communication mechanism and published
interfaces to shared resources
are constructed from heterogeneous hardware and
software probably from different vendors but all vendors
conform to a published standard
11
Challenges of Distributed Systems

Security


Information resources are of high intrinsic
value to users – security is vitally
important!
Must have:



confidentiality – protection against disclosure to
unauthorized individuals
integrity – protection against alteration or
corruption
availability – protection against interference
with the means to access the resources

Denial of service attack, Trojan horse attachment
12
Challenges of Distributed Systems

Scalability

System is scalable if it will remain effective
when there is a significant increase in the
number of resources and the number of
users


i.e. The Internet
Challenges




Controlling the cost of physical resources
Controlling the performance loss
Preventing software resources from running out
Avoiding performance bottlenecks
13
Computers in the Internet
Date
1979, Dec.
1989, July
1999, July
2003, Jan.
Computers
Web servers
188
0
130,000
56,218,000
171,638,297
0
5,560,866
35,424,956
14
Computers vs. Web servers in
the Internet
Date
1993, July
1995, July
1997, July
1999, July
2001, July
Computers
Web servers
Percentage
1,776,000
130
0.008
6,642,000
19,540,000
56,218,000
125,888,197
23,500
1,203,096
6,598,697
31,299,592
42,298,371
0.4
6
12
25
15
Challenges of Distributed Systems

Failure Handling



More failure types can occur in processes and
networks
Failures are partial – some components fail while
others continue to function
Techniques for handling failures:





Detecting failures (checksums)
Masking failures (retransmit dropped messages)
Tolerating failures (user keeps trying)
Recovery from failures (rollback state of component)
Redundancy (hardware)
16
Challenges of Distributed Systems

Concurrency

Several clients attempt to access a shared
resource at the same time


Program threads
Distributed software must be responsible
for ensuring that servers and applications
operate correctly in a concurrent
environment
17
Challenges of Distributed Systems

Transparency


Concealment from the user and the
application programmer of the separation
of components in a distributed system
System is perceived as a “whole” rather
than a collection of independent
components

Programmer only concerned with the design of
their particular applications
18
Transparency Forms




Access transparency: enables local and remote
resources to be accessed using identical operations.
Location transparency: enables resources to be
accessed without knowledge of their physical or
network location (for example, which building or IP
address).
Concurrency transparency: enables several processes
to operate concurrently using shared resources
without interference between them.
Replication transparency: enables multiple instances
of resources to be used to increase reliability and
performance without knowledge of the replicas by
users or application programmers.
19
Transparency Forms




Failure transparency: enables the concealment of
faults, allowing users and application programs to
complete their tasks despite the failure of hardware
or software components.
Mobility transparency: allows the movement of
resources and clients within a system without
affecting the operation of users or programs.
Performance transparency: allows the system to be
reconfigured to improve performance as loads vary.
Scaling transparency: allows the system and
applications to expand in scale without change to the
system structure or the application algorithms.
20
The Enterprise
21
Enterprise Framework



Schema for classifying and organizing
the topics related to managing the
enterprise
Assists the organization to become
more accountable and responsive
Shows how enterprise architecture
considers the design and operation of
an organization from many aspects,
perspectives, and diciplines
22
Zachman Framework for Enterprise Architecture
(www.zifa.com)
Distributed System Architectures

Architecture Model – what is it?



Defines the way in which the components
of systems interact with one another
Defines the way in which components are
mapped onto the underlying network of
computers
Decompose architecture of a distributed
system into both vertical and horizontal
tiers
24
Layers

Application, operating system, and
hardware layers
Layer
Platform
Layer
Example
Application Layer
Internet Explorer
Operating System Layer
Microsoft XP
Hardware Layer
Dell Inspiron (pentium)
25
Tiers – Internet Example


Three tiers – client, web server, and
persistence
Each tier consists of one or more
components that collectively fulfill a
common purpose
Tier
Client Tier
Web-Server Tier
Persistence Tier
Example
Internet Explorer
Apache
Oracle Database
26
Logical and Physical Tiers

Physical tiers



Based on the assumption that application
software components found in the different
tiers reside on different computers
Or, on different processors
Logical tiers

If system deployed on a single computer
but has the separate software tiers
27
Physical Tiers (3)
Windows XP
Unix Host
IE
Browser
1
*
Client Tier
Apache
Web Server
Linux Host
1
Oracle
Database
1
Web-Server Tier
Database Tier
Logical Tiers (3)
Windows XP
IE
Browser
Apache
Web Server
1
1
*
Client Tier
Oracle
Database
1
Web-Server Tier
Database Tier
Case Study: Single Tier Architecture – Student Services
Application (S3)
The simplicity of single-tier architectures provides a convenient initial framework for
subsequently examining more sophisticated multi-tier architectures.
localhost
Tiers based on common
«Jar»
functionality
StudentServices
Logical
Client
Tier
package sss.ui
Logical
Server
Tier
package sss.server
Logical
Persistence
Tier
package sss.io
AdvisorPanel
Advisor
FileMgr
SchedulePanel
Scheduler
ObjectStream
package sss.doman
«create»
«create»
Schedule
Doman is shared
between all tiers
Student
Case Study: Two-Tier Physical Architecture (S3)
The simplified design given in the previous figure can be transformed into a
two-tier physical architecture by deploying the classes related to the client
aspects of the design on a different host computer from the classes related
to the server aspects of the design.
Client
Server
«Jar»
S3Client
ui
«Jar»
S3Server
«tcp/ip»
«import»
domain
server
io
«import»
domain
Case Study: Three-Tier Physical Architecture (S3)
The simplified two tier architecture presented in the previous figure can be
transformed into a three-tier physical architecture by separating the
handling of persistence in the server tier into its own tier.
Database replaces
file I/O
Client
DbServer
Server
«Jar»
S3Client
ui
«tcp/ip»
«import»
domain
«Jar»
S3Db
«Jar»
S3Server
server
«import»
domain
«jdbc»
db
«import»
Package1
Case Study: Multi-Tier Physical Architecture
Large Telecommunication’s Company
The key feature of this architecture concerns the fact that any client can
communicate with any web server, but these clients cannot directly
communicate with other tiers.
DbServer
Client
Webserver
AppServer
DbServer
Client
Webserver
Legacy
Client
AppServer
Webserver
Client
Legacy
Middleware

Software layer that provides an abstract
programming interface that hides, via encapsulation,
the details associated with heterogeneity in both the
layers and tiers of distributed architecture
Layers
Examples
Application Layer
Internet Explorer
Firefox Browser
Middleware Layer
Distributed Component
Object Model (DCOM)
Web Services
Platform Layer
MS/Dell
Sun Solaris
33
Case Study – Student Services –
EJB as Middleware


While contemplating the design of the new
student services application, you realize that
using a middleware platform will result in a
more robust application that can be
developed in a shorter time.
A web survey identifies three major
middleware contenders:



W3’s Web Services (http://www.w3.org/)
Microsoft’s .Net (http://www.microsoft.com)
Sun’s EJB Application Server (http://java.sun.com)
34
Case Study (S3) Architecture
A multi-tier architecture for the student services application
utilizing a Java 2 Enterprise Edition (J2EE),
Java Enterprise Bean (EJB) & corresponding EJB application
server middleware technologies
Client
S3
User Interface
Tiers
Business Logic
S3
Server
Persistence
S3
Database
Middleware
J2EE
Client EJB
J2EE
EJB App Server
J2EE
Oracle JDBC
Platform
Microsoft XP
(Dell/Pentium)
Solaris Unix
(Sun)
Open Linux
(HP)
Application
Layers
483 Student uses XP uses MySQL 35
Component Transaction Monitor
(CTM)

Component Transaction Monitor (CTM)



Server-side application that combines the features
of a traditional Transaction Processing Monitors
(TPMs) and more recent distributed Object
Request Brokers (ORBs)
TPM – emerged in 1960s at IBM to handle large
online transaction processing associated with large
systems (i.e., airline reservation system)
ORB – emerged to support the deployment of
objects across a network (i.e, RMI, in Week 2)
36
Object Life Cycle

Life cycle of an object is the “life” of an object
from creation, modification, and deletion


Object instance – not domain data associated with
the application (saved data)
Factory Design Pattern


Often used to encapsulate the functionality
associated with the life cycle maintenance of an
object
This pattern separates the business logic of the
domain being modeled from the object’s life cycle
37
Case Study: Student Factory in a
Distributed System

Student Factory contains methods for
creating, finding, and removing a Student
object
StudentFactory
«create»
+create() : Student
+find(in id : int) : Student
+findByName(in name : String) : Student
+remove(in id : int) : void
Student object contains operations
associated with the domain application
(i.e., addCourse (course))
Student
38
Case Study: Student Factory in a
Distributed System using Singleton


Factory can be conveniently implemented using a
Singleton design pattern as shown in this example
Hides the actual location of the business object from
the user (i.e., two Student objects located on
different servers)
StudentFactory
-singleton : StudentFactory = new StudentFactory()
«create»
Student
+getSingleton() : StudentFactory
-StudentFactory()
+create() : Student
+find(in id : int) : Student
+remove(in id : int) : void
Given this singleton design, a new Student object can be created using Java:
Student aStudent = StudentFactory.getSingleton().create();
39
Persistence Tier



In multi-tier enterprise architectures, the
persistence tier encapsulates the logic for
saving data to, and loading data from,
persistent storage
Within a CTM (application) server, enterprise
information in the business-logic tier is
represented using remote objects.
As with all objects, a remote object captures
state by encapsulating local data as a set of
attributes. Consequently, the long-term
persistence of an object requires saving its
attribute values to non-volatile storage.
40
Persistence Tier

Examples




Java serialized objects (CS 434)
Local files using XML
Databases
Design patterns developed to capture
the saving of data to files or database

Data Access Object (DAO pattern)
41
Design Patterns

What is a design pattern?



Patterns provide generic, reusable designs
that solve problems at the design level
Provides a proven, tested solution for a
class of similar design problems
Lend common terminology that you can
use to make your own designs easier to
document and understand

Provides a common “language” for developers
to use in meetings and documentation
42
DAO Design Pattern
«use»
«interface»
DataAccessObject
+create(in id : int) : BusinessObject
+find(in id : int) : BusinessObject
+save(in obj : BusinessObject) : void
Factory
BusinessObject
«use»
PersistenceMgr
«use»
«instantiate»
DataObject
+create(in id : int) : BusinessObject
+find(in id : int) : BusinessObject
+save(in obj : BusinessObject) : void
43
DAO Design Pattern



DAO used to separate business logic aspects of an
object from the code used to access persistent
storage
In a distributed system, using a factory to find a
remote object does not always require loading the
object from persistent storage – it could have been
previously loaded and found on the network
S3 Case study example – when the Student object is
sent a message to “save” itself, the Student will
delegate the request via a message sent to a
corresponding data access object (DAO)

DAO “knows” how to “save” the object (write it to a
particular database system, i.e., MySQL) and “where”
(location of DB)
44
DAO Factory Pattern
«uses»
BusinessObject
DaoFactory
«uses»
DatabaseDaoFactory
+create() : DataAccessObject
«instantiate»
«uses»
«interface»
DataAccessObject
+create(in id : int) : BusinessObject
+find(in id : int) : BusinessObject
+save(in obj : BusinessObject) : void
«implement»
XmlFileDaoFactory
+create() : DataAccessObject
«implement»
«instantiate»
MySqlDAO
XmlDAO
+create(in id : int) : BusinessObject
+find(in id : int) : BusinessObject
+save(in obj : BusinessObject) : void
+create(in id : int) : BusinessObject
+find(in id : int) : BusinessObject
+save(in obj : BusinessObject) : void
45
DAO Factory Pattern


A DAO factory pattern is often used to further
separate the application server from the persistence
engine.
For example, consider a situation in which data may
be saved either to a relational MySQL database or to
an XML file.



Business object, such as student, would request a data
access object from a data access object (DAO) factory.
Since the factory knows which type of persistence is
being used, it returns a data access object (DAO)
corresponding to the appropriate persistence engine.
Specifically, if the application is currently using a
MySQL database for persistence, a MySqlDAO object
46
will be returned.
Case Study:
S3 Saving a Student
Student
«use»
-name : String
-plan : DegreePlan
-schedule : Schedule
-studentId : int
+getName() : String
+setName(in name : String) : void
+getPlan() : DegreePlan
+setPlan(in p : DegreePlan) : void
+getSchedule() : Schedule
+setSchedule(in s : Schedule) : void
+getStudentId() : int
+setStudentId(in id : int) : void
+save() : void
MySqlDaoFactory
«interface»
StudentDAO
+create(in id : int) : Student
+find(in id : int) : Student
+save(in s : Student) : void
«implements»
StudentMySqlDAO
«use»
JDBC
-dbMgr : JDBC
+create(in id : int) : Student
+find(in id : int) : Student
+save(in s : Student) : void
«instantiate»
+getStudentDAO() : StudentDAO
47
Case Study:
S3 Saving a Student



Having investigated the benefits of the DAO
approach to persistence, you decide to
update your Student persistence to utilize a
DAO and an associated DAO factory.
While developing your design, you realize
that you’re not exactly sure how the student
data access object should be implemented,
but remember that JDBC can be used to save
Java objects to a relational database.
So, you have your DAO use JDBC.
48
JDBC


Java DataBase Connectivity (JDBC)
consists of a set of interfaces whose
methods can be used to connect to and
access data within a relational database
using SQL.
The salient methods of these JDBC
interfaces are defined in the following
UML diagram.
49
package java.sql
DriverManager
Only a subset of the java.sql interfaces and
operations relevant to the examples found
in this section are displayed in this diagram.
+getConnection(in url : String) : Connection
«instantiate»
«interface»
Driver
+connect(in url : String, in info : Properties) : Connection
«instantiate»
«interface»
Connection
+createStatement(in sql : String) : Statement
+prepareCall(in sql : String) : CallableStatement
+prepareStatement(in sql : String) : PreparedStatement
«instantiate»
«instantiate»
«implements»
«interface»
ResultSet
+getFloat(in columnIndex : int) : float
+getFloat(in columnName : String) : float
+getInt(in columnIndex : int) : int
+getInt(in columnName : String) : int
+getString(in columnIndex : int) : String
+getString(in columnName : String) : String
+next() : boolean
package com.mysql.jdbc
jdbc::Driver
«iimplements»
jdbc::ResultSet
«interface»
Statement
+executeQuery() : ResultSet
+executeUpdate() : int
«interface»
PreparedStatement
+setFloat(in index : int, in x : float) : void
+setInt(in index : int, in x : int) : void
+setString(in index : int, in x : String) : void
«interface»
CallableStatement
«iimplements»
jdbc::PreparedStatement
jdbc::Connection
Connecting to the Database


The first step in using JDBC is to load the driver you
are using into the JVM.
The following code, which only needs to be executed
once, will load the MySQL driver.
Note: Explicitly loading the driver in this fashion allows you to avoid
errors that occur in certain JVMs
try {
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
catch (ClassNotFoundException e) { // Class not on the CLASSPATH
. . .
catch (InstantiationException e) { // Error in the instantiation
. . .
catch (IllegalAccessException e) { // Security policy violation
. . .
} // try
51
Connecting to the Database


The next step is to obtain a connection to the database.
JDBC uses a Uniform Resource Locator (URL) to specify the
information required to establish this connection. The
general form of this URL is:


jdbc:<subprotocol>:<subname>
For example, the URL establishing a connection between a
Java application and a MySQL database executing on the
same computer is:
"jdbc:mysql://localhost/rubs?user=root&password=dbadmin”
52
Connecting to the Database


The jdbc token designates that this URL uses the JDBC protocol.
The mysql token indicates that the <subname> adheres to the
mysql sub-protocol, which specifies that a MySQL database named
rubs located on the localhost is to be used and that our Java
application is logging into this database as the user “root” with a
password of “dbadmin”.
String url =
"jdbc:mysql://localhost/rubs?user=root&password=dbadmin";
try {
Connection dbConn = DriverManager.getConnection(connUrl);
} catch (SQLException e) {
. . . // Unknown db or login failed
} // try
53
Querying the Database


Assume the following book table is exists the previous
rubs database
The SQL query required to obtain the name and price
of the book with id 81763 is:
select name,price from Book where id = 81763;
Id:int
Name:varchar
Price:float
12294
Pride and Prejudice
7.95
81763
The Little Prince
5.50
54
Querying the Database



The JDBC code required to issue this query to the database begins
by first declaring a prepared statement.
In this prepared statement, the optional arguments of the query are
indicated with a question mark (?).
Once all of the parameters of the statement have been supplied
values, the statement can be executed and the results of this
execution will be returned in a result set.
int id = 81763;
try {
String sqlStmt = “select Name,Price from Book where id = ?”;
PreparedStatement stmt = conn.prepareStatement(sqlStmt);
stmt.setInt(1, id);
ResultSet rs = stmt.execute();
. . .
} catch (SQLException e) {
. . .
} // try
55
Mapping Result Sets to
Objects


If one or more rows in the table were found, the
ResultSet’s next() method returns true.
As book ids are unique, a maximum of one row can
be found for the previous query.



Hence, success can be checked with a single if statement
(as opposed to a while loop).
Once the database row has been obtained from the
database, it is necessary to map the data into an
object.
This can easily be accomplished by creating a new
instance corresponding to the row and using mutator
methods to assign the appropriate values
56
Mapping Result Sets to
Objects
if (rs.next()) {
Book book = new Book(id);
book.setBook(rs.getString(“Name”));
book.setPrice(rs.getFloat(“Price”));
return book;
} else {
System.out.printl(“There is no book with id “ + id);
} // if
Note: There are a number of more generic ways to accomplish
this mapping, however, to keep things simple, they are not be discussed
in this course.
57
CRUD Operations




Create, Update, and Delete operations
Handled by constructing the appropriate SQL
statement and using JDBC to execute the
corresponding statement.
Since no results are expected from these
operations, the executeQuery method is
not used.
Instead, the executeUpdate method is
used, which returns the number of rows that
were successfully created, updated, or
deleted.
58
CREATE Row in Table
try {
sqlStmt = “insert into Book (id, name, price)
values (?, ?, ?)”;
PreparedStatement stmt =
conn.prepareStatement(sqlStmt);
stmt.setInt(1, book.getId());
stmt.setString(2, book.getName());
stmt.setFloat(1, book.getPrice());
int rowsInserted = stmt.executeUpdate(sqlStmt);
} catch (SQLException e) {
. . .
} // try
59
DELETE Row in Table
try {
sqlStmt = “delete from into Book where is = ?”;
PreparedStatement stmt = conn.prepareStatement(sqlStmt);
stmt.setInt(1, book.getId());
int rowsDeleted = stmt.executeUpdate(sqlStmt);
} catch (SQLException e) {
. . .
} // try
60
UPDATE Row in Table
try {
sqlStmt = “update Book set price = ? where id = ?”;
PreparedStatement stmt = conn.prepareStatement(sqlStmt);
stmt.setPrice(1, newPrice);
stmt.setInt(2, book.getId());
int rowsUpdated = stmt.executeUpdate(sqlStmt);
} catch (SQLException e) {
. . .
} // try
61
JDBC Resources

JDBC Tutorial -
http://www.stardeveloper.com/articles/display.html?article=2003090401&
page=1





JDBC Basics http://java.sun.com/docs/books/tutorial/jdbc/basics/index.html
JDBC Product Overview http://java.sun.com/products/jdbc/overview.html
Getting Started with JDBC API –
http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/getstart/GettingStar
tedTOC.fm.html
Duke's Bakery - A JDBC Order Entry Prototype - Part I http://java.sun.com/developer/technicalArticles/Database/dukesbakery/
Duke's Bakery - A JDBC Order Entry Prototype - Part 2 http://java.sun.com/developer/technicalArticles/Database/dukesbake
ry2/
62
SQL Resources


SQL Tutorial http://www.stardeveloper.com/articles/display.html?a
rticle=2002030801&page=1
MySQL – www.mysql.com
63
Other Resources

J2EE Core Patterns (DAO especially)


http://java.sun.com/blueprints/corej2eepat
terns/index.html
Middleware: A History of Objects,
Components, and the Web

http://www.awprofessional.com/articles/art
icle.asp?p=345781&seqNum=2&rl=1
64
Download