Enterprise Architecture Java SE / Java EE Training Awareness M. Reha, Enterprise Architecture 2009-04-10, v0.1 Confidential and Proprietary AAA NCNU © 2008, 2009 > > > > Agenda Course #1: • Introduction to the Java Language and the Java Platform Course #2: • • “Hello World” Java programming example Encapsulation, Inheritance, Interfaces Course #3: • Introduction to the Java EE Platform Course #4: • • “Hello World” Java EE web application programming example Web Tier, Business/Services Tier, Persistence Tier 2 Course #1 Introduction to the Java Language and the Java Platform 3 Course Objectives > Learn about the history of Java. > Learn about the Java Platform. > Learn what the fundamentals of the Java language. 4 What is Java? > > > > > > > > > > Is Java just a programming language? What is the JDK? What is Java Standard Edition (Java SE)? What is Java Enterprise Edition (Java EE)? What is the JVM? What is a Java EE Application Server? What is a Java Applet? What is Java Swing? What is a Java Portlet What else? • • • • • • Java ME – Java Mobile Edition (for mobile phones / devices) Java RT – Java Real Time (for embedded real time applications) Java TV – Java for TV (for TV and Set Top Box applications) Java DB – Java based RDBMS Java Card – Java for Smart Cards Java FX – Java building next generation RIA’s 5 What is Java? Java is a programming language! Java used as a platform to build applications ranging from web, desktop, mobile, and more! JDK (for SE and EE) Compilers, tools, documentation for the developer Java EE Runtime Implements the Java EE API’s Java SE Runtime Also referred to as JRE Implements Java SE API’s Implements Java SE for CDC Browser Web Pages Portlets Java Applet JavaFX Applet Java Desktop Application (Swing, Console, JavaFX) Mobile Application and Consumer (Java ME, JavaFX, JavaTV) Java EE Application Server WebSphere, Oracle WebLogic, JBoss, etc Java Virtual Machine (JVM) Java Virtual Machine (JVM) Sun, IBM, Oracle, Apple, etc. Nokia, Philips, Sony, 6 Introduction to the Java Programming Language > > > > > Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to byte-code that can run on any Java virtual machine (JVM) regardless of computer architecture. The original and reference implementation Java compilers, virtual machines, and class libraries were developed by Sun from 1995. As of May 2007, in compliance with the specifications of the Java Community Process, Sun made available most of their Java technologies as free software under the GNU General Public License. 7 History of the Java Platform Java SE Platform: • JDK 1.0 released in January 1996 • JDK 1.1 released in February 1997 • J2SE 1.2 released in January 1998 • J2SE 1.3 released in May 2000 • J2SE 1.4 released in February 2002 • J2SE 5.0 released in September 2004 • J2SE 6.0 released in December 2006 • J2SE 7.0 released in end of 2010 We are here in 2002! We have some ability to leverage J2SE 5.0 Java RI will be based on J2SE5.0 Java EE Platform: • Java Platform Edition JPE announced in May 1998 • J2EE 1.2 released in December 1999 (peak of the .COM era) • J2EE 1.3 released in September 2001 (end of .COM era) We are here in 2003! • J2EE 1.4 released in November 2003 Java RI will be based on EE 5 • EE 5 released in May 2006 • EE 6 scheduled release for the end of 2008 (approval of JCP specification) Lots of enterprises are still on J2EE 1.3 from 2002! The Portlet Specification was not released until October 2003. 8 > > > > Java Language – Language Basics Java is a strongly typed language. This means all variables must be declared with a type before using. • Example: int count = 0 • int is the type for a variable called count that is initialized to 0 Java Primitives are defined by the Java Language (and are reserved keywords) and are very similar in syntax to the C/C++ programming language: • byte, short, int, long, float, double, boolean, char • There are wrapper classes for most primitive types (Integer class wraps an int) Java Operators are special symbols that perform specific operations on one or more operands (much like the C/C++ language): • ++ --, * /, >> <<, == !=, < >, & | ^, && || Java Control Flow statements for program control and decision making and are very similar to to the C/C++ programming language: • if else, switch, while, do while, for • exception handling (not really flow control!) 9 > > > > Java Language – Classes and Objects Java was the first Internet “aware” and Security “aware” object orientated programming language. (Almost) everything declared in Java defined by a Object implemented in a Class. One of the first main stream object oriented languages. • Object: • An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life. • Real-world objects share two characteristics: They all have state and behavior. • Class: • A class is a blueprint or prototype from which objects are created. • Created by using the new Java keyword. Example: ClassA a = new ClassA(); • Unused classes are removed from memory via the Garbage Collector • There is (in theory) no memory management coding required by programmer Classes can inherit state and behavior from other classes through Inheritance. • ClassA extends ClassB Classes can define contracts with the outside world (or other classes) through interfaces. • ClassA implements ClassB 10 Java Language – Runtime Library > A library of classes that are included as part of the Java Standard Edition which are implemented in ALL Java Runtime distributions (IBM, Oracle/JRockit, Sun, etc.). > > Utility: I/O, String, Date, Time, Calendar, Internationalization, Math, Collection, … Networking: HTTP, Cookies, TCP URL and Sockets, and UDP Datagrams, … Database: JDBC, Prepared Statements, ResultSets, Transactions, … Component Model: JavaBean, … Multimedia: Sound, 2D Graphics, 3D Graphics (extension lib), … User Interface: AWT, Swing, Applet, … Web Services: XML, SOAP Misc: JNI, JMX, RMI, Serialization, Logging, Zip, Regular Expressions, … System: Threads, Concurrency (Locks, Mutex, etc), Security, JNDI, … Deployment: Java Web Start, Java Plug-In, … > These are all documented in the Java Development Kit (JDK)! > > > > > > > > 11 Java SE 6 – JDK and JRE Image Courtesy of Sun http://java.sun.com/javase/6/docs/ 12 Course #2 Example Java SE Programming 13 Course Objectives > Learn what a simple Java class looks like. > Learn about Encapsulation. > Learn about Inheritance and Interfaces. > Walk through a “Hello World” console application. 14 “Hello World” in the Java Programming Language Name of Object In Java == ObjectName.java Also known as a Class HelloWorld Object private String message; Data / State private int count; Behavior public sayHello(); / Operations The Objects internal data, attributes, or object state The Objects behavior, methods, or operations 15 “Hello World” in the Java Programming Language 16 More “Hello World” in the Java Programming Language Extends the behavior of HelloWorld HelloWorld BaseHello private String message; public sayLoudHellIo(); private someUtility(); public sayHello(); private setText(); 17 More “Hello World” in the Java Programming Language 18 Course #3 Introduction to Java EE 19 Course Objectives > Learn about the history of the Java EE Platform. > Learn about basic technologies in the Java EE Platform. > Learn about current trends of the Java EE Platform. 20 What is Enterprise Java a.k.a. Java EE? Java EE defines standard API’s for building web applications. > Introduced right before the .COM era started. > The Java EE Standard is defined by Sun and the JCP. > There have been lots of contributions from the Open Source Community to fill in gaps from the Java EE Standard and enrich the capabilities for building web applications. > A Java EE Application Server implements the Java EE Standard and provides a platform to execute web applications. Sometimes the Application Server vendor “enhances” the platform by adding value added features and capabilities such as Administration Consoles, Debugging facilities, and Monitoring. > A Java EE Application Server is rapidly becoming a commodity with a number of very robust and scalable open source alternatives now available on the market. > 21 > > > > Introduction to the Java EE Stack? Web Tier Technologies: • • • • Servlet – lowest level (above protocols and sockets) to handle HTTP request Java Server Pages – markup (like HTML tags) to build dynamic pages Java Standard Template Library – standard tags for conditional, loops, etc. Java Server Faces – web framework (built from JSP) Business/Service Tier Technologies: • • Enterprise Java Bean EJB – business components JTA – transaction API Persistence Tier Technologies: • • JDBC – lowest level database programming Java Persistence API JPA – Object Relational Mapping framework Integration Technologies: • • • Java Connector Architecture JCA – API to access legacy systems (like SAP) Java Messaging Service JMX – send JMS messages (like MQ) Web Services JAX-WS, JAX-B, JAX-R – web service stack support 22 J2EE Platform from post .COM era (2002-2004) Client (mostly browser based) Utilities and Core Services Logging (Wrapper) Tracing (Wrapper) Exception Framework Base Classes/Frameworks Alert (like HP Open View) Cache (Wrapper) Static Data Security/SSO * Web Application Application Server Governance Standards, Best Practices/Guidelines Architecture Review Boards etc. Struts 1.x (MVC) JSTL (Tag Library) MyFaces/Sun JSF RI Apache Commons (Utility) Apache Log4j (Logging) Hibernate(Persistence) iBatis (Persistence) iText (PDF) POE (MS Docs) Quartz (Timer Service) Castor (XML Framework) Apache Xerces/Xalan (XML) Apache Axis (Web Services) SSO OSCache/EHCache (Cache) * EJB Web Session Entity MDB JSP Servlet JCA JTA Mail JMS JAAS JMX JAXB Integration/Middleware Business Rule Engine ETL Messaging/MQ FTP Web Services Proprietary Scripts etc. Screen Scraping * J2SE 1.3 – 1.4 JNI RMI JNDI JDBC JavaBean Java 3D Java 2D Swing AWT MVC DAO Command Factory Business Delegate Business Façade Decorator Value Object * * Open Source J2EE 1.3 – 1.4 JAX-R Design Patterns Containers and Services for UI, Business, Database Security Administration and Deployment Value Add Services (Proprietary Frameworks etc.) JAX-RPC SDLC and Development Tools XP, Scrum, RUP, Waterfall Eclipse, IBM WSAD/RAD, NetBeans, JBuilder, IntelliJ Code Analyzers (Checkstyle, FindBugs), Unit Test Frameworks (JUnit, TestNG) UI: HTML, CSS/DHTML, JavaScript, AJAX, Applets, Flash Application Logic, Business Logic, Data Access Logic Enterprise Application Integration (EAI) 23 > > > > > > > > > > > J2EE Platform Observations from 2002-2004 Leveraged lots of open source libraries to fill in the J2EE specification gaps (like Web MVC Framework, XML, Web Services). Soon there would be competing and redundant technologies such as XML, Web Services, Logging, etc.. The Enterprise and Application Architect definitely had their work cut out for them. What technologies do we use? Some J2EE specifications were of little value to the enterprise (for example, Entity Beans (CMP or BMP) and Stateful EJB’s…..J2EE 1.2 only supported remote Session Beans!). Enterprise Integration was tightly coupled and reuse of enterprise assets not fully thought out or realized. Application Servers often provided proprietary (and competing) technologies and frameworks (Portlets, Web, Security, etc.). Lots of programming models to learn. Governance was often over looked causing lots of inconsistencies in architecture and duplication of code/frameworks. Most development methodologies were still very “water fall”. XP was just taking off. Development Tools needed improving. Generally there was very high TCO for 1st generation (MVC-1) and 2nd generation (MVC-2) applications. De-facto Standard Application Servers: WebLogic, WebSphere, and some Oracle. Increasing frustration with J2EE standard (some of it was justified and some was not). 24 J2EE Web 1.5/2.0 Application Architecture (2005-present) Client (not just browser based anymore) Web Application Struts2 Framework Open Source Struts2 (MVC) Apache Commons (Utility) iBatis (Persistence) iText (PDF) POE (MS Docs) Quartz (Timer Service) Apache Axis (Web Services) OSCache/EHCache (Cache) * Rails/Grails Framework GWT Framework Utilities and Core Services Logging/Tracing (Wrapper) Exception Framework Base Classes/Frameworks Alert (like HP Open View) Cache (Wrapper) Static Data Security/SSO * Object Model Application Domain Model Business Rule Engine Presentation HTML, CSS, JavaScript, AJAX JSF, SpringMVC, JSP, Servlets, JSTL Facelets, Seam, Spring WebFlow Business POJO (via Spring or Session) Message Driven Beans Timer Beans Web Services SOA EAI ESB, BPM WS-* UDDI WSDL XML JCA ETL JMS/MQ Data Access JDBC, SQL, SP JPA/Hibernate/TopLink/iBatis OLTP DB J2EE Application Server (now some open source) EE 5 J2SE 5 Spring DI AOP SpringMVC WebFlow Security Open JDK Java, Ruby, Groovy, Python, Scala 25 Legacy Systems And Legacy DB Or DW > > > > > > > > > Observations from 2005-2007 Move away from Struts 1.x or proprietary frameworks to newer web frameworks like JSF (plus Facelets, Seam, and Ajax4Jsf) or Struts2 or SpringMVC (with WebFlow). Move toward annotation based configuration (versus mass of XML configuration files). Less Open Source required (due to maturity of EE specification, Spring, and open source application servers like JBoss, Glassfish, Tomcat 5/6). Apache Foundation, Spring, Craig McClanahan (JSF), Rod Johnson(String/EJB3), Gavin King(Hibernate/JPA) were really influencing and pushing the Java/J2EE platform forward. Spring Framework getting lots of traction in the industry (dependency injection (simple but powerful!), POJO based for simpler programming model, AOP (for security, transactions, tracing, etc), wrappers for integration with EJB, WS, etc.). NetBeans IDE is becoming a viable and powerful IDE (Eclipse finally has some competition). Eclipse Foundation followed suite and also released Eclipse Europa. No need to buy a J2EE IDE now. Rather then reinvent we must reuse in the Enterprise, move from vertical applications to Enterprise wide applications => SOA and leverage full Web Service stack, ESB, BPM. New EE web applications can be built much quicker and with much less code. My last project, using JSF and Spring and iBatis, was built with 50% less code, delivered on time (actually over delivered by adding more features requested from our customer), and was 25% under budget. Google influence => Google Web Toolkit, Google Docs, Google Maps, etc. Sun and Microsoft finally working together (WS-* in 2006) => that is a good thing for everybody! 26 Course #4 Example Java EE Programming 27 Course Objectives > Learn about the layers of a Java EE application. > Learn a few common/popular design patterns. > Walk through a “Hello World” web application. 28 The Layers of a Java EE Application Java EE Application Layers Desktop browser, mobile phone, STB, TV Browser Creates views for presentation, handling form data, and navigation JSP or Web framework such as JSF, Struts, or SpringMVC with HTML, CSS, and JavaScript Business Layer Implements business services and enterprise integration services EJB, Web Services, Message Driven Beans, Timer Beans, SpringBeans Persistence Layer Implements data persistence services JPA, Hibernate, iBatis, SQL, JDBC Client Layer Presentation Layer 29 The Presentation Layer > > > > > Designed using a very popular MVC design pattern used to build Presentation Layer. Implemented by all major web frameworks. Helps to enforce separation of concerns so you don’t mix presentation logic, business logic, and persistence logic together. Used to render HTML (generally) to a browser. Model View Controller • Model: data from business services to display • View: views or web pages • Controller: handles page events and navigation between pages 30 The MVC Design Pattern Diagram 1 Browser Request Response Controller Forward JSP (View) JavaBean (Model) Application Server Services Enterprise Servers and Data Sources 31 The MVC Design Pattern Diagram 2 Browser Request (POST) Include JS, CSS Response Include Page Fragments Event Handler System Exception Forward Controller xhtml xhtml JSP (View) Error JSP To Error JSP (from System Exception) Application Exception Request or Session JavaBean (Model) Include Tab Libs Data binding with tag lbrary Delegate Business Tier 32 The Business Layer > > > > > > Driven by your business use cases defined by your business requirements. Implements your business services. Should be designed using interfaces (design by contract). Can be a façade to other enterprise services deployed on an ESB or other SOA infrastructure. Acts as façade to persistence layer or enterprise data services. Supports other responsibilities: • • Transaction Management (using a service container) Security (using a service container) 33 The Business Layer Diagram 1 Client Application Exception TO System Exception Interface Business Delegate Begin Tx Implemtation Factory Business Service 1 Facade Business Rules TO Interface Interface Interface Interface DAO 1 DAO 2 DAO 3 Service 2 Transaction Mgr Transaction Boundary TO End Tx Config 34 The Business Layer Diagram 2 Client Begin Tx Interface + Implemtation Business Service 1 Facade Business Rules Config or Annotations TO Interface Interface Interface Interface DAO 1 DAO 2 DAO 3 Service 2 Transaction Mgr Dependency Injection: DAO1, DAO2, DAO3, and Service 1 and 2 TO System Exception Transaction Boundary Application Exception End Tx Config or Annotations 35 The Persistence Layer > > > > > Designed using the CRUD design pattern. Is simply responsible for persistence of your entity or domain object model. Should not be aware of transaction boundaries. Supported today by modern Object Relational Mapping (ORM) frameworks such as JPA, Hibernate, and iBatis. C R U D operations: • Create: add or insert operation • Read: read operation • Update: update operation • Delete: delete operation 36 “Hello World” Java EE Web Application > Can you really build a working N-Tier Hello World web application in less then 10 classes? > Let’s go build a simple web application ……… 37 “Hello World” Application > 2 UI Events: • • > Button click handler for the ‘Test Me’ button Button click handler for the ‘Save Me’ button 2 Business Use Cases: • Validate the Model • • > Business Rule: If Name is ‘Mark’ then Model can be persisted Save the Model Model: • Simple JavaBean that just has a Name attribute 38 “Hello World” Web Tier Implementation > View => JSF Page > Controller => JSF Event Handler class • • > The 2 UI Events are implemented in a JSF Event Handler class Business service is injected into this class The Model => clean separation between Web Application Model and the Domain Model, so we don’t mix UI data with our Business data 39 “Hello World” Business Tier Implementation > Uses Stateless EJB 3.0 JavaBeans. > Uses Container Managed Transactions > DAO is injected into this class > Implements our 2 business use cases • • Can be designed using Noun’s and Verbs discovered when you write your use case. Validate the Model • • Validate is the verb and the Noun is Model => validate(Model) Save the Model • Save is the verb and the Noun is Model => save(Model) 40 “Hello World” Data Access Tier Implementation > > > > > Implements standard CRUD operations via an Interface Uses Stateless EJB 3.0 JavaBeans (DI issue with EE5). Forces Transactions to be declared external to DAO JPA support is injected into this class Implements our single Update CRUD use case 41 “Hello World” Conclusion > A simple Java EE 5 web application was written using 3 implementation classes, 2 interfaces, 2 model classes, 1 Controller class, and 1 JSF page. > This demo application made use of EE 5 dependency injection and container managed transactions, which virtually eliminated the need to implement any infrastructure classes (and lots of old design patterns). > This demo application could have been enhanced by using more elaborate use of Base Classes, which you develop as part of a standard corporate Application Framework. > It really is that easy (if you are using modern technology)! 42 > > > > > Where Can I Learn More? Go to the Java TCC site to links for lots of good industry references. Go read the AAA-NCNU Java Standards. Go read the AAA-NCNU Java Best Practices. Get my Java EE Application Design Template. Get the Java Reference Implementation. 43 Appendix 44 References Anonymous. 2009. Wikipedia. Retrieved April 10, 2009 from http://www.wikipedia.com Sun Java Tutorials, Retrieved May 14, 2009 from http://java.sun.com/docs/books/tutorial/java/concepts/index.html 45