From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- .../src/examples/stateless/HelloWorldBean.java | 12 ++++ .../examples/stateless/META-INF/application.xml | 17 +++++ .../src/examples/stateless/PricerBean.java | 66 +++++++++++++++++ .../src/examples/stateless/TaxRateBean.java | 20 ++++++ .../src/examples/stateless/WEB-INF/web.xml | 25 +++++++ .../src/examples/stateless/build.xml | 26 +++++++ .../examples/stateless/client/PricerClient.java | 36 ++++++++++ .../stateless/container/HelloWorldServlet.java | 54 ++++++++++++++ .../stateless/container/PricerClientServlet.java | 84 ++++++++++++++++++++++ .../stateless/interceptors/AuditorInterceptor.java | 18 +++++ .../stateless/interceptors/LoggerInterceptor.java | 16 +++++ .../examples/stateless/interfaces/HelloWorld.java | 8 +++ .../src/examples/stateless/interfaces/Pricer.java | 9 +++ .../stateless/interfaces/PricerInjection.java | 5 ++ .../stateless/interfaces/PricerLookup.java | 5 ++ .../src/examples/stateless/interfaces/TaxRate.java | 8 +++ 16 files changed, 409 insertions(+) create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/HelloWorldBean.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/META-INF/application.xml create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/PricerBean.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/TaxRateBean.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/WEB-INF/web.xml create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/build.xml create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/client/PricerClient.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/container/HelloWorldServlet.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/container/PricerClientServlet.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interceptors/AuditorInterceptor.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interceptors/LoggerInterceptor.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interfaces/HelloWorld.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interfaces/Pricer.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interfaces/PricerInjection.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interfaces/PricerLookup.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interfaces/TaxRate.java (limited to 'Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless') diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/HelloWorldBean.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/HelloWorldBean.java new file mode 100644 index 0000000..289e962 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/HelloWorldBean.java @@ -0,0 +1,12 @@ +package examples.stateless; + +import javax.ejb.Stateless; + +import examples.stateless.interfaces.HelloWorld; + +@Stateless +public class HelloWorldBean implements HelloWorld { + public String hi() { + return "hi!"; + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/META-INF/application.xml b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/META-INF/application.xml new file mode 100644 index 0000000..a7cc0e6 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/META-INF/application.xml @@ -0,0 +1,17 @@ + + + StatelessExamples + + StatelessExamplesEjb.jar + + + + StatelessExamplesWeb.war + /StatelessExamples + + + diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/PricerBean.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/PricerBean.java new file mode 100644 index 0000000..58697d6 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/PricerBean.java @@ -0,0 +1,66 @@ +package examples.stateless; + +import javax.ejb.EJB; +import javax.ejb.Remote; +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; +import javax.ejb.Stateless; +import javax.interceptor.AroundInvoke; +import javax.interceptor.Interceptors; +import javax.interceptor.InvocationContext; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import examples.stateless.interceptors.AuditorInterceptor; +import examples.stateless.interceptors.LoggerInterceptor; +import examples.stateless.interfaces.PricerInjection; +import examples.stateless.interfaces.PricerLookup; +import examples.stateless.interfaces.TaxRate; + +@Stateless +@Interceptors({LoggerInterceptor.class,AuditorInterceptor.class}) +@Remote({PricerInjection.class,PricerLookup.class}) +public class PricerBean implements PricerInjection, PricerLookup { + private TaxRate taxRate; + + @EJB + private TaxRate taxRate2; + + public double getTaxLookup(double cost, String state) { + double tax = -1; + tax = cost * taxRate.getTaxRate(state); + return tax; + } + + public double getTaxInjection(double cost, String state) { + double tax = -1; + tax = cost * taxRate2.getTaxRate(state); + return tax; + } + + @PostConstruct + public void postConstruct() { + try { + InitialContext ic = new InitialContext(); + taxRate = (TaxRate)ic.lookup(TaxRate.class.getName()); + } + catch (NamingException e) { + // some kind of appropriate handling here + } + } + + @PreDestroy + public void preDestroy() { + taxRate = null; + } + + @AroundInvoke + public Object logger(InvocationContext inv) throws Exception { + System.out.println("Intercepted call via internal method to: "+inv.getMethod().getName()); + Object[] params = inv.getParameters(); + for (int i=0;i + + + + + + PricerClient + PricerClient + examples.stateless.container.PricerClientServlet + + + HelloWorld + HelloWorld + examples.stateless.container.HelloWorldServlet + + + + PricerClient + /PricerClient + + + HelloWorld + /HelloWorld + + diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/build.xml b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/build.xml new file mode 100644 index 0000000..389568b --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/build.xml @@ -0,0 +1,26 @@ + + ]> + + + + + + + + + + + + + &include; + + + + + + + + + + + diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/client/PricerClient.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/client/PricerClient.java new file mode 100644 index 0000000..5762ab0 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/client/PricerClient.java @@ -0,0 +1,36 @@ +package examples.stateless.client; + +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import examples.stateless.interfaces.PricerInjection; +import examples.stateless.interfaces.PricerLookup; + +public class PricerClient { + public static void main(String[] args) { + try { + InitialContext ic = new InitialContext(); + + // Prefixing the name with the pound sign (#) is a convention of Glassfish + // when using multiple interfaces to make up the business interface. The + // specification is unclear on how clients should lookup beans that implement + // more than one interface. + + PricerLookup pricerLookup = (PricerLookup)ic.lookup("#"+PricerLookup.class.getName()); + PricerInjection pricerInjection = (PricerInjection)ic.lookup("#"+PricerInjection.class.getName()); + //PricerLookup pricerLookup = (PricerLookup)ic.lookup(PricerLookup.class.getName()); + //PricerInjection pricerInjection = (PricerInjection)ic.lookup(PricerInjection.class.getName()); + //Pricer pricerLookup = (Pricer)ic.lookup(Pricer.class.getName()); + //Pricer pricerInjection = pricerLookup; + + System.out.println("Tax (using lookup) on: 8.5 for State: ny is: "+ + pricerLookup.getTaxLookup(8.5,"ny")); + + System.out.println("Tax (using injection) on: 8.5 for State: ny is: "+ + pricerInjection.getTaxInjection(8.5,"ny")); + } + catch (NamingException e) { + e.printStackTrace(); + } + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/container/HelloWorldServlet.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/container/HelloWorldServlet.java new file mode 100644 index 0000000..61c78a6 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/container/HelloWorldServlet.java @@ -0,0 +1,54 @@ +package examples.stateless.container; + +import javax.ejb.EJB; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.servlet.http.HttpServlet; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import examples.stateless.interfaces.HelloWorld; + +/** + * Servlet Class + * + * @web.servlet name="HelloWorld" + * display-name="Name for HelloWorld" + * description="Description for HelloWorld" + * @web.servlet-mapping url-pattern="/HelloWorld" + * @web.servlet-init-param name="A parameter" + * value="A value" + */ +public class HelloWorldServlet extends HttpServlet { + @EJB HelloWorld hwInjected; + + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, + IOException { + + doPost(req,resp); + } + + protected void doPost(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, + IOException { + + try { + InitialContext ic = new InitialContext(); + HelloWorld hw = (HelloWorld)ic.lookup(HelloWorld.class.getName()); + + PrintWriter pw = resp.getWriter(); + resp.setContentType("text/html"); + pw.println("

Lookup output: "+hw.hi()+"

"); + pw.println("

Injection output: "+hwInjected.hi()+"

"); + } + catch (NamingException e) { + e.printStackTrace(); + } + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/container/PricerClientServlet.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/container/PricerClientServlet.java new file mode 100644 index 0000000..09b59d6 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/container/PricerClientServlet.java @@ -0,0 +1,84 @@ +package examples.stateless.container; + +import javax.ejb.EJB; +import javax.naming.InitialContext; +import javax.naming.NameClassPair; +import javax.naming.NamingEnumeration; +import javax.naming.NamingException; +import javax.servlet.http.HttpServlet; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import examples.stateless.interfaces.PricerInjection; +import examples.stateless.interfaces.PricerLookup; + +/** + * Servlet Class + * + * @web.servlet name="PricerClient" display-name="Name for PricerClient" + * description="Description for PricerClient" + * @web.servlet-mapping url-pattern="/PricerClient" + * @web.servlet-init-param name="A parameter" value="A value" + */ +public class PricerClientServlet extends HttpServlet { + @EJB PricerInjection pi2; + @EJB PricerLookup pl2; + + + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + + doPost(req, resp); + } + + protected void doPost(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + + double price = 85.0; + String state = "ny"; + + PrintWriter pw = resp.getWriter(); + resp.setContentType("text/html"); + pw.println("\n\t"); + + try { + InitialContext ic = new InitialContext(); + pw.println("

Here are the JNDI names bound for beans in this example:

"); + pw.println("
    "); + NamingEnumeration ne = ic.list(""); + while (ne.hasMore()) { + NameClassPair ncp = (NameClassPair) ne.next(); + pw.println("
  1. " + ncp.getName()); + } + pw.println("
"); + + // Pricer pricer = (Pricer)ic.lookup(Pricer.class.getName()); + PricerInjection pi = (PricerInjection)ic.lookup("#"+PricerInjection.class.getName()); + PricerLookup pl = (PricerLookup)ic.lookup("#"+PricerLookup.class.getName()); + + pw.println("Tax (using lookup) on: " + price + " for State: " + + state + " is: " + pl.getTaxLookup(price, state) + "
"); + + pw.println("Tax (using injection) on: " + price + " for State: " + + state + " is: " + pi.getTaxInjection(price, state) + + "
"); + + pw.println("Tax (using lookup from @EJB) on: " + price + " for State: " + + state + " is: " + pl2.getTaxLookup(price, state) + "
"); + + pw.println("Tax (using injection @EJB) on: " + price + " for State: " + + state + " is: " + pi2.getTaxInjection(price, state) + + "
"); + } + catch (NamingException e) { + e.printStackTrace(); + } + + pw.println("\t\n"); + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interceptors/AuditorInterceptor.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interceptors/AuditorInterceptor.java new file mode 100644 index 0000000..853a891 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interceptors/AuditorInterceptor.java @@ -0,0 +1,18 @@ +package examples.stateless.interceptors; + +import javax.interceptor.AroundInvoke; +import javax.interceptor.InvocationContext; + +public class AuditorInterceptor { + @AroundInvoke + public Object checkCost(InvocationContext inv) throws Exception { + if (inv.getMethod().getName().startsWith("getTax")) { + Object[] o = inv.getParameters(); + double cost = ((Double)o[0]).doubleValue(); + if (cost > 50) { + System.out.println("Cost is > 50!"); + } + } + return inv.proceed(); + } +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interceptors/LoggerInterceptor.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interceptors/LoggerInterceptor.java new file mode 100644 index 0000000..ee37d23 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interceptors/LoggerInterceptor.java @@ -0,0 +1,16 @@ +package examples.stateless.interceptors; + +import javax.interceptor.AroundInvoke; +import javax.interceptor.InvocationContext; + +public class LoggerInterceptor { + @AroundInvoke + public Object logger(InvocationContext inv) throws Exception { + System.out.println("Intercepted call via external class to: "+inv.getMethod().getName()); + Object[] params = inv.getParameters(); + for (int i=0;i