210 likes | 340 Views
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
E N D
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 • JBoss – EJB-container • Tomcat – Servlet-container • Inkludert i prosjektet: • Ant – byggeverktøy • JUnit – testverktøy – HTTPUnit
Client Presentation Logic Integration Session Bean Java Server Pages Internet Explorer Tomcat 4.0.1 JBoss 2.4.3 JDBC Entity Bean XML Servlet Message Driven B Template, Etc. J2EEs fem-lags arkitektur Resource Servlet Container MySQL 3.23.44 Web- Browser Relational Database JDBC EJB Container Windows Client CORBA OO Database XML Third Party System Java Swing Client JMS
Oversikt over utviklingsprosessen • Skriv kode i valgfri editor • Bygg med Ant • Test med JUnit • Deploy i JBoss
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>
ant j2ee-build C:\java\projects\devSTEP>ant j2ee-build Buildfile: build.xml prepare: compile: [mkdir] Created dir: C:\java\projects\devSTEP\build\classes [javac] Compiling 17 source files to C:\java\projects\devSTEP\build\classes ejb: [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 files login-war: [war] Building war: C:\java\projects\devSTEP\build\lib\tomcatlogin.war hello-war: [war] Building war: C:\java\projects\devSTEP\build\lib\hellotomcat.war j2ee-build: BUILD SUCCESSFUL Total time: 6 seconds
JUnit på 5 minutter 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; } }
sessionbeans.jar >jar tf sessionbeans.jar META-INF/MANIFEST.MF META-INF/ejb-jar.xml no/systek/devSTEP/sessionbeans/Demo.class no/systek/devSTEP/sessionbeans/DemoBean.class no/systek/devSTEP/sessionbeans/DemoHome.class no/systek/devSTEP/sessionbeans/InterestBean.class no/systek/devSTEP/sessionbeans/Interest.class no/systek/devSTEP/sessionbeans/InterestHome.class public interface DemoHome extends EJBHome { public Democreate() 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){} } public interfaceDemo extends EJBObject { public String demoSelect() throws RemoteException; }
no.systek.devSTEP.sessionbeans.ATestSession public 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"); } }
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
hellotomcat.war >jar tf build\lib\hellotomcat.war index.jsp META-INF/MANIFEST.MF WEB-INF/classes/no/systek/devSTEP/hellotomcat/HelloTomcat.class WEB-INF/classes/no/systek/devSTEP/hellotomcat/HelloTomcat2.class WEB-INF/apps-hellotomcat.xml WEB-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(”..."); } }
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
CMP kode <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 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; } } 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 ...; }
Container Managed Persistance >jar tf cmpexample.jar META-INF/MANIFEST.MF META-INF/ejb-jar.xml no/systek/devSTEP/cmpexample/CmpDemoBean.class no/systek/devSTEP/cmpexample/CmpDemo.class no/systek/devSTEP/cmpexample/CmpDemoHome.class
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
Bean Managed Persistance CREATE TABLE BmpDemoBean ( id INTEGER NOT NULL PRIMARY KEY, name VARCHAR (32), zip VARCHAR(5) ); 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 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 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(); }