commit 561396dae4b9b079d0a05038dbe65e2e6f184e81 Author: Tomasz Półgrabia Date: Mon Jan 4 12:21:52 2021 +0100 JavaEE showcase project. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..79d184d --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.gradle +.idea +*.iml +build +out +gradle.properties \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..b2091e1 --- /dev/null +++ b/README.md @@ -0,0 +1,53 @@ +# Requirements + +- Wildfly (tested with 18.0.0 Final) + +# Initial setup + +- Queue ( java:/jms/queue/MyQueue ) +- Connection factory ( java:/ConnectionFactory - tested with in-vm ) + +# Cargo deployment + +Container ids used in the gradle plugin for cargo: + +1. Tomcat tomcat9x +2. Jetty jetty9x +3. Wildfly 9.* wildfly9x +4. Wildfly 17.* wildfly17x + +# Postgresql wildfly setup + +## creating postgresql driver + +modules/org/postgresql and put there module.xml + + + + + + + + + + + + + +then in jboss-cli + + ./subsystem=datasources/jdbc-driver=postgresql:add(driver-module-name="org.postgresql") + +it should create the jdbc driver + +## creating data source + + ./subsystem=datasources/data-source=MyDataSource:add(driver-name="postgresql", jndi-name="java:/MyDataSource", connection-url="jdbc:postgresql://localhost:5432/simpleservice", user-name="simpleservice", password="simpleservice", min-pool-size=5, max-pool-size=15) + + +# Useful links +1. https://thoughts-on-java.org/jpa-persistence-xml/ +2. https://www.stenusys.com/how_to_setup_postgresql_datasource_with_wildfly/ +3. Adding messaging queue / topics - https://docs.jboss.org/author/display/WFLY10/Messaging+configuration +4. One another way 4. One another way https://cleanprogrammer.net/how-to-configure-jms-in-wildfly/ +5. Markdown syntax https://daringfireball.net/projects/markdown/syntax diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..5bb91a4 --- /dev/null +++ b/build.gradle @@ -0,0 +1,42 @@ +buildscript { + repositories { + jcenter() + } + + dependencies { + classpath "com.bmuschko:gradle-cargo-plugin:2.6.1" + } +} + +subprojects { + + group 'pl.tpolgrabia.trainings.javaee' + version '1.0-SNAPSHOT' + + repositories { + mavenCentral() + } + + apply plugin: 'com.bmuschko.cargo' + + dependencies { + def cargoVersion = '1.6.5' + cargo "org.codehaus.cargo:cargo-core-uberjar:$cargoVersion", + "org.codehaus.cargo:cargo-ant:$cargoVersion" + } + + cargo { + containerId = 'wildfly10x' + + remote { + hostname = project.ext["cargo.wildfly.hostname"] + username = project.ext["cargo.wildfly.username"] + password = project.ext["cargo.wildfly.password"] + + containerProperties { + property 'cargo.jboss.management-http.port', + project.ext["cargo.wildfly.port"] + } + } + } +} \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..87b738c Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..9ef30b0 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Fri Nov 01 18:15:48 CET 2019 +distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStorePath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..af6708f --- /dev/null +++ b/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..6d57edc --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/java-demo1-jsf-web/build.gradle b/java-demo1-jsf-web/build.gradle new file mode 100644 index 0000000..72f9158 --- /dev/null +++ b/java-demo1-jsf-web/build.gradle @@ -0,0 +1,10 @@ +apply plugin: 'war' + +dependencies { + implementation project(':javaee-demo1-ejb') + implementation 'org.slf4j:slf4j-api:1.7.22' + providedCompile 'javax:javaee-api:8.0' + testImplementation group: 'junit', name: 'junit', version: '4.11' +} + + diff --git a/java-demo1-jsf-web/src/main/java/pl/tpolgrabia/trainings/javaee/controllers/HelloController.java b/java-demo1-jsf-web/src/main/java/pl/tpolgrabia/trainings/javaee/controllers/HelloController.java new file mode 100644 index 0000000..0021e93 --- /dev/null +++ b/java-demo1-jsf-web/src/main/java/pl/tpolgrabia/trainings/javaee/controllers/HelloController.java @@ -0,0 +1,57 @@ +package pl.tpolgrabia.trainings.javaee.controllers; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pl.tpolgrabia.trainings.javaee.javaeedemo.ejb.TodosDao; +import pl.tpolgrabia.trainings.javaee.javaeedemo.ejb.UserDao; +import pl.tpolgrabia.trainings.javaee.javaeedemo.entities.User; + +import javax.annotation.PostConstruct; +import javax.ejb.EJB; +import javax.enterprise.context.RequestScoped; +import javax.inject.Named; +import java.util.List; + +@Named +@RequestScoped +public class HelloController { + private static final Logger logger = LoggerFactory.getLogger(HelloController.class); + + @EJB + private TodosDao todosDao; + + @EJB + private UserDao userDao; + + private String message; + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public List getAllTodos() { + return todosDao.findAll(); + } + + public List getAllUsers() { + return userDao.findAll(); + } + + public String handleSubmit() { + logger.info("Hello submit"); + return "index"; + } + + public void handleSimpleAction() { + logger.info("Hello action"); + } + + @PostConstruct + public void init() { + this.message = "Submit me"; + } +} diff --git a/java-demo1-jsf-web/src/main/webapp/WEB-INF/web.xml b/java-demo1-jsf-web/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..09e7507 --- /dev/null +++ b/java-demo1-jsf-web/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,23 @@ + + + + JSF example + + + index.xhtml + + + + faces + javax.faces.webapp.FacesServlet + 1 + + + + faces + *.xhtml + + \ No newline at end of file diff --git a/java-demo1-jsf-web/src/main/webapp/index.xhtml b/java-demo1-jsf-web/src/main/webapp/index.xhtml new file mode 100644 index 0000000..9ad1751 --- /dev/null +++ b/java-demo1-jsf-web/src/main/webapp/index.xhtml @@ -0,0 +1,27 @@ + + +JSF 2.2 + +

Hello JSF

+
+ +
+ +
    + +
  • #{todo}
  • +
    +
+ +
    + +
  1. #{user.firstName}
  2. +
    +
+ + \ No newline at end of file diff --git a/javaee-demo1-ear/build.gradle b/javaee-demo1-ear/build.gradle new file mode 100644 index 0000000..22fe35a --- /dev/null +++ b/javaee-demo1-ear/build.gradle @@ -0,0 +1,15 @@ +apply plugin: 'ear' + +dependencies { + deploy project(':javaee-demo1-ejb') + deploy project(path: ':javaee-demo1-web', configuration: 'archives') +} + +ear { + appDirName 'src/main/app' // use application metadata found in this folder + libDirName 'APP-INF/lib' + deploymentDescriptor { // custom entries for application.xml: + initializeInOrder = true + webModule('javaee-demo1-web-' + project.version + '.war', '/api') + } +} \ No newline at end of file diff --git a/javaee-demo1-ear/src/main/webapp/WEB-INF/application.xml b/javaee-demo1-ear/src/main/webapp/WEB-INF/application.xml new file mode 100644 index 0000000..099ef81 --- /dev/null +++ b/javaee-demo1-ear/src/main/webapp/WEB-INF/application.xml @@ -0,0 +1,17 @@ + + JBossDukesBank + + + javaee-demo1-ejb-1.0-SNAPSHOT.jar + + + + javaee-demo1-web-1.0-SNAPSHOT.war + api + + + + \ No newline at end of file diff --git a/javaee-demo1-ejb/build.gradle b/javaee-demo1-ejb/build.gradle new file mode 100644 index 0000000..b1e4469 --- /dev/null +++ b/javaee-demo1-ejb/build.gradle @@ -0,0 +1,14 @@ +plugins { + id 'java' +} + +group 'pl.tpolgrabia.trainings.javaee' + +sourceCompatibility = 1.8 + +dependencies { + implementation 'javax:javaee-api:8.0.1' + // wildfly 10 version I use :-) + implementation 'org.slf4j:slf4j-api:1.7.22' + testCompile group: 'junit', name: 'junit', version: '4.12' +} diff --git a/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/ejb/FakeTodosDao.java b/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/ejb/FakeTodosDao.java new file mode 100644 index 0000000..cef776e --- /dev/null +++ b/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/ejb/FakeTodosDao.java @@ -0,0 +1,13 @@ +package pl.tpolgrabia.trainings.javaee.javaeedemo.ejb; + +import javax.ejb.Stateless; +import java.util.Arrays; +import java.util.List; + +@Stateless +public class FakeTodosDao implements TodosDao { + @Override + public List findAll() { + return Arrays.asList("Todo 1", "Todo 2", "Todo 3"); + } +} diff --git a/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/ejb/JmsSimpleMessageSendingBean.java b/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/ejb/JmsSimpleMessageSendingBean.java new file mode 100644 index 0000000..9ee18ce --- /dev/null +++ b/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/ejb/JmsSimpleMessageSendingBean.java @@ -0,0 +1,36 @@ +package pl.tpolgrabia.trainings.javaee.javaeedemo.ejb; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Resource; +import javax.ejb.Stateless; +import javax.jms.ConnectionFactory; +import javax.jms.JMSContext; +import javax.jms.Queue; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; + +@Stateless +@Path("/hello") +public class JmsSimpleMessageSendingBean implements SimpleMessageSendingBean { + + private static final Logger logger = LoggerFactory.getLogger(JmsSimpleMessageSendingBean.class); + + @Resource(lookup = "java:/ConnectionFactory") + private ConnectionFactory connectionFactory; + + @Resource(lookup = "java:/jms/queue/MyQueue") + private Queue queue; + + @Override + @Path("/say/{what}") + public void sendHello( + @PathParam("what") String what) { + + try (JMSContext ctx = connectionFactory.createContext()) { + logger.info("Sending hello {}", what); + ctx.createProducer().send(queue, what); + } + } +} diff --git a/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/ejb/JpaUserDao.java b/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/ejb/JpaUserDao.java new file mode 100644 index 0000000..9830e43 --- /dev/null +++ b/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/ejb/JpaUserDao.java @@ -0,0 +1,19 @@ +package pl.tpolgrabia.trainings.javaee.javaeedemo.ejb; + +import pl.tpolgrabia.trainings.javaee.javaeedemo.entities.User; + +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import java.util.List; + +@Stateless +public class JpaUserDao implements UserDao { + @PersistenceContext(unitName = "my-persistence-unit") + private EntityManager em; + + @Override + public List findAll() { + return em.createQuery("select u from User u", User.class).getResultList(); + } +} diff --git a/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/ejb/SimpleMessageReceivingBean.java b/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/ejb/SimpleMessageReceivingBean.java new file mode 100644 index 0000000..fe02fe2 --- /dev/null +++ b/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/ejb/SimpleMessageReceivingBean.java @@ -0,0 +1,33 @@ +package pl.tpolgrabia.trainings.javaee.javaeedemo.ejb; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.ejb.ActivationConfigProperty; +import javax.ejb.MessageDriven; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; + +@MessageDriven(activationConfig = { + @ActivationConfigProperty( + propertyName = "destinationLookup", + propertyValue = "java:/jms/queue/MyQueue"), + @ActivationConfigProperty( + propertyName = "destinationType", + propertyValue = "javax.jms.Queue"), +}) +public class SimpleMessageReceivingBean implements MessageListener { + + private Logger logger = LoggerFactory.getLogger(SimpleMessageReceivingBean.class); + + @Override + public void onMessage(Message message) { + try { + String data = message.getBody(String.class); + logger.info("Got message {}", data); + } catch (JMSException e) { + throw new IllegalStateException("JMS exception", e); + } + } +} diff --git a/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/ejb/SimpleMessageSendingBean.java b/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/ejb/SimpleMessageSendingBean.java new file mode 100644 index 0000000..01c7d77 --- /dev/null +++ b/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/ejb/SimpleMessageSendingBean.java @@ -0,0 +1,8 @@ +package pl.tpolgrabia.trainings.javaee.javaeedemo.ejb; + +import javax.ejb.Local; + +@Local +public interface SimpleMessageSendingBean { + void sendHello(String what); +} diff --git a/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/ejb/TodosDao.java b/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/ejb/TodosDao.java new file mode 100644 index 0000000..713c599 --- /dev/null +++ b/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/ejb/TodosDao.java @@ -0,0 +1,9 @@ +package pl.tpolgrabia.trainings.javaee.javaeedemo.ejb; + +import javax.ejb.Local; +import java.util.List; + +@Local +public interface TodosDao { + List findAll(); +} diff --git a/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/ejb/UserDao.java b/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/ejb/UserDao.java new file mode 100644 index 0000000..dea1529 --- /dev/null +++ b/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/ejb/UserDao.java @@ -0,0 +1,11 @@ +package pl.tpolgrabia.trainings.javaee.javaeedemo.ejb; + +import pl.tpolgrabia.trainings.javaee.javaeedemo.entities.User; + +import javax.ejb.Local; +import java.util.List; + +@Local +public interface UserDao { + List findAll(); +} diff --git a/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/entities/User.java b/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/entities/User.java new file mode 100644 index 0000000..0e12d8e --- /dev/null +++ b/javaee-demo1-ejb/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/entities/User.java @@ -0,0 +1,43 @@ +package pl.tpolgrabia.trainings.javaee.javaeedemo.entities; + +import javax.persistence.*; +import java.io.Serializable; + +@Entity +@Table(name = "users") +public class User implements Serializable { + private static final long serialVersionUID = -4484248656820095286L; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "user_name") + private String userName; + + @Column(name = "first_name") + private String firstName; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } +} diff --git a/javaee-demo1-ejb/src/main/resources/META-INF/persistence.xml b/javaee-demo1-ejb/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000..28fbe4a --- /dev/null +++ b/javaee-demo1-ejb/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,13 @@ + + + + java:/MyDataSource + pl.tpolgrabia.trainings.javaee.javaeedemo.entities.User + + + + + + \ No newline at end of file diff --git a/javaee-demo1-web/build.gradle b/javaee-demo1-web/build.gradle new file mode 100644 index 0000000..69d0229 --- /dev/null +++ b/javaee-demo1-web/build.gradle @@ -0,0 +1,9 @@ +plugins { + id 'war' +} + +dependencies { + implementation project(':javaee-demo1-ejb') + providedCompile 'javax:javaee-api:8.0' + testImplementation group: 'junit', name: 'junit', version: '4.11' +} diff --git a/javaee-demo1-web/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/controllers/HelloController.java b/javaee-demo1-web/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/controllers/HelloController.java new file mode 100644 index 0000000..6eaeea2 --- /dev/null +++ b/javaee-demo1-web/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/controllers/HelloController.java @@ -0,0 +1,26 @@ +package pl.tpolgrabia.trainings.javaee.javaeedemo.controllers; + +import pl.tpolgrabia.trainings.javaee.javaeedemo.ejb.SimpleMessageSendingBean; + +import javax.ejb.EJB; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.core.Response; + +@Path("/hello") +public class HelloController { + + @EJB + private SimpleMessageSendingBean simpleMessageSendingBean; + + @GET + @Path("/say/{what}") + public Response handleSay( + @PathParam("what") String what) { + simpleMessageSendingBean.sendHello(what); + return Response.status(Response.Status.OK) + .entity("Sent " + what) + .build(); + } +} diff --git a/javaee-demo1-web/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/controllers/MathController.java b/javaee-demo1-web/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/controllers/MathController.java new file mode 100644 index 0000000..b3372c7 --- /dev/null +++ b/javaee-demo1-web/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/controllers/MathController.java @@ -0,0 +1,26 @@ +package pl.tpolgrabia.trainings.javaee.javaeedemo.controllers; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +@Path("/math") +public class MathController { + @GET + @Path("/add/{x}/{y}") + @Produces(MediaType.APPLICATION_JSON) + public Response handleAdd( + @PathParam("x") int x, + @PathParam("y") int y) { + MathResponse response = new MathResponse(); + response.setX(x); + response.setY(y); + response.setZ(x + y); + return Response.status(Response.Status.OK) + .entity(response) + .build(); + } +} diff --git a/javaee-demo1-web/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/controllers/MathResponse.java b/javaee-demo1-web/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/controllers/MathResponse.java new file mode 100644 index 0000000..1b8043e --- /dev/null +++ b/javaee-demo1-web/src/main/java/pl/tpolgrabia/trainings/javaee/javaeedemo/controllers/MathResponse.java @@ -0,0 +1,31 @@ +package pl.tpolgrabia.trainings.javaee.javaeedemo.controllers; + +public class MathResponse { + private int x; + private int y; + private int z; + + public int getX() { + return x; + } + + public void setX(int x) { + this.x = x; + } + + public int getY() { + return y; + } + + public void setY(int y) { + this.y = y; + } + + public int getZ() { + return z; + } + + public void setZ(int z) { + this.z = z; + } +} diff --git a/javaee-demo1-web/src/main/webapp/WEB-INF/web.xml b/javaee-demo1-web/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..027423c --- /dev/null +++ b/javaee-demo1-web/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,14 @@ + + + Restful Web Application + + + javax.ws.rs.core.Application + /* + + + \ No newline at end of file diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..3b501a2 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,6 @@ +rootProject.name = 'javaee-demo1' +include 'javaee-demo1-ejb' +include 'javaee-demo1-ear' +include 'javaee-demo1-web' +include 'java-demo1-jsf-web' +