java 2 enterprise edition workshop
DESCRIPTION
Java 2 Enterprise Edition Workshop. STEP-Platformen i praksis Johannes Brodwall, Systek. Program. Oversikt over STEP Installasjon av programmer Tomcat MySQL JBoss Installasjon av devSTEP referanseprosjekt. Programmer som inngår. Installeres separat MySQL – SQL database - PowerPoint PPT PresentationTRANSCRIPT
Copyright © 2001 Systek AS [email protected]
Java 2 Enterprise Edition Workshop
STEP-Platformen i praksis
Johannes Brodwall, Systek
Copyright © 2001 Systek AS [email protected]
Program
• Oversikt over STEP• Installasjon av programmer
• Tomcat• MySQL• JBoss
• Installasjon av devSTEP referanseprosjekt
Copyright © 2001 Systek AS [email protected]
Programmer som inngår
• Installeres separat• MySQL – SQL database• JBoss – EJB-container• Tomcat – Servlet-container
• Inkludert i prosjektet:• Ant – byggeverktøy• JUnit – testverktøy – HTTPUnit
Copyright © 2001 Systek AS [email protected]
MySQL 3.23.44MySQL 3.23.44
Resource
JDBCJDBC
Integration
JBoss 2.4.3JBoss 2.4.3
Logic
Tomcat 4.0.1Tomcat 4.0.1
Presentation
InternetExplorer
InternetExplorer
Client
ServletContainer
J2EEs fem-lags arkitektur
Web-Browser
WindowsClient
Java SwingClient
Java SwingClient
JavaServerPages
XMLServlet
Template,Etc.
EJBContainer
SessionBean
EntityBean
MessageDriven B
RelationalDatabase
OODatabase
ThirdParty
System
JDBC
CORBA
XML
JMS
Copyright © 2001 Systek AS [email protected]
Oversikt over utviklingsprosessen
• Skriv kode i valgfri editor• Bygg med Ant• Test med JUnit• Deploy i JBoss
Copyright © 2001 Systek AS [email protected]
Ant på 5 minutter
<project name="devSTEP" default=”run" basedir="."> <property name="root.output" value="build" /> <property name=”main.class" value=”no.systek.workshop.Main" />
<target name="compile"> <mkdir dir="${root.classes}" /> <javac srcdir=”src" destdir="${root.output}/classes"> <classpath><fileset dir=”lib” includes=”*.jar” /></classpath> </javac> <copy todir=”${root.output}/classes"> <fileset dir="src" includes="**/*.properties"/> </copy> </target>
<target name=”run” depends="compile"> <java classname=”${main.class}”> <classpath> <fileset dir=”lib” includes=”*.jar” /> <pathelement name=”${root.output}/classes” /> </classpath> </java> </target></project>
<project name="devSTEP" default=”run" basedir="."> <property name="root.output" value="build" /> <property name=”main.class" value=”no.systek.workshop.Main" />
<target name="compile"> <mkdir dir="${root.classes}" /> <javac srcdir=”src" destdir="${root.output}/classes"> <classpath><fileset dir=”lib” includes=”*.jar” /></classpath> </javac> <copy todir=”${root.output}/classes"> <fileset dir="src" includes="**/*.properties"/> </copy> </target>
<target name=”run” depends="compile"> <java classname=”${main.class}”> <classpath> <fileset dir=”lib” includes=”*.jar” /> <pathelement name=”${root.output}/classes” /> </classpath> </java> </target></project>
Copyright © 2001 Systek AS [email protected]
ant j2ee-build
C:\java\projects\devSTEP>ant j2ee-buildBuildfile: build.xml
prepare:compile: [mkdir] Created dir: C:\java\projects\devSTEP\build\classes [javac] Compiling 17 source files to C:\java\projects\devSTEP\build\classesejb: [mkdir] Created dir: C:\java\projects\devSTEP\build\lib [ejbjar] building bmpexample.jar with 4 files [ejbjar] building cmpexample.jar with 4 files [ejbjar] building sessionbeans.jar with 7 fileslogin-war: [war] Building war: C:\java\projects\devSTEP\build\lib\tomcatlogin.warhello-war: [war] Building war: C:\java\projects\devSTEP\build\lib\hellotomcat.warj2ee-build:
BUILD SUCCESSFULTotal time: 6 seconds
C:\java\projects\devSTEP>ant j2ee-buildBuildfile: build.xml
prepare:compile: [mkdir] Created dir: C:\java\projects\devSTEP\build\classes [javac] Compiling 17 source files to C:\java\projects\devSTEP\build\classesejb: [mkdir] Created dir: C:\java\projects\devSTEP\build\lib [ejbjar] building bmpexample.jar with 4 files [ejbjar] building cmpexample.jar with 4 files [ejbjar] building sessionbeans.jar with 7 fileslogin-war: [war] Building war: C:\java\projects\devSTEP\build\lib\tomcatlogin.warhello-war: [war] Building war: C:\java\projects\devSTEP\build\lib\hellotomcat.warj2ee-build:
BUILD SUCCESSFULTotal time: 6 seconds
Copyright © 2001 Systek AS [email protected]
JUnit på 5 minutterpublic class TestCalculator extends junit.framework.TestCase {
public TestCalculator(String testName) { super(testName); }
public void testPlus() throws CalculatorInputException { Calculator calculator = new Calculator(); assertEquals(1 + 3, calculator.calculate("1 plus 3")); }}
public class TestCalculator extends junit.framework.TestCase {
public TestCalculator(String testName) { super(testName); }
public void testPlus() throws CalculatorInputException { Calculator calculator = new Calculator(); assertEquals(1 + 3, calculator.calculate("1 plus 3")); }}
public class Calculator {
public int calculate(String expression) throws CalculatorInputException { StringTokenizer tokenizer = new StringTokenizer(expression);
int firstOperand = parseInt(tokenizer.nextToken()); String operator = tokenizer.nextToken().trim(); int secondOperand = parseInt(tokenizer.nextToken());
if ( operator.equalsIgnoreCase("plus") ) return firstOperand + secondOperand; }}
public class Calculator {
public int calculate(String expression) throws CalculatorInputException { StringTokenizer tokenizer = new StringTokenizer(expression);
int firstOperand = parseInt(tokenizer.nextToken()); String operator = tokenizer.nextToken().trim(); int secondOperand = parseInt(tokenizer.nextToken());
if ( operator.equalsIgnoreCase("plus") ) return firstOperand + secondOperand; }}
Copyright © 2001 Systek AS [email protected]
JUnit på 5 minutter – kjøring
Copyright © 2001 Systek AS [email protected]
JBoss start
Copyright © 2001 Systek AS [email protected]
sessionbeans.jar>jar tf sessionbeans.jarMETA-INF/MANIFEST.MFMETA-INF/ejb-jar.xmlno/systek/devSTEP/sessionbeans/Demo.classno/systek/devSTEP/sessionbeans/DemoBean.classno/systek/devSTEP/sessionbeans/DemoHome.classno/systek/devSTEP/sessionbeans/InterestBean.classno/systek/devSTEP/sessionbeans/Interest.classno/systek/devSTEP/sessionbeans/InterestHome.class
public interface Demo extends EJBObject { public String demoSelect() throws RemoteException;}
public interface DemoHome extends EJBHome { public Demo create() throws RemoteException, CreateException;}
public class DemoBean implements SessionBean { public String demoSelect() {return "Hello World"; }
public void ejbCreate() {}
public void ejbActivate() {} public void ejbRemove() {} public void ejbPassivate() {} public void setSessionContext(SessionContext ctx){}}
Copyright © 2001 Systek AS [email protected]
no.systek.devSTEP.sessionbeans.ATestSessionpublic class ATestSession extends TestCase {
public ATestSession(String testCase) { super(testCase);
System.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); System.setProperty("java.naming.provider.url", "localhost:1099"); System.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); }
public void testDemo() throws CreateException, RemoteException, NamingException { InitialContext ctx = new InitialContext(); DemoHome home = (DemoHome)PortableRemoteObject.narrow(ctx.lookup("Demo"), DemoHome.class); Demo demo = home.create(); String answer = demo.demoSelect(); assertEquals(answer, "Hello World"); }}
Copyright © 2001 Systek AS [email protected]
Installasjon av Tomcat
• Last ned og pakk ut Tomcat• Last ned catalina-service.jar og plasser i JBoss’s lib/ext• Rediger konfigurasjon for JBoss
• Legg til Tomcat's classpath i JBoss's conf/default/jboss.conf fil
• Legg til JDK's tools.jar i JBoss's conf/default/jboss.conf fil
• Legg til EmbeddedCatalinaServiceSX i JBoss's conf/default/jboss.jcml fil
• Restart JBoss
Copyright © 2001 Systek AS [email protected]
hellotomcat.war>jar tf build\lib\hellotomcat.warindex.jspMETA-INF/MANIFEST.MFWEB-INF/classes/no/systek/devSTEP/hellotomcat/HelloTomcat.classWEB-INF/classes/no/systek/devSTEP/hellotomcat/HelloTomcat2.classWEB-INF/apps-hellotomcat.xmlWEB-INF/web.xml
public class HelloTomcat extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html><head>"); out.println("<title>My first Servlet</title>"); out.println(”..."); }}
Copyright © 2001 Systek AS [email protected]
Tomcat sikkerhet under Jboss
• Legg til users.properties og roles.properties i JBoss's conf/default
• Deploy tomcatlogin.war• Test med
no.systek.devSTEP.tomcatlogin.ATestServletLogin
• Legg merke til at autentisering under JBoss ikke fungerer 100%, jeg skal forsøke å finne ut hva som er problemet
Copyright © 2001 Systek AS [email protected]
CMP kodepublic class CmpDemoBean implements EntityBean { public Integer id; public String name; public String zip;
public Integer getId() { return id; }
public String getName() { return name; } public void setName(String newName){ this.name = newName; } public String getZipCode() { return zip; } public void setZipCode(String newZip) { this.zip = newZip; }}
<ejb-jar> ... <enterprise-beans> <entity> ... <persistence-type>Container</persistence-type> <prim-key-class>java.lang.Integer</prim-key-class>
<cmp-field><field-name>id</field-name></cmp-field> <cmp-field><field-name>name</field-name></cmp-field> <cmp-field><field-name>zip</field-name></cmp-field> <primkey-field>id</primkey-field> </entity> ...
public interface CmpDemoHome extends EJBHome { public CmpDemo create() throws ...; public CmpDemo findByPrimaryKey(Integer id) throws ...; public Collection findByName(String name) throws ...; public Collection findByZip(String zip) throws ...; public Collection findAll() throws ...;}
public interface CmpDemo extends EJBObject { public Integer getId() throws ...; public String getName() throws ...; public void setName(String newName) throws ...; public String getZipCode() throws ...; public void setZipCode(String newZip) throws ...;}
Copyright © 2001 Systek AS [email protected]
Container Managed Persistance
>jar tf cmpexample.jarMETA-INF/MANIFEST.MFMETA-INF/ejb-jar.xmlno/systek/devSTEP/cmpexample/CmpDemoBean.classno/systek/devSTEP/cmpexample/CmpDemo.classno/systek/devSTEP/cmpexample/CmpDemoHome.class
Copyright © 2001 Systek AS [email protected]
MySQL
• Last ned mysql-3.24.44-win.zip og kjør setup.exe • Last ned JDBC-driveren og legg den til i JBoss's lib/ext
katalog • Start MySQL service under Windows: net start mysql • Legg til databasen 'step' med brukeren 'step' og passord
'step-by-step' i MySQL • Sett opp JBoss
• Endre datakilde i jboss.jcml: Driver (JdbcProvider), URL, JDBCUser og Password
• Bruk type-mapping "mySQL" i standardjaws.xml • Restart Jboss
Copyright © 2001 Systek AS [email protected]
MySQL eksempel
Copyright © 2001 Systek AS [email protected]
Bean Managed Persistance
public Integer ejbCreate(String name, String zip) { this.name = name; this.zip = zip;
Connection conn = datasource.getConnection();
String idQuery = "SELECT Max(id)+1 FROM BmpDemoBean"; ResultSet rs = conn.createStatement().executeQuery(idQuery); rs.next(); this.id = new Integer(rs.getInt(1));
String query = "INSERT INTO BmpDemoBean (ID, NAME, ZIP) VALUES (?, ?, ?)"; PreparedStatement stmt = conn.prepareStatement(query);
stmt.setInt(1, id.intValue()); stmt.setString(2, this.name); stmt.setString(3, this.zip); stmt.execute();
conn.close(); return id;}
public Collection ejbFindByName(String name) { Connection conn = datasource.getConnection();
String query = "SELECT id FROM BmpDemoBean WHERE NAME = ?"; PreparedStatement stmt = conn.prepareStatement(query); stmt.setString(1, name); ResultSet rs = stmt.executeQuery();
Collection result = new ArrayList(); while ( rs.next() ) { result.add(new Integer(rs.getInt(1))); }
conn.close(); return result;}
public void ejbStore() { Connection conn = datasource.getConnection();
String query = "UPDATE BmpDemoBean SET name = ?, zip = ? " + "WHERE id = ?"; PreparedStatement stmt = conn.prepareStatement(query); stmt.setString(1, this.name); stmt.setString(2, this.zip); stmt.setInt(3, this.id.intValue()); stmt.execute();
conn.close();}
CREATE TABLE BmpDemoBean ( id INTEGER NOT NULL PRIMARY KEY, name VARCHAR (32), zip VARCHAR(5));
Copyright © 2001 Systek AS [email protected]
Ant acceptance