You are on page 1of 80

Java SL285

l Java
l Java
l SCJD

: Linda

PDF "pdfFactory" www.fineprint.com.cn



Linda
TomcatJavaWeb
Java 2

SL275SL285SL314SL425
www.personal.kent.edu/~hli4

Java
www.personal.kent.edu/~hli4/javasource.htm

: sky_cat_linda@yahoo.com

PDF "pdfFactory" www.fineprint.com.cn


Java
1.TomcatJavaWeb


http://www.phei.com.cn

http://www.personal.kent.edu/~hli4/

2.Java 2


http://www.book.sh.cn/

http://www.personal.kent.edu/~hli4/

PDF "pdfFactory" www.fineprint.com.cn


BrokerTool

BrokerApp Broker Database

ABC
ABC
BrokerTool

PDF "pdfFactory" www.fineprint.com.cn


l
l
l ID
l

PDF "pdfFactory" www.fineprint.com.cn



1.SqlServer
2. Rational Rose UML
3.Jbuilder Java

PDF "pdfFactory" www.fineprint.com.cn


BrokerTool

ssn char(15) ssn char(15)


cust_name char(40) symbol char(8)
symbol char(8)
address char(100) price real
quantity int(4)

Customer Shares Stock

BrokerDB.sql

PDF "pdfFactory" www.fineprint.com.cn


MVC
l Model:

l View:Model
l Controller:

PDF "pdfFactory" www.fineprint.com.cn


Model,View,Controller

Model

State query
State change
Change notification

View
selection

View Controller
User gestures

PDF "pdfFactory" www.fineprint.com.cn


MVC in BrokerTool

server
BrokerModel

BrokerView1 BrokerView2

BrokerController1 BrokerController2

client1 client2
PDF "pdfFactory" www.fineprint.com.cn
MVC Sequence Diagram
BrokerToolApp BrokerModel BrokerView BrokerController

1: brokerModel()

2: brokerView(model)

3: addCustomerChangeLis tener(this )

4: ModelController(model,view)

5: addUserGestureListener(this)

BrokerDB.mdl

PDF "pdfFactory" www.fineprint.com.cn


MVC Sequence Diagram

BrokerView BrokerController BrokerModel


: user

1: add Customer

2: handleAddCustom erGest ure(Customer c)

3: addCustomer(Cust omer c ust)

4: handleCustomerChange(Customer )

5: refresh

PDF "pdfFactory" www.fineprint.com.cn


MVC Sequence Diagram

BrokerView BrokerController BrokerModel


: user

1: addCustomer

2: handleGetCustomerGesture(String custId)

3: getCustomer(String custId)

4: showDisplay(Customer cust)

PDF "pdfFactory" www.fineprint.com.cn


Use Case
Use case1: Create new customer
Use case2: Delete customer
Use case3: Update customer fields
Use case4: View customer details
Use case5: View customer list
Use case6: View customer portfolio
Use case7: Buy shares on behalf of a customer
Use case8: Sell shares on behalf of a customer
Use case9: View stock prices

PDF "pdfFactory" www.fineprint.com.cn


Domain Objects

Customer

1
St ock 1

Portfolio
0..n 0..n
Share 1

0..n

PDF "pdfFactory" www.fineprint.com.cn


MVC

<<Interface>>
BrokerModel

State change
State query

Change Notification <<Interface>>


view selection BrokerController

<<Interface>>
BrokerView

User gesture notifi cation

PDF "pdfFactory" www.fineprint.com.cn


BrokerView
public interface BrokerView {
void addUserGestureListener(BrokerController b)throws
BrokerException;
void showDisplay(Object display)throws BrokerException;
void handleCustomerChange(Customer cust)throws
BrokerException;
}

PDF "pdfFactory" www.fineprint.com.cn


BrokerController
public interface BrokerController {
void handleGetCustomerGesture(String id);
void handleAddCustomerGesture(Customer c);
void handleDeleteCustomerGesture(Customer c);
void handleUpdateCustomerGesture(Customer c);
void handleGetAllCustomersGesture();
}

PDF "pdfFactory" www.fineprint.com.cn


