Develop Better Java EE Applications With Eclipse Web Tools Platform

8 downloads 292074 Views 7MB Size Report
www.eclipse.org/webtools/. • Included in Ganymede and Europa release. • Current version 3.0.2. • Development tools for web and Java EE development.
T-4 - Develop Better Java EE Applications With Eclipse Web Tools Platform Christopher M. Judd President/Consultant Judd Solutions, LLC

Christopher M. Judd • President/Consultant of Judd Solutions • Leader • Creator Open Source projects FallME and Fiddle

Agenda • Java EE/J2EE Landscape • WTP Overview – WST – JST

• • • • •

EJBs Web Packaging Web Services Conclusion

J2EE Development is Complicated!!!

But Java EE and the Eclipse Web Tools Platform is simplifying enterprise development!!!

J2EE/Java EE Architecture

Current Java EE Tool Landscape • Based on Web Tools Platform – – – –

Eclipse Ganymede JEE IBM Rational Application Developer CodeGear JBuilder Genuitec MyEclipse

• NetBeans • JetBrains IntelliJ IDEA

Managing Complexity • Hand code classes, interfaces deployment descriptors • Diagrams

• Doclet Annotations

/** * A generated session bean * @ejb.bean name="TransService" * description="A session bean named TransService" * display-name="TransService" * jndi-name="TransService" * type="Stateless" * transaction-type="Container" */ public abstract class TransServiceBean implements javax.ejb.SessionBean {

• Java 5 Annotations

@Stateless public class TicketServiceImpl implements TicketService {

Agenda • J2EE/Java EE Landscape • WTP Overview – WST – JST

• • • • •

EJBs Web Packaging Web Services Conclusion

Web Tools Platform Project (WTP) • • • • •

Top Level Eclipse Project www.eclipse.org/webtools/ Included in Ganymede and Europa release Current version 3.0.2 Development tools for web and Java EE development – No runtime dependencies – Vendor extensible

• Dependencies – – – – –

Java 5 SDK or higher Eclipse 3.4.1 EMF 2.4.1 GEF 3.4.1 DTP 1.6.1

WTP Subprojects • Web Standard Tools (WST) – Web artifacts – Defined by open standards bodies

• J2EE Standard Tools (JST) – J2EE components – Java Community Process (JCP) – Depends on WST

• Dali – EJB 3.0 Java Persistance API (JPA) support

• JavaServer Faces Tools (JSF) • AJAX Toolkit Framework (ATF)

WTP Scope

WTP Installation Options • Ganymede • All-in-one – – – –

Eclipse 3.4.1 Eclipse Modeling Framework (EMF) 2.4.1 Graphic Editor Framework (GEF) 3.4.1 Data Tools Platform (DTP) 1.6.1

• Update Manager – Eclipse.org update site

• Piecemeal * ATF is a separate install

WTP Vendor Support • • • • • • • •

IBM Oracle/BEA CodeGear RedHat/JBoss ObjectWeb Exadel Genuitec Versant

Agenda • J2EE/Java EE Landscape • WTP Overview – WST – JST

• • • • •

EJBs Web Packaging Web Services Conclusion

WST Scope • World Wide Web Consortium (W3C) – http://www.w3.org – HTML, XHTML, CSS, XML, XSLT, XML Schema, XML Query

• European Computer Manufactures Association (ECMA) – http://www.ecma-international.org – JavaScript and C#

• International Engineering Task Force (IETF) – http://www.ietf.org – TCP/IP

• American National Standards Institute (ANSI) – http://www.ansi.org – SQL, C/C++

• Organizations for Advancement of Structured Information Standards (OASIS) – http://www.oasis-open.org – e-Business standards for web services

• Web Services Interoperability Organizations (WS-I) – http://www.ws-i.org – Interoperable message exchange between web services

TCP/IP Monitor • Window > Preferences > Run/Debug > TCP/IP Monitor

• Window > Show View > Other > Debug > TCP/IP Monitor

Structured Source Editors • Web and XML artifacts – HTML, JSP, CSS, XML, XSD, DTD

• Features – – – – – – –

Syntax Highlighting Content Assist Error Highlighting Delimiter Matching Source Formatting Inline editing of languages Templates

XML Editor

XML Schema Editor • Graphically or via source edit XML Schema files

Web Service Tools • WSDL Editor • Java to WSDL and WSDL to Java wizards

Test Web Service • Web Service Explorer can be used to test out Web Services

Agenda • J2EE/Java EE Landscape • WTP Overview – WST – JST

• • • • •

EJBs Web Packaging Web Services Conclusion

JST Scope J2EE 1.4 support • • • • • • • • •

EJB 2.1 Servlet 2.4 JSP 2.0 JMS 1.1 JTA 1.0 JavaMail 1.3 JAF 1.0 JAXP 1.2 Connector 1.5

• • • • • • • •

Web Services 1.1 JAX-RPC 1.1 SAAJ 1.2 JAXR 1.0 J2EE Management 1.0 JMX 1.2 J2EE Deployment 1.1 JACC 1.0

J2EE 1.4 XDoclet Annotations • Plug-able annotations platform • XDoclet – Open Source – Attribute Oriented Programming (AOP) framework – xdoclet.sourceforge.net

• Generates – – – –

Bean Interfaces Bean Util classes EJB Deployment Descriptors Web Deployment Descriptors

JST Scope Java EE 5 support • • • • • • • • • • •

EJB 3.0 JPA JSR-220 Servlet 2.5 JSP 2.1 JSF 1.2 JMS 1.1 JTA 1.1 JavaMail 1.4.1 JAF 1.1 Connector 1.5 Common Annotations

• • • • • • • •

JAX-WS 2.0 JAX-RPC 1.1 JAXB 2.0 SAAJ 1.1.2 SAAJ 1.2 J2EE Management J2EE Deployment JACC

JST Scope • • • •

JSR 45 – Debugging Support JSR 175 – Metadata (annotations) JSR 181 – Metadata for Web Services Integration with popular open source application servers

JST Projects • Projects Types – – – – –

Enterprise Application Project (ear) EJB Project (ejb-jar) Dynamic Web Project (war) Application Client Project (jar) Connector Project (rar)

JST Projects • Project Layouts – Project per module

Enterprise Application Project (ear)

Application Client Project (jar) EJB Project (ejb-jar)

Dynamic Web Project (war)

Java EE Perspective

Web Perspective

JPA Perspective

Infrastructure

• Configure workspace • Application server • Data source

Workspace Setup • Configure application server runtime • Configure database (really DTP)

Supported Servers • Apache Tomcat – 3.2 – 4.1 – 5.5

• • • • •

- 4.0 - 5.0 - 6.0

Apache Geronimo 1.0 IBM WebSphere 6.0.x JBoss 3.2.x, 4.2, 4.0, 5.0 JOnAS 4.x Oracle OC4J Standalone 10.1.3

Download Additional Server Adapters • • • • •

Pramati Server Apache Geronimo GlassFish Jetty IBM WebSphere Application Server Community Edition • IBM WebSphere • Oracle WebLogic

Defining Server 1. 2. 3. 4.

Configure server appropriate JDK Configure server runtime Configure server Test server

Configure JDK • Window > Preferences > Java > Installed JREs

Note: You may have to add tools.jar for JSP support

Configure Server Runtime • Window > Preferences > Server > Installed Runtimes

Configure Server Use Server View in J2EE Perspective 1. Add server (New > Server)

Test Server 1. Start server

2. Verify server is up

Server View Usability Tip • Move Server View above Console view – Easier application server startup – Easier deployment

Workspace Setup • Configure application server runtime • Configure database (really DTP)

Define Driver • Window > Preferences > Data Management > Connectivity > Driver Definitions • Many driver templates for common databases

Define Driver • Select driver template

Define Driver • Configure driver classpath & properties

Database Development Perspective

Creating Connection Profile • New Connection Profile button on Data Source Explorer

Creating Connection Profile • Select Profile Type • Provide a descriptive name • Optionally provide a description

Creating Connection Profile • Configure driver details and test connection

Data Source Explorer • View database elements • Context menus – – – – –

Connect Filter Generate DDL Create Tables Data

• Import/Export Connection Profiles

Create Enterprise Application Project • New > Project > Java EE > Enterprise Application Project – Add EJB and Web modules – Select Application Server

Projects • Tour > EAR • TourEJB > EJB • TourWeb > WAR

Lab 1 1. Install Ganymede (if not installed already) 2. Install Glassfish V2 UR2 –

Instructions - https://glassfish.dev.java.net/downloads/v2ur2-b04.html

3. Configure Glassfish Server 4. Install Derby –

Unzip db-derby-10.4.2.0-bin.zip

5. Start Derby –

/bin/startNetworkServer.bat

6. Configure Derby driver 7. Configure Derby Connection Profile 8. Create new EAR, EJB and Web projects

Agenda • J2EE/Java EE Landscape • WTP Overview – WST – JST

• • • • •

EJBs Web Packaging Web Services Conclusion

EJB Types • Session – Server-side business logic – Stateful – Stateless

• Entity – Persistable business objects – CMP – BMP

• Message-Driven – Consumers of JMS messages

EJB 2.x Files • Session – – – – – – –

Remote Interface Home Interface Local Interface Local Home Interface Bean Class EJB Deployment Descriptor App Server Specific EJB Deployment Descriptor

EJB 2.x Files • Entity – – – – – – – –

Remote Interface Home Interface Local Interface Local Home Interface Bean Class EJB Deployment Descriptor App Server Specific EJB Deployment Descriptor App Server Specific CMP Deployment Descriptor

EJB 2.x Files • Message-Driven Bean – Bean Class – EJB Deployment Descriptor – App Server Specific EJB Deployment Descriptor

EJB 2.x Problems • • • •

Too much work Too much to know Too much XML Too many patterns – DTO – Locator – Service

• Difficult to test • Breaks object oriented principals

EJB 3.0 Solution • • • • •

Plain Old Java Objects (POJO) Annotations over XML Java Persistence API (JPA) Dependency Injection Fewer files

Create Entity Bean Process 1. 2. 3. 4.

Enable JPA on EJB project Generate Entity Bean Configure application server data source Configure JNDI in application

Enable JPA on EJB project • Enable Java Persistence 1.0 – EJB Project Properties > Project Facets

• Configure JPA – EJB Project Properties > JPA – Discover annotated classes automatically

Generate Entity Bean • Right-click on EJB project > JPA Tools > Generate Entities • Generate from existing tables in database – Select Database connection – Synchronize Classes in persistence.xml – Select tables

Entity Bean Source @Entity public class Countries implements Serializable { @Id @Column(name="COUNTRY_ISO_CODE") private String countryIsoCode; private String country; private String region; @OneToMany(mappedBy="countryIsoCode") private Set citiesCollection; private static final long serialVersionUID = 1L; public Countries() { super(); } public String getCountryIsoCode() { return this.countryIsoCode; } public void setCountryIsoCode(String countryIsoCode) { this.countryIsoCode = countryIsoCode; } public Set getCitiesCollection() { return this.citiesCollection; } public void setCitiesCollection(Set citiesCollection) { this.citiesCollection = citiesCollection; } //… Other properties removed for simplification }

Entity Bean Support • Content Assist (Ctrl + Space) for Annotations • JPA Details view

Configure App Server Data Source • Application server and database specific

Configure JNDI in Application • ejbModule\META-INF\persistence.xml – Add transaction type – Add JTA data source

jdbc/TourDataSource com.juddsolutions.entities.Airlines com.juddsolutions.entities.Cities com.juddsolutions.entities.Countries com.juddsolutions.entities.Flights com.juddsolutions.entities.FlightsPK

Create Session Bean Process 1. Create Session Interface 2. Create Session Class – –

Inject EntityManager Implement Session Bean

Create Session Interface • Standard Java Interface • Optional Local or Remote (defaults to Local)

public interface FlightService { List findAllFlights(); }

Create Session Class • Standard Java Class • Stateful or Stateless annotation • Inject EntityManager and/or other Session Beans @Stateless public class FlightServiceImpl implements FlightService { @PersistenceContext EntityManager entityManager; @SuppressWarnings("unchecked") @Override public List findAllFlights() { return (List) entityManager.createQuery( "SELECT f FROM Flight f").getResultList(); } }

Lab 2 1. Enable JPA on EJB project 2. Generate Flights Entity Bean 3. Configure Glassfish JNDI – –

Create connection pool Create JDBC resource

4. Configure application datasource 5. Create FlightService Session Bean – –

Create FlightService interface Create FlightService implementation

Agenda • J2EE/Java EE Landscape • WTP Overview – WST – JST

• • • • •

EJBs Web Packaging Web Services Conclusion

Web Artifact Support • JavaServer Faces (JSF) • JavaServer Pages (JSP) – Tag Libraries

• Servlets – Filters – Listeners

• HTML • Cascading Style Sheets (CSS) • JavaScript

Enable EJB J2EE Module • Provides access to EJB classes • Web Project Properties > Java EE Module Dependencies

Enable JavaServer Faces • Web Project Properties > Project Facets

Create JSP

Insert title here

Design Page • Add JSF Core and HTML components from Palette

Create Managed Beans • Using faces-config.xml editor

Implement Managed Bean public class FlightBean { @EJB private FlightService service; private String airportCode; private List flights; public void setAirportCode(String airportCode) { this.airportCode = airportCode; } public String getAirportCode() { return airportCode; } public List getFlights() { return flights; } public String find() { flights = service.findAllFlightsByDepartureAirport(airportCode); return "success"; } }

Implement Navigation • Using faces-config.xml editor

Deploy • Initial Deployment - Right click on server and Add and Remove Projects • Redeploys – Right click on server and Publish

Test • http://localhost:8080/TourWeb/faces/

Debugging • Just start server in debug mode and debug like standard Java application

Lab 3 1. Create a JSP page. 2. Design Interface for searching by airport and displaying data in DataTable. 3. Create and configure managed bean. 4. Create navigation mapping. 5. Deploy and test.

Agenda • J2EE/Java EE Landscape • WTP Overview – WST – JST

• • • • •

EJBs Web Packaging Web Services Conclusion

Packaging • Export > Java EE > EAR file • Deploy to standard Java EE App Server

Deployment • • • • •

Container specific Admin console Deployment directory Ant task Server View

Agenda • J2EE/Java EE Landscape • WTP Overview – WST – JST

• • • • •

EJBs Web Packaging Web Services Conclusion

Web Services • • • •

Web Services Overview Consuming Producing Web Services (bottom up) Testing

Simple Object Access Protocol From the draft W3C specification: SOAP is a lightweight protocol for exchange of information in a decentralized, distributed environment. It is an XML based protocol that consists of three parts: an envelope that defines a framework for describing what is in a message and how to process it, a set of encoding rules for expressing instances of application-defined datatypes, and a convention for representing remote procedure calls and responses. http://www.w3.org/TR/soap/

WSDL • Web Service Definition Language • Describes – What the service can do – Where it resides – How to invoke it

• Elements – – – – – –

Types – data type definition Message – definition of data being communicated Port Type – abstract set of operations Binding – concrete protocol and data format Service – collection of related endpoints Port – binding and a network address

Web Service Implementations • Apache Axis – – – – –

Apache Web Services Project Open Source SOAP implementation Version 1.4 http://ws.apache.org/axis/

• Apache Axis 2 – – – – –

Pull Parser Multiple Binding Frameworks SOAP, REST, Async Version 1.3 http://ws.apache.org/axis2/

Weather Forecast Web Service • Forecasting weather by zip code – – – –

7 day forecast Place name State code Latitude and Longitude

• Found at X Methods (www.xmethods.net) • Service description – www.webservicex.net/WeatherForec ast.asmx

Weather Forecast Web Service Service description

Data transfer objects

Consuming Web Services Strategy A common anti-pattern is to generate the web service client stub code directly into an application. Tip: Generate client code in its own project and export it as a jar • • • •

Less files to maintain Reuse web service client code Generated code does not follow code quality standards Can always regenerate

Consuming Web Services Steps (Axis) 1. 2. 3. 4.

Create Java Project Download WSDL Generate Web Service Client from WSDL Export client Jar

Create Project • Create a standard Java Project • File > New > Project… > Java Project

Download WSDL • • • •

Download WSDL Add WSDL to project Include it in the exported Jar file Provides traceability incase there is a need to know which WSDL version was used to generate stubs and Jar

Generate Web Service Client • Right click on WSDL and choose Web Services > Generate Client or File > New > Web Services > Web Service Client • Specify Java Proxy • Monitor traffic using TCP/IP Monintor

Generate Web Service Client • Specify project to generate the code in

Generate Web Service Client • Optionally, can specify namespace to package mapping • Convert targetNamespace to a more conventional Java package naming convention.

Output • Classes – – – – – –

Service interface Dynamic proxy service impl Stub service impl Service locator interface Service locator impl Data transfer objects

• Axis Jars – – – – – –

axis.jar jaxrpc.jar saaj.jar wsdl4j-1.5.1.jar commons-discovery-0.2.jar commons-logging-1.0.4.jar

* When using the client Jar, these Axis jars will be required in the classpath.

Using Generate Classes // Get service locator

WeatherForecastLocator forcastLocator = new WeatherForecastLocator(); // Get SOAP service

WeatherForecastSoap wfSoap = forcastLocator.getWeatherForecastSoap(); // Call remote web service

WeatherForecasts forecasts = wfSoap.getWeatherByZipCode(zipCode); WeatherData[] details = forecasts.getDetails(); System.out.println("\nWeather information for " + forecasts.getPlaceName()); System.out.println(forecasts.getLatitude()); System.out.println(forecasts.getLongitude()); for (int i = 0; i < details.length; i++) { WeatherData detail = details[i]; System.out.println(detail.getDay()); System.out.println(detail.getWeatherImage()); System.out.println(detail.getMaxTemperatureF()); }

Consuming Web Services Steps (Axis2) 1. 2. 3. 4.

Configure Axis2 Runtime Create Dynamic Project with Axis2 Facet Download WSDL Generate Web Service Client from WSDL

Configure Axis2 Runtime • Configurable to support multiple Axis2 versions

Create Dynamic Project • Create a Dynamic Web Project – File > New > Project… > Dynamic Project – Enable Axis2 Facets

Download WSDL • • • •

Download WSDL Add WSDL to project Include it in the exported Jar file Provides traceability incase there is a need to know which WSDL version was used to generate stubs and Jar

Generate Web Service Client • Right click on WSDL and choose Web Services > Generate Client or File > New > Web Services > Web Service Client • Change Web service Runtime

Generate Web Service Client • Select port – SOAP – GET – POST

• Databinding – ADB (Axis2 Databinding Framework)

• Standardize package name • Identify synchronous and/or asynchronous calls

Output • Classes – Stub – CallbackHandler

• Added Axis2 Jars to WebContent/WEBINF/lib

* When using the client Jar, Axis2 jars from WEB-INF/lib will be required in the classpath.

Using Generate Classes // Create client stub

WeatherForecastStub stub = new WeatherForecastStub(); // Setup parameter data

GetWeatherByZipCode zipCode = new GetWeatherByZipCode(); zipCode.setZipCode("90210"); // Call remote method

GetWeatherByZipCodeResponse response = stub.GetWeatherByZipCode(zipCode); WeatherForecasts forecasts = response.getGetWeatherByZipCodeResult(); WeatherData[] details = forecasts.getDetails().getWeatherData(); System.out.println("\nWeather information for " + forecasts.getPlaceName()); System.out.println("Latitude: " + forecasts.getLatitude()); System.out.println("Longitude: " + forecasts.getLongitude()); for (int i = 0; i < details.length; i++) { WeatherData detail = details[i]; System.out.println(detail.getDay()); System.out.println(detail.getWeatherImage()); System.out.println(detail.getMaxTemperatureF()); }

Producing Web Services • Bottom up – Java code -> Generate WSDL

• Top down – WSDL -> Generate Java code

Bottom Up Steps 1. Create Dynamic Web Project 2. Create Service and DTOs 3. Generate Web Service

Create Dynamic Web Project • File > New > Other > Web > Dynamic Web Project • Project Name • Target server

Create Dynamic Web Project • Axis2 Web Services Facet

Create Dynamic Web Project • Context Root • Directories

Create Service public class OrderService { private static final float DISCOUNT = 0.9f; public float quote(float price, int quantity) { if (quantity > 100) { price = price * DISCOUNT; } return price * quantity; } }

Generate Web Service • New > File > Other > Web Services > Web Service or Right click on Class: Web Services > Create Web Services • Change Web service runtime to Apache Axis2 • Select the amount to be generated • Monitor using TCP/IP Monitor

Generate Web Service • Create or auto generate a Axis2 deployment descriptor

Generate Web Service • Start server • Generate EAR • Deploy to local application server

Output • Web Service – Axis deployment descriptors – WSDL

• Admin Client

Output • http://localhost:8080//axis2-web

WSDL • http://localhost:8080//services/?wsdl

Data Types • Simple Java data types • JavaBeans – No parameter constructor

• Arrays

TCP/IP Monitoring • View SOAP request and response

Web Service Explorer Testing • Test any Web Services

WSDL Editor • Graphically edit WSDL

Agenda • J2EE/Java EE Landscape • WTP Overview – WST – JST

• • • • •

EJBs Web Packaging Web Services Conclusion

Conclusion

WTP not only has the tools necessary for developing, deploying and testing J2EE and Java EE applications, it is the foundation of many of today’s leading enterprise development tools and has wide industry support.

Contact Information • • • •

Website: http://www.juddsolutions.com Email: [email protected] Blog: http://juddsolutions.blogspot.com/ Beginning Groovy and Grails – http://www.apress.com/book/view/1430210451

• Pro Eclipse JST – http://www.apress.com/book/bookDisplay.html?bID=447

• Enterprise Java Development on a Budget – http://www.apress.com/book/bookDisplay.html?bID=197

Questions ? Please fill out your evaluations.

Slides can be found at http://www.juddsolutions.com/ew2008/

Suggest Documents