├── extra ├── jsf-primefaces-example │ ├── README.txt │ ├── src │ │ └── main │ │ │ └── webapp │ │ │ ├── index.jsp │ │ │ ├── hello.xhtml │ │ │ └── index.html │ └── pom.xml ├── cdi-camel │ └── src │ │ └── main │ │ ├── webapp │ │ ├── META-INF │ │ │ └── MANIFEST.MF │ │ ├── hello.xhtml │ │ ├── WEB-INF │ │ │ ├── glassfish-web.xml │ │ │ └── web.xml │ │ ├── file-listing.xhtml │ │ ├── index.xhtml │ │ └── table-data.xhtml │ │ └── java │ │ └── jsfdemo │ │ ├── service │ │ ├── Service.java │ │ ├── ServiceApplicationMain.java │ │ └── ServiceContainer.java │ │ ├── web │ │ ├── managedbean │ │ │ ├── FileListing.java │ │ │ ├── Hello.java │ │ │ └── TableData.java │ │ └── WebAppListener.java │ │ └── services │ │ └── camel │ │ ├── MyTcpClientMain.java │ │ ├── MyTcpServerRoute.java │ │ └── CamelService.java ├── jsf-demo │ ├── src │ │ └── main │ │ │ ├── webapp │ │ │ ├── META-INF │ │ │ │ └── MANIFEST.MF │ │ │ ├── hello.xhtml │ │ │ ├── index.html │ │ │ ├── file-listing.xhtml │ │ │ ├── index.xhtml │ │ │ ├── WEB-INF │ │ │ │ └── web.xml │ │ │ └── table-data.xhtml │ │ │ └── java │ │ │ └── jsfdemo │ │ │ ├── service │ │ │ ├── Service.java │ │ │ ├── ServiceApplicationMain.java │ │ │ ├── ServiceApplication.java │ │ │ └── ServiceContainer.java │ │ │ └── web │ │ │ ├── managedbean │ │ │ ├── FileListing.java │ │ │ ├── Hello.java │ │ │ └── TableData.java │ │ │ └── WebAppListener.java │ └── pom.xml ├── jpa-sakila │ ├── database │ │ ├── sakila.mwb │ │ ├── sakila-data.sql │ │ └── grant-user.sql │ ├── README.txt │ ├── src │ │ └── main │ │ │ ├── webapp │ │ │ ├── WEB-INF │ │ │ │ └── beans.xml │ │ │ └── index.jsp │ │ │ └── java │ │ │ └── zemian │ │ │ └── sakila │ │ │ ├── SalesByFilmCategory.java │ │ │ ├── FilmActorPK.java │ │ │ ├── FilmCategoryPK.java │ │ │ └── SalesByStore.java │ └── pom.xml ├── performance │ ├── src │ │ └── main │ │ │ ├── webapp │ │ │ └── index.jsp │ │ │ └── java │ │ │ └── zemian │ │ │ └── performance │ │ │ └── data │ │ │ └── PerfTestStore.java │ ├── scripts │ │ ├── mysqlVersion.groovy │ │ └── helloPerformance.groovy │ ├── pom.xml │ └── database │ │ └── mysql.sql ├── subproject-template │ ├── src │ │ └── main │ │ │ └── webapp │ │ │ └── index.jsp │ └── pom.xml ├── mvc-example │ └── src │ │ └── main │ │ ├── webapp │ │ └── WEB-INF │ │ │ ├── jsp │ │ │ ├── test │ │ │ │ └── template-plain.jsp │ │ │ ├── hello.jsp │ │ │ ├── footer.jsp │ │ │ ├── about.jsp │ │ │ ├── index.jsp │ │ │ ├── sysprops.jsp │ │ │ └── header.jsp │ │ │ └── web.xml │ │ ├── java │ │ └── zemian │ │ │ └── mvcexample │ │ │ ├── web │ │ │ ├── Controller.java │ │ │ ├── controller │ │ │ │ ├── SysPropsController.java │ │ │ │ ├── HelloController.java │ │ │ │ └── DirectView.java │ │ │ ├── WebRequest.java │ │ │ ├── MView.java │ │ │ └── WebAppStartup.java │ │ │ └── service │ │ │ └── Utils.java │ │ └── resources │ │ └── log4j.properties ├── hello-script │ ├── web │ │ ├── src │ │ │ └── main │ │ │ │ └── webapp │ │ │ │ ├── hello.jsp │ │ │ │ └── script.jsp │ │ └── pom.xml │ └── pom.xml └── glassfish-logging-example │ ├── src │ └── main │ │ ├── webapp │ │ └── index.jsp │ │ ├── resources │ │ └── log4j.properties │ │ └── java │ │ └── zemian │ │ └── glassfishloggingexample │ │ └── web │ │ ├── WebAppStartup2.java │ │ └── WebAppStartup.java │ └── pom.xml ├── ee6-specs ├── JAXB2.2.pdf ├── cdi-spec.pdf ├── ejb-3_1-fr-spec.pdf ├── jsf-2_0-fr-spec.pdf ├── jax_rs-1_1-mrel-spec.pdf ├── jaxp-1_4-mrel-spec.pdf ├── jaxrs-1.0-final-spec.pdf ├── jstl-1_2-mrel2-spec.pdf ├── servlet-3_0-final-spec.pdf ├── servlet-3_0-mrel-spec.pdf ├── connector-1_6-final-spec.pdf ├── interceptor-1-2-mrel-spec.pdf ├── javaee_platform-6_0-fr-spec.pdf ├── persistence-2_0-final-spec.pdf ├── bean_validation-1_0-final-spec.pdf ├── rc144-010d-eclipselinkjpa-3_1.pdf ├── jsr160-jmx-1_4-mrel4-spec-FINAL-v1_0.pdf ├── java-activation-framework_jaf-1.1-mrel.pdf └── JSR_173_Spec_License_Streaming_API_XML_1.3_10.22.13.pdf ├── servlet3-example ├── src │ ├── main │ │ ├── resources │ │ │ └── zemian │ │ │ │ └── servlet3example │ │ │ │ └── service │ │ │ │ ├── config.properties │ │ │ │ └── servlet3example-users.properties │ │ ├── java │ │ │ └── zemian │ │ │ │ └── servlet3example │ │ │ │ ├── service │ │ │ │ ├── Service.java │ │ │ │ ├── Application.java │ │ │ │ ├── Config.java │ │ │ │ └── UserService.java │ │ │ │ └── web │ │ │ │ ├── HelloServlet.java │ │ │ │ ├── SessionListener.java │ │ │ │ ├── UserServlet.java │ │ │ │ ├── LoginSession.java │ │ │ │ ├── SysPropsServlet.java │ │ │ │ ├── IndexServlet.java │ │ │ │ ├── TableDisplayServlet.java │ │ │ │ └── HtmlWriterServlet.java │ │ └── webapp │ │ │ ├── WEB-INF │ │ │ └── web.xml │ │ │ └── main.css │ └── test │ │ └── java │ │ └── zemian │ │ └── servlet3example │ │ └── service │ │ └── ApplicationTest.java ├── README.txt └── pom.xml ├── rest-example ├── script │ ├── hello.js │ ├── servletContextAttributes.js │ └── jdni.js ├── src │ └── main │ │ ├── webapp │ │ ├── WEB-INF │ │ │ └── web.xml │ │ └── index.jsp │ │ └── java │ │ └── zemian │ │ └── restexample │ │ └── resource │ │ ├── IVersionResource.java │ │ ├── AbstractVersionResource.java │ │ ├── VersionResource.java │ │ ├── VersionAllCapResource.java │ │ ├── RestApplication.java │ │ ├── ScriptResource.java │ │ └── ClassAnalysisResource.java └── pom.xml ├── servlet3-cdi-example ├── src │ └── main │ │ ├── resources │ │ └── zemian │ │ │ └── servlet3example │ │ │ └── service │ │ │ └── users.properties │ │ ├── webapp │ │ ├── WEB-INF │ │ │ ├── beans.xml │ │ │ └── web.xml │ │ ├── index.xhtml │ │ └── main.css │ │ └── java │ │ └── zemian │ │ └── servlet3example │ │ └── web │ │ ├── WebAppStartup.java │ │ ├── SysPropsServlet.java │ │ ├── HelloServlet.java │ │ ├── SessionListener.java │ │ ├── UserServlet.java │ │ ├── LoginSession.java │ │ ├── IndexServlet.java │ │ ├── TableDisplayServlet.java │ │ └── HtmlWriterServlet.java └── pom.xml ├── common-service ├── src │ ├── main │ │ ├── resources │ │ │ └── zemian │ │ │ │ └── service │ │ │ │ └── util │ │ │ │ └── runscript-test.js │ │ └── java │ │ │ └── zemian │ │ │ └── service │ │ │ ├── logging │ │ │ ├── package-info.java │ │ │ ├── Log.java │ │ │ ├── LogFactory.java │ │ │ ├── Logger.java │ │ │ ├── Message.java │ │ │ └── JdkLog.java │ │ │ ├── security │ │ │ ├── Encrypt.java │ │ │ └── Md5Base64Encrypt.java │ │ │ ├── config │ │ │ └── Config.java │ │ │ └── util │ │ │ └── Tuple.java │ └── test │ │ └── java │ │ └── zemian │ │ └── service │ │ ├── logging │ │ ├── LoggerTest.java │ │ └── JdkLogTest.java │ │ ├── util │ │ ├── TupleTest.java │ │ └── UtilsTest.java │ │ └── security │ │ └── Md5Base64EncryptTest.java └── pom.xml ├── jsp-example ├── src │ └── main │ │ ├── webapp │ │ ├── jsp-redirect.jsp │ │ ├── hello.jsp │ │ ├── template-plain.jsp │ │ ├── index.jsp │ │ ├── WEB-INF │ │ │ ├── tags │ │ │ │ └── myapp │ │ │ │ │ └── serverTime.tag │ │ │ ├── web.xml │ │ │ └── utils.tld │ │ ├── footer.jsp │ │ ├── tag-file-example.jsp │ │ ├── about.jsp │ │ └── jsp-vars.jsp │ │ └── java │ │ ├── resources │ │ └── application.properties │ │ └── zemian │ │ └── jspexample │ │ └── web │ │ └── WebAppStartup.java └── pom.xml ├── servlet3-jsp-example ├── src │ └── main │ │ ├── webapp │ │ └── WEB-INF │ │ │ ├── jsp │ │ │ ├── test │ │ │ │ ├── index.jsp │ │ │ │ └── hello.jsp │ │ │ ├── index.jsp │ │ │ ├── footer.jsp │ │ │ ├── about.jsp │ │ │ └── header.jsp │ │ │ └── web.xml │ │ ├── resources │ │ └── log4j.properties │ │ └── java │ │ └── zemian │ │ └── servlet3jspexample │ │ ├── AboutServlet.java │ │ ├── IndexServlet.java │ │ ├── Application.java │ │ ├── WebAppStartup.java │ │ └── TestServlet.java └── pom.xml ├── jpa-example ├── README.txt ├── database │ ├── mysql-test.sql │ └── mysql.sql ├── src │ ├── main │ │ ├── webapp │ │ │ ├── WEB-INF │ │ │ │ └── beans.xml │ │ │ └── index.jsp │ │ ├── resources │ │ │ └── META-INF │ │ │ │ └── persistence.xml │ │ └── java │ │ │ └── zemian │ │ │ └── jpaexample │ │ │ ├── dao │ │ │ ├── UserDao.java │ │ │ ├── JpaDao.java │ │ │ └── samples │ │ │ │ └── UserSamples.java │ │ │ ├── data │ │ │ └── User.java │ │ │ └── service │ │ │ └── UserService.java │ └── test │ │ └── java │ │ └── zemian │ │ └── jpaexample │ │ └── dao │ │ └── JpaDaoTest.java └── pom.xml ├── cdi-example ├── src │ └── main │ │ ├── java │ │ ├── zemian │ │ │ └── cdiexample │ │ │ │ ├── service │ │ │ │ ├── Service.java │ │ │ │ ├── HelloService.java │ │ │ │ ├── SysPropsService.java │ │ │ │ └── Application.java │ │ │ │ └── web │ │ │ │ ├── ServiceListingServlet.java │ │ │ │ ├── HelloServlet.java │ │ │ │ └── WebAppStartup.java │ │ └── resources │ │ │ └── application.properties │ │ └── webapp │ │ ├── WEB-INF │ │ └── beans.xml │ │ ├── service-listing.jsp │ │ ├── index.jsp │ │ └── index.xhtml └── pom.xml ├── .gitignore ├── jsf-example ├── src │ └── main │ │ ├── webapp │ │ ├── WEB-INF │ │ │ ├── beans.xml │ │ │ └── web.xml │ │ ├── mymapping.myhtml │ │ ├── hello2.jsp │ │ ├── hello.xhtml │ │ ├── welcome.xhtml │ │ ├── index.xhtml │ │ ├── resources │ │ │ └── css │ │ │ │ └── main.css │ │ ├── file-listing.xhtml │ │ ├── template.xhtml │ │ ├── table-data.xhtml │ │ ├── form-inputs.xhtml │ │ └── form-selections.xhtml │ │ └── java │ │ └── zemian │ │ └── jsfexample │ │ ├── web │ │ └── controller │ │ │ ├── MyMapping.java │ │ │ ├── Welcome.java │ │ │ ├── FileListing.java │ │ │ ├── Hello.java │ │ │ ├── Hello2.java │ │ │ └── TableData.java │ │ └── service │ │ └── UserService.java └── pom.xml └── README.txt /extra/jsf-primefaces-example/README.txt: -------------------------------------------------------------------------------- 1 | http://www.primefaces.org/gettingStarted 2 | -------------------------------------------------------------------------------- /extra/cdi-camel/src/main/webapp/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Class-Path: 3 | 4 | -------------------------------------------------------------------------------- /extra/jsf-demo/src/main/webapp/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Class-Path: 3 | 4 | -------------------------------------------------------------------------------- /extra/jsf-primefaces-example/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | <% response.sendRedirect("index.jsf"); %> 2 | -------------------------------------------------------------------------------- /ee6-specs/JAXB2.2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saltnlight5/java-ee6-examples/HEAD/ee6-specs/JAXB2.2.pdf -------------------------------------------------------------------------------- /ee6-specs/cdi-spec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saltnlight5/java-ee6-examples/HEAD/ee6-specs/cdi-spec.pdf -------------------------------------------------------------------------------- /servlet3-example/src/main/resources/zemian/servlet3example/service/config.properties: -------------------------------------------------------------------------------- 1 | appName=Servlet3Example 2 | -------------------------------------------------------------------------------- /rest-example/script/hello.js: -------------------------------------------------------------------------------- 1 | "Hello World!" + 2 | "[servletContext=" + servletContext + ", request=" + request + "]"; -------------------------------------------------------------------------------- /ee6-specs/ejb-3_1-fr-spec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saltnlight5/java-ee6-examples/HEAD/ee6-specs/ejb-3_1-fr-spec.pdf -------------------------------------------------------------------------------- /ee6-specs/jsf-2_0-fr-spec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saltnlight5/java-ee6-examples/HEAD/ee6-specs/jsf-2_0-fr-spec.pdf -------------------------------------------------------------------------------- /servlet3-cdi-example/src/main/resources/zemian/servlet3example/service/users.properties: -------------------------------------------------------------------------------- 1 | admin=admin123 2 | test=test123 3 | -------------------------------------------------------------------------------- /ee6-specs/jax_rs-1_1-mrel-spec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saltnlight5/java-ee6-examples/HEAD/ee6-specs/jax_rs-1_1-mrel-spec.pdf -------------------------------------------------------------------------------- /ee6-specs/jaxp-1_4-mrel-spec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saltnlight5/java-ee6-examples/HEAD/ee6-specs/jaxp-1_4-mrel-spec.pdf -------------------------------------------------------------------------------- /ee6-specs/jaxrs-1.0-final-spec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saltnlight5/java-ee6-examples/HEAD/ee6-specs/jaxrs-1.0-final-spec.pdf -------------------------------------------------------------------------------- /ee6-specs/jstl-1_2-mrel2-spec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saltnlight5/java-ee6-examples/HEAD/ee6-specs/jstl-1_2-mrel2-spec.pdf -------------------------------------------------------------------------------- /ee6-specs/servlet-3_0-final-spec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saltnlight5/java-ee6-examples/HEAD/ee6-specs/servlet-3_0-final-spec.pdf -------------------------------------------------------------------------------- /ee6-specs/servlet-3_0-mrel-spec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saltnlight5/java-ee6-examples/HEAD/ee6-specs/servlet-3_0-mrel-spec.pdf -------------------------------------------------------------------------------- /extra/jpa-sakila/database/sakila.mwb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saltnlight5/java-ee6-examples/HEAD/extra/jpa-sakila/database/sakila.mwb -------------------------------------------------------------------------------- /servlet3-example/src/main/resources/zemian/servlet3example/service/servlet3example-users.properties: -------------------------------------------------------------------------------- 1 | admin=admin123 2 | test=test123 3 | -------------------------------------------------------------------------------- /ee6-specs/connector-1_6-final-spec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saltnlight5/java-ee6-examples/HEAD/ee6-specs/connector-1_6-final-spec.pdf -------------------------------------------------------------------------------- /ee6-specs/interceptor-1-2-mrel-spec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saltnlight5/java-ee6-examples/HEAD/ee6-specs/interceptor-1-2-mrel-spec.pdf -------------------------------------------------------------------------------- /ee6-specs/javaee_platform-6_0-fr-spec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saltnlight5/java-ee6-examples/HEAD/ee6-specs/javaee_platform-6_0-fr-spec.pdf -------------------------------------------------------------------------------- /ee6-specs/persistence-2_0-final-spec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saltnlight5/java-ee6-examples/HEAD/ee6-specs/persistence-2_0-final-spec.pdf -------------------------------------------------------------------------------- /extra/jpa-sakila/database/sakila-data.sql: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saltnlight5/java-ee6-examples/HEAD/extra/jpa-sakila/database/sakila-data.sql -------------------------------------------------------------------------------- /extra/performance/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |

Hello

5 | 6 | 7 | -------------------------------------------------------------------------------- /ee6-specs/bean_validation-1_0-final-spec.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saltnlight5/java-ee6-examples/HEAD/ee6-specs/bean_validation-1_0-final-spec.pdf -------------------------------------------------------------------------------- /ee6-specs/rc144-010d-eclipselinkjpa-3_1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saltnlight5/java-ee6-examples/HEAD/ee6-specs/rc144-010d-eclipselinkjpa-3_1.pdf -------------------------------------------------------------------------------- /extra/subproject-template/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |

Hello

5 | 6 | 7 | -------------------------------------------------------------------------------- /common-service/src/main/resources/zemian/service/util/runscript-test.js: -------------------------------------------------------------------------------- 1 | println("Using Parameter n=" + n); 2 | println("Match result = " + Math.pow(12, n)); 3 | -------------------------------------------------------------------------------- /extra/mvc-example/src/main/webapp/WEB-INF/jsp/test/template-plain.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |

Hello World!

5 | 6 | 7 | -------------------------------------------------------------------------------- /ee6-specs/jsr160-jmx-1_4-mrel4-spec-FINAL-v1_0.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saltnlight5/java-ee6-examples/HEAD/ee6-specs/jsr160-jmx-1_4-mrel4-spec-FINAL-v1_0.pdf -------------------------------------------------------------------------------- /jsp-example/src/main/webapp/jsp-redirect.jsp: -------------------------------------------------------------------------------- 1 | <%-- This should redirect back to home page --%> 2 | <% response.sendRedirect(request.getContextPath() + "/index.jsp"); %> -------------------------------------------------------------------------------- /ee6-specs/java-activation-framework_jaf-1.1-mrel.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saltnlight5/java-ee6-examples/HEAD/ee6-specs/java-activation-framework_jaf-1.1-mrel.pdf -------------------------------------------------------------------------------- /extra/jpa-sakila/database/grant-user.sql: -------------------------------------------------------------------------------- 1 | CREATE USER 'sakila'@'localhost' IDENTIFIED BY 'sakila123'; 2 | GRANT ALL ON sakila.* TO 'sakila'@'localhost'; 3 | FLUSH privileges; 4 | -------------------------------------------------------------------------------- /servlet3-jsp-example/src/main/webapp/WEB-INF/jsp/test/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ include file="../header.jsp" %> 2 | 3 |

Test Page

4 | 5 | <%@ include file="../footer.jsp" %> 6 | -------------------------------------------------------------------------------- /servlet3-jsp-example/src/main/webapp/WEB-INF/jsp/test/hello.jsp: -------------------------------------------------------------------------------- 1 | <%@ include file="../header.jsp" %> 2 | 3 |

Hello ${name}!

4 | 5 | <%@ include file="../footer.jsp" %> 6 | -------------------------------------------------------------------------------- /ee6-specs/JSR_173_Spec_License_Streaming_API_XML_1.3_10.22.13.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/saltnlight5/java-ee6-examples/HEAD/ee6-specs/JSR_173_Spec_License_Streaming_API_XML_1.3_10.22.13.pdf -------------------------------------------------------------------------------- /rest-example/script/servletContextAttributes.js: -------------------------------------------------------------------------------- 1 | names = servletContext.getAttributeNames(); 2 | while(names.hasMoreElements()) { 3 | name = names.nextElement(); 4 | println(name); 5 | } 6 | -------------------------------------------------------------------------------- /jsp-example/src/main/webapp/hello.jsp: -------------------------------------------------------------------------------- 1 | <%@ include file="header.jsp" %> 2 | 3 |

Hello World!

4 |

Page served on <%= new java.util.Date()%>

5 | 6 | <%@ include file="footer.jsp" %> 7 | -------------------------------------------------------------------------------- /jsp-example/src/main/webapp/template-plain.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |

Hello World!

5 |

Page served on <%= new java.util.Date()%>

6 | 7 | 8 | -------------------------------------------------------------------------------- /servlet3-jsp-example/src/main/webapp/WEB-INF/jsp/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ include file="header.jsp" %> 2 | 3 |

This is a simple Servlet + JSP web application example.

4 | 5 | <%@ include file="footer.jsp" %> 6 | -------------------------------------------------------------------------------- /extra/hello-script/web/src/main/webapp/hello.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |

Hello World!

5 |

Page served on <%= new java.util.Date()%>

6 | 7 | 8 | -------------------------------------------------------------------------------- /jpa-example/README.txt: -------------------------------------------------------------------------------- 1 | # Setup 2 | 1. Create database 3 | 4 | # How to generate Entity from database 5 | 1. Create database service in NetBeans 6 | 2. File > New > Persistence > "Entities from Database" 7 | 8 | -------------------------------------------------------------------------------- /extra/mvc-example/src/main/webapp/WEB-INF/jsp/hello.jsp: -------------------------------------------------------------------------------- 1 | <%@ include file="header.jsp" %> 2 | 3 |

Server Message

4 |

${message}

5 |

Page served on ${serverTime}

6 | 7 | <%@ include file="footer.jsp" %> 8 | -------------------------------------------------------------------------------- /jsp-example/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ include file="header.jsp" %> 2 | 3 |

Explore EE 6 JSP and JSTL features

4 |

Ensure your server supports JSTL tag library.

5 | 6 | <%@ include file="footer.jsp" %> 7 | -------------------------------------------------------------------------------- /jsp-example/src/main/webapp/WEB-INF/tags/myapp/serverTime.tag: -------------------------------------------------------------------------------- 1 | <%@ attribute name="pattern" required="true" %> 2 | <%@ tag import="java.util.Date, java.text.SimpleDateFormat" %> 3 | <%= new SimpleDateFormat(pattern).format(new Date()) %> 4 | -------------------------------------------------------------------------------- /extra/performance/scripts/mysqlVersion.groovy: -------------------------------------------------------------------------------- 1 | import zemian.service.util.* 2 | JdbcTemplate.withJdbc("jdbc:mysql://localhost/test", "root", "root123") { jdbc -> 3 | println('MySQL Server Version: ' + jdbc.queryObject('select version()')) 4 | } -------------------------------------------------------------------------------- /jsp-example/src/main/webapp/footer.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |
6 | 7 |
8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /rest-example/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | -------------------------------------------------------------------------------- /servlet3-jsp-example/src/main/webapp/WEB-INF/jsp/footer.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |
6 | 7 |
8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /cdi-example/src/main/java/zemian/cdiexample/service/Service.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | package zemian.cdiexample.service; 5 | 6 | /** 7 | * 8 | * @author zedeng 9 | */ 10 | public interface Service { 11 | String getName(); 12 | } 13 | -------------------------------------------------------------------------------- /common-service/src/main/java/zemian/service/logging/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * A more user friendly log service. Application may use this as a facade 3 | * instead of an actual logger implementation. 4 | * 5 | * @see Logger 6 | */ 7 | package zemian.service.logging; -------------------------------------------------------------------------------- /extra/jpa-sakila/README.txt: -------------------------------------------------------------------------------- 1 | = Example database 2 | 3 | - We will use the MySQL example database "shakila" and model it using EE JPA for this module. 4 | - The sakila sample database is designed to represent a DVD rental store. 5 | - See http://dev.mysql.com/doc/sakila/en/ 6 | -------------------------------------------------------------------------------- /extra/performance/scripts/helloPerformance.groovy: -------------------------------------------------------------------------------- 1 | import zemian.service.util.* 2 | JdbcTemplate.withJdbc("jdbc:mysql://localhost/test", "root", "root123") { jdbc -> 3 | jdbc.eachResultSet('select version()') { rs -> 4 | println(rs) 5 | return true 6 | } 7 | 8 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### Eclipse 2 | .metadata/ 3 | .settings/ 4 | .classpath 5 | .project 6 | 7 | ### NetBeans 8 | nb-configuration.xml 9 | nbbuild.xml 10 | nbdist/ 11 | nbproject/ 12 | 13 | ### IntelliJ 14 | .idea/ 15 | *.iml 16 | 17 | ### Maven 18 | target/ 19 | -------------------------------------------------------------------------------- /extra/mvc-example/src/main/java/zemian/mvcexample/web/Controller.java: -------------------------------------------------------------------------------- 1 | package zemian.mvcexample.web; 2 | 3 | /** 4 | * User: zedeng 5 | * Date Time: 5/21/15 8:47 PM 6 | */ 7 | public interface Controller { 8 | MView handle(WebRequest wreq) throws Exception; 9 | } 10 | -------------------------------------------------------------------------------- /extra/mvc-example/src/main/webapp/WEB-INF/jsp/footer.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |
6 | 7 |
8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /jpa-example/database/mysql-test.sql: -------------------------------------------------------------------------------- 1 | insert into users(username, password) 2 | values ('manager', 'password'), 3 | ('manager1', 'password'), 4 | ('manager2', 'password'), 5 | ('employee', 'password'), 6 | ('employee1', 'password'), 7 | ('employee2', 'password'); -------------------------------------------------------------------------------- /jpa-example/database/mysql.sql: -------------------------------------------------------------------------------- 1 | create table users ( 2 | username varchar(64) primary key, 3 | password varchar(256) not null, 4 | createdon datetime not null default current_timestamp 5 | ); 6 | insert into users(username, password) 7 | values ('admin', 'admin123'); 8 | -------------------------------------------------------------------------------- /jsp-example/src/main/webapp/tag-file-example.jsp: -------------------------------------------------------------------------------- 1 | <%@ include file="header.jsp" %> 2 | 3 | <%@ taglib prefix="myapp" tagdir="/WEB-INF/tags/myapp" %> 4 |

Hello World!

5 |

Page served on

6 | 7 | <%@ include file="footer.jsp" %> 8 | -------------------------------------------------------------------------------- /cdi-example/src/main/java/resources/application.properties: -------------------------------------------------------------------------------- 1 | # -- welcome -- 2 | welcomeTitle=JSF Blank Application 3 | 4 | welcomeHeading=Welcome! 5 | 6 | welcomeMessage=This is a JSF blank application. \ 7 | You can find the application.properties file with this message in the src/resources folder. 8 | -------------------------------------------------------------------------------- /jsp-example/src/main/java/resources/application.properties: -------------------------------------------------------------------------------- 1 | # -- welcome -- 2 | welcomeTitle=JSF Blank Application 3 | 4 | welcomeHeading=Welcome! 5 | 6 | welcomeMessage=This is a JSF blank application. \ 7 | You can find the application.properties file with this message in the src/resources folder. 8 | -------------------------------------------------------------------------------- /extra/cdi-camel/src/main/java/jsfdemo/service/Service.java: -------------------------------------------------------------------------------- 1 | package jsfdemo.service; 2 | 3 | /** 4 | * A unit of work as service component. 5 | * 6 | * @author zedeng 7 | * 8 | */ 9 | public interface Service { 10 | void start() throws Exception; 11 | void stop() throws Exception; 12 | } 13 | -------------------------------------------------------------------------------- /extra/cdi-camel/src/main/webapp/hello.xhtml: -------------------------------------------------------------------------------- 1 | 4 | 5 | JSF Demo 6 | 7 | 8 | #{hello.world} 9 | 10 | -------------------------------------------------------------------------------- /extra/jsf-demo/src/main/java/jsfdemo/service/Service.java: -------------------------------------------------------------------------------- 1 | package jsfdemo.service; 2 | 3 | /** 4 | * A unit of work as service component. 5 | * 6 | * @author zedeng 7 | * 8 | */ 9 | public interface Service { 10 | void start() throws Exception; 11 | void stop() throws Exception; 12 | } 13 | -------------------------------------------------------------------------------- /extra/jsf-demo/src/main/webapp/hello.xhtml: -------------------------------------------------------------------------------- 1 | 4 | 5 | JSF Demo 6 | 7 | 8 | #{hello.world} 9 | 10 | -------------------------------------------------------------------------------- /extra/jsf-demo/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Start Page 5 | 6 | 7 | 8 |

Hello World!

9 | 10 | 11 | -------------------------------------------------------------------------------- /rest-example/src/main/java/zemian/restexample/resource/IVersionResource.java: -------------------------------------------------------------------------------- 1 | package zemian.restexample.resource; 2 | 3 | import javax.ws.rs.GET; 4 | import javax.ws.rs.Path; 5 | 6 | public interface IVersionResource { 7 | @Path("versionValue") 8 | @GET 9 | String getVersion(); 10 | } 11 | -------------------------------------------------------------------------------- /cdi-example/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /jpa-example/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /jsf-example/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /servlet3-example/src/main/java/zemian/servlet3example/service/Service.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3example.service; 2 | 3 | /** 4 | * A component that provide init and destroy lifecycle in an application. 5 | * 6 | * @author zedeng 7 | * 8 | */ 9 | public interface Service { 10 | void init(); 11 | void destroy(); 12 | } 13 | -------------------------------------------------------------------------------- /extra/jpa-sakila/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /extra/performance/src/main/java/zemian/performance/data/PerfTestStore.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.performance.data; 6 | 7 | import zemian.service.util.JdbcTemplate; 8 | 9 | /** 10 | * 11 | * @author zedeng 12 | */ 13 | public class PerfTestStore { 14 | private JdbcTemplate jdbc; 15 | } 16 | -------------------------------------------------------------------------------- /servlet3-cdi-example/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /extra/cdi-camel/src/main/webapp/WEB-INF/glassfish-web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | /jsf-demo 6 | -------------------------------------------------------------------------------- /jsf-example/src/main/java/zemian/jsfexample/web/controller/MyMapping.java: -------------------------------------------------------------------------------- 1 | package zemian.jsfexample.web.controller; 2 | 3 | import javax.faces.bean.ManagedBean; 4 | 5 | @ManagedBean 6 | public class MyMapping { 7 | private String message = "Hello"; 8 | 9 | public String getMessage() { 10 | return message; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /jsp-example/src/main/webapp/about.jsp: -------------------------------------------------------------------------------- 1 | <%@ include file="header.jsp" %> 2 | 3 |

This is an Java EE 6 Example Demo application. It's currently maintained by 4 | Zemian Deng. 5 | You may send email to report problem or request a feature. 6 |

7 | 8 | <%@ include file="footer.jsp" %> 9 | -------------------------------------------------------------------------------- /extra/mvc-example/src/main/webapp/WEB-INF/jsp/about.jsp: -------------------------------------------------------------------------------- 1 | <%@ include file="header.jsp" %> 2 | 3 |

This is an Java EE 6 Example Demo application. It's currently maintained by 4 | Zemian Deng. 5 | You may send email to report problem or request a feature. 6 |

7 | 8 | <%@ include file="footer.jsp" %> 9 | -------------------------------------------------------------------------------- /jsf-example/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | welcome.jsf 6 | 7 | -------------------------------------------------------------------------------- /servlet3-example/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | index 6 | 7 | -------------------------------------------------------------------------------- /rest-example/src/main/java/zemian/restexample/resource/AbstractVersionResource.java: -------------------------------------------------------------------------------- 1 | package zemian.restexample.resource; 2 | 3 | public abstract class AbstractVersionResource implements IVersionResource { 4 | public String getAppName() { 5 | return "rest-exampl"; 6 | } 7 | 8 | public String getVersionValue() { 9 | return "1.0"; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /servlet3-cdi-example/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | index 6 | 7 | -------------------------------------------------------------------------------- /servlet3-jsp-example/src/main/webapp/WEB-INF/jsp/about.jsp: -------------------------------------------------------------------------------- 1 | <%@ include file="header.jsp" %> 2 | 3 |

This is an Java EE 6 Example Demo application. It's currently maintained by 4 | Zemian Deng. 5 | You may send email to report problem or request a feature. 6 |

7 | 8 | <%@ include file="footer.jsp" %> 9 | -------------------------------------------------------------------------------- /extra/hello-script/web/src/main/webapp/script.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Script Result

4 |
 5 | <%= zemian.service.util.Utils.runGroovyTestScript(
 6 | 		"pageContext", pageContext
 7 | 		, "request", request
 8 | 		, "response", response
 9 | 		, "session", session
10 | 		, "application", application
11 | 		, "out", out) %>
12 | 
13 | 14 | -------------------------------------------------------------------------------- /extra/mvc-example/src/main/webapp/WEB-INF/jsp/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ include file="header.jsp" %> 2 | 3 |

Welcome

4 |

Home of Model View Controller Demo.

5 | 10 | 11 | <%@ include file="footer.jsp" %> 12 | -------------------------------------------------------------------------------- /extra/mvc-example/src/main/webapp/WEB-INF/jsp/sysprops.jsp: -------------------------------------------------------------------------------- 1 | <%@ include file="header.jsp" %> 2 | 3 |

Java System Properties

4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
${entry.key}${entry.value}
12 | 13 | <%@ include file="footer.jsp" %> 14 | 15 | -------------------------------------------------------------------------------- /jsp-example/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | index.jsp 6 | 7 | -------------------------------------------------------------------------------- /servlet3-jsp-example/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | index 6 | 7 | -------------------------------------------------------------------------------- /extra/glassfish-logging-example/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |

Glassfish Examples

5 |

Explore Glassfish Server specific features

6 | 9 |

Page served on <%= new java.util.Date()%>

10 | 11 | 12 | -------------------------------------------------------------------------------- /extra/mvc-example/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | main/index 6 | 7 | -------------------------------------------------------------------------------- /rest-example/src/main/java/zemian/restexample/resource/VersionResource.java: -------------------------------------------------------------------------------- 1 | package zemian.restexample.resource; 2 | 3 | import javax.ws.rs.Path; 4 | 5 | @Path("v1/version") 6 | public class VersionResource extends AbstractVersionResource implements IVersionResource { 7 | @Override 8 | public String getVersion() { 9 | return getAppName() + "-" + getVersionValue(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /jsf-example/src/main/java/zemian/jsfexample/web/controller/Welcome.java: -------------------------------------------------------------------------------- 1 | package zemian.jsfexample.web.controller; 2 | 3 | import java.util.Date; 4 | 5 | import javax.faces.bean.ManagedBean; 6 | 7 | @ManagedBean 8 | public class Welcome { 9 | 10 | private Date currentTime = new Date(); 11 | 12 | public Date getCurrentTime() { 13 | return currentTime; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /extra/jsf-primefaces-example/src/main/webapp/hello.xhtml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /rest-example/src/main/java/zemian/restexample/resource/VersionAllCapResource.java: -------------------------------------------------------------------------------- 1 | package zemian.restexample.resource; 2 | 3 | import javax.ws.rs.Path; 4 | 5 | @Path("v2/version") 6 | public class VersionAllCapResource extends AbstractVersionResource implements IVersionResource { 7 | @Override 8 | public String getVersion() { 9 | return (getAppName() + "-" + getVersionValue()).toUpperCase(); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /extra/jsf-primefaces-example/src/main/webapp/index.html: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |

Welcome to PrimeFaces JSF Example

13 | 14 |
15 | -------------------------------------------------------------------------------- /extra/mvc-example/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # Root logger option 2 | log4j.rootLogger=INFO, stdout 3 | 4 | # Application loggers 5 | log4j.zemian=DEBUG 6 | 7 | # Direct log messages to stdout 8 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 9 | log4j.appender.stdout.Target=System.out 10 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 11 | log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c:%L - %m%n 12 | -------------------------------------------------------------------------------- /servlet3-jsp-example/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # Root logger option 2 | log4j.rootLogger=INFO, stdout 3 | 4 | # Application loggers 5 | log4j.zemian=DEBUG 6 | 7 | # Direct log messages to stdout 8 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 9 | log4j.appender.stdout.Target=System.out 10 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 11 | log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c:%L - %m%n 12 | -------------------------------------------------------------------------------- /jsf-example/src/main/webapp/mymapping.myhtml: -------------------------------------------------------------------------------- 1 | <%-- 2 | NOTE that you can't create your own JSF extension using the default configuation. 3 | The .xhtml is required??? 4 | 5 | http://localhost:8080/jsf-example/faces/mymapping.myxhtml 6 | --%> 7 | 8 | 9 |

Custom Mapping with myhtml extension JSF bean. Msg = #{myMapping.message}

10 |
11 | -------------------------------------------------------------------------------- /extra/glassfish-logging-example/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # Root logger option 2 | log4j.rootLogger=INFO, stdout 3 | 4 | # Application loggers 5 | log4j.zemian=DEBUG 6 | 7 | # Direct log messages to stdout 8 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 9 | log4j.appender.stdout.Target=System.out 10 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 11 | log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c:%L - %m%n 12 | -------------------------------------------------------------------------------- /jsf-example/src/main/java/zemian/jsfexample/web/controller/FileListing.java: -------------------------------------------------------------------------------- 1 | package zemian.jsfexample.web.controller; 2 | 3 | import java.io.File; 4 | 5 | import javax.faces.bean.ManagedBean; 6 | 7 | @ManagedBean 8 | public class FileListing { 9 | 10 | private File dir = new File("."); 11 | 12 | public File getDir() { 13 | return dir; 14 | } 15 | 16 | public File[] getFiles() { 17 | return dir.listFiles(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /jsf-example/src/main/webapp/hello2.jsp: -------------------------------------------------------------------------------- 1 | <%-- 2 | NOTE: that you can't use #{} inside a JSP page! 3 | And you can't use .jsp as JSF view extension. 4 | 5 | This page will result in error: 6 | org.apache.jasper.JasperException: /hello2.jsp(3,13) PWC6228: #{...} not allowed in a template text body. 7 | --%> 8 | 9 | 10 |

#{hello2.world}

11 |
12 | -------------------------------------------------------------------------------- /extra/jsf-demo/src/main/webapp/file-listing.xhtml: -------------------------------------------------------------------------------- 1 | 4 | 5 | JSF Demo 6 | 7 | 8 |

Dir Listing: #{fileListing.dir.absolutePath}

9 | 14 |
15 | -------------------------------------------------------------------------------- /extra/cdi-camel/src/main/webapp/file-listing.xhtml: -------------------------------------------------------------------------------- 1 | 4 | 5 | JSF Demo 6 | 7 | 8 |

Dir Listing: #{fileListing.dir.absolutePath}

9 | 14 |
15 | -------------------------------------------------------------------------------- /jsf-example/src/main/webapp/hello.xhtml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 |

#{hello.world}

9 |
10 |
11 |
12 | -------------------------------------------------------------------------------- /extra/jsf-demo/src/main/webapp/index.xhtml: -------------------------------------------------------------------------------- 1 | 4 | 5 | JSF Demo 6 | 7 | 8 | 13 | 14 | -------------------------------------------------------------------------------- /extra/cdi-camel/src/main/webapp/index.xhtml: -------------------------------------------------------------------------------- 1 | 4 | 5 | JSF Demo 6 | 7 | 8 | 13 | 14 | -------------------------------------------------------------------------------- /extra/cdi-camel/src/main/java/jsfdemo/web/managedbean/FileListing.java: -------------------------------------------------------------------------------- 1 | package jsfdemo.web.managedbean; 2 | 3 | import java.io.File; 4 | 5 | import javax.faces.bean.ManagedBean; 6 | 7 | /** 8 | * A test bean for testing JSF page. 9 | * 10 | * @author zedeng 11 | * 12 | */ 13 | @ManagedBean 14 | public class FileListing { 15 | private File dir = new File("."); 16 | 17 | public File getDir() { 18 | return dir; 19 | } 20 | 21 | public File[] getFiles() { 22 | return dir.listFiles(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /extra/jsf-demo/src/main/java/jsfdemo/web/managedbean/FileListing.java: -------------------------------------------------------------------------------- 1 | package jsfdemo.web.managedbean; 2 | 3 | import java.io.File; 4 | 5 | import javax.faces.bean.ManagedBean; 6 | 7 | /** 8 | * A test bean for testing JSF page. 9 | * 10 | * @author zedeng 11 | * 12 | */ 13 | @ManagedBean 14 | public class FileListing { 15 | private File dir = new File("."); 16 | 17 | public File getDir() { 18 | return dir; 19 | } 20 | 21 | public File[] getFiles() { 22 | return dir.listFiles(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /cdi-example/src/main/java/zemian/cdiexample/service/HelloService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.cdiexample.service; 6 | 7 | import javax.enterprise.context.RequestScoped; 8 | 9 | /** 10 | * 11 | * @author zedeng 12 | */ 13 | @RequestScoped 14 | public class HelloService implements Service { 15 | 16 | @Override 17 | public String getName() { 18 | return getClass().getSimpleName(); 19 | } 20 | 21 | public String getMessage() { 22 | return "Hello World."; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /common-service/src/main/java/zemian/service/security/Encrypt.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | package zemian.service.security; 5 | 6 | /** 7 | * A service to secure data by encryption and/or decryption of a given input 8 | * data. Note that not all implementations need to provide decryption, such as 9 | * MD5 which is only a one way hash algorithm. 10 | * 11 | * @author zedeng 12 | */ 13 | public interface Encrypt { 14 | byte[] encrypt(byte[] plainData); 15 | byte[] decrypt(byte[] securedData); 16 | } 17 | -------------------------------------------------------------------------------- /cdi-example/src/main/webapp/service-listing.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html; charset=UTF-8" %> 2 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 3 | 4 | 5 | 6 |

Service Listing

7 | 8 | 9 | 10 | 11 | 12 | 13 |
${service.name}
14 |

Page served on <%= new java.util.Date()%>

15 | 16 | 17 | -------------------------------------------------------------------------------- /cdi-example/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |

CDI Examples

5 |

Explore EE 6 CDI features

6 | 11 |

Page served on <%= new java.util.Date()%>

12 | 13 | 14 | -------------------------------------------------------------------------------- /rest-example/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |

JPA Examples

5 |

Explore EE 6 Java Persistence API features

6 |

Web pages

7 | 10 |

REST resources

11 | 14 |

Page served on <%= new java.util.Date()%>

15 | 16 | 17 | -------------------------------------------------------------------------------- /jpa-example/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | jdbc/jpa-example 5 | zemian.jpaexample.data.User 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /extra/cdi-camel/src/main/java/jsfdemo/web/managedbean/Hello.java: -------------------------------------------------------------------------------- 1 | package jsfdemo.web.managedbean; 2 | 3 | import java.io.File; 4 | 5 | import javax.faces.bean.ManagedBean; 6 | 7 | /** 8 | * A test bean for testing JSF page. 9 | * 10 | * @author zedeng 11 | * 12 | */ 13 | @ManagedBean 14 | public class Hello { 15 | private String world = "Hello World!"; 16 | 17 | public String getWorld() { 18 | return world; 19 | } 20 | 21 | File dir = new File("."); 22 | public File getDir() { 23 | return dir; 24 | } 25 | public File[] getFiles() { 26 | return dir.listFiles(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /extra/jsf-demo/src/main/java/jsfdemo/web/managedbean/Hello.java: -------------------------------------------------------------------------------- 1 | package jsfdemo.web.managedbean; 2 | 3 | import java.io.File; 4 | 5 | import javax.faces.bean.ManagedBean; 6 | 7 | /** 8 | * A test bean for testing JSF page. 9 | * 10 | * @author zedeng 11 | * 12 | */ 13 | @ManagedBean 14 | public class Hello { 15 | private String world = "Hello World!"; 16 | 17 | public String getWorld() { 18 | return world; 19 | } 20 | 21 | File dir = new File("."); 22 | public File getDir() { 23 | return dir; 24 | } 25 | public File[] getFiles() { 26 | return dir.listFiles(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /rest-example/src/main/java/zemian/restexample/resource/RestApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package zemian.restexample.resource; 7 | 8 | import javax.ws.rs.ApplicationPath; 9 | import javax.ws.rs.core.Application; 10 | 11 | /** 12 | * This is the main entry into REST Application that bootstrap the provider. 13 | * @author zedeng 14 | */ 15 | @ApplicationPath("rest") 16 | public class RestApplication extends Application { 17 | } 18 | -------------------------------------------------------------------------------- /servlet3-example/src/test/java/zemian/servlet3example/service/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3example.service; 2 | 3 | 4 | import org.hamcrest.CoreMatchers; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | /* 9 | * Zemian Deng 2014 10 | */ 11 | 12 | /** 13 | * 14 | * @author zedeng 15 | */ 16 | public class ApplicationTest { 17 | @Test 18 | public void testConfigPackageName() { 19 | String packagePath = "/" + Config.class.getPackage().getName().replaceAll("\\.", "/"); 20 | Assert.assertThat(packagePath, CoreMatchers.is("/zemian/servlet3example/service")); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /jsf-example/src/main/webapp/welcome.xhtml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 |

Welcome to EE 6 JSF Examples

9 |

Explore built-in JSF components.

10 |

Page created on: #{welcome.currentTime}

11 |
12 |
13 |
14 | -------------------------------------------------------------------------------- /rest-example/script/jdni.js: -------------------------------------------------------------------------------- 1 | // Access EJB beans through JNDI 2 | // See http://docs.oracle.com/javaee/6/tutorial/doc/gipjf.html 3 | 4 | // Debug 5 | importPackage(Packages.zemian.jpaexample.web.rest); 6 | JndiUtils.printJndiTree(); 7 | 8 | // Get a local EJB within the same module deployment 9 | importPackage(Packages.zemian.jpaexample.web.rest); 10 | userService = JndiUtils.lookup("java:module/UserService"); 11 | users = userService.findMostActiveUsers() 12 | println("Found " + users.size() + " users."); 13 | for (i=0; i < users.size(); i++) { 14 | user = users.get(0); 15 | println(user); 16 | } 17 | users.size(); 18 | -------------------------------------------------------------------------------- /common-service/src/main/java/zemian/service/config/Config.java: -------------------------------------------------------------------------------- 1 | package zemian.service.config; 2 | 3 | import java.util.List; 4 | import java.util.Set; 5 | 6 | /** 7 | * A config service that provide configuration properties for an application. 8 | * 9 | *

A implementation should take care to load the data from a certain source 10 | * such as Properties file or database.

11 | * 12 | * @author zedeng 13 | */ 14 | public interface Config { 15 | String getConfigName(); 16 | Set getKeys(); 17 | String getValue(String key); 18 | List getGroupValues(String groupKey); 19 | Config getParent(); 20 | } 21 | -------------------------------------------------------------------------------- /cdi-example/src/main/java/zemian/cdiexample/service/SysPropsService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.cdiexample.service; 6 | 7 | import javax.enterprise.context.ApplicationScoped; 8 | import zemian.service.logging.Logger; 9 | 10 | /** 11 | * 12 | * @author zedeng 13 | */ 14 | @ApplicationScoped 15 | public class SysPropsService implements Service { 16 | //private static final Logger LOGGER = new Logger(Hello.class); 17 | 18 | @Override 19 | public String getName() { 20 | return getClass().getSimpleName(); 21 | } 22 | 23 | public String getSysProp(String key) { 24 | return System.getProperty(key); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /jsf-example/src/main/java/zemian/jsfexample/web/controller/Hello.java: -------------------------------------------------------------------------------- 1 | package zemian.jsfexample.web.controller; 2 | 3 | 4 | import javax.faces.bean.ManagedBean; 5 | import zemian.service.logging.Logger; 6 | 7 | /** 8 | * This managed bean will create new instance per HTTP request. 9 | * 10 | * @author zedeng 11 | */ 12 | @ManagedBean 13 | public class Hello { 14 | private static final Logger LOGGER = new Logger(Hello.class); 15 | public Hello() { 16 | LOGGER.info("Hello component created: " + this); 17 | } 18 | 19 | private String world = "Hello World!"; 20 | 21 | public String getWorld() { 22 | return world; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /jsf-example/src/main/java/zemian/jsfexample/web/controller/Hello2.java: -------------------------------------------------------------------------------- 1 | package zemian.jsfexample.web.controller; 2 | 3 | 4 | import javax.faces.bean.ManagedBean; 5 | import zemian.service.logging.Logger; 6 | 7 | /** 8 | * This managed bean will create new instance per HTTP request. 9 | * 10 | * @author zedeng 11 | */ 12 | @ManagedBean 13 | public class Hello2 { 14 | private static final Logger LOGGER = new Logger(Hello2.class); 15 | public Hello2() { 16 | LOGGER.info("Hello2 component created: " + this); 17 | } 18 | 19 | private String world = "Hello2 World!"; 20 | 21 | public String getWorld() { 22 | return world; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /servlet3-jsp-example/src/main/java/zemian/servlet3jspexample/AboutServlet.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3jspexample; 2 | 3 | import javax.servlet.ServletException; 4 | import javax.servlet.annotation.WebServlet; 5 | import javax.servlet.http.HttpServlet; 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | import java.io.IOException; 9 | 10 | @WebServlet("/about") 11 | public class AboutServlet extends HttpServlet { 12 | @Override 13 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 14 | req.getRequestDispatcher("/WEB-INF/jsp/about.jsp").forward(req, resp); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /servlet3-jsp-example/src/main/java/zemian/servlet3jspexample/IndexServlet.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3jspexample; 2 | 3 | import javax.servlet.ServletException; 4 | import javax.servlet.annotation.WebServlet; 5 | import javax.servlet.http.HttpServlet; 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | import java.io.IOException; 9 | 10 | @WebServlet("/index") 11 | public class IndexServlet extends HttpServlet { 12 | @Override 13 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 14 | req.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(req, resp); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /cdi-example/src/main/webapp/index.xhtml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 8 | 9 | 10 | 11 | 12 | <h:outputText value="#{msg.welcomeTitle}" /> 13 | 14 | 15 | 16 |

17 |

18 | 19 | 20 | -------------------------------------------------------------------------------- /jsf-example/src/main/java/zemian/jsfexample/web/controller/TableData.java: -------------------------------------------------------------------------------- 1 | package zemian.jsfexample.web.controller; 2 | 3 | import java.math.BigDecimal; 4 | import java.util.List; 5 | 6 | import javax.faces.bean.ManagedBean; 7 | import javax.faces.bean.SessionScoped; 8 | import javax.inject.Inject; 9 | import zemian.jsfexample.data.User; 10 | import zemian.jsfexample.service.UserService; 11 | 12 | /** 13 | * @author zedeng 14 | * 15 | */ 16 | @ManagedBean 17 | @SessionScoped 18 | public class TableData { 19 | 20 | @Inject 21 | private UserService userService; 22 | 23 | public List getUsers() { 24 | return userService.findAllUsers(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /jsf-example/src/main/webapp/index.xhtml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 8 | 9 | 10 | 11 | 12 | <h:outputText value="#{msg.welcomeTitle}" /> 13 | 14 | 15 | 16 |

17 |

18 | 19 | 20 | -------------------------------------------------------------------------------- /servlet3-cdi-example/src/main/webapp/index.xhtml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 8 | 9 | 10 | 11 | 12 | <h:outputText value="#{msg.welcomeTitle}" /> 13 | 14 | 15 | 16 |

17 |

18 | 19 | 20 | -------------------------------------------------------------------------------- /servlet3-example/src/main/webapp/main.css: -------------------------------------------------------------------------------- 1 | /* Horizontal Navigation List */ 2 | ul.navlist li { 3 | display: inline; 4 | list-style-type: none; 5 | padding-right: 20px; 6 | } 7 | 8 | /* List Styling */ 9 | ul { 10 | list-style-type: circle; 11 | list-style-position: outside; 12 | list-style-image: none; 13 | } 14 | 15 | /* Table Styling */ 16 | table, th, td { 17 | border: 1px solid black; 18 | border-collapse: collapse; 19 | } 20 | tr.odd { 21 | background: white 22 | } 23 | tr.even { 24 | background: lightgray 25 | } 26 | td { 27 | padding: 15px; 28 | vertical-align: top; 29 | } 30 | th { 31 | background-color: black; 32 | color: white; 33 | } -------------------------------------------------------------------------------- /jsp-example/src/main/webapp/WEB-INF/utils.tld: -------------------------------------------------------------------------------- 1 | 2 | 3 | utils 4 | utils 5 | 1.0 6 | utils 7 | utils 8 | 9 | toString 10 | zemian.service.util.Utils 11 | java.lang.String toString(java.lang.Object) 12 | 13 | 14 | -------------------------------------------------------------------------------- /servlet3-cdi-example/src/main/webapp/main.css: -------------------------------------------------------------------------------- 1 | /* Horizontal Navigation List */ 2 | ul.navlist li { 3 | display: inline; 4 | list-style-type: none; 5 | padding-right: 20px; 6 | } 7 | 8 | /* List Styling */ 9 | ul { 10 | list-style-type: circle; 11 | list-style-position: outside; 12 | list-style-image: none; 13 | } 14 | 15 | /* Table Styling */ 16 | table, th, td { 17 | border: 1px solid black; 18 | border-collapse: collapse; 19 | } 20 | tr.odd { 21 | background: white 22 | } 23 | tr.even { 24 | background: lightgray 25 | } 26 | td { 27 | padding: 15px; 28 | vertical-align: top; 29 | } 30 | th { 31 | background-color: black; 32 | color: white; 33 | } -------------------------------------------------------------------------------- /jsf-example/src/main/webapp/resources/css/main.css: -------------------------------------------------------------------------------- 1 | /* Horizontal Navigation List */ 2 | ul.navlist li { 3 | display: inline; 4 | list-style-type: none; 5 | padding-right: 20px; 6 | } 7 | 8 | /* List Styling */ 9 | ul { 10 | list-style-type: circle; 11 | list-style-position: outside; 12 | list-style-image: none; 13 | } 14 | 15 | /* Table Styling */ 16 | table, th, td { 17 | border: 1px solid black; 18 | border-collapse: collapse; 19 | } 20 | tr.odd { 21 | background: white 22 | } 23 | tr.even { 24 | background: lightgray 25 | } 26 | td { 27 | padding: 15px; 28 | vertical-align: top; 29 | } 30 | th { 31 | background-color: black; 32 | color: white; 33 | } -------------------------------------------------------------------------------- /extra/mvc-example/src/main/java/zemian/mvcexample/web/controller/SysPropsController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.mvcexample.web.controller; 6 | 7 | import zemian.mvcexample.web.Controller; 8 | import zemian.mvcexample.web.MView; 9 | import zemian.mvcexample.web.WebRequest; 10 | 11 | /** 12 | * A controller that return "sysProps" of the Java System Properties. 13 | * 14 | * @author zedeng 15 | */ 16 | public class SysPropsController implements Controller { 17 | 18 | @Override 19 | public MView handle(WebRequest wreq) { 20 | MView mview = new MView(wreq.getControllerName()); 21 | mview.put("sysProps", System.getProperties()); 22 | return mview; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /servlet3-jsp-example/src/main/java/zemian/servlet3jspexample/Application.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.servlet3jspexample; 6 | 7 | /** 8 | * A singleton application context space to store global variables and states. 9 | * 10 | * @author zedeng 11 | */ 12 | public class Application { 13 | public static final String SERVLET_CONTEXT_KEY = Application.class.getName(); 14 | 15 | private static Application INSTANCE = new Application(); 16 | 17 | private Application() { 18 | } 19 | 20 | public static Application getInstance() { 21 | return INSTANCE; 22 | } 23 | 24 | public void init() { 25 | } 26 | 27 | public void destroy() { 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /common-service/src/test/java/zemian/service/logging/LoggerTest.java: -------------------------------------------------------------------------------- 1 | package zemian.service.logging; 2 | 3 | import org.junit.Test; 4 | 5 | /** 6 | * @author zedeng 7 | */ 8 | public class LoggerTest { 9 | private static final Logger LOGGER = new Logger(LoggerTest.class); 10 | 11 | @Test 12 | public void testLogger() { 13 | LOGGER.trace("test"); 14 | LOGGER.debug("test"); 15 | LOGGER.info("test"); 16 | LOGGER.warn("test"); 17 | //LOGGER.error(new RuntimeException("Checkpoint"), "test"); 18 | 19 | LOGGER.info("Hello %s", "World."); 20 | 21 | // Note that you can't use MessaggeFormat syntax! 22 | LOGGER.info("Hello {0}", "World."); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /extra/cdi-camel/src/main/java/jsfdemo/services/camel/MyTcpClientMain.java: -------------------------------------------------------------------------------- 1 | package jsfdemo.services.camel; 2 | 3 | import org.apache.camel.ProducerTemplate; 4 | 5 | /** 6 | * A Camel client to send messages to a TCP endpoint. 7 | * 8 | * @author zedeng 9 | * 10 | */ 11 | public class MyTcpClientMain extends CamelService { 12 | 13 | public static void main(String[] args) throws Exception { 14 | MyTcpClientMain service = new MyTcpClientMain(); 15 | service.start(); 16 | service.stop(); 17 | } 18 | 19 | @Override 20 | protected void afterCamelStarted() { 21 | ProducerTemplate producerTemplate = getCamelContext().createProducerTemplate(); 22 | producerTemplate.asyncSendBody("netty:tcp://localhost:12345", "Hello World!"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /extra/cdi-camel/src/main/java/jsfdemo/services/camel/MyTcpServerRoute.java: -------------------------------------------------------------------------------- 1 | package jsfdemo.services.camel; 2 | 3 | import org.apache.camel.builder.RouteBuilder; 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | 7 | /** 8 | * A Camel route that listen on a TCP port for incoming messages. 9 | * 10 | * @author zedeng 11 | * 12 | */ 13 | public class MyTcpServerRoute extends RouteBuilder { 14 | private static final Logger LOGGER = LoggerFactory.getLogger(MyTcpServerRoute.class); 15 | 16 | @Override 17 | public void configure() throws Exception { 18 | LOGGER.info("I will be listening on 'localhost' port 12345"); 19 | from("netty:tcp://localhost:12345") 20 | .to("log://" + CamelService.class.getSimpleName()); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /jsf-example/src/main/webapp/file-listing.xhtml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 |

Directory Listing:
#{fileListing.dir.absolutePath}

9 |
    10 | 11 |
  • #{file.path}
  • 12 |
    13 |
14 |
15 |
16 |
17 | 18 | -------------------------------------------------------------------------------- /jpa-example/src/main/java/zemian/jpaexample/dao/UserDao.java: -------------------------------------------------------------------------------- 1 | package zemian.jpaexample.dao; 2 | 3 | import java.util.List; 4 | 5 | import zemian.jpaexample.data.User; 6 | import zemian.service.logging.Logger; 7 | 8 | public class UserDao extends JpaDao { 9 | private static final Logger LOGGER = new Logger(UserDao.class); 10 | 11 | public UserDao() { 12 | super(User.class); 13 | } 14 | 15 | @SuppressWarnings("unchecked") 16 | public List findMostActiveUsers() { 17 | LOGGER.debug("Querying database for most active users."); 18 | // Return all users for now. 19 | List resultList = getEntityManager().createQuery("SELECT e FROM User e").getResultList(); 20 | return resultList; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /servlet3-cdi-example/src/main/java/zemian/servlet3example/web/WebAppStartup.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3example.web; 2 | 3 | import javax.servlet.ServletContextEvent; 4 | import javax.servlet.ServletContextListener; 5 | import javax.servlet.annotation.WebListener; 6 | import zemian.service.logging.Logger; 7 | 8 | @WebListener 9 | public class WebAppStartup implements ServletContextListener { 10 | private static final Logger LOGGER = new Logger(WebAppStartup.class); 11 | 12 | @Override 13 | public void contextInitialized(ServletContextEvent event) { 14 | LOGGER.debug("WebApp is starting up."); 15 | } 16 | 17 | @Override 18 | public void contextDestroyed(ServletContextEvent event) { 19 | LOGGER.info("WebApp destroyed."); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /extra/cdi-camel/src/main/java/jsfdemo/web/WebAppListener.java: -------------------------------------------------------------------------------- 1 | package jsfdemo.web; 2 | 3 | import javax.servlet.ServletContextEvent; 4 | import javax.servlet.ServletContextListener; 5 | 6 | import jsfdemo.service.ServiceApplication; 7 | 8 | /** 9 | * Application startup and shutdown from Servlet environment. 10 | * 11 | * @author zedeng 12 | * 13 | */ 14 | public class WebAppListener implements ServletContextListener { 15 | private ServiceApplication serviceApplication = ServiceApplication.getInstance(); 16 | 17 | @Override 18 | public void contextInitialized(ServletContextEvent event) { 19 | serviceApplication.quickStart(); 20 | } 21 | 22 | @Override 23 | public void contextDestroyed(ServletContextEvent event) { 24 | serviceApplication.quickStop(); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /extra/jsf-demo/src/main/java/jsfdemo/web/WebAppListener.java: -------------------------------------------------------------------------------- 1 | package jsfdemo.web; 2 | 3 | import javax.servlet.ServletContextEvent; 4 | import javax.servlet.ServletContextListener; 5 | 6 | import jsfdemo.service.ServiceApplication; 7 | 8 | /** 9 | * Application startup and shutdown from Servlet environment. 10 | * 11 | * @author zedeng 12 | * 13 | */ 14 | public class WebAppListener implements ServletContextListener { 15 | private ServiceApplication serviceApplication = ServiceApplication.getInstance(); 16 | 17 | @Override 18 | public void contextInitialized(ServletContextEvent event) { 19 | serviceApplication.quickStart(); 20 | } 21 | 22 | @Override 23 | public void contextDestroyed(ServletContextEvent event) { 24 | serviceApplication.quickStop(); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /jpa-example/src/test/java/zemian/jpaexample/dao/JpaDaoTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.jpaexample.dao; 6 | 7 | import org.hamcrest.CoreMatchers; 8 | import org.junit.Assert; 9 | import org.junit.Ignore; 10 | import org.junit.Test; 11 | import zemian.jpaexample.data.User; 12 | 13 | /** 14 | * 15 | * @author zedeng 16 | */ 17 | public class JpaDaoTest { 18 | @Ignore // TODO: we need EM injection! 19 | @Test 20 | public void testOnDemandEntityDao() { 21 | 22 | JpaDao userDao = new JpaDao<>(); 23 | userDao.setEntityClass(User.class); // Depend on how you use EM, you may or may not need this. 24 | User user = userDao.getEntityManager().find(User.class, "zemian"); 25 | Assert.assertThat(user, CoreMatchers.notNullValue()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /extra/mvc-example/src/main/java/zemian/mvcexample/web/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.mvcexample.web.controller; 6 | 7 | import zemian.mvcexample.web.Controller; 8 | import zemian.mvcexample.web.MView; 9 | import zemian.mvcexample.web.WebRequest; 10 | 11 | import java.util.Date; 12 | 13 | /** 14 | * Just a test controller that return "message" and "serverTime" as model map. 15 | * This is mainly for testing purpose. 16 | * 17 | * @author zedeng 18 | */ 19 | public class HelloController implements Controller { 20 | 21 | @Override 22 | public MView handle(WebRequest wreq) { 23 | MView mview = new MView(wreq.getControllerName()); 24 | mview.put("message", "Hello World."); 25 | mview.put("serverTime", new Date()); 26 | return mview; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /jpa-example/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |

JPA Examples

5 |

Explore EE 6 Java Persistence API features

6 |

Web pages

7 | 11 |

REST resources

12 | 17 |

Page served on <%= new java.util.Date()%>

18 | 19 | 20 | -------------------------------------------------------------------------------- /extra/jpa-sakila/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |

JPA Examples

5 |

Explore EE 6 Java Persistence API features

6 |

Web pages

7 | 11 |

REST resources

12 | 17 |

Page served on <%= new java.util.Date()%>

18 | 19 | 20 | -------------------------------------------------------------------------------- /servlet3-cdi-example/src/main/java/zemian/servlet3example/web/SysPropsServlet.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3example.web; 2 | 3 | import java.io.IOException; 4 | import java.util.TreeMap; 5 | import javax.servlet.ServletException; 6 | import javax.servlet.annotation.WebServlet; 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | 10 | @WebServlet("/sys-props") 11 | public class SysPropsServlet extends HtmlWriterServlet { 12 | 13 | @Override 14 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 15 | HtmlWriter html = createHtmlWriter(req, resp); 16 | TreeMap sysProps = new TreeMap(System.getProperties()); 17 | html.header() 18 | .h(1, "Java System Properties") 19 | .table(sysProps) 20 | .footer(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /common-service/src/main/java/zemian/service/logging/Log.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.service.logging; 6 | 7 | /** 8 | * A log service to allow application log different level of messages. 9 | * 10 | *

We will capture message data in a POJO instead of direct parameters like 11 | * many other logger implementation. This might seems verbose at first, but it 12 | * provide many benefits because a Message may have many optional data to be 13 | * logged. This design can keep this interface clean. You may use Message.msg() 14 | * factory method to shorten the code. 15 | * 16 | * @author zedeng 17 | */ 18 | public interface Log { 19 | public void error(Message message); 20 | public void warn(Message message); 21 | public void info(Message message); 22 | public void debug(Message message); 23 | public void trace(Message message); 24 | } 25 | -------------------------------------------------------------------------------- /jpa-example/src/main/java/zemian/jpaexample/data/User.java: -------------------------------------------------------------------------------- 1 | package zemian.jpaexample.data; 2 | 3 | import java.util.Date; 4 | import javax.persistence.Entity; 5 | import javax.persistence.Id; 6 | import javax.persistence.Table; 7 | 8 | @Entity 9 | @Table(name = "USERS") 10 | public class User { 11 | 12 | @Id 13 | private String username; 14 | private String password; 15 | private Date createdOn; 16 | 17 | public String getUsername() { 18 | return username; 19 | } 20 | 21 | public void setUsername(String username) { 22 | this.username = username; 23 | } 24 | 25 | public String getPassword() { 26 | return password; 27 | } 28 | 29 | public void setPassword(String password) { 30 | this.password = password; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "User(username=" + username + ")"; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /extra/glassfish-logging-example/src/main/java/zemian/glassfishloggingexample/web/WebAppStartup2.java: -------------------------------------------------------------------------------- 1 | package zemian.glassfishloggingexample.web; 2 | 3 | import javax.servlet.ServletContextEvent; 4 | import javax.servlet.ServletContextListener; 5 | import javax.servlet.annotation.WebListener; 6 | import zemian.service.logging.Logger; 7 | 8 | /** 9 | * An app listener that uses SLF4J logger. 10 | * 11 | * @author zedeng 12 | */ 13 | @WebListener 14 | public class WebAppStartup2 implements ServletContextListener { 15 | private static final Logger LOGGER = new Logger(WebAppStartup2.class); 16 | 17 | @Override 18 | public void contextInitialized(ServletContextEvent event) { 19 | LOGGER.debug("WebApp is starting up."); 20 | LOGGER.info("WebApp initialized."); 21 | } 22 | 23 | @Override 24 | public void contextDestroyed(ServletContextEvent event) { 25 | LOGGER.info("WebApp destroyed."); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /extra/jsf-demo/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | jsf-demo 7 | 8 | faces/index.xhtml 9 | 10 | 11 | Faces Servlet 12 | javax.faces.webapp.FacesServlet 13 | 1 14 | 15 | 16 | Faces Servlet 17 | /faces/* 18 | 19 | 20 | 21 | jsfdemo.web.WebAppListener 22 | 23 | -------------------------------------------------------------------------------- /servlet3-example/src/main/java/zemian/servlet3example/web/HelloServlet.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3example.web; 2 | 3 | import java.io.IOException; 4 | import java.io.PrintWriter; 5 | import javax.servlet.ServletException; 6 | import javax.servlet.annotation.WebServlet; 7 | import javax.servlet.http.HttpServlet; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | 11 | @WebServlet("/hello") 12 | public class HelloServlet extends HttpServlet { 13 | 14 | @Override 15 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 16 | PrintWriter writer = resp.getWriter(); 17 | writer.println(""); 18 | writer.println(""); 19 | writer.println(""); 20 | writer.println("

Hello World!

"); 21 | writer.println(""); 22 | writer.println(""); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /extra/cdi-camel/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | jsf-demo 7 | 8 | faces/index.xhtml 9 | 10 | 11 | Faces Servlet 12 | javax.faces.webapp.FacesServlet 13 | 1 14 | 15 | 16 | Faces Servlet 17 | /faces/* 18 | 19 | 20 | 21 | jsfdemo.web.WebAppListener 22 | 23 | -------------------------------------------------------------------------------- /servlet3-cdi-example/src/main/java/zemian/servlet3example/web/HelloServlet.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3example.web; 2 | 3 | import java.io.IOException; 4 | import java.io.PrintWriter; 5 | import javax.servlet.ServletException; 6 | import javax.servlet.annotation.WebServlet; 7 | import javax.servlet.http.HttpServlet; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | 11 | @WebServlet("/hello") 12 | public class HelloServlet extends HttpServlet { 13 | 14 | @Override 15 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 16 | PrintWriter writer = resp.getWriter(); 17 | writer.println(""); 18 | writer.println(""); 19 | writer.println(""); 20 | writer.println("

Hello World!

"); 21 | writer.println(""); 22 | writer.println(""); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /servlet3-example/src/main/java/zemian/servlet3example/web/SessionListener.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3example.web; 2 | 3 | import javax.servlet.annotation.WebListener; 4 | import javax.servlet.http.HttpSessionEvent; 5 | import javax.servlet.http.HttpSessionListener; 6 | import zemian.service.logging.Logger; 7 | 8 | /** 9 | * Just a session listener demo that LOG a message whenever a HTTP session object is created. 10 | * @author zedeng 11 | */ 12 | @WebListener 13 | public class SessionListener implements HttpSessionListener { 14 | private static final Logger LOGGER = new Logger(SessionListener.class); 15 | 16 | @Override 17 | public void sessionCreated(HttpSessionEvent se) { 18 | LOGGER.debug("Session created %s", se.getSession()); 19 | } 20 | 21 | @Override 22 | public void sessionDestroyed(HttpSessionEvent se) { 23 | LOGGER.debug("Session destroyed %s", se.getSession()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /servlet3-cdi-example/src/main/java/zemian/servlet3example/web/SessionListener.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3example.web; 2 | 3 | import javax.servlet.annotation.WebListener; 4 | import javax.servlet.http.HttpSessionEvent; 5 | import javax.servlet.http.HttpSessionListener; 6 | import zemian.service.logging.Logger; 7 | 8 | /** 9 | * Just a session listener demo that LOG a message whenever a HTTP session object is created. 10 | * @author zedeng 11 | */ 12 | @WebListener 13 | public class SessionListener implements HttpSessionListener { 14 | private static final Logger LOGGER = new Logger(SessionListener.class); 15 | 16 | @Override 17 | public void sessionCreated(HttpSessionEvent se) { 18 | LOGGER.debug("Session created %s", se.getSession()); 19 | } 20 | 21 | @Override 22 | public void sessionDestroyed(HttpSessionEvent se) { 23 | LOGGER.debug("Session destroyed %s", se.getSession()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /extra/glassfish-logging-example/src/main/java/zemian/glassfishloggingexample/web/WebAppStartup.java: -------------------------------------------------------------------------------- 1 | package zemian.glassfishloggingexample.web; 2 | 3 | import javax.servlet.ServletContextEvent; 4 | import javax.servlet.ServletContextListener; 5 | import javax.servlet.annotation.WebListener; 6 | 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | 10 | /** 11 | * An app listener that uses SLF4J logger. 12 | * 13 | * @author zedeng 14 | */ 15 | @WebListener 16 | public class WebAppStartup implements ServletContextListener { 17 | 18 | private static final Logger LOGGER = LoggerFactory.getLogger(WebAppStartup.class); 19 | 20 | @Override 21 | public void contextInitialized(ServletContextEvent event) { 22 | LOGGER.debug("WebApp is starting up."); 23 | LOGGER.info("WebApp initialized."); 24 | } 25 | 26 | @Override 27 | public void contextDestroyed(ServletContextEvent event) { 28 | LOGGER.info("WebApp destroyed."); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /extra/hello-script/web/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 4.0.0 7 | 8 | 9 | zemian 10 | hello-script-pom 11 | 1.0-SNAPSHOT 12 | ../pom.xml 13 | 14 | 15 | hello-webapp 16 | war 17 | 18 | 19 | 20 | 21 | org.apache.maven.plugins 22 | maven-war-plugin 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /common-service/src/main/java/zemian/service/security/Md5Base64Encrypt.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.service.security; 6 | 7 | import java.security.MessageDigest; 8 | import javax.xml.bind.DatatypeConverter; 9 | 10 | /** 11 | * 12 | * @author zedeng 13 | */ 14 | public class Md5Base64Encrypt implements Encrypt { 15 | @Override 16 | public byte[] encrypt(byte[] plainData) { 17 | try { 18 | MessageDigest md = MessageDigest.getInstance("SHA-256"); 19 | byte[] digestedData = md.digest(plainData); 20 | String result = DatatypeConverter.printBase64Binary(digestedData); 21 | return result.getBytes("UTF-8"); 22 | } catch (Exception e) { 23 | throw new RuntimeException("Failed to hash data.", e); 24 | } 25 | } 26 | 27 | @Override 28 | public byte[] decrypt(byte[] securedData) { 29 | throw new UnsupportedOperationException("Not supported."); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /extra/mvc-example/src/main/java/zemian/mvcexample/web/WebRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | package zemian.mvcexample.web; 5 | 6 | import javax.servlet.Servlet; 7 | import javax.servlet.http.HttpServletRequest; 8 | 9 | /** 10 | * A class to encapsulate all the data needed to process a request by a Controller. 11 | * 12 | * @author zedeng 13 | */ 14 | public class WebRequest { 15 | private HttpServletRequest httpServletRequest; 16 | private String controllerName; 17 | 18 | public String getControllerName() { 19 | return controllerName; 20 | } 21 | 22 | public void setControllerName(String controllerName) { 23 | this.controllerName = controllerName; 24 | } 25 | 26 | public void setHttpServletRequest(HttpServletRequest httpServletRequest) { 27 | this.httpServletRequest = httpServletRequest; 28 | } 29 | 30 | public HttpServletRequest getHttpServletRequest() { 31 | return httpServletRequest; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /jsp-example/src/main/java/zemian/jspexample/web/WebAppStartup.java: -------------------------------------------------------------------------------- 1 | package zemian.jspexample.web; 2 | 3 | import javax.servlet.ServletContextEvent; 4 | import javax.servlet.ServletContextListener; 5 | import javax.servlet.annotation.WebListener; 6 | import zemian.service.logging.Logger; 7 | 8 | @WebListener 9 | public class WebAppStartup implements ServletContextListener { 10 | private static final Logger LOGGER = new Logger(WebAppStartup.class); 11 | 12 | @Override 13 | public void contextInitialized(ServletContextEvent event) { 14 | LOGGER.debug("WebApp is starting up."); 15 | 16 | // Insert some variables to show in jsp-vars.jsp 17 | event.getServletContext().setAttribute("myAppName", "JspExample-1.0-SNAPSHOT"); 18 | 19 | LOGGER.info("WebApp initialized."); 20 | } 21 | 22 | @Override 23 | public void contextDestroyed(ServletContextEvent event) { 24 | LOGGER.info("WebApp destroyed."); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /common-service/src/main/java/zemian/service/logging/LogFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.service.logging; 6 | 7 | /** 8 | * A factory class to create Log service instances. This is the main entry point 9 | * to use the Log service. 10 | * 11 | *

Example:

12 | *
13 |  class MyService {
14 |    static Log LOG = LogFactory.createLog(MyService.class);
15 |    public void run() {
16 |      LOG.info(Message.msg("%s service is running now.", this));
17 |    }
18 |  }
19 |  
20 | * 21 | * Or you can use the Logger helper class to have less imports. 22 | *
23 |  class MyService2 {
24 |    static Logger LOGGER = new Logger(MyService2.class);
25 |    public void run() {
26 |      LOGGER.info("%s service is running now.", this));
27 |    }
28 |  }
29 |  
30 | * 31 | * @author zedeng 32 | */ 33 | public class LogFactory { 34 | public static Log createLog(Class loggerClass) { 35 | return new JdkLog(loggerClass); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /common-service/src/test/java/zemian/service/util/TupleTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.service.util; 6 | 7 | import org.hamcrest.CoreMatchers; 8 | import org.junit.*; 9 | import static org.junit.Assert.*; 10 | import static zemian.service.util.Tuple.tuple; 11 | 12 | /** 13 | * 14 | * @author zedeng 15 | */ 16 | public class TupleTest { 17 | @Test 18 | public void testTuple() { 19 | Tuple t = Tuple.tuple("A", "B"); 20 | assertThat(t.getA(), CoreMatchers.is("A")); 21 | assertThat(t.getB(), CoreMatchers.is("B")); 22 | 23 | Tuple t2 = Tuple.tuple("A", 123); 24 | assertThat(t2.getA(), CoreMatchers.is("A")); 25 | assertThat(t2.getB(), CoreMatchers.is(123)); 26 | 27 | Tuple, String> t3 = tuple(tuple("A", "B"), "C"); 28 | assertThat(t3.getA(), CoreMatchers.is(tuple("A", "B"))); 29 | assertThat(t3.getB(), CoreMatchers.is("C")); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /jpa-example/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 4.0.0 6 | 7 | 8 | zemian 9 | java-ee6-examples-parent-pom 10 | 1.0-SNAPSHOT 11 | ../pom.xml 12 | 13 | 14 | zemian 15 | jpa-example 16 | jar 17 | 18 | 19 | 20 | junit 21 | junit 22 | test 23 | 24 | 25 | zemian 26 | common-service 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | # About java-ee6-examples 2 | 3 | Explore Java EE 6 by examples 4 | You will find Maven based projects that explore some of major EE 6 5 | components or specs with working code examples. Most of the examples 6 | are tested on Glassfish Server 3. 7 | 8 | The purpose of this project is just for my own learning and exploration. 9 | You may use these as tutorial and further explore the EE stack yourself. 10 | 11 | Author: Zemian Deng 12 | You may also want to visit my blog at http://saltnlight5.blogspot.com 13 | to see more detailed explanations with some of these examples. 14 | 15 | Project Source Code Hosting: 16 | https://github.com/saltnlight5/java-ee6-examples 17 | 18 | # Dependencies 19 | These sample applications try not to use any external dependencies jars other 20 | than the EE API provided by application server. However on some modules 21 | examples, you do need to install MySQL driver jar into your application server 22 | and setup DataSource in order to work. See the individual module example's README 23 | file for details. 24 | -------------------------------------------------------------------------------- /common-service/src/test/java/zemian/service/security/Md5Base64EncryptTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.service.security; 6 | 7 | import org.hamcrest.CoreMatchers; 8 | import org.junit.Assert; 9 | import org.junit.Test; 10 | 11 | /** 12 | * 13 | * @author zedeng 14 | */ 15 | public class Md5Base64EncryptTest { 16 | 17 | private Encrypt encrypt = new Md5Base64Encrypt(); 18 | 19 | @Test 20 | public void testEncrypt() { 21 | byte[] hashed = encrypt.encrypt("Zemian".getBytes()); 22 | Assert.assertThat(new String(hashed), CoreMatchers.is("6GhpmX+6PjN0K3oQZEOaBSn06zJqkMGs395uqI8P4GI=")); 23 | 24 | hashed = encrypt.encrypt("Zemian Deng".getBytes()); 25 | Assert.assertThat(new String(hashed), CoreMatchers.is("yek+nr13av13NkYinByRyufYNrs++117qZpKgsRsmTM=")); 26 | 27 | hashed = encrypt.encrypt("S3cret123&^%".getBytes()); 28 | System.out.println(new String(hashed)); 29 | Assert.assertThat(new String(hashed), CoreMatchers.is("jro0If66LNlKtR919shouOJw10KSGyN2/y5HtBX/4NM=")); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /servlet3-cdi-example/src/main/java/zemian/servlet3example/web/UserServlet.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3example.web; 2 | 3 | import java.io.IOException; 4 | import java.util.ArrayList; 5 | import java.util.Collections; 6 | import java.util.List; 7 | import javax.inject.Inject; 8 | import javax.servlet.ServletException; 9 | import javax.servlet.annotation.WebServlet; 10 | import javax.servlet.http.HttpServletRequest; 11 | import javax.servlet.http.HttpServletResponse; 12 | import zemian.servlet3example.service.UserService; 13 | 14 | @WebServlet("/user") 15 | public class UserServlet extends HtmlWriterServlet { 16 | 17 | @Inject 18 | private UserService userService; 19 | 20 | @Override 21 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 22 | List users = new ArrayList<>(userService.getUsers()); 23 | Collections.sort(users); 24 | HtmlWriter html = createHtmlWriter(req, resp); 25 | html.header() 26 | .h(1, "List of Users") 27 | .ul(users) 28 | .footer(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /extra/hello-script/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 4.0.0 6 | 7 | 8 | zemian 9 | java-ee6-examples-parent-pom 10 | 1.0-SNAPSHOT 11 | ../pom.xml 12 | 13 | 14 | hello-script-pom 15 | pom 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | zemian 25 | common-service 26 | 27 | 28 | org.codehaus.groovy 29 | groovy-all 30 | 2.4.1 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /servlet3-example/src/main/java/zemian/servlet3example/web/UserServlet.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3example.web; 2 | 3 | import zemian.servlet3example.service.Application; 4 | import java.io.IOException; 5 | import java.util.ArrayList; 6 | import java.util.Collections; 7 | import java.util.List; 8 | import javax.servlet.ServletException; 9 | import javax.servlet.annotation.WebServlet; 10 | import javax.servlet.http.HttpServletRequest; 11 | import javax.servlet.http.HttpServletResponse; 12 | import zemian.servlet3example.service.UserService; 13 | 14 | @WebServlet("/user") 15 | public class UserServlet extends HtmlWriterServlet { 16 | 17 | @Override 18 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 19 | UserService userStore = Application.getInstance().getUserService(); 20 | List users = new ArrayList<>(userStore.getUsers()); 21 | Collections.sort(users); 22 | HtmlWriter html = createHtmlWriter(req, resp); 23 | html.header() 24 | .h(1, "List of Users") 25 | .ul(users) 26 | .footer(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /cdi-example/src/main/java/zemian/cdiexample/web/ServiceListingServlet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.cdiexample.web; 6 | 7 | import java.io.IOException; 8 | import java.util.List; 9 | import javax.inject.Inject; 10 | import javax.servlet.ServletException; 11 | import javax.servlet.annotation.WebServlet; 12 | import javax.servlet.http.HttpServlet; 13 | import javax.servlet.http.HttpServletRequest; 14 | import javax.servlet.http.HttpServletResponse; 15 | import zemian.cdiexample.service.Application; 16 | import zemian.cdiexample.service.Service; 17 | 18 | /** 19 | * 20 | * @author zedeng 21 | */ 22 | @WebServlet("/service-listing") 23 | public class ServiceListingServlet extends HttpServlet { 24 | 25 | @Inject 26 | private Application application; 27 | 28 | @Override 29 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 30 | List services = application.getServices(); 31 | req.setAttribute("services", services); 32 | req.getRequestDispatcher("/service-listing.jsp").forward(req, resp); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /extra/mvc-example/src/main/java/zemian/mvcexample/web/MView.java: -------------------------------------------------------------------------------- 1 | package zemian.mvcexample.web; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | import java.util.Set; 6 | 7 | /** 8 | * User: zedeng 9 | * Date Time: 5/22/15 7:02 AM 10 | */ 11 | public class MView { 12 | private String viewName; 13 | private Map model = new HashMap<>(); 14 | 15 | public MView(String viewName) { 16 | this.viewName = viewName; 17 | } 18 | 19 | public void put(String name, Object data) { 20 | model.put(name, data); 21 | } 22 | 23 | public boolean hasData() { 24 | return model.size() > 0; 25 | } 26 | 27 | public String getViewName() { 28 | return viewName; 29 | } 30 | 31 | public Set getNames() { 32 | return model.keySet(); 33 | } 34 | 35 | public T get(String name) { 36 | return (T)model.get(name); 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return "MView{" + 42 | "viewName='" + viewName + '\'' + 43 | ", model=" + model + 44 | '}'; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /rest-example/src/main/java/zemian/restexample/resource/ScriptResource.java: -------------------------------------------------------------------------------- 1 | package zemian.restexample.resource; 2 | 3 | import javax.ws.rs.DefaultValue; 4 | import javax.ws.rs.GET; 5 | import javax.ws.rs.Path; 6 | import javax.ws.rs.QueryParam; 7 | import javax.ws.rs.core.Application; 8 | import javax.ws.rs.core.Context; 9 | import javax.ws.rs.core.HttpHeaders; 10 | import javax.ws.rs.core.UriInfo; 11 | 12 | import zemian.service.util.Utils; 13 | 14 | @Path("sscript") 15 | public class ScriptResource { 16 | @Context 17 | private Application application; 18 | @Context 19 | private UriInfo uriInfo; 20 | @Context 21 | private HttpHeaders httpHeaders; 22 | 23 | @GET 24 | public String runScript(@QueryParam("file") @DefaultValue("") String file) { 25 | String result = null; 26 | Object[] params = new Object[]{ 27 | "application", application 28 | , "uriInfo", uriInfo 29 | , "httpHeaders", httpHeaders 30 | }; 31 | if (file.equals("")) { 32 | result = "" + Utils.runGroovyTestScript(params); 33 | } else { 34 | result = "" + Utils.runScript(file, params); 35 | } 36 | return result; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /jsf-example/src/main/webapp/template.xhtml: -------------------------------------------------------------------------------- 1 | 4 | 5 | Zemian's EE 6 JSF Examples 6 | 7 | 8 | 9 | 19 |
20 | 21 |

Welcome to EE 6 JSF Examples

22 |
23 |
24 |
25 | -------------------------------------------------------------------------------- /rest-example/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 4.0.0 6 | 7 | 8 | zemian 9 | java-ee6-examples-parent-pom 10 | 1.0-SNAPSHOT 11 | ../pom.xml 12 | 13 | 14 | zemian 15 | rest-example 16 | war 17 | 18 | 19 | 20 | 21 | org.apache.maven.plugins 22 | maven-war-plugin 23 | 24 | 25 | 26 | 27 | 28 | 29 | zemian 30 | common-service 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /common-service/src/main/java/zemian/service/logging/Logger.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.service.logging; 6 | 7 | /** 8 | * A convenient wrapper entry class to this logging package. This is used 9 | * so user do not need to import many classes and methods provided here is more 10 | * easier to use. 11 | * 12 | * @author zedeng 13 | */ 14 | public class Logger { 15 | private Log log; 16 | public Logger(Class loggerClass) { 17 | log = LogFactory.createLog(loggerClass); 18 | } 19 | public void error(Exception cause, String message, Object ... params) { 20 | log.error(Message.msg(cause, message, params)); 21 | } 22 | public void warn(String message, Object ... params) { 23 | log.warn(Message.msg(message, params)); 24 | } 25 | public void info(String message, Object ... params) { 26 | log.info(Message.msg(message, params)); 27 | } 28 | public void debug(String message, Object ... params) { 29 | log.debug(Message.msg(message, params)); 30 | } 31 | public void trace(String message, Object ... params) { 32 | log.trace(Message.msg(message, params)); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /servlet3-example/README.txt: -------------------------------------------------------------------------------- 1 | = web.xml templates 2 | 3 | == 3.0 You can use annotation, or explicit with web.xml 4 | 5 | 9 | 10 | index 11 | 12 | 13 | 14 | == 2.5 15 | 16 | 20 | 21 | index 22 | 23 | 24 | IndexServlet 25 | IndexServlet 26 | 27 | 28 | IndexServlet 29 | /index 30 | 31 | 32 | -------------------------------------------------------------------------------- /servlet3-example/src/main/java/zemian/servlet3example/service/Application.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.servlet3example.service; 6 | 7 | /** 8 | * A singleton application context space to store global variables and states. 9 | * 10 | * @author zedeng 11 | */ 12 | public class Application implements Service { 13 | public static final String SERVLET_CONTEXT_KEY = Application.class.getName(); 14 | 15 | private static Application INSTANCE = new Application(); 16 | 17 | private Config config; 18 | private UserService userService; 19 | 20 | private Application() { 21 | } 22 | 23 | public static Application getInstance() { 24 | return INSTANCE; 25 | } 26 | 27 | @Override 28 | public void init() { 29 | config = new Config("config.properties"); 30 | 31 | userService = new UserService(); 32 | userService.init(); 33 | } 34 | 35 | @Override 36 | public void destroy() { 37 | userService.destroy(); 38 | } 39 | 40 | public UserService getUserService() { 41 | return userService; 42 | } 43 | 44 | public Config getConfig() { 45 | return config; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /servlet3-example/src/main/java/zemian/servlet3example/web/LoginSession.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.servlet3example.web; 6 | 7 | import java.io.Serializable; 8 | import java.util.Date; 9 | import java.util.UUID; 10 | 11 | /** 12 | * A application session object that holds logged in user information. 13 | * 14 | * @author zedeng 15 | */ 16 | public class LoginSession implements Serializable { 17 | private static final long serialVersionUID = 1L; 18 | public static final String LOGIN_SESSION_KEY = LoginSession.class.getSimpleName(); 19 | private String id = UUID.randomUUID().toString(); 20 | private Date dateCreated = new Date(); 21 | private String username; 22 | 23 | public LoginSession(String username) { 24 | this.username = username; 25 | } 26 | 27 | public String getUsername() { 28 | return username; 29 | } 30 | 31 | public String getId() { 32 | return id; 33 | } 34 | 35 | public Date getDateCreated() { 36 | return dateCreated; 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return "SessionData(id=" + id + ", username=" + username + ", dateCreated=" + dateCreated + ")"; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /jsf-example/src/main/java/zemian/jsfexample/service/UserService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.jsfexample.service; 6 | 7 | import java.util.List; 8 | import zemian.jsfexample.data.User; 9 | import zemian.service.util.Utils; 10 | 11 | /** 12 | * 13 | * @author zedeng 14 | */ 15 | public class UserService { 16 | public List findAllUsers() { 17 | return Utils.list( 18 | createSampleUser("tester1", "Zemian", "Tester"), 19 | createSampleUser("tester2", "John", "Tester"), 20 | createSampleUser("tester3", "Jay", "Tester"), 21 | createSampleUser("tester4", "Joe", "Tester"), 22 | createSampleUser("tester5", "Jane", "Tester") 23 | ); 24 | } 25 | 26 | private User createSampleUser(String username, String firstName, String lastName) { 27 | User user = new User(); 28 | user.setUsername(username); 29 | user.setFirstName(firstName); 30 | user.setLastName(lastName); 31 | user.setEmail(username + "@test.com"); 32 | user.setAddressLine1("123 Abc St"); 33 | user.setCity("Orlando"); 34 | user.setState("FL"); 35 | user.setZipCode("32829"); 36 | return user; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /servlet3-cdi-example/src/main/java/zemian/servlet3example/web/LoginSession.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.servlet3example.web; 6 | 7 | import java.io.Serializable; 8 | import java.util.Date; 9 | import java.util.UUID; 10 | 11 | /** 12 | * A application session object that holds logged in user information. 13 | * 14 | * @author zedeng 15 | */ 16 | public class LoginSession implements Serializable { 17 | private static final long serialVersionUID = 1L; 18 | public static final String LOGIN_SESSION_KEY = LoginSession.class.getSimpleName(); 19 | private String id = UUID.randomUUID().toString(); 20 | private Date dateCreated = new Date(); 21 | private String username; 22 | 23 | public LoginSession(String username) { 24 | this.username = username; 25 | } 26 | 27 | public String getUsername() { 28 | return username; 29 | } 30 | 31 | public String getId() { 32 | return id; 33 | } 34 | 35 | public Date getDateCreated() { 36 | return dateCreated; 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return "SessionData(id=" + id + ", username=" + username + ", dateCreated=" + dateCreated + ")"; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /jsp-example/src/main/webapp/jsp-vars.jsp: -------------------------------------------------------------------------------- 1 | <%@ include file="header.jsp" %> 2 | 3 |

JSP Examples

4 |

Implicit Variables

5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 |
NameInstanceExample
applicationScope${applicationScope}${applicationScope['myAppName']}
sessionSope${sessionSope}${sessionSope['loginSession']}
pageScope${pageScope}${pageScope['javax.servlet.jsp.jspConfig']}
requestScope${requestScope}${requestScope['foo']}
param${param}${param['query']}
header${header}${header['user-agent']}
cookie${cookie}${cookie['JSESSIONID']}
pageContext${pageContext}${pageContext.request.contextPath}
34 | 35 | <%@ include file="footer.jsp" %> 36 | -------------------------------------------------------------------------------- /servlet3-example/src/main/java/zemian/servlet3example/service/Config.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3example.service; 2 | 3 | import java.io.Reader; 4 | import java.util.Properties; 5 | import java.util.Set; 6 | 7 | /** 8 | * Load and provide config properties for other services. It first check for user directory 9 | * for a config file, if not found then load a classpath resource as default. 10 | * 11 | * @author zedeng 12 | * 13 | */ 14 | public class Config { 15 | private Properties config = new Properties(); 16 | 17 | public Config(String fileName) { 18 | FileUtils.loadOptionalFile(fileName, getClass().getPackage().getName(), new FileUtils.ReaderAction() { 19 | @Override 20 | public void onReader(Reader reader) throws Exception { 21 | config.load(reader); 22 | } 23 | }); 24 | } 25 | 26 | public Set getKeys() { 27 | return config.stringPropertyNames(); 28 | } 29 | 30 | public String getValue(String key) { 31 | return config.getProperty(key); 32 | } 33 | 34 | public String getValue(String key, String def) { 35 | String result = config.getProperty(key); 36 | if (result == null) 37 | result = def; 38 | return result; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /common-service/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 4.0.0 6 | 7 | 8 | zemian 9 | java-ee6-examples-parent-pom 10 | 1.0-SNAPSHOT 11 | ../pom.xml 12 | 13 | 14 | zemian 15 | common-service 16 | jar 17 | 18 | 19 | 20 | junit 21 | junit 22 | test 23 | 24 | 25 | mysql 26 | mysql-connector-java 27 | test 28 | 29 | 30 | com.h2database 31 | h2 32 | test 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /cdi-example/src/main/java/zemian/cdiexample/web/HelloServlet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.cdiexample.web; 6 | 7 | import java.io.IOException; 8 | import java.io.PrintWriter; 9 | import java.util.List; 10 | import javax.inject.Inject; 11 | import javax.servlet.ServletException; 12 | import javax.servlet.annotation.WebServlet; 13 | import javax.servlet.http.HttpServlet; 14 | import javax.servlet.http.HttpServletRequest; 15 | import javax.servlet.http.HttpServletResponse; 16 | import zemian.cdiexample.service.HelloService; 17 | import zemian.cdiexample.service.Service; 18 | 19 | /** 20 | * 21 | * @author zedeng 22 | */ 23 | @WebServlet("/hello") 24 | public class HelloServlet extends HttpServlet { 25 | 26 | @Inject 27 | private HelloService helloService; 28 | 29 | @Override 30 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 31 | String message = helloService.getMessage(); 32 | 33 | PrintWriter writer = resp.getWriter(); 34 | writer.println(""); 35 | writer.println(""); 36 | writer.println(""); 37 | writer.println("

" + message + "

"); 38 | writer.println(""); 39 | writer.println(""); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /jsp-example/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 4.0.0 6 | 7 | 8 | zemian 9 | java-ee6-examples-parent-pom 10 | 1.0-SNAPSHOT 11 | ../pom.xml 12 | 13 | 14 | zemian 15 | jsp-example 16 | war 17 | 18 | 19 | 20 | 21 | org.apache.maven.plugins 22 | maven-war-plugin 23 | 24 | 25 | 26 | 27 | 28 | 29 | junit 30 | junit 31 | test 32 | 33 | 34 | zemian 35 | common-service 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /extra/subproject-template/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 4.0.0 6 | 7 | 8 | zemian 9 | java-ee6-examples-parent-pom 10 | 1.0-SNAPSHOT 11 | ../pom.xml 12 | 13 | 14 | subproject-template 15 | war 16 | 17 | 18 | 19 | 20 | org.apache.maven.plugins 21 | maven-war-plugin 22 | 23 | 24 | 25 | 26 | 27 | 28 | junit 29 | junit 30 | test 31 | 32 | 33 | zemian 34 | common-service 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /servlet3-example/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 4.0.0 6 | 7 | 8 | zemian 9 | java-ee6-examples-parent-pom 10 | 1.0-SNAPSHOT 11 | ../pom.xml 12 | 13 | 14 | zemian 15 | servlet3-example 16 | war 17 | 18 | 19 | 20 | 21 | org.apache.maven.plugins 22 | maven-war-plugin 23 | 24 | 25 | 26 | 27 | 28 | 29 | junit 30 | junit 31 | test 32 | 33 | 34 | zemian 35 | common-service 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /cdi-example/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 4.0.0 6 | 7 | 8 | zemian 9 | java-ee6-examples-parent-pom 10 | 1.0-SNAPSHOT 11 | ../pom.xml 12 | 13 | 14 | zemian 15 | cdi-example 16 | war 17 | 18 | 19 | 20 | 21 | org.apache.maven.plugins 22 | maven-war-plugin 23 | 24 | 25 | 26 | 27 | 28 | 29 | junit 30 | junit 31 | test 32 | 33 | 34 | zemian 35 | common-service 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /jsf-example/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 4.0.0 6 | 7 | 8 | zemian 9 | java-ee6-examples-parent-pom 10 | 1.0-SNAPSHOT 11 | ../pom.xml 12 | 13 | 14 | zemian 15 | jsf-example 16 | war 17 | 18 | 19 | 20 | junit 21 | junit 22 | test 23 | 24 | 25 | zemian 26 | common-service 27 | 28 | 29 | 30 | 31 | 32 | 33 | org.apache.maven.plugins 34 | maven-war-plugin 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /servlet3-example/src/main/java/zemian/servlet3example/web/SysPropsServlet.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3example.web; 2 | 3 | import java.io.IOException; 4 | import java.util.Map; 5 | import java.util.TreeMap; 6 | import javax.servlet.ServletException; 7 | import javax.servlet.annotation.WebServlet; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | import zemian.servlet3example.service.Application; 11 | import zemian.servlet3example.service.Config; 12 | 13 | @WebServlet("/sys-props") 14 | public class SysPropsServlet extends HtmlWriterServlet { 15 | 16 | @Override 17 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 18 | Map appConfig = new TreeMap(); 19 | Config config = Application.getInstance().getConfig(); 20 | for (String key : config.getKeys()) { 21 | appConfig.put(key, config.getValue(key)); 22 | } 23 | 24 | Map sysProps = new TreeMap(System.getProperties()); 25 | 26 | HtmlWriter html = createHtmlWriter(req, resp); 27 | html.header() 28 | .h(1, "Application Config Properties") 29 | .table(appConfig) 30 | .h(1, "Java System Properties") 31 | .table(sysProps) 32 | .footer(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /servlet3-cdi-example/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 4.0.0 6 | 7 | 8 | zemian 9 | java-ee6-examples-parent-pom 10 | 1.0-SNAPSHOT 11 | ../pom.xml 12 | 13 | 14 | zemian 15 | servlet3-cdi-example 16 | war 17 | 18 | 19 | 20 | 21 | org.apache.maven.plugins 22 | maven-war-plugin 23 | 24 | 25 | 26 | 27 | 28 | 29 | junit 30 | junit 31 | test 32 | 33 | 34 | zemian 35 | common-service 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /cdi-example/src/main/java/zemian/cdiexample/service/Application.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.cdiexample.service; 6 | 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | import javax.annotation.PostConstruct; 10 | import javax.annotation.PreDestroy; 11 | import javax.enterprise.context.ApplicationScoped; 12 | import javax.enterprise.event.Observes; 13 | import javax.enterprise.inject.Instance; 14 | import javax.inject.Inject; 15 | import zemian.service.logging.Logger; 16 | 17 | /** 18 | * 19 | * @author zedeng 20 | */ 21 | @ApplicationScoped 22 | public class Application { 23 | private static final Logger LOGGER = new Logger(Application.class); 24 | 25 | /** All available services discovered by CDI */ 26 | @Inject 27 | private Instance services; 28 | 29 | public List getServices() { 30 | List result = new ArrayList<>(); 31 | for (Service service : services) 32 | result.add(service); 33 | return result; 34 | } 35 | 36 | @PostConstruct 37 | public void init() { 38 | LOGGER.info("Application %s is starting with %d services", this, getServices().size()); 39 | } 40 | 41 | @PreDestroy 42 | public void destroy() { 43 | LOGGER.info("Application %s is shutting down.", this); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /jsf-example/src/main/webapp/table-data.xhtml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 |

Data Table Example

9 | 10 | 11 | 12 | Username 13 | #{user.username} 14 | 15 | 16 | First Name 17 | #{user.firstName} 18 | 19 | 20 | Last Name 21 | #{user.lastName} 22 | 23 | 24 | Email 25 | #{user.email} 26 | 27 | 28 |
29 |
30 |
31 | -------------------------------------------------------------------------------- /jsf-example/src/main/webapp/form-inputs.xhtml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 |

Form Process

9 | 10 |

Simple Inputs

11 | 12 | 13 |

14 | 15 | 16 |

17 | 18 |

19 | 20 | 21 |

22 | 23 |

24 | 25 | 26 |

27 | 28 | 29 |
30 |
31 |
32 |
33 | -------------------------------------------------------------------------------- /extra/mvc-example/src/main/java/zemian/mvcexample/web/WebAppStartup.java: -------------------------------------------------------------------------------- 1 | package zemian.mvcexample.web; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import zemian.mvcexample.service.Application; 6 | 7 | import javax.servlet.ServletContext; 8 | import javax.servlet.ServletContextEvent; 9 | import javax.servlet.ServletContextListener; 10 | import javax.servlet.annotation.WebListener; 11 | 12 | @WebListener 13 | public class WebAppStartup implements ServletContextListener { 14 | private static final Logger LOGGER = LoggerFactory.getLogger(WebAppStartup.class); 15 | 16 | @Override 17 | public void contextInitialized(ServletContextEvent event) { 18 | Application app = Application.getInstance(); 19 | app.init(); 20 | 21 | ServletContext ctx = event.getServletContext(); 22 | ctx.setAttribute("app", app); 23 | 24 | // Save the contextPath for easy JSP link display 25 | ctx.setAttribute("contextPath", ctx.getContextPath()); 26 | ctx.setAttribute("controllerPath", ctx.getContextPath() + app.getControllerServletPath()); 27 | 28 | LOGGER.info("Application {} is initialized.", app); 29 | } 30 | 31 | @Override 32 | public void contextDestroyed(ServletContextEvent event) { 33 | Application app = Application.getInstance(); 34 | app.destroy(); 35 | LOGGER.info("Application {} is destroyed.", app); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /rest-example/src/main/java/zemian/restexample/resource/ClassAnalysisResource.java: -------------------------------------------------------------------------------- 1 | package zemian.restexample.resource; 2 | 3 | import java.net.URL; 4 | import java.util.List; 5 | 6 | import javax.ws.rs.GET; 7 | import javax.ws.rs.Path; 8 | import javax.ws.rs.QueryParam; 9 | 10 | import zemian.service.util.Utils; 11 | 12 | @Path("/class-analysis") 13 | public class ClassAnalysisResource { 14 | 15 | @GET 16 | @Path("/resource") 17 | public String getResource(@QueryParam("name") String name) { 18 | ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 19 | URL res = classLoader.getResource(name); 20 | return res.toString(); 21 | } 22 | 23 | @GET 24 | @Path("/resource-content") 25 | public String getResourceContent(@QueryParam("name") String name) { 26 | List lines = Utils.readResourceLines(name); 27 | StringBuffer sb = new StringBuffer(); 28 | for (String line : lines) { 29 | sb.append(line).append("\n"); 30 | } 31 | return sb.toString(); 32 | } 33 | 34 | @GET 35 | @Path("/class") 36 | public String getClassLocation(@QueryParam("name") String name) throws ClassNotFoundException { 37 | ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 38 | Class clazz = classLoader.loadClass(name); 39 | URL loc = clazz.getProtectionDomain().getCodeSource().getLocation(); 40 | return loc.toString(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /servlet3-jsp-example/src/main/java/zemian/servlet3jspexample/WebAppStartup.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3jspexample; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | import javax.servlet.ServletContextEvent; 7 | import javax.servlet.ServletContextListener; 8 | import javax.servlet.annotation.WebListener; 9 | 10 | @WebListener 11 | public class WebAppStartup implements ServletContextListener { 12 | private static final Logger LOGGER = LoggerFactory.getLogger(WebAppStartup.class); 13 | 14 | 15 | @Override 16 | public void contextInitialized(ServletContextEvent event) { 17 | LOGGER.debug("WebApp is starting up."); 18 | Application app = Application.getInstance(); 19 | app.init(); 20 | 21 | // Since we are providing singleton access to application, storing 22 | // it into ServletContext is really redundant, but for demo purpose, one 23 | // can store it there and it can be access by all web components without 24 | // having to call the singleton access as alternative. 25 | event.getServletContext().setAttribute(Application.SERVLET_CONTEXT_KEY, app); 26 | LOGGER.info("WebApp initialized."); 27 | } 28 | 29 | @Override 30 | public void contextDestroyed(ServletContextEvent event) { 31 | Application app = Application.getInstance(); 32 | app.destroy(); 33 | LOGGER.info("WebApp destroyed."); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /extra/jsf-demo/src/main/java/jsfdemo/service/ServiceApplicationMain.java: -------------------------------------------------------------------------------- 1 | package jsfdemo.service; 2 | 3 | /** 4 | * Application startup and shutdown from standalone SE, command line environment. 5 | * 6 | *

After application has started, the process will continue to run on foreground. User can hit CTRL+C to shutdown. 7 | * 8 | * @author zedeng 9 | * 10 | */ 11 | public class ServiceApplicationMain { 12 | private static final ServiceApplication serviceApplication = ServiceApplication.getInstance(); 13 | 14 | public static void main(String[] args) throws Exception { 15 | new ServiceApplicationMain().run(); 16 | } 17 | 18 | public void run() { 19 | registerShutdownHook(); 20 | startApplication(); 21 | goToAwaitMode(); 22 | } 23 | 24 | private void goToAwaitMode() { 25 | // Put current thread into await mode. User can hit CTRL+C to shutdown. 26 | synchronized(serviceApplication) { 27 | try { 28 | serviceApplication.wait(); 29 | } catch (InterruptedException e) { 30 | // TODO Auto-generated catch block 31 | e.printStackTrace(); 32 | } 33 | } 34 | } 35 | 36 | private void startApplication() { 37 | serviceApplication.quickStart(); 38 | } 39 | 40 | private void registerShutdownHook() { 41 | Runtime.getRuntime().addShutdownHook(new Thread() { 42 | @Override 43 | public void run() { 44 | serviceApplication.quickStop(); 45 | } 46 | }); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /extra/cdi-camel/src/main/java/jsfdemo/service/ServiceApplicationMain.java: -------------------------------------------------------------------------------- 1 | package jsfdemo.service; 2 | 3 | /** 4 | * Application startup and shutdown from standalone SE, command line environment. 5 | * 6 | *

After application has started, the process will continue to run on foreground. User can hit CTRL+C to shutdown. 7 | * 8 | * @author zedeng 9 | * 10 | */ 11 | public class ServiceApplicationMain { 12 | private static final ServiceApplication serviceApplication = ServiceApplication.getInstance(); 13 | 14 | public static void main(String[] args) throws Exception { 15 | new ServiceApplicationMain().run(); 16 | } 17 | 18 | public void run() { 19 | registerShutdownHook(); 20 | startApplication(); 21 | goToAwaitMode(); 22 | } 23 | 24 | private void goToAwaitMode() { 25 | // Put current thread into await mode. User can hit CTRL+C to shutdown. 26 | synchronized(serviceApplication) { 27 | try { 28 | serviceApplication.wait(); 29 | } catch (InterruptedException e) { 30 | // TODO Auto-generated catch block 31 | e.printStackTrace(); 32 | } 33 | } 34 | } 35 | 36 | private void startApplication() { 37 | serviceApplication.quickStart(); 38 | } 39 | 40 | private void registerShutdownHook() { 41 | Runtime.getRuntime().addShutdownHook(new Thread() { 42 | @Override 43 | public void run() { 44 | serviceApplication.quickStop(); 45 | } 46 | }); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /extra/performance/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 4.0.0 6 | 7 | 8 | zemian 9 | java-ee6-examples-parent-pom 10 | 1.0-SNAPSHOT 11 | ../pom.xml 12 | 13 | 14 | performance 15 | war 16 | 17 | 18 | 19 | 20 | org.apache.maven.plugins 21 | maven-war-plugin 22 | 23 | 24 | 25 | 26 | 27 | 28 | junit 29 | junit 30 | test 31 | 32 | 33 | zemian 34 | common-service 35 | 36 | 37 | mysql 38 | mysql-connector-java 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /extra/jsf-primefaces-example/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 4.0.0 6 | 7 | 8 | zemian 9 | java-ee6-examples-parent-pom 10 | 1.0-SNAPSHOT 11 | ../pom.xml 12 | 13 | 14 | zemian 15 | jsf-primefaces-example 16 | war 17 | 18 | 19 | 20 | 21 | org.apache.maven.plugins 22 | maven-war-plugin 23 | 24 | 25 | 26 | 27 | 28 | 29 | junit 30 | junit 31 | test 32 | 33 | 34 | zemian 35 | common-service 36 | 37 | 38 | org.primefaces 39 | primefaces 40 | 5.1 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /cdi-example/src/main/java/zemian/cdiexample/web/WebAppStartup.java: -------------------------------------------------------------------------------- 1 | package zemian.cdiexample.web; 2 | 3 | import java.util.Enumeration; 4 | import javax.inject.Inject; 5 | import javax.servlet.ServletContext; 6 | import javax.servlet.ServletContextEvent; 7 | import javax.servlet.ServletContextListener; 8 | import javax.servlet.annotation.WebListener; 9 | import zemian.cdiexample.service.Application; 10 | import zemian.service.logging.Logger; 11 | 12 | @WebListener 13 | public class WebAppStartup implements ServletContextListener { 14 | private static final Logger LOGGER = new Logger(WebAppStartup.class); 15 | 16 | @Inject 17 | private Application application; 18 | 19 | @Override 20 | public void contextInitialized(ServletContextEvent event) { 21 | LOGGER.debug("WebApp is starting up."); 22 | //logServletContextAttributes(event.getServletContext()); 23 | 24 | LOGGER.info("Application %s is ready.", application); 25 | 26 | LOGGER.info("WebApp initialized."); 27 | } 28 | 29 | @Override 30 | public void contextDestroyed(ServletContextEvent event) { 31 | LOGGER.info("WebApp destroyed."); 32 | } 33 | 34 | private void logServletContextAttributes(ServletContext sc) { 35 | Enumeration names = sc.getAttributeNames(); 36 | while (names.hasMoreElements()) { 37 | String name = names.nextElement(); 38 | LOGGER.info("SC attr '%s'=%s", name, sc.getAttribute(name)); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /extra/jpa-sakila/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 4.0.0 6 | 7 | 8 | zemian 9 | java-ee6-examples-parent-pom 10 | 1.0-SNAPSHOT 11 | ../../pom.xml 12 | 13 | 14 | zemian 15 | jpa-sakila 16 | jar 17 | 18 | 19 | 20 | junit 21 | junit 22 | test 23 | 24 | 25 | zemian 26 | common-service 27 | 28 | 29 | org.eclipse.persistence 30 | eclipselink 31 | 2.5.2 32 | 33 | 34 | org.eclipse.persistence 35 | org.eclipse.persistence.jpa.modelgen.processor 36 | 2.5.2 37 | provided 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /jpa-example/src/main/java/zemian/jpaexample/service/UserService.java: -------------------------------------------------------------------------------- 1 | package zemian.jpaexample.service; 2 | 3 | import java.io.UnsupportedEncodingException; 4 | import java.util.List; 5 | 6 | import javax.annotation.PostConstruct; 7 | import javax.ejb.Stateless; 8 | import javax.inject.Inject; 9 | 10 | import zemian.jpaexample.dao.UserDao; 11 | import zemian.jpaexample.data.User; 12 | import zemian.service.logging.Logger; 13 | import zemian.service.security.Encrypt; 14 | 15 | @Stateless 16 | public class UserService { 17 | private static final Logger LOGGER = new Logger(UserService.class); 18 | 19 | @Inject 20 | private UserDao userDao; 21 | 22 | @Inject 23 | private Encrypt encrypt; 24 | 25 | @PostConstruct 26 | public void init() { 27 | LOGGER.info("Service inited."); 28 | } 29 | 30 | public void encryptPasswordAndSaveUser(User user, String plainPassword) { 31 | try { 32 | byte[] hashedPassword = encrypt.encrypt(plainPassword.getBytes("UTF-8")); 33 | user.setPassword(new String(hashedPassword, "UTF-8")); 34 | userDao.save(user); 35 | } catch (UnsupportedEncodingException e) { 36 | throw new RuntimeException("Failed to save and encrypt user with password.", e); 37 | } 38 | } 39 | 40 | public List findMostActiveUsers() { 41 | LOGGER.debug("Finding all active users."); 42 | LOGGER.info("Using userDao=%s", userDao); 43 | return userDao.findMostActiveUsers(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /extra/mvc-example/src/main/java/zemian/mvcexample/web/controller/DirectView.java: -------------------------------------------------------------------------------- 1 | package zemian.mvcexample.web.controller; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import zemian.mvcexample.service.Application; 6 | import zemian.mvcexample.web.Controller; 7 | import zemian.mvcexample.web.MView; 8 | import zemian.mvcexample.web.WebRequest; 9 | 10 | import javax.servlet.http.HttpServletRequest; 11 | 12 | /** 13 | * A simple controller that does nothing but to return empty data model. User may use this to map as pass through 14 | * to a view page. 15 | * 16 | * User: zedeng 17 | * Date Time: 5/21/15 9:23 PM 18 | */ 19 | public class DirectView implements Controller { 20 | private static final Logger LOGGER = LoggerFactory.getLogger(DirectView.class); 21 | 22 | private Application app; 23 | 24 | public DirectView(Application app) { 25 | this.app = app; 26 | } 27 | 28 | @Override 29 | public MView handle(WebRequest wreq) throws Exception { 30 | String subPath = getSubPath(wreq.getControllerName(), wreq.getHttpServletRequest()); 31 | LOGGER.debug("Testing controller with subPath={}", subPath); 32 | return new MView(wreq.getControllerName() + subPath); 33 | } 34 | 35 | private String getSubPath(String controllerName, HttpServletRequest req) { 36 | String uri = req.getRequestURI(); 37 | String base = req.getContextPath() + app.getControllerServletPath() + "/" + controllerName; 38 | String result = uri.substring(base.length()); 39 | return result; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /common-service/src/main/java/zemian/service/util/Tuple.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.service.util; 6 | 7 | import java.util.Objects; 8 | 9 | /** 10 | * A tuple of two elements. 11 | * 12 | * @author zedeng 13 | */ 14 | public class Tuple { 15 | private A a; 16 | private B b; 17 | 18 | public A getA() { 19 | return a; 20 | } 21 | 22 | public void setA(A a) { 23 | this.a = a; 24 | } 25 | 26 | public B getB() { 27 | return b; 28 | } 29 | 30 | public void setB(B b) { 31 | this.b = b; 32 | } 33 | 34 | @Override 35 | public int hashCode() { 36 | int hash = 7; 37 | hash = 23 * hash + Objects.hashCode(this.a); 38 | hash = 23 * hash + Objects.hashCode(this.b); 39 | return hash; 40 | } 41 | 42 | @Override 43 | public boolean equals(Object obj) { 44 | if (obj == null) { 45 | return false; 46 | } 47 | if (getClass() != obj.getClass()) { 48 | return false; 49 | } 50 | final Tuple other = (Tuple) obj; 51 | if (!Objects.equals(this.a, other.a)) { 52 | return false; 53 | } 54 | if (!Objects.equals(this.b, other.b)) { 55 | return false; 56 | } 57 | return true; 58 | } 59 | 60 | @Override 61 | public String toString() { 62 | return "Tuple(a=" + a + ", b=" + b + ")"; 63 | } 64 | 65 | public static Tuple tuple(X a, Y b) { 66 | Tuple result = new Tuple<>(); 67 | result.setA(a); 68 | result.setB(b); 69 | return result; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /jsf-example/src/main/webapp/form-selections.xhtml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 |

Form Process

9 | 10 |

Selections

11 |

12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |

20 |

21 | 22 | 23 | 24 | 25 |

26 | 27 | 28 |
29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /servlet3-jsp-example/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 4.0.0 6 | 7 | 8 | zemian 9 | java-ee6-examples-parent-pom 10 | 1.0-SNAPSHOT 11 | ../pom.xml 12 | 13 | 14 | zemian 15 | servlet3-jsp-example 16 | war 17 | 18 | 19 | 20 | 21 | org.apache.maven.plugins 22 | maven-war-plugin 23 | 24 | 25 | 26 | 27 | 28 | 29 | junit 30 | junit 31 | test 32 | 33 | 34 | zemian 35 | common-service 36 | 37 | 38 | org.slf4j 39 | slf4j-api 40 | 1.7.12 41 | 42 | 43 | org.slf4j 44 | slf4j-log4j12 45 | 1.7.12 46 | runtime 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /extra/cdi-camel/src/main/java/jsfdemo/web/managedbean/TableData.java: -------------------------------------------------------------------------------- 1 | package jsfdemo.web.managedbean; 2 | 3 | import java.math.BigDecimal; 4 | 5 | import javax.faces.bean.ManagedBean; 6 | import javax.faces.bean.SessionScoped; 7 | 8 | /** 9 | * Example from http://www.mkyong.com/jsf2/jsf-2-datatable-example/ 10 | * 11 | * @author zedeng 12 | * 13 | */ 14 | @ManagedBean 15 | @SessionScoped 16 | public class TableData { 17 | 18 | private static final Order[] orderList = new Order[] { 19 | 20 | new Order("A0001", "Intel CPU", 21 | new BigDecimal("700.00"), 1), 22 | new Order("A0002", "Harddisk 10TB", 23 | new BigDecimal("500.00"), 2), 24 | new Order("A0003", "Dell Laptop", 25 | new BigDecimal("11600.00"), 8), 26 | new Order("A0004", "Samsung LCD", 27 | new BigDecimal("5200.00"), 3), 28 | new Order("A0005", "A4Tech Mouse", 29 | new BigDecimal("100.00"), 10) 30 | }; 31 | 32 | public Order[] getOrderList() { 33 | 34 | return orderList; 35 | 36 | } 37 | 38 | public static class Order{ 39 | 40 | String orderNo; 41 | String productName; 42 | BigDecimal price; 43 | int qty; 44 | 45 | public Order(String orderNo, String productName, 46 | BigDecimal price, int qty) { 47 | 48 | this.orderNo = orderNo; 49 | this.productName = productName; 50 | this.price = price; 51 | this.qty = qty; 52 | } 53 | 54 | public String getOrderNo() { 55 | return orderNo; 56 | } 57 | 58 | public String getProductName() { 59 | return productName; 60 | } 61 | 62 | public BigDecimal getPrice() { 63 | return price; 64 | } 65 | 66 | public int getQty() { 67 | return qty; 68 | } 69 | } 70 | } -------------------------------------------------------------------------------- /extra/jsf-demo/src/main/java/jsfdemo/web/managedbean/TableData.java: -------------------------------------------------------------------------------- 1 | package jsfdemo.web.managedbean; 2 | 3 | import java.math.BigDecimal; 4 | 5 | import javax.faces.bean.ManagedBean; 6 | import javax.faces.bean.SessionScoped; 7 | 8 | /** 9 | * Example from http://www.mkyong.com/jsf2/jsf-2-datatable-example/ 10 | * 11 | * @author zedeng 12 | * 13 | */ 14 | @ManagedBean 15 | @SessionScoped 16 | public class TableData { 17 | 18 | private static final Order[] orderList = new Order[] { 19 | 20 | new Order("A0001", "Intel CPU", 21 | new BigDecimal("700.00"), 1), 22 | new Order("A0002", "Harddisk 10TB", 23 | new BigDecimal("500.00"), 2), 24 | new Order("A0003", "Dell Laptop", 25 | new BigDecimal("11600.00"), 8), 26 | new Order("A0004", "Samsung LCD", 27 | new BigDecimal("5200.00"), 3), 28 | new Order("A0005", "A4Tech Mouse", 29 | new BigDecimal("100.00"), 10) 30 | }; 31 | 32 | public Order[] getOrderList() { 33 | 34 | return orderList; 35 | 36 | } 37 | 38 | public static class Order{ 39 | 40 | String orderNo; 41 | String productName; 42 | BigDecimal price; 43 | int qty; 44 | 45 | public Order(String orderNo, String productName, 46 | BigDecimal price, int qty) { 47 | 48 | this.orderNo = orderNo; 49 | this.productName = productName; 50 | this.price = price; 51 | this.qty = qty; 52 | } 53 | 54 | public String getOrderNo() { 55 | return orderNo; 56 | } 57 | 58 | public String getProductName() { 59 | return productName; 60 | } 61 | 62 | public BigDecimal getPrice() { 63 | return price; 64 | } 65 | 66 | public int getQty() { 67 | return qty; 68 | } 69 | } 70 | } -------------------------------------------------------------------------------- /extra/performance/database/mysql.sql: -------------------------------------------------------------------------------- 1 | create table envs ( 2 | id int primary key auto_increment 3 | , createdon datetime not null default current_timestamp 4 | , name varchar (256) not null 5 | , props text null 6 | ); 7 | insert into envs(id, name, props) 8 | values(1, 'zemian_labtop_pc' 9 | , '{"hostname": "ZEDENG-US" 10 | , "os": "Windows 7 Service Pack 1 64 bits" 11 | , "processor": "Intel Core i5-3340M @ 2.70GHz" 12 | , "memory": "8GB"}'); 13 | 14 | create table tests ( 15 | id int primary key auto_increment 16 | , createdon datetime not null default current_timestamp 17 | , envid int not null 18 | , name varchar (256) not null 19 | , description text null 20 | 21 | , foreign key (envid) references envs(id) 22 | ); 23 | insert into tests(envid, name, description) 24 | values(1, 'hello_test', 'Just a test.'); 25 | 26 | create table testruns ( 27 | id int primary key auto_increment 28 | , testid int not null 29 | , purpose varchar(256) not null 30 | , tester varchar(256) not null 31 | , startedon datetime null 32 | , stoppedon datetime null 33 | , status varchar(32) null 34 | , request text null 35 | , response text null 36 | 37 | , foreign key (testid) references tests(id) 38 | ); 39 | insert into testruns(testid, tester, startedon, stoppedon, status) 40 | values 41 | (1, 'sample_test', 'zemian.deng@oracle.com', now() + 0, now() + 1, 'PASSED') 42 | , (1, 'sample_test', 'zemian.deng@oracle.com', now() + 1, now() + 2, 'PASSED') 43 | , (1, 'sample_test', 'zemian.deng@oracle.com', now() + 2, now() + 1, 'FAILED') 44 | , (1, 'sample_test', 'zemian.deng@oracle.com', now() + 3, now() + 2, 'PASSED') 45 | , (1, 'sample_test', 'zemian.deng@oracle.com', now() + 4, now() + 1, 'PASSED'); 46 | -------------------------------------------------------------------------------- /jpa-example/src/main/java/zemian/jpaexample/dao/JpaDao.java: -------------------------------------------------------------------------------- 1 | package zemian.jpaexample.dao; 2 | 3 | import javax.persistence.EntityManager; 4 | import javax.persistence.PersistenceContext; 5 | import zemian.service.logging.Logger; 6 | 7 | public class JpaDao { 8 | private static final Logger LOGGER = new Logger(JpaDao.class); 9 | 10 | @PersistenceContext 11 | protected EntityManager entityManager; 12 | 13 | protected Class entityClass; 14 | 15 | public JpaDao() { 16 | } 17 | 18 | public JpaDao(Class entityClass) { 19 | setEntityClass(entityClass); 20 | } 21 | 22 | public void setEntityClass(Class entityClass) { 23 | this.entityClass = entityClass; 24 | } 25 | 26 | public void setEntityManager(EntityManager entityManager) { 27 | this.entityManager = entityManager; 28 | } 29 | 30 | public EntityManager getEntityManager() { 31 | return entityManager; 32 | } 33 | 34 | public void save(T entity) { 35 | LOGGER.debug("Saving entity=%s", entity); 36 | entityManager.persist(entity); 37 | } 38 | 39 | public T update(T entity) { 40 | LOGGER.debug("Updating entity=%s", entity); 41 | return entityManager.merge(entity); 42 | } 43 | 44 | public T find(Object primaryKey) { 45 | LOGGER.debug("Finding entityClass=%s, primaryKey=%s", entityClass, primaryKey); 46 | return entityManager.find(entityClass, primaryKey); 47 | } 48 | 49 | public void delete(Object primaryKey) { 50 | LOGGER.debug("Deleting entity primaryKey=%s", primaryKey); 51 | T ref = entityManager.getReference(entityClass, primaryKey); 52 | entityManager.remove(ref); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /servlet3-jsp-example/src/main/java/zemian/servlet3jspexample/TestServlet.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3jspexample; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | import javax.servlet.ServletException; 7 | import javax.servlet.annotation.WebServlet; 8 | import javax.servlet.http.HttpServlet; 9 | import javax.servlet.http.HttpServletRequest; 10 | import javax.servlet.http.HttpServletResponse; 11 | import java.io.IOException; 12 | 13 | /** 14 | * A note about Servlet and JSP req forwarding. If you do not place the .jsp files into "WEB-INF/jsp/test" 15 | * and simply match under "webapp/test", your view forward will never succeed because the mapping of this servlet 16 | * will comes first even when you forward the request! Therefore a view will never be reached. 17 | * 18 | * Just ensure the view you are using does not match to the Servlet path mapping used. 19 | * 20 | * Note#2. The forward view name should starts with "/". 21 | */ 22 | @WebServlet("/test/*") 23 | public class TestServlet extends HttpServlet { 24 | private static final Logger LOGGER = LoggerFactory.getLogger(TestServlet.class); 25 | 26 | @Override 27 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 28 | //String base = req.getContextPath() + req.getServletPath(); 29 | String base = req.getContextPath(); 30 | String view = req.getRequestURI().substring(base.length() + 1); 31 | LOGGER.info("RequestURI: {}", req.getRequestURI()); 32 | LOGGER.debug("QueryString {}", req.getQueryString()); 33 | 34 | if (view.equals("test/hello")) { 35 | req.setAttribute("name", "World"); 36 | } 37 | 38 | view = "/WEB-INF/jsp/" + view + ".jsp"; 39 | LOGGER.info("Forward to view: {}", view); 40 | req.getRequestDispatcher(view).forward(req, resp); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /extra/mvc-example/src/main/java/zemian/mvcexample/service/Utils.java: -------------------------------------------------------------------------------- 1 | package zemian.mvcexample.service; 2 | 3 | import org.apache.commons.io.IOUtils; 4 | 5 | import java.io.File; 6 | import java.io.FileReader; 7 | import java.io.InputStream; 8 | import java.text.SimpleDateFormat; 9 | import java.util.*; 10 | 11 | public class Utils { 12 | public static String FILE_ENCODING = "UTF-8"; 13 | 14 | public static Properties loadProperties(String name) { 15 | try { 16 | try (InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(name)) { 17 | Properties result = new Properties(); 18 | result.load(inStream); 19 | return result; 20 | } 21 | } catch (Exception e) { 22 | throw new RuntimeException("Failed to read claspath resource to Properties: " + name, e); 23 | } 24 | } 25 | 26 | public static String loadString(String name) { 27 | try { 28 | try (InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(name)) { 29 | return IOUtils.toString(inStream); 30 | } 31 | } catch (Exception e) { 32 | throw new RuntimeException("Failed to read classpath resource to String: " + name, e); 33 | } 34 | } 35 | 36 | /** Quickly create a map object from a list of String, Object pairs. */ 37 | public static Map map(Object ... pairs) { 38 | Map result = new HashMap(); 39 | for (int i = 0; i < pairs.length; i+=2) { 40 | result.put(pairs[i].toString(), pairs[i+1]); 41 | } 42 | return result; 43 | } 44 | 45 | public static String timestamp() { 46 | return new SimpleDateFormat("MMddyyyy-HHmm").format(new Date()); 47 | } 48 | 49 | public static List readLines(File file) throws Exception { 50 | try (FileReader reader = new FileReader(file)) { 51 | List lines = IOUtils.readLines(reader); 52 | return lines; 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /servlet3-example/src/main/java/zemian/servlet3example/web/IndexServlet.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3example.web; 2 | 3 | import java.io.IOException; 4 | import javax.servlet.ServletException; 5 | import javax.servlet.annotation.WebServlet; 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | @WebServlet("/index") 10 | public class IndexServlet extends HtmlWriterServlet { 11 | 12 | @Override 13 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 14 | HtmlWriter html = createHtmlWriter(req, resp); 15 | String message = getMessage(req, html); 16 | 17 | html.header() 18 | .p(message) 19 | .h(1, "Welcome to Servlet 3 Example") 20 | .p("Let's explore Java Servlet 3.x Features.") 21 | .ul( 22 | html.link("Index", "/index"), 23 | html.link("Hello", "/hello"), 24 | html.link("Table Display", "/tables"), 25 | html.link("Form", "/form"), 26 | html.link("Sys Props", "/sys-props"), 27 | html.link("List of users", "/user"), 28 | html.link("Thread Execution Information", "/thread-info") 29 | ) 30 | .footer(); 31 | } 32 | 33 | private String getMessage(HttpServletRequest req, HtmlWriter html) { 34 | // Construct a server message based on user logged in with session data or not. 35 | String message = ""; 36 | LoginSession loginSession = LoginServlet.getOptionalLoginSession(req); 37 | if (loginSession != null) { 38 | message = "Welcome " + loginSession.getUsername() + 39 | "! You have been logged in since " + loginSession.getDateCreated(); 40 | message += "(" + html.link("Logout", "/login?logout") + ")"; 41 | } 42 | return message; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /servlet3-cdi-example/src/main/java/zemian/servlet3example/web/IndexServlet.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3example.web; 2 | 3 | import java.io.IOException; 4 | import javax.servlet.ServletException; 5 | import javax.servlet.annotation.WebServlet; 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | @WebServlet("/index") 10 | public class IndexServlet extends HtmlWriterServlet { 11 | 12 | @Override 13 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 14 | HtmlWriter html = createHtmlWriter(req, resp); 15 | String message = getMessage(req, html); 16 | 17 | html.header() 18 | .p(message) 19 | .h(1, "Welcome to Servlet 3 Example") 20 | .p("Let's explore Java Servlet 3.x Features.") 21 | .ul( 22 | html.link("Index", "/index"), 23 | html.link("Hello", "/hello"), 24 | html.link("Table Display", "/tables"), 25 | html.link("Form", "/form"), 26 | html.link("Sys Props", "/sys-props"), 27 | html.link("List of users", "/user"), 28 | html.link("Thread Execution Information", "/thread-info") 29 | ) 30 | .footer(); 31 | } 32 | 33 | private String getMessage(HttpServletRequest req, HtmlWriter html) { 34 | // Construct a server message based on user logged in with session data or not. 35 | String message = ""; 36 | LoginSession loginSession = LoginServlet.getOptionalLoginSession(req); 37 | if (loginSession != null) { 38 | message = "Welcome " + loginSession.getUsername() + 39 | "! You have been logged in since " + loginSession.getDateCreated(); 40 | message += "(" + html.link("Logout", "/login?logout") + ")"; 41 | } 42 | return message; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /servlet3-example/src/main/java/zemian/servlet3example/service/UserService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.servlet3example.service; 6 | 7 | import java.io.Reader; 8 | import java.util.HashSet; 9 | import java.util.Properties; 10 | import java.util.Set; 11 | import zemian.service.logging.Logger; 12 | 13 | /** 14 | * User DAO that uses Properties file to store users info. 15 | * 16 | * @author zedeng 17 | */ 18 | public class UserService implements Service { 19 | private static final Logger LOGGER = new Logger(UserService.class); 20 | // Username and password store 21 | private Properties usersProps = new Properties(); 22 | 23 | /** 24 | * Load $HOME/java-ee-example/servlet3example-users.properties if exists, else it will 25 | * use the one in classpath under "/zemian/servlet3example/service/servlet3example-users.properties". 26 | * 27 | * @throws RuntimeException - if both file and resource name not found. 28 | */ 29 | @Override 30 | public void init() { 31 | FileUtils.loadOptionalFile("servlet3example-users.properties", getClass().getPackage().getName(), new FileUtils.ReaderAction() { 32 | @Override 33 | public void onReader(Reader reader) throws Exception { 34 | usersProps.load(reader); 35 | } 36 | }); 37 | } 38 | 39 | @Override 40 | public void destroy() { 41 | // Do nothing. 42 | } 43 | 44 | public boolean validate(String username, String password) { 45 | String storedPassword = usersProps.getProperty(username); 46 | return storedPassword != null && storedPassword.equals(password); 47 | } 48 | 49 | public Set getUsers() { 50 | Set result = new HashSet<>(); 51 | for (String username : usersProps.stringPropertyNames()) 52 | result.add(username); 53 | return result; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /extra/jsf-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | deng.jsfdemo 6 | jsf-demo 7 | 1.0-SNAPSHOT 8 | war 9 | 10 | 11 | UTF-8 12 | 13 | 14 | 15 | 16 | org.slf4j 17 | slf4j-api 18 | 1.7.7 19 | 20 | 21 | javax 22 | javaee-web-api 23 | 7.0 24 | provided 25 | 26 | 27 | 28 | 29 | 30 | 31 | org.apache.maven.plugins 32 | maven-compiler-plugin 33 | 3.1 34 | 35 | 1.7 36 | 1.7 37 | 38 | 39 | 40 | org.apache.maven.plugins 41 | maven-war-plugin 42 | 2.3 43 | 44 | false 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /servlet3-jsp-example/src/main/webapp/WEB-INF/jsp/header.jsp: -------------------------------------------------------------------------------- 1 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 2 | <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 3 | 4 | 5 | 6 | 7 | 8 | Servlet3 JSP Example 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 21 | 22 | 23 | 24 | 25 | 40 | 41 |
42 |
43 | 46 |
47 |
48 | 49 |
-------------------------------------------------------------------------------- /extra/cdi-camel/src/main/webapp/table-data.xhtml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 10 | 34 | 35 | 36 | 37 |

JSF 2 dataTable example

38 | 39 | 43 | 44 | 45 | 46 | Order No 47 | 48 | #{o.orderNo} 49 | 50 | 51 | 52 | Product Name 53 | #{o.productName} 54 | 55 | 56 | 57 | Price 58 | #{o.price} 59 | 60 | 61 | 62 | Quantity 63 | #{o.qty} 64 | 65 | 66 | 67 | 68 |
69 | -------------------------------------------------------------------------------- /extra/jsf-demo/src/main/webapp/table-data.xhtml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 10 | 34 | 35 | 36 | 37 |

JSF 2 dataTable example

38 | 39 | 43 | 44 | 45 | 46 | Order No 47 | 48 | #{o.orderNo} 49 | 50 | 51 | 52 | Product Name 53 | #{o.productName} 54 | 55 | 56 | 57 | Price 58 | #{o.price} 59 | 60 | 61 | 62 | Quantity 63 | #{o.qty} 64 | 65 | 66 | 67 | 68 |
69 | -------------------------------------------------------------------------------- /common-service/src/test/java/zemian/service/util/UtilsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.service.util; 6 | 7 | import org.hamcrest.CoreMatchers; 8 | import org.junit.*; 9 | 10 | /** 11 | * 12 | * @author zedeng 13 | */ 14 | public class UtilsTest { 15 | 16 | @Test 17 | public void demo() { 18 | } 19 | 20 | /** 21 | * These are not really asserting test, but to run it manually to do 22 | * a quick eye inspection of the Utils does work. 23 | */ 24 | @Test 25 | public void demoUtils() { 26 | System.out.println("Empty list: " + Utils.list()); 27 | System.out.println("Full list: " + Utils.list("A", "B", "C")); 28 | System.out.println("Mixed list: " + Utils.list("A", 123)); 29 | 30 | System.out.println("Empty map: " + Utils.map()); 31 | System.out.println("Full map: " + Utils.map("A", "aaa", "B", "bbb", "C", "ccc")); 32 | 33 | System.out.println("String join: " + Utils.join("/", "A", "B", "C")); 34 | 35 | Utils.printClassLoader("JUnit ClassLoader."); 36 | 37 | Utils.printCheckpoint(); 38 | 39 | System.out.println("Eval script: " + Utils.evalScript("js", "Math.pow(12, n);", "n", 2)); 40 | 41 | System.out.println("Run script: "); 42 | Utils.runScript("src/main/resources/zemian/service/util/runscript-test.js", "n", 2); 43 | 44 | // Disable just in case so we do not run something at random. 45 | //System.out.println("Run test scripts: "); 46 | //Utils.runTestScript("n", 2); 47 | } 48 | 49 | @Test 50 | public void testJoin() { 51 | Assert.assertThat(Utils.join("/"), CoreMatchers.is("")); 52 | Assert.assertThat(Utils.join("/", "A"), CoreMatchers.is("A")); 53 | Assert.assertThat(Utils.join("/", "A", "B"), CoreMatchers.is("A/B")); 54 | Assert.assertThat(Utils.join("/", "A", "B", "C"), CoreMatchers.is("A/B/C")); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /extra/cdi-camel/src/main/java/jsfdemo/services/camel/CamelService.java: -------------------------------------------------------------------------------- 1 | package jsfdemo.services.camel; 2 | 3 | import jsfdemo.service.Service; 4 | 5 | import org.apache.camel.CamelContext; 6 | import org.apache.camel.builder.RouteBuilder; 7 | import org.apache.camel.impl.DefaultCamelContext; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | 11 | /** 12 | * A service to start/stop Apache Camel for routes and messages processing. Users would need to add 13 | * a RouteBuilder to this service to add custom logic. 14 | * 15 | * @author zedeng 16 | * 17 | */ 18 | public class CamelService implements Service { 19 | private static final Logger LOGGER = LoggerFactory.getLogger(CamelService.class); 20 | private CamelContext camelContext = new DefaultCamelContext(); 21 | 22 | @Override 23 | public void start() throws Exception { 24 | initCamel(); 25 | startCamel(); 26 | afterCamelStarted(); 27 | } 28 | 29 | protected void afterCamelStarted() { 30 | } 31 | 32 | @Override 33 | public void stop() { 34 | try { 35 | stopCamel(); 36 | } catch (Exception e) { 37 | throw new RuntimeException("Failed to stop Camel.", e); 38 | } 39 | } 40 | 41 | protected void initCamel() throws Exception { 42 | } 43 | 44 | protected void startCamel() throws Exception { 45 | camelContext.start(); 46 | LOGGER.info("Camel started: {}", camelContext); 47 | } 48 | 49 | protected void stopCamel() { 50 | try { 51 | camelContext.stop(); 52 | LOGGER.info("Camel stopped."); 53 | } catch (Exception e) { 54 | LOGGER.error("Failed to stop camel.", e); 55 | } 56 | } 57 | 58 | public CamelContext getCamelContext() { 59 | return camelContext; 60 | } 61 | 62 | public void addCamelRoute(RouteBuilder routeBuilder) { 63 | try { 64 | camelContext.addRoutes(routeBuilder); 65 | } catch (Exception e) { 66 | throw new RuntimeException("Failed to add camel route: " + routeBuilder, e); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /extra/mvc-example/src/main/webapp/WEB-INF/jsp/header.jsp: -------------------------------------------------------------------------------- 1 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 2 | <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 3 | 4 | 5 | 6 | 7 | 8 | ${app.name} 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 21 | 22 | 23 | 24 | 25 | 40 | 41 |
42 |
43 | 47 |
48 |
49 | 50 |
-------------------------------------------------------------------------------- /common-service/src/main/java/zemian/service/logging/Message.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.service.logging; 6 | 7 | /** 8 | * A message data to capture all the information that will be logged by service. 9 | * 10 | *

A message string may contains parameter holders and then later by calling 11 | * getFormatedMessage() to actually bind it together. The syntax of the message 12 | * format is same as the java.lang.String.format(). Separating message and the 13 | * parameters allow message to be construct lazily. This can save some 14 | * performance in long run, and it could make the message string more readable. 15 | * 16 | * @author zedeng 17 | */ 18 | public class Message { 19 | private String message; 20 | private Object[] messageParameters; 21 | private Exception cause; 22 | 23 | public static Message msg(String message, Object ... parameters) { 24 | return msg(null, message, parameters); 25 | } 26 | 27 | public static Message msg(Exception cause, String message, Object ... parameters) { 28 | Message result = new Message(); 29 | result.setCause(cause); 30 | result.setMessage(message); 31 | result.setMessageParameters(parameters); 32 | return result; 33 | } 34 | 35 | public String getFormatedMessage() { 36 | return String.format(message, messageParameters); 37 | } 38 | 39 | public String getMessage() { 40 | return message; 41 | } 42 | 43 | public void setMessage(String message) { 44 | this.message = message; 45 | } 46 | 47 | public Object[] getMessageParameters() { 48 | return messageParameters; 49 | } 50 | 51 | public void setMessageParameters(Object[] messageParameters) { 52 | this.messageParameters = messageParameters; 53 | } 54 | 55 | public Exception getCause() { 56 | return cause; 57 | } 58 | 59 | public void setCause(Exception cause) { 60 | this.cause = cause; 61 | } 62 | 63 | 64 | } 65 | -------------------------------------------------------------------------------- /extra/jpa-sakila/src/main/java/zemian/sakila/SalesByFilmCategory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.sakila; 6 | 7 | import java.io.Serializable; 8 | import java.math.BigDecimal; 9 | import javax.persistence.Basic; 10 | import javax.persistence.Column; 11 | import javax.persistence.Entity; 12 | import javax.persistence.NamedQueries; 13 | import javax.persistence.NamedQuery; 14 | import javax.persistence.Table; 15 | import javax.validation.constraints.NotNull; 16 | import javax.validation.constraints.Size; 17 | import javax.xml.bind.annotation.XmlRootElement; 18 | 19 | /** 20 | * 21 | * @author zedeng 22 | */ 23 | @Entity 24 | @Table(name = "sales_by_film_category") 25 | @XmlRootElement 26 | @NamedQueries({ 27 | @NamedQuery(name = "SalesByFilmCategory.findAll", query = "SELECT s FROM SalesByFilmCategory s"), 28 | @NamedQuery(name = "SalesByFilmCategory.findByCategory", query = "SELECT s FROM SalesByFilmCategory s WHERE s.category = :category"), 29 | @NamedQuery(name = "SalesByFilmCategory.findByTotalSales", query = "SELECT s FROM SalesByFilmCategory s WHERE s.totalSales = :totalSales")}) 30 | public class SalesByFilmCategory implements Serializable { 31 | private static final long serialVersionUID = 1L; 32 | @Basic(optional = false) 33 | @NotNull 34 | @Size(min = 1, max = 25) 35 | @Column(name = "category") 36 | private String category; 37 | // @Max(value=?) @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation 38 | @Column(name = "total_sales") 39 | private BigDecimal totalSales; 40 | 41 | public SalesByFilmCategory() { 42 | } 43 | 44 | public String getCategory() { 45 | return category; 46 | } 47 | 48 | public void setCategory(String category) { 49 | this.category = category; 50 | } 51 | 52 | public BigDecimal getTotalSales() { 53 | return totalSales; 54 | } 55 | 56 | public void setTotalSales(BigDecimal totalSales) { 57 | this.totalSales = totalSales; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /servlet3-example/src/main/java/zemian/servlet3example/web/TableDisplayServlet.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3example.web; 2 | 3 | import static zemian.service.util.Utils.list; 4 | import static zemian.service.util.Utils.map; 5 | 6 | import java.io.IOException; 7 | import java.util.List; 8 | import java.util.Map; 9 | import javax.servlet.ServletException; 10 | import javax.servlet.annotation.WebServlet; 11 | import javax.servlet.http.HttpServletRequest; 12 | import javax.servlet.http.HttpServletResponse; 13 | 14 | @WebServlet("/tables") 15 | public class TableDisplayServlet extends HtmlWriterServlet { 16 | @Override 17 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 18 | List> dataList = list( 19 | list("Foo", "Low", 99.00), 20 | list("Bar", "High", 100.00), 21 | list("Joe", "Medium", 99.5) 22 | ); 23 | List> dataListWithHeaders = list(); 24 | dataListWithHeaders.add(list("NAME", "DESCRIPTION", "VALUES")); 25 | dataListWithHeaders.addAll(dataList); 26 | 27 | Map dataMap = map( 28 | "Foo", "123", 29 | "Bar", "123" 30 | ); 31 | List> emptListWithHeader = list( 32 | list("NAME", "DESCRIPTION", "VALUE") 33 | ); 34 | List> emptList = list( 35 | list("NAME", "DESCRIPTION", "VALUE") 36 | ); 37 | 38 | HtmlWriter html = createHtmlWriter(req, resp); 39 | html.header() 40 | .h(1, "Table With Data List") 41 | .table(dataListWithHeaders) 42 | .h(1, "Table With Data List Without RowIndex and Header") 43 | .table(dataList, "myDataTable", false, false) 44 | .h(1, "Table With Data Map") 45 | .table(dataMap) 46 | .h(1, "Empty Table With Header") 47 | .table(emptListWithHeader) 48 | .h(1, "Empty Table") 49 | .table(emptList) 50 | .footer(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /servlet3-cdi-example/src/main/java/zemian/servlet3example/web/TableDisplayServlet.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3example.web; 2 | 3 | import static zemian.service.util.Utils.list; 4 | import static zemian.service.util.Utils.map; 5 | 6 | import java.io.IOException; 7 | import java.util.List; 8 | import java.util.Map; 9 | import javax.servlet.ServletException; 10 | import javax.servlet.annotation.WebServlet; 11 | import javax.servlet.http.HttpServletRequest; 12 | import javax.servlet.http.HttpServletResponse; 13 | 14 | @WebServlet("/tables") 15 | public class TableDisplayServlet extends HtmlWriterServlet { 16 | @Override 17 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 18 | List> dataList = list( 19 | list("Foo", "Low", 99.00), 20 | list("Bar", "High", 100.00), 21 | list("Joe", "Medium", 99.5) 22 | ); 23 | List> dataListWithHeaders = list(); 24 | dataListWithHeaders.add(list("NAME", "DESCRIPTION", "VALUES")); 25 | dataListWithHeaders.addAll(dataList); 26 | 27 | Map dataMap = map( 28 | "Foo", "123", 29 | "Bar", "123" 30 | ); 31 | List> emptListWithHeader = list( 32 | list("NAME", "DESCRIPTION", "VALUE") 33 | ); 34 | List> emptList = list( 35 | list("NAME", "DESCRIPTION", "VALUE") 36 | ); 37 | 38 | HtmlWriter html = createHtmlWriter(req, resp); 39 | html.header() 40 | .h(1, "Table With Data List") 41 | .table(dataListWithHeaders) 42 | .h(1, "Table With Data List Without RowIndex and Header") 43 | .table(dataList, "myDataTable", false, false) 44 | .h(1, "Table With Data Map") 45 | .table(dataMap) 46 | .h(1, "Empty Table With Header") 47 | .table(emptListWithHeader) 48 | .h(1, "Empty Table") 49 | .table(emptList) 50 | .footer(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /extra/jsf-demo/src/main/java/jsfdemo/service/ServiceApplication.java: -------------------------------------------------------------------------------- 1 | package jsfdemo.service; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | /** 7 | * An single application context. We will assemble all the services together to form an application. Users should add 8 | * new services into this class, and use this as the entry and exit points of the application. 9 | * 10 | *

Different runtime environments need to ensure to invoke #start() and #stop() as part of the life cycle to ensure 11 | * all depended services are properly started and stopped. 12 | * 13 | * @author zedeng 14 | * 15 | */ 16 | public class ServiceApplication implements Service { 17 | private static final Logger LOGGER = LoggerFactory.getLogger(ServiceApplication.class); 18 | private static final ServiceApplication INSTANCE = new ServiceApplication(); 19 | private ServiceContainer serviceContainer = new ServiceContainer(); 20 | 21 | private ServiceApplication() { 22 | } 23 | 24 | public static ServiceApplication getInstance() { 25 | return INSTANCE; 26 | } 27 | 28 | @Override 29 | public void start() throws Exception { 30 | addServices(); 31 | serviceContainer.start(); 32 | LOGGER.info("Application started."); 33 | } 34 | 35 | @Override 36 | public void stop() throws Exception { 37 | serviceContainer.stop(); 38 | LOGGER.info("Application stopped."); 39 | } 40 | 41 | /** 42 | * Convenient method to call #start() without checked exception. 43 | */ 44 | public void quickStart() { 45 | try { 46 | start(); 47 | } catch (Exception e) { 48 | throw new RuntimeException("Failed to start application", e); 49 | } 50 | } 51 | 52 | /** 53 | * Convenient method to call #stop() without checked exception. 54 | */ 55 | public void quickStop() { 56 | try { 57 | serviceContainer.stop(); 58 | } catch (Exception e) { 59 | throw new RuntimeException("Failed to stop application.", e); 60 | } 61 | } 62 | 63 | private void addServices() { 64 | //serviceContainer.add(createCamelService()); 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /extra/jpa-sakila/src/main/java/zemian/sakila/FilmActorPK.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.sakila; 6 | 7 | import java.io.Serializable; 8 | import javax.persistence.Basic; 9 | import javax.persistence.Column; 10 | import javax.persistence.Embeddable; 11 | import javax.validation.constraints.NotNull; 12 | 13 | /** 14 | * 15 | * @author zedeng 16 | */ 17 | @Embeddable 18 | public class FilmActorPK implements Serializable { 19 | @Basic(optional = false) 20 | @NotNull 21 | @Column(name = "actor_id") 22 | private short actorId; 23 | @Basic(optional = false) 24 | @NotNull 25 | @Column(name = "film_id") 26 | private short filmId; 27 | 28 | public FilmActorPK() { 29 | } 30 | 31 | public FilmActorPK(short actorId, short filmId) { 32 | this.actorId = actorId; 33 | this.filmId = filmId; 34 | } 35 | 36 | public short getActorId() { 37 | return actorId; 38 | } 39 | 40 | public void setActorId(short actorId) { 41 | this.actorId = actorId; 42 | } 43 | 44 | public short getFilmId() { 45 | return filmId; 46 | } 47 | 48 | public void setFilmId(short filmId) { 49 | this.filmId = filmId; 50 | } 51 | 52 | @Override 53 | public int hashCode() { 54 | int hash = 0; 55 | hash += (int) actorId; 56 | hash += (int) filmId; 57 | return hash; 58 | } 59 | 60 | @Override 61 | public boolean equals(Object object) { 62 | // TODO: Warning - this method won't work in the case the id fields are not set 63 | if (!(object instanceof FilmActorPK)) { 64 | return false; 65 | } 66 | FilmActorPK other = (FilmActorPK) object; 67 | if (this.actorId != other.actorId) { 68 | return false; 69 | } 70 | if (this.filmId != other.filmId) { 71 | return false; 72 | } 73 | return true; 74 | } 75 | 76 | @Override 77 | public String toString() { 78 | return "zemian.sakila.FilmActorPK[ actorId=" + actorId + ", filmId=" + filmId + " ]"; 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /common-service/src/main/java/zemian/service/logging/JdkLog.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.service.logging; 6 | 7 | import java.util.logging.Level; 8 | import java.util.logging.Logger; 9 | 10 | /** 11 | * A log service that uses JDK java.util.logging to provide actual logging. 12 | * 13 | * @author zedeng 14 | */ 15 | public class JdkLog implements Log { 16 | // A LOGGER is use in this class itself to help debug loading of JDK logger. 17 | private static final Logger LOGGER = Logger.getLogger(JdkLog.class.getName()); 18 | // The jdkLogger is the instance that does the actual loggigng for a particular 19 | // given class that wants logging functionality. 20 | private Logger jdkLogger; 21 | 22 | public JdkLog(Class loggerClass) { 23 | //LOGGER.fine("Creating new LogService for " + loggerClass); 24 | jdkLogger = Logger.getLogger(loggerClass.getName()); 25 | } 26 | 27 | @Override 28 | public void error(Message message) { 29 | if (jdkLogger.isLoggable(Level.SEVERE)) { 30 | jdkLogger.log(Level.SEVERE, message.getFormatedMessage(), message.getCause()); 31 | } 32 | } 33 | 34 | @Override 35 | public void warn(Message message) { 36 | if (jdkLogger.isLoggable(Level.WARNING)) { 37 | jdkLogger.log(Level.WARNING, message.getFormatedMessage(), message.getCause()); 38 | } 39 | } 40 | 41 | @Override 42 | public void info(Message message) { 43 | if (jdkLogger.isLoggable(Level.INFO)) { 44 | jdkLogger.log(Level.INFO, message.getFormatedMessage(), message.getCause()); 45 | } 46 | } 47 | 48 | @Override 49 | public void debug(Message message) { 50 | if (jdkLogger.isLoggable(Level.FINE)) { 51 | jdkLogger.log(Level.FINE, message.getFormatedMessage(), message.getCause()); 52 | } 53 | } 54 | 55 | @Override 56 | public void trace(Message message) { 57 | if (jdkLogger.isLoggable(Level.FINER)) { 58 | jdkLogger.log(Level.FINER, message.getFormatedMessage(), message.getCause()); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /common-service/src/test/java/zemian/service/logging/JdkLogTest.java: -------------------------------------------------------------------------------- 1 | package zemian.service.logging; 2 | 3 | import static zemian.service.logging.Message.msg; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Note that depending on your JDK logging.properties configuration, you might not 8 | * able to see all level of logging messages! 9 | * 10 | *

There is no asserts in this test, but just to exercise the logging calls 11 | * and user needs to visually inspect the outcome. 12 | * 13 | * @author zedeng 14 | */ 15 | public class JdkLogTest { 16 | private Log log = LogFactory.createLog(JdkLogTest.class); 17 | 18 | @Test 19 | public void testError() { 20 | log.error(msg("Test error message")); 21 | log.error(msg("Test %s message with parameters: %d %d %d", "error", 1, 2, 3)); 22 | log.error(msg(new RuntimeException("Checkpoint"), "Test %s message", "error")); 23 | } 24 | 25 | @Test 26 | public void testWarn() { 27 | log.warn(msg("Test warn message")); 28 | log.warn(msg("Test %s message with parameters: %d %d %d", "warn", 1, 2, 3)); 29 | log.warn(msg(new RuntimeException("Checkpoint"), "Test %s message", "warn")); 30 | } 31 | 32 | @Test 33 | public void testInfo() { 34 | log.info(msg("Test info message")); 35 | log.info(msg("Test %s message with parameters: %d %d %d", "info", 1, 2, 3)); 36 | log.info(msg(new RuntimeException("Checkpoint"), "Test %s message", "info")); 37 | } 38 | 39 | @Test 40 | public void testDebug() { 41 | log.debug(msg("Test debug message")); 42 | log.debug(msg("Test %s message with parameters: %d %d %d", "debug", 1, 2, 3)); 43 | log.debug(msg(new RuntimeException("Checkpoint"), "Test %s message", "debug")); 44 | } 45 | 46 | @Test 47 | public void testTrace() { 48 | log.trace(msg("Test error message")); 49 | log.trace(msg("Test %s message with parameters: %d %d %d", "trace", 1, 2, 3)); 50 | log.trace(msg(new RuntimeException("Checkpoint"), "Test %s message", "trace")); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /servlet3-cdi-example/src/main/java/zemian/servlet3example/web/HtmlWriterServlet.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3example.web; 2 | 3 | import java.io.IOException; 4 | import javax.servlet.http.HttpServlet; 5 | import javax.servlet.http.HttpServletRequest; 6 | import javax.servlet.http.HttpServletResponse; 7 | 8 | public abstract class HtmlWriterServlet extends HttpServlet { 9 | 10 | protected static final long serialVersionUID = 1L; 11 | 12 | protected HtmlWriter createHtmlWriter(HttpServletRequest req, HttpServletResponse resp) { 13 | try { 14 | // Create a HtmlWriter that's customized for this application, such 15 | // as header and footer. 16 | HtmlWriter writer = new HtmlWriter(); 17 | writer.setWriter(resp.getWriter()); 18 | writer.setContextPath(req.getContextPath()); 19 | writer.setHeader(header(writer)); 20 | writer.setFooter(footer(writer)); 21 | return writer; 22 | } catch (IOException e) { 23 | throw new RuntimeException("Failed to create HtmlWriter.", e); 24 | } 25 | } 26 | 27 | protected String header(HtmlWriter html) { 28 | String contextPath = getServletContext().getContextPath(); 29 | StringBuilder sb = new StringBuilder(); 30 | sb.append(""); 31 | sb.append(""); 32 | sb.append("

"); 33 | sb.append(" "); 34 | sb.append("
"); 35 | sb.append(""); 36 | sb.append(""); 41 | return sb.toString(); 42 | } 43 | 44 | protected String footer(HtmlWriter html) { 45 | StringBuilder sb = new StringBuilder(); 46 | sb.append(""); 47 | sb.append(""); 48 | return sb.toString(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /servlet3-example/src/main/java/zemian/servlet3example/web/HtmlWriterServlet.java: -------------------------------------------------------------------------------- 1 | package zemian.servlet3example.web; 2 | 3 | import java.io.IOException; 4 | import javax.servlet.http.HttpServlet; 5 | import javax.servlet.http.HttpServletRequest; 6 | import javax.servlet.http.HttpServletResponse; 7 | 8 | public abstract class HtmlWriterServlet extends HttpServlet { 9 | 10 | protected static final long serialVersionUID = 1L; 11 | 12 | protected HtmlWriter createHtmlWriter(HttpServletRequest req, HttpServletResponse resp) { 13 | try { 14 | // Create a HtmlWriter that's customized for this application, such 15 | // as header and footer. 16 | HtmlWriter writer = new HtmlWriter(); 17 | writer.setWriter(resp.getWriter()); 18 | writer.setContextPath(req.getContextPath()); 19 | writer.setHeader(header(writer)); 20 | writer.setFooter(footer(writer)); 21 | return writer; 22 | } catch (IOException e) { 23 | throw new RuntimeException("Failed to create HtmlWriter.", e); 24 | } 25 | } 26 | 27 | protected String header(HtmlWriter html) { 28 | String contextPath = getServletContext().getContextPath(); 29 | StringBuilder sb = new StringBuilder(); 30 | sb.append(""); 31 | sb.append(""); 32 | sb.append("
"); 33 | sb.append(" "); 34 | sb.append("
"); 35 | sb.append(""); 36 | sb.append(""); 41 | return sb.toString(); 42 | } 43 | 44 | protected String footer(HtmlWriter html) { 45 | StringBuilder sb = new StringBuilder(); 46 | sb.append(""); 47 | sb.append(""); 48 | return sb.toString(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /jpa-example/src/main/java/zemian/jpaexample/dao/samples/UserSamples.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package zemian.jpaexample.dao.samples; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | import java.util.Random; 11 | import java.util.UUID; 12 | import javax.ejb.Stateless; 13 | import javax.inject.Inject; 14 | import zemian.jpaexample.dao.UserDao; 15 | import zemian.jpaexample.data.User; 16 | import zemian.service.logging.Logger; 17 | 18 | /** 19 | * 20 | * @author zedeng 21 | */ 22 | @Stateless 23 | public class UserSamples { 24 | private static final Logger LOGGER = new Logger(UserSamples.class); 25 | private static final String NUMS = "0123456789"; 26 | private static final String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 27 | private static final String LETTERS_NUMS = LETTERS + NUMS; 28 | 29 | @Inject 30 | private UserDao userDao; 31 | 32 | private String randLettersNums(int size) { 33 | int max = LETTERS_NUMS.length(); 34 | Random rand = new Random(); 35 | StringBuilder sb = new StringBuilder(size); 36 | for (int i = 0; i < size; i++) { 37 | int index = rand.nextInt(max); 38 | sb.append(LETTERS_NUMS.charAt(index)); 39 | } 40 | return sb.toString(); 41 | } 42 | 43 | public List createRandomNameUsers(String baseUsername, int numOfUsers) { 44 | List result = new ArrayList<>(); 45 | String batchId = randLettersNums(4).toLowerCase(); 46 | LOGGER.debug("Generating users with batchId=%s", batchId); 47 | for (int i = 0; i < numOfUsers; i++) { 48 | User user = new User(); 49 | user.setUsername(batchId + "_" + baseUsername + "_" + i); 50 | user.setPassword(UUID.randomUUID().toString()); 51 | 52 | userDao.save(user); 53 | result.add(user); 54 | } 55 | return result; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /extra/glassfish-logging-example/pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 4.0.0 7 | 8 | 9 | zemian 10 | java-ee6-examples-parent-pom 11 | 1.0-SNAPSHOT 12 | ../pom.xml 13 | 14 | 15 | zemian 16 | glassfish-logging-example 17 | war 18 | 19 | 20 | 21 | 22 | org.apache.maven.plugins 23 | maven-war-plugin 24 | 25 | 26 | 27 | 28 | 29 | 30 | org.slf4j 31 | slf4j-api 32 | 1.7.7 33 | 34 | 35 | org.slf4j 36 | slf4j-jdk14 37 | 1.7.7 38 | runtime 39 | 40 | 48 | 56 | 57 | zemian 58 | common-service 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /extra/jpa-sakila/src/main/java/zemian/sakila/FilmCategoryPK.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.sakila; 6 | 7 | import java.io.Serializable; 8 | import javax.persistence.Basic; 9 | import javax.persistence.Column; 10 | import javax.persistence.Embeddable; 11 | import javax.validation.constraints.NotNull; 12 | 13 | /** 14 | * 15 | * @author zedeng 16 | */ 17 | @Embeddable 18 | public class FilmCategoryPK implements Serializable { 19 | @Basic(optional = false) 20 | @NotNull 21 | @Column(name = "film_id") 22 | private short filmId; 23 | @Basic(optional = false) 24 | @NotNull 25 | @Column(name = "category_id") 26 | private short categoryId; 27 | 28 | public FilmCategoryPK() { 29 | } 30 | 31 | public FilmCategoryPK(short filmId, short categoryId) { 32 | this.filmId = filmId; 33 | this.categoryId = categoryId; 34 | } 35 | 36 | public short getFilmId() { 37 | return filmId; 38 | } 39 | 40 | public void setFilmId(short filmId) { 41 | this.filmId = filmId; 42 | } 43 | 44 | public short getCategoryId() { 45 | return categoryId; 46 | } 47 | 48 | public void setCategoryId(short categoryId) { 49 | this.categoryId = categoryId; 50 | } 51 | 52 | @Override 53 | public int hashCode() { 54 | int hash = 0; 55 | hash += (int) filmId; 56 | hash += (int) categoryId; 57 | return hash; 58 | } 59 | 60 | @Override 61 | public boolean equals(Object object) { 62 | // TODO: Warning - this method won't work in the case the id fields are not set 63 | if (!(object instanceof FilmCategoryPK)) { 64 | return false; 65 | } 66 | FilmCategoryPK other = (FilmCategoryPK) object; 67 | if (this.filmId != other.filmId) { 68 | return false; 69 | } 70 | if (this.categoryId != other.categoryId) { 71 | return false; 72 | } 73 | return true; 74 | } 75 | 76 | @Override 77 | public String toString() { 78 | return "zemian.sakila.FilmCategoryPK[ filmId=" + filmId + ", categoryId=" + categoryId + " ]"; 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /extra/jpa-sakila/src/main/java/zemian/sakila/SalesByStore.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Zemian Deng 2014 3 | */ 4 | 5 | package zemian.sakila; 6 | 7 | import java.io.Serializable; 8 | import java.math.BigDecimal; 9 | import javax.persistence.Column; 10 | import javax.persistence.Entity; 11 | import javax.persistence.NamedQueries; 12 | import javax.persistence.NamedQuery; 13 | import javax.persistence.Table; 14 | import javax.validation.constraints.Size; 15 | import javax.xml.bind.annotation.XmlRootElement; 16 | 17 | /** 18 | * 19 | * @author zedeng 20 | */ 21 | @Entity 22 | @Table(name = "sales_by_store") 23 | @XmlRootElement 24 | @NamedQueries({ 25 | @NamedQuery(name = "SalesByStore.findAll", query = "SELECT s FROM SalesByStore s"), 26 | @NamedQuery(name = "SalesByStore.findByStore", query = "SELECT s FROM SalesByStore s WHERE s.store = :store"), 27 | @NamedQuery(name = "SalesByStore.findByManager", query = "SELECT s FROM SalesByStore s WHERE s.manager = :manager"), 28 | @NamedQuery(name = "SalesByStore.findByTotalSales", query = "SELECT s FROM SalesByStore s WHERE s.totalSales = :totalSales")}) 29 | public class SalesByStore implements Serializable { 30 | private static final long serialVersionUID = 1L; 31 | @Size(max = 101) 32 | @Column(name = "store") 33 | private String store; 34 | @Size(max = 91) 35 | @Column(name = "manager") 36 | private String manager; 37 | // @Max(value=?) @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation 38 | @Column(name = "total_sales") 39 | private BigDecimal totalSales; 40 | 41 | public SalesByStore() { 42 | } 43 | 44 | public String getStore() { 45 | return store; 46 | } 47 | 48 | public void setStore(String store) { 49 | this.store = store; 50 | } 51 | 52 | public String getManager() { 53 | return manager; 54 | } 55 | 56 | public void setManager(String manager) { 57 | this.manager = manager; 58 | } 59 | 60 | public BigDecimal getTotalSales() { 61 | return totalSales; 62 | } 63 | 64 | public void setTotalSales(BigDecimal totalSales) { 65 | this.totalSales = totalSales; 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /extra/jsf-demo/src/main/java/jsfdemo/service/ServiceContainer.java: -------------------------------------------------------------------------------- 1 | package jsfdemo.service; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.List; 6 | 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | 10 | /** 11 | * Start and stop a list of services as a container. 12 | * 13 | * @author zedeng 14 | * 15 | */ 16 | public class ServiceContainer implements Service { 17 | private static final Logger LOGGER = LoggerFactory.getLogger(ServiceContainer.class); 18 | private List services = new ArrayList<>(); 19 | 20 | public void add(Service service) { 21 | services.add(service); 22 | } 23 | 24 | /** 25 | * Start a list of services in order. If one failed, it will stop the previous initialized services and throw the original failed exception. 26 | */ 27 | @Override 28 | public void start() throws Exception { 29 | startServicesInOrder(); 30 | } 31 | 32 | /** 33 | * Stop list of initialized services in reversed order. 34 | */ 35 | @Override 36 | public void stop() { 37 | stopServicesInReverse(services); 38 | } 39 | 40 | private void startServicesInOrder() throws Exception { 41 | List completedList = new ArrayList<>(); 42 | for (Service service : services) { 43 | try { 44 | service.start(); 45 | LOGGER.info("Service {} started.", service); 46 | completedList.add(service); 47 | } catch (Exception e) { 48 | LOGGER.error("Failed to start service {}. Will stop previous started services now.", service, e); 49 | stopServicesInReverse(completedList); 50 | throw e; 51 | } 52 | } 53 | } 54 | 55 | private void stopServicesInReverse(List serviceList) { 56 | List reversedList = new ArrayList<>(serviceList); 57 | Collections.reverse(reversedList); 58 | for (Service service : reversedList) { 59 | try { 60 | service.stop(); 61 | LOGGER.info("Service {} stopped.", service); 62 | } catch (Exception e) { 63 | // We should not re-throw the exception during stop so other service can have a chance to shutdown as well. 64 | LOGGER.error("Failed to stop service {}.", service, e); 65 | } 66 | } 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /extra/cdi-camel/src/main/java/jsfdemo/service/ServiceContainer.java: -------------------------------------------------------------------------------- 1 | package jsfdemo.service; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.List; 6 | 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | 10 | /** 11 | * Start and stop a list of services as a container. 12 | * 13 | * @author zedeng 14 | * 15 | */ 16 | public class ServiceContainer implements Service { 17 | private static final Logger LOGGER = LoggerFactory.getLogger(ServiceContainer.class); 18 | private List services = new ArrayList<>(); 19 | 20 | public void add(Service service) { 21 | services.add(service); 22 | } 23 | 24 | /** 25 | * Start a list of services in order. If one failed, it will stop the previous initialized services and throw the original failed exception. 26 | */ 27 | @Override 28 | public void start() throws Exception { 29 | startServicesInOrder(); 30 | } 31 | 32 | /** 33 | * Stop list of initialized services in reversed order. 34 | */ 35 | @Override 36 | public void stop() { 37 | stopServicesInReverse(services); 38 | } 39 | 40 | private void startServicesInOrder() throws Exception { 41 | List completedList = new ArrayList<>(); 42 | for (Service service : services) { 43 | try { 44 | service.start(); 45 | LOGGER.info("Service {} started.", service); 46 | completedList.add(service); 47 | } catch (Exception e) { 48 | LOGGER.error("Failed to start service {}. Will stop previous started services now.", service, e); 49 | stopServicesInReverse(completedList); 50 | throw e; 51 | } 52 | } 53 | } 54 | 55 | private void stopServicesInReverse(List serviceList) { 56 | List reversedList = new ArrayList<>(serviceList); 57 | Collections.reverse(reversedList); 58 | for (Service service : reversedList) { 59 | try { 60 | service.stop(); 61 | LOGGER.info("Service {} stopped.", service); 62 | } catch (Exception e) { 63 | // We should not re-throw the exception during stop so other service can have a chance to shutdown as well. 64 | LOGGER.error("Failed to stop service {}.", service, e); 65 | } 66 | } 67 | } 68 | 69 | } 70 | --------------------------------------------------------------------------------