BrokerModel
public interface BrokerModel {

public void addChangeListener(BrokerView bv) throws


BrokerException;
public void addCustomer(Customer cust) throws BrokerException;
public void deleteCustomer(Customer cust) throws BrokerException;
public void updateCustomer(Customer cust) throws BrokerException;
public Customer getCustomer(String id) throws BrokerException;
public Customer[] getAllCustomers() throws BrokerException;

PDF "pdfFactory" www.fineprint.com.cn


java
BrokerTool
src
trader
rmi
db
gui
nw
ticker

PDF "pdfFactory" www.fineprint.com.cn


(
l Build1:
BrokerException

Cus tomer

BrokerContr BrokerModel BrokerView


oller

PDF "pdfFactory" www.fineprint.com.cn



l Build2:BrokerModel

BrokerException

Customer

BrokerView
BrokerContr
BrokerModelDbImpl
oller
BrokerModel

PDF "pdfFactory" www.fineprint.com.cn



l Build3:BrokerView

BrokerException
BrokerGui

Customer

BrokerViewImpl

BrokerContr BrokerView
oller

BrokerModelDbImpl

BrokerModel

PDF "pdfFactory" www.fineprint.com.cn



l Build4:BrokerController,
l Build5:Socket
l Build6:BrokerModel
l Build7:RMI
l Build8:Stockportfolioviewcontroller

PDF "pdfFactory" www.fineprint.com.cn


()
l config/broker.conf
JDBC_DRIVER = com.microsoft.jdbc.sqlserver.SQLServerDriver
DB_URL = jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=BrokerDB
DB_USER = sa
DB_PASSWORD =

PDF "pdfFactory" www.fineprint.com.cn



l Propertiesconfig/broker.confPropertyReader.java

public class PropertyReader {


static private Properties ps;

static{
ps=new Properties();
try{
FileInputStream in=new FileInputStream(new File("config/broker.conf"));
ps.load(in);
}catch(Exception e){e.printStackTrace();}
}

public static String get(String key){


return (String)ps.get(key);
}
}

PDF "pdfFactory" www.fineprint.com.cn



l

JDBC_DRIVER=PropertyReader.get("JDBC_DRIVER");
DB_URL=PropertyReader.get("DB_URL");
DB_USER=PropertyReader.get("DB_USER");
DB_PASSWORD=PropertyReader.get("DB_PASSWORD");

Class jdbcDriver=Class.forName(JDBC_DRIVER);
java.sql.DriverManager.registerDriver((Driver)jdbcDriver.newInstance());

con=java.sql.DriverManager.getConnection( DB_URL,DB_USER,DB_PASSWORD);

PDF "pdfFactory" www.fineprint.com.cn


DBService
public interface DBService {

public Statement getStatement() throws Exception;


public void closeStatement(Statement stmt);
public void modifyTable( String sql) throws Exception;

PDF "pdfFactory" www.fineprint.com.cn


DBServiceImpl

package trader.db;

import java.sql.*;
import java.util.*;
import java.io.*;
import trader.util.PropertyReader;
public class DBServiceImpl implements DBService
{
private String JDBC_DRIVER;
private String DB_URL;
private String DB_USER;
private String DB_PASSWORD;
private Connection con;

PDF "pdfFactory" www.fineprint.com.cn


DBServiceImpl

public DBServiceImpl() throws Exception{

JDBC_DRIVER=PropertyReader.get("JDBC_DRIVER");
DB_URL=PropertyReader.get("DB_URL");
DB_USER=PropertyReader.get("DB_USER");
DB_PASSWORD=PropertyReader.get("DB_PASSWORD");

Class jdbcDriver=Class.forName(JDBC_DRIVER);
java.sql.DriverManager.registerDriver((Driver)jdbcDriver.newInstance());

con=createConnection();
}

PDF "pdfFactory" www.fineprint.com.cn


DBServiceImpl

private Connection createConnection()throws Exception{


return
java.sql.DriverManager.getConnection( DB_URL,DB_USER,DB_PA
SSWORD);

PDF "pdfFactory" www.fineprint.com.cn


DBServiceImpl

public Statement getStatement() throws Exception


{
if(con.isClosed()){
try{
con.close();
}catch(Exception e){}
}

con=createConnection();
return con.createStatement();
}

PDF "pdfFactory" www.fineprint.com.cn


DBServiceImpl

public void closeStatement(Statement stmt){


try{
stmt.close();
}catch(Exception e){e.printStackTrace();}
}

public void modifyTable( String sql) throws Exception


{
Statement stmt=getStatement();
try {
stmt.executeUpdate(sql);
}finally{stmt.close();}
}
}

PDF "pdfFactory" www.fineprint.com.cn


DBConnectPool
pool
poolFlag

getConnection()
releaseConnection()

PDF "pdfFactory" www.fineprint.com.cn



<<Interface>>
DBService

DBServiceWithPoolImpl
DBServiceImpl

DBConnectPool
pool
poolFlag

getConnection()
releaseConnection()

PDF "pdfFactory" www.fineprint.com.cn


BokerModelDbImpl

DBS ervice

<<Interface>>
BrokerModel
BrokerModelDbImpl

PDF "pdfFactory" www.fineprint.com.cn


BokerModelDbImpl
public void deleteCustomer(Customer cust) throws BrokerException{
try{
if(!ssnExists(cust.getId())){
throw new BrokerException("Record for "+cust.getId()+" not found");
}
String request;
request="delete customer where ssn='"+cust.getId()+"'";
dbService.modifyTable(request);
fireModelChangeEvent(cust);
}catch(Exception e){
e.printStackTrace();
throw new BrokerException("BrokerDbImpl.deleteCustomer\n"+e);
}
}

PDF "pdfFactory" www.fineprint.com.cn


()

<<Interface>>
BrokerGui
BrokerView

BrokerViewImpl

PDF "pdfFactory" www.fineprint.com.cn


selPan

cardPan

logPan

PDF "pdfFactory" www.fineprint.com.cn


()

JTable

PDF "pdfFactory" www.fineprint.com.cn


l BorderLayout
contentPane=frame.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(cardPan,BorderLayout.CENTER);
contentPane.add(selPan,BorderLayout.NORTH);
contentPane.add(logPan,BorderLayout.SOUTH);

PDF "pdfFactory" www.fineprint.com.cn


l logPanBorderLayout

private void buildLogPanel(){


logPan.setLayout(new BorderLayout());
logPan.add(logLb,BorderLayout.NORTH);
logPan.add(logSp,BorderLayout.CENTER);
}

PDF "pdfFactory" www.fineprint.com.cn


l cardPancustPanallCustPan

protected CardLayout card=new CardLayout();



cardPan.setLayout(card);
cardPan.add(custPan,"customer");
cardPan.add(allCustPan,"allcustomers");

PDF "pdfFactory" www.fineprint.com.cn


BrokerControllerImplBroker2TierApp

<<Interface>>
BrokerController

BrokerControllerImpl

Broker2TierApp
BrokerModelDbImpl

BrokerViewImpl

PDF "pdfFactory" www.fineprint.com.cn


TCP/IP

Broker2TierApp
BrokerDB

PDF "pdfFactory" www.fineprint.com.cn


()
NwClient NwServer

BrokerModelNwImpl
BrokerModelDbImpl

BrokerViewImpl
<<Interface>>
BrokerModel

addCustomer()

BrokerControllerImpl

PDF "pdfFactory" www.fineprint.com.cn


()
l SocketCommand
(ObjectInputStream ,ObjectOutputStream)
l BrokerModelNwImplBrokerModelNwClient
Command
l BrokerModeDbImplBrokerModel

PDF "pdfFactory" www.fineprint.com.cn



NwClient NwServer

BrokerModelNwImpl
BrokerModelDbImpl

BrokerViewImpl <<Int erface>>


BrokerModel

BrokerControllerImpl

PDF "pdfFactory" www.fineprint.com.cn


NwClient

public class NwClient {

public NwClient(String host,int port) {


}
public void send(Object obj) throws Exception{
}
public Object receive() throws Exception{
}
public void connect(){
}
public void connect(String host,int port){
}
public void close(){
}
}

PDF "pdfFactory" www.fineprint.com.cn


addCustomer
BrokerViewImpl BrokerControllerImpl BrokerModelNwImpl NwClient AddCustomerC NwServer BrokerModelDbImpl
: user ommand

1: addCustomer(cust)

2: handleAddCustomerGesture(Customer cust)

3: addCustomer(Customer cust)

4: new AddCustomerCommand(Customer cust)

5: send(Command cmd)

6: execute(BrokerModel model)

7: addCustomer(Customer cust)

8: receive()

PDF "pdfFactory" www.fineprint.com.cn



Command

execute()

NewCustCo
BuyCommand
SellCommand mmand

execute()
execute() execute()

PDF "pdfFactory" www.fineprint.com.cn


NwClient Command NwS erver

1: creat eBuyCommand

2: send(BuyCommand)

3: execute

4: recieve(BuyCommand)

PDF "pdfFactory" www.fineprint.com.cn


Command.java

public abstract class Command implements Serializable{

protected Object result=null;


protected Exception excep=null;
protected abstract void execute(BrokerModel broker);
public Object getResult() throws Exception{
if(this.result!=null){
return this.result;
}
if(this.excep!=null){
throw this.excep;
}
return null;
}
}

PDF "pdfFactory" www.fineprint.com.cn


BokerTool

Command
Get AllCustomerCommand
execute()

AddCustomerCommand UpdateCustomerCommand

DeleteCustomerCommand GetCustomerCommand

PDF "pdfFactory" www.fineprint.com.cn


AddCustomerCommand

public class AddCustomerCommand extends Command{

public AddCustomerCommand(Customer cust) {


this.cust=cust;
}

public void execute(BrokerModel broker){


try{
broker.addCustomer(cust);
}catch(Exception ex){
this.excep=ex;
}
}
}

PDF "pdfFactory" www.fineprint.com.cn


CommandServer

redesign

NwServer BrokerServer

CommandServer

PDF "pdfFactory" www.fineprint.com.cn


<<Interface>>
IoStrategy
NwServer

BrokerServer

PDF "pdfFactory" www.fineprint.com.cn


IoStrategy
package trader.nw;
import java.net.*;

public interface IoStrategy {


void ioService(Socket socket);
}

PDF "pdfFactory" www.fineprint.com.cn


BrokerServer
public class BrokerServer implements IoStrategy{
private BrokerModel broker;

public BrokerServer(BrokerModel broker) {


this.broker=broker;
}

PDF "pdfFactory" www.fineprint.com.cn


BrokerServer
public void ioService(Socket socket){
InputStream is;
ObjectInputStream ois;
OutputStream os;
ObjectOutputStream oos;
try{
os=socket.getOutputStream();
oos=new ObjectOutputStream(os);
is=socket.getInputStream();
ois=new ObjectInputStream(is);
while(true){
Command cmd=(Command)ois.readObject();
cmd.execute(broker);
oos.writeObject(cmd);
}
}catch(Exception e){ e.printStackTrace(); }
}
}

PDF "pdfFactory" www.fineprint.com.cn


NwServer
public class NwServer {
private int port;
private ServerSocket serverSocket;
private IoStrategy ioServer;
private Socket socket;

public NwServer(int port, IoStrategy ioServer) {


this.port=port;
this.ioServer=ioServer;
try{
serverSocket=new ServerSocket(port);
}catch(Exception e){
System.out.println("NwServer: "+e);
}
}

PDF "pdfFactory" www.fineprint.com.cn


NwServer

public void acceptConnection(){


Socket skt;
while(true){
try{
skt=serverSocket.accept();
ioServer.ioService(skt);

}catch(Exception e){
System.out.println("NwServer.acceptConnection: "+e);
}
}
}

PDF "pdfFactory" www.fineprint.com.cn


Broker3TierClient
:Broker3TierServer

PDF "pdfFactory" www.fineprint.com.cn


NwServer IoThreadPoolManager <<Interface>>


IoStrategy

n
<<Interface>>
Runnable IoThread BrokerServer

PDF "pdfFactory" www.fineprint.com.cn


IoThread
package trader.nw;
import java.net.*;
public class IoThread extends Thread{
private Socket skt=null;
private IoStrategy ioServer;
private int ioThreadId;
private static int ioThreadCounter=1;

public IoThread(IoStrategy ioServer) {


this.ioServer=ioServer;
ioThreadId=ioThreadCounter++;
}

PDF "pdfFactory" www.fineprint.com.cn


IoThread
public boolean isIdle(){
return skt==null;
}

public synchronized void setSocket(Socket s){


if(skt==null){
skt=s;
notify();
}
}

PDF "pdfFactory" www.fineprint.com.cn


IoThread
public synchronized void run(){
while(true){
try{
wait();
ioServer.ioService(skt);
skt=null;
}catch(Exception e){
skt=null;
e.printStackTrace();
}
}
}
}

PDF "pdfFactory" www.fineprint.com.cn


IoThreadPoolManager

public class IoThreadPoolManager implements IoStrategy{

private BrokerModel brokerModel;


private ArrayList clientIoThreadList;

public IoThreadPoolManager(BrokerModel broker) {


this.brokerModel=broker;
clientIoThreadList=new ArrayList(10);
this.createNewThread();
}

PDF "pdfFactory" www.fineprint.com.cn


IoThreadPoolManager
public void ioService(Socket socket){
IoThread ioThread;
boolean idleThreadFound=false;
for(int i=0;i<clientIoThreadList.size();i++){
ioThread=(IoThread)clientIoThreadList.get(i);
if(ioThread.isIdle()){
ioThread.setSocket(socket);
idleThreadFound=true;
break; }
}
if(!idleThreadFound){
ioThread=this.createNewThread();
ioThread.setSocket(socket);
}
}

PDF "pdfFactory" www.fineprint.com.cn


IoThreadPoolManager

private IoThread createNewThread(){


BrokerServer brokerServer=new BrokerServer(brokerModel);
IoThread ioThread=new IoThread(brokerServer);
clientIoThreadList.add(ioThread);
ioThread.start();
try{
Thread.sleep(1000);
}catch(Exception e){}
return ioThread;
}
}

PDF "pdfFactory" www.fineprint.com.cn


Broker3TierClient
:Broker3TierThreadedServer

PDF "pdfFactory" www.fineprint.com.cn


MessageReceiver

MessageReceiver
MessageSender

MessageReceiver

MessageReceiver

Server
client

PDF "pdfFactory" www.fineprint.com.cn


BrokerModelDbImpl
addCustomer

BrokerViewImpl MessageReceiver MessageSender BrokerModelDbImpl

1: handleCustomerChange(Cus tomer cust )

2: sendMessage(Object o)

3: andleCustomerChange(Customer cust)

PDF "pdfFactory" www.fineprint.com.cn


MessageReceiver MessageSender

<<Interface>>
BrokerView

BrokerViewImpl BrokerModelDbImpl

PDF "pdfFactory" www.fineprint.com.cn



Broker3TierMsgClient
:Broker3TierMsgServer

PDF "pdfFactory" www.fineprint.com.cn


RMI

<<Interface>>
Remote

UnicastRemoteObject

<<Interface>>
BrokerModel
BrokerModelDbImpl

PDF "pdfFactory" www.fineprint.com.cn


BrokerRMIServer

public class BrokerRMIServer {


public static void main( String args[] )
{
try
{
BrokerModel brokerModel=new BrokerModelDbImpl();
Naming.rebind("//localhost/TheBroker",brokerModel);
System.out.println( "BrokerRMIServer : bound in registry" );
}
catch( Exception e )
{
System.out.println("BrokerRMIServer err: "+e);
}
}
}

PDF "pdfFactory" www.fineprint.com.cn


BrokerRMIClient

public class BrokerRMIClient {


BrokerModel model;
BrokerView view;
BrokerController con;
public static void main( String args[] ) {
String hostName = "localhost";
if( args.length > 0 )
hostName = args[0];
try{
BrokerRMIClient client=new BrokerRMIClient();
client.model = (BrokerModel) Naming.lookup( "rmi://" + hostName + /TheBroker" );
client.view=new BrokerViewImpl(client.model);
client.con=new BrokerControllerImpl(client.model,client.view);
} catch( Exception e ){ System.out.println( "BrokerRMIClient exception: " +e) ;}
}

PDF "pdfFactory" www.fineprint.com.cn


addCustomer

BrokerViewImpl BrokerModelDbImpl
handleCustomer
Change

PDF "pdfFactory" www.fineprint.com.cn


< < Int erfac e> >


Rem ot e
(from trader.rm i)

< < Interfac e> > < < Int erfac e> >
B rok erV iew B rok erM odel
Chan ge Not ific ati on

addCus tom er()

B rok erV iewIm pl


B rok erM odelD bIm pl

Unic as tRem oteO bjec t


(from trader.rm i)

PDF "pdfFactory" www.fineprint.com.cn

You might also like