diff options
Diffstat (limited to 'Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless')
16 files changed, 409 insertions, 0 deletions
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 @@ +<?xml version="1.0" encoding="UTF-8"?>
+<application version="1.4"
+ xmlns="http://java.sun.com/xml/ns/j2ee"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
+ http://java.sun.com/xml/ns/j2ee/application_1_4.xsd">
+ <display-name>StatelessExamples</display-name>
+ <module>
+ <ejb>StatelessExamplesEjb.jar</ejb>
+ </module>
+ <module>
+ <web>
+ <web-uri>StatelessExamplesWeb.war</web-uri>
+ <context-root>/StatelessExamples</context-root>
+ </web>
+ </module>
+</application>
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<params.length;i++) {
+ System.out.println("\tparam: "+params[i]);
+ }
+ return inv.proceed();
+ }
+}
diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/TaxRateBean.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/TaxRateBean.java new file mode 100644 index 0000000..fb51d29 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/TaxRateBean.java @@ -0,0 +1,20 @@ +package examples.stateless;
+
+import javax.ejb.Stateless;
+
+import examples.stateless.interfaces.TaxRate;
+
+@Stateless
+public class TaxRateBean implements TaxRate {
+ public double getTaxRate(String state) {
+ if (state.equalsIgnoreCase("ny")) {
+ return 0.0875;
+ }
+ else if (state.equalsIgnoreCase("nj")) {
+ return 0.03;
+ }
+ else {
+ return 0.0;
+ }
+ }
+}
diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/WEB-INF/web.xml b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/WEB-INF/web.xml new file mode 100644 index 0000000..0100696 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/WEB-INF/web.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> + <distributable/> + + <servlet> + <display-name>PricerClient</display-name> + <servlet-name>PricerClient</servlet-name> + <servlet-class>examples.stateless.container.PricerClientServlet</servlet-class> + </servlet> + <servlet> + <display-name>HelloWorld</display-name> + <servlet-name>HelloWorld</servlet-name> + <servlet-class>examples.stateless.container.HelloWorldServlet</servlet-class> + </servlet> +
+ <servlet-mapping> + <servlet-name>PricerClient</servlet-name> + <url-pattern>/PricerClient</url-pattern> + </servlet-mapping> + <servlet-mapping> + <servlet-name>HelloWorld</servlet-name> + <url-pattern>/HelloWorld</url-pattern> + </servlet-mapping> +</web-app> 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 @@ +<?xml version="1.0"?> +<!DOCTYPE project [ <!ENTITY include SYSTEM "../../../etc/common.xml"> ]> + +<project name="ejb3-examples-stateless" default="main" basedir="../../.."> + <!-- basic settings --> + <property name="app.pkg" value="examples/stateless"/> + <property name="app.name" value="StatelessExamples"/> + <property name="create.client" value="true"/> + <property name="create.ejb" value="true"/> + <property name="create.war" value="true"/> + <property name="create.ear" value="true"/> + <property name="deploy" value="true"/> + + <!-- Include common.xml --> + &include; + + <target name="main" depends="compile.common,package.common,deploy.common"/> + + <target name="run"> + <antcall target="execClient"> + <param name="client.class" value="examples.stateless.client.PricerClient"/> + </antcall> + </target> +</project> + + 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("<h4>Lookup output: "+hw.hi()+"</h4>");
+ pw.println("<h4>Injection output: "+hwInjected.hi()+"</h4>");
+ }
+ 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("<HTML>\n\t<BODY>");
+
+ try {
+ InitialContext ic = new InitialContext();
+ pw.println("<h3>Here are the JNDI names bound for beans in this example:</h3>");
+ pw.println("<ol>");
+ NamingEnumeration ne = ic.list("");
+ while (ne.hasMore()) {
+ NameClassPair ncp = (NameClassPair) ne.next();
+ pw.println("<li>" + ncp.getName());
+ }
+ pw.println("</ol>");
+
+ // 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) + "<br>");
+
+ pw.println("Tax (using injection) on: " + price + " for State: "
+ + state + " is: " + pi.getTaxInjection(price, state)
+ + "<br>");
+
+ pw.println("Tax (using lookup from @EJB) on: " + price + " for State: "
+ + state + " is: " + pl2.getTaxLookup(price, state) + "<br>");
+
+ pw.println("Tax (using injection @EJB) on: " + price + " for State: "
+ + state + " is: " + pi2.getTaxInjection(price, state)
+ + "<br>");
+ }
+ catch (NamingException e) {
+ e.printStackTrace();
+ }
+
+ pw.println("\t</BODY>\n</HTML>");
+ }
+}
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<params.length;i++) {
+ System.out.println("\tparam: "+params[i]);
+ }
+ return inv.proceed();
+ }
+}
diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interfaces/HelloWorld.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interfaces/HelloWorld.java new file mode 100644 index 0000000..5d99f5b --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interfaces/HelloWorld.java @@ -0,0 +1,8 @@ +package examples.stateless.interfaces;
+
+import javax.ejb.Remote;
+
+@Remote
+public interface HelloWorld {
+ public String hi();
+}
diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interfaces/Pricer.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interfaces/Pricer.java new file mode 100644 index 0000000..9f74419 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interfaces/Pricer.java @@ -0,0 +1,9 @@ +package examples.stateless.interfaces; + +import javax.ejb.Remote; + +@Remote +public interface Pricer { + public double getTaxLookup(double cost, String state); + public double getTaxInjection(double cost, String state); +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interfaces/PricerInjection.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interfaces/PricerInjection.java new file mode 100644 index 0000000..4fcecb5 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interfaces/PricerInjection.java @@ -0,0 +1,5 @@ +package examples.stateless.interfaces; + +public interface PricerInjection { + public double getTaxInjection(double cost, String state); +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interfaces/PricerLookup.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interfaces/PricerLookup.java new file mode 100644 index 0000000..76996c6 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interfaces/PricerLookup.java @@ -0,0 +1,5 @@ +package examples.stateless.interfaces; + +public interface PricerLookup { + public double getTaxLookup(double cost, String state); +} diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interfaces/TaxRate.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interfaces/TaxRate.java new file mode 100644 index 0000000..9ab2b8f --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Micah Examples/src/examples/stateless/interfaces/TaxRate.java @@ -0,0 +1,8 @@ +package examples.stateless.interfaces; + +import javax.ejb.Remote; + +@Remote +public interface TaxRate { + public double getTaxRate(String state); +} |
