[jboss-cvs] JBossAS SVN: r82450 - in projects/ejb3/trunk/docs/tutorial: injection and 7 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Dec 19 12:28:47 EST 2008


Author: jaikiran
Date: 2008-12-19 12:28:47 -0500 (Fri, 19 Dec 2008)
New Revision: 82450

Added:
   projects/ejb3/trunk/docs/tutorial/injection/
   projects/ejb3/trunk/docs/tutorial/injection/build.xml
   projects/ejb3/trunk/docs/tutorial/injection/injection.html
   projects/ejb3/trunk/docs/tutorial/injection/injection.wiki
   projects/ejb3/trunk/docs/tutorial/injection/jndi.properties
   projects/ejb3/trunk/docs/tutorial/injection/log4j.xml
   projects/ejb3/trunk/docs/tutorial/injection/pom.xml
   projects/ejb3/trunk/docs/tutorial/injection/src/
   projects/ejb3/trunk/docs/tutorial/injection/src/org/
   projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/
   projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/
   projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/
   projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/bean/
   projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/bean/Calculator.java
   projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/bean/CalculatorBean.java
   projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/bean/ShoppingCart.java
   projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/bean/ShoppingCartBean.java
   projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/client/
   projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/client/Client.java
Log:
Initial version of the injection tutorial

Added: projects/ejb3/trunk/docs/tutorial/injection/build.xml
===================================================================
--- projects/ejb3/trunk/docs/tutorial/injection/build.xml	                        (rev 0)
+++ projects/ejb3/trunk/docs/tutorial/injection/build.xml	2008-12-19 17:28:47 UTC (rev 82450)
@@ -0,0 +1,81 @@
+<?xml version="1.0"?>
+
+<!-- ======================================================================= -->
+<!-- JBoss build file                                                       -->
+<!-- ======================================================================= -->
+
+<project name="JBoss" default="ejbjar" basedir=".">
+
+   <property environment="env"/>
+   <property name="src.dir" value="${basedir}/src"/>
+	<property name="jboss.home" value="${env.JBOSS_HOME}"/>
+	<property name="jboss.server.config" value="default"/>
+	<property name="build.dir" value="${basedir}/build"/>
+   	<property name="build.classes.dir" value="${build.dir}/classes"/>
+	<property name="build.artifact" value="jboss-ejb3-tutorial-injection.jar"/>
+
+   <!-- Build classpath -->
+   <path id="classpath">
+      <!-- So that we can get jndi.properties for InitialContext -->
+      <pathelement location="${basedir}"/>
+   		<!-- Only the jbossall-client.jar should ideally be sufficient -->
+      <fileset dir="${jboss.home}/client">
+         <include name="**/jbossall-client.jar"/>
+      </fileset>
+      <pathelement location="${build.classes.dir}"/>
+   </path>
+
+   <property name="build.classpath" refid="classpath"/>
+
+   <!-- =================================================================== -->
+   <!-- Prepares the build directory                                        -->
+   <!-- =================================================================== -->
+   <target name="prepare">
+      <mkdir dir="${build.dir}"/>
+      <mkdir dir="${build.classes.dir}"/>
+   </target>
+
+   <!-- =================================================================== -->
+   <!-- Compiles the source code                                            -->
+   <!-- =================================================================== -->
+   <target name="compile" depends="prepare">
+      <javac srcdir="${src.dir}"
+         destdir="${build.classes.dir}"
+         debug="on"
+         deprecation="on"
+         optimize="off"
+         includes="**">
+         <classpath refid="classpath"/>
+      </javac>
+   </target>
+
+   <target name="ejbjar" depends="compile">
+      <jar jarfile="build/${build.artifact}">
+         <fileset dir="${build.classes.dir}">
+            <include name="**/*.class"/>
+         </fileset>
+      </jar>
+      <copy file="build/${build.artifact}" todir="${jboss.home}/server/${jboss.server.config}/deploy"/>
+   </target>
+
+   <target name="run" depends="ejbjar">
+      <java classname="org.jboss.tutorial.injection.client.Client" fork="yes" dir=".">
+         <classpath refid="classpath"/>
+      </java>
+   </target>
+
+   <!-- =================================================================== -->
+   <!-- Cleans up generated stuff                                           -->
+   <!-- =================================================================== -->
+   <target name="clean.db">
+      <delete dir="${jboss.home}/server/${jboss.server.config}/data/hypersonic"/>
+   </target>
+
+   <target name="clean">
+      <delete dir="${build.dir}"/>
+      <delete file="${jboss.home}/server/${jboss.server.config}/deploy/${build.artifact}"/>
+   </target>
+
+
+</project>
+


Property changes on: projects/ejb3/trunk/docs/tutorial/injection/build.xml
___________________________________________________________________
Name: svn:executable
   + *

Added: projects/ejb3/trunk/docs/tutorial/injection/injection.html
===================================================================
--- projects/ejb3/trunk/docs/tutorial/injection/injection.html	                        (rev 0)
+++ projects/ejb3/trunk/docs/tutorial/injection/injection.html	2008-12-19 17:28:47 UTC (rev 82450)
@@ -0,0 +1,83 @@
+<html>
+<body>
+<p>
+<h2>Dependency Injection</h2>
+
+To facilitate test driven development, the EJB 3.0 specification allows you to use annotations to inject dependencies through annotations on fields or setter methods.  Instead of complicated XML ejb-refs or resource refs, you can use the <tt>@EJB</tt> and <tt>@Resource</tt> annotations to set the value of a field or to call a setter method within your session bean with anything registered within JNDI.  You can use the <tt>@EJB</tt> annotation to inject EJB references and <tt>@Resource</tt> to access datasources.
+</p><p>
+Open up <a href="src/org/jboss/tutorial/injection/bean/ShoppingCartBean.java">ShoppingCartBean.java</a>.  ShoppingCartBean uses the Calculator stateless session EJB to do calculations.  The example shows two ways to get access to the Calculator EJB.  One is:
+</p><p>
+<pre>
+   @EJB
+   private Calculator calculator;
+</pre>
+</p><p>
+When the ShoppingCartBean instance is created, the EJB container will set the calculator field using the jndiName of that particular referenced EJB.
+</p><p>
+You are not limited to injecting dependencies on fields.  You can also use @EJB on a setter method.  The below example from ShoppingCartBean uses the <tt>@EJB</tt> annotation to inject the reference to the Calculator session bean:
+</p><p>
+<pre>
+   private Calculator set;
+
+   @EJB(beanName="org.jboss.tutorial.injection.bean.CalculatorBean")
+   public void setCalculator(Calculator c)
+   {
+      set = c;
+   }
+</pre>
+</p><p>
+The @javax.annotation.Resource annotation allows you to inject resources.
+</p><p>
+<pre>
+
+   @Resource(mappedName="DefaultDS")
+   private javax.sql.DataSource ds;
+</pre>
+</p><p>
+In JBoss, whenever the mappedName() attribute is specified (@Resource, @EJB), jboss will use that as the GLOBAL jndi name to look it up.
+</p><p>
+The @Resource annotation is used to inject these singletons as well.
+<pre>
+   @Resource javax.ejb.SessionContext ctx;
+   @Resource javax.ejb.TimerService timer;
+   @Resource javax.ejb.UserTransaction ut;
+</pre>
+</p><p>
+<tt>@EJB</tt> and <tt>@Resource</tt> also create an entry within the JNDI ENC of the bean.  So, the above @EJB injection will create an entry for the reference calculator bean under "java:comp.ejb3/env/ejb/calculator".
+Note, the ENC name <i>SHOULD</i> be "java:/comp/env", but the JBoss Application server is currently being refactored in this
+area, so an EJB3 specific namespace is being used.
+   
+<h4>Building and Running</h4>
+
+To build and run the example, make sure you have <tt>ejb3.deployer</tt> installed in JBoss 4.0.x and have JBoss running.  See the reference manual on how to install EJB 3.0.  
+<pre>
+Unix:    $ export JBOSS_HOME=&lt;where your jboss 4.0 distribution is&gt;
+Windows: $ set JBOSS_HOME=&lt;where your jboss 4.0 distribution is&gt;
+$ ant
+$ ant run
+
+run:
+run:
+     [java] Buying 1 memory stick
+     [java] 2004-10-06 19:37:16,869 INFO org.jboss.remoting.InvokerRegistry[main] - Failed to load soap remoting transpo
+rt: org/apache/axis/AxisFault
+     [java] Buying another memory stick
+     [java] Buying a laptop
+     [java] Print cart:
+     [java] 2     Memory stick
+     [java] 1     Laptop
+     [java] Checkout
+</pre>
+</p><p>
+The INFO message you can ignore.  It will be fixed in later releases of JBoss 4.0.
+</p><p>
+<h4>Jar structure</h4>
+
+EJB 3.0 beans must be packaged in a JAR file with the suffix <tt>.jar</tt>.  Running the ant script above creates a JAR file within the deploy/ directory of JBoss.  All that needs to be in that jar is your server-side class files.  So basically just the ShoppingCartBean and the interfaces it implements.  JBoss will automatically browse the JAR file to determine if any EJBs are annotated by any classes within it.  THere is no precompilation step.
+</p><p>
+</p><p>
+</p><p>
+</p><p>
+</p>
+</body>
+</html>


Property changes on: projects/ejb3/trunk/docs/tutorial/injection/injection.html
___________________________________________________________________
Name: svn:executable
   + *

Added: projects/ejb3/trunk/docs/tutorial/injection/injection.wiki
===================================================================
--- projects/ejb3/trunk/docs/tutorial/injection/injection.wiki	                        (rev 0)
+++ projects/ejb3/trunk/docs/tutorial/injection/injection.wiki	2008-12-19 17:28:47 UTC (rev 82450)
@@ -0,0 +1,74 @@
+!!!Dependency Injection
+To facilitate test driven development, the EJB 3.0 specification allows you to use annotations to inject dependencies through annotations on fields or setter methods.  Instead of complicated XML ejb-refs or resource refs, you can use the {{@EJB}} and {{@Resource}} annotations to set the value of a field or to call a setter method within your session bean with anything registered within JNDI.  You can use the {{@EJB}} annotation to inject EJB references and {{@Resource}} to access datasources.
+
+Open up [ShoppingCartBean.java|src/org/jboss/tutorial/injection/bean/ShoppingCartBean.java].  ShoppingCartBean uses the Calculator stateless session EJB to do calculations.  The example shows two ways to get access to the Calculator EJB.  One is:
+
+{{{
+   @EJB
+   private Calculator calculator;
+}}}
+
+When the ShoppingCartBean instance is created, the EJB container will set the calculator field using the jndiName of that particular referenced EJB.
+
+You are not limited to injecting dependencies on fields.  You can also use @EJB on a setter method.  The below example from ShoppingCartBean uses the {{@EJB}} annotation to inject the reference to the Calculator session bean:
+
+{{{
+   private Calculator set;
+
+   @EJB(beanName="org.jboss.tutorial.injection.bean.CalculatorBean")
+   public void setCalculator(Calculator c)
+   {
+      set = c;
+   }
+}}}
+
+The @javax.annotation.Resource annotation allows you to inject resources.
+
+{{{
+
+   @Resource(mappedName="DefaultDS")
+   private javax.sql.DataSource ds;
+}}}
+
+In JBoss, whenever the mappedName() attribute is specified (@Resource, @EJB), jboss will use that as the GLOBAL jndi name to look it up.
+
+The @Resource annotation is used to inject these singletons as well.
+{{{
+   @Resource javax.ejb.SessionContext ctx;
+   @Resource javax.ejb.TimerService timer;
+   @Resource javax.ejb.UserTransaction ut;
+}}}
+
+{{@EJB}} and {{@Resource}} also create an entry within the JNDI ENC of the bean.  So, the above @EJB injection will create an entry for the reference calculator bean under "java:comp.ejb3/env/ejb/calculator".
+Note, the ENC name ''SHOULD'' be "java:/comp/env", but the JBoss Application server is currently being refactored in this
+area, so an EJB3 specific namespace is being used.
+   
+!Building and Running
+To build and run the example, make sure you have {{ejb3.deployer}} installed in JBoss 4.0.x and have JBoss running.  See the reference manual on how to install EJB 3.0.  
+{{{
+Unix:    $ export JBOSS_HOME=<where your jboss 4.0 distribution is>
+Windows: $ set JBOSS_HOME=<where your jboss 4.0 distribution is>
+$ ant
+$ ant run
+
+run:
+run:
+     [java] Buying 1 memory stick
+     [java] 2004-10-06 19:37:16,869 INFO org.jboss.remoting.InvokerRegistry[main] - Failed to load soap remoting transpo
+rt: org/apache/axis/AxisFault
+     [java] Buying another memory stick
+     [java] Buying a laptop
+     [java] Print cart:
+     [java] 2     Memory stick
+     [java] 1     Laptop
+     [java] Checkout
+}}}
+
+The INFO message you can ignore.  It will be fixed in later releases of JBoss 4.0.
+
+!Jar structure
+EJB 3.0 beans must be packaged in a JAR file with the suffix {{.jar}}.  Running the ant script above creates a JAR file within the deploy/ directory of JBoss.  All that needs to be in that jar is your server-side class files.  So basically just the ShoppingCartBean and the interfaces it implements.  JBoss will automatically browse the JAR file to determine if any EJBs are annotated by any classes within it.  THere is no precompilation step.
+
+
+
+


Property changes on: projects/ejb3/trunk/docs/tutorial/injection/injection.wiki
___________________________________________________________________
Name: svn:executable
   + *

Added: projects/ejb3/trunk/docs/tutorial/injection/jndi.properties
===================================================================
--- projects/ejb3/trunk/docs/tutorial/injection/jndi.properties	                        (rev 0)
+++ projects/ejb3/trunk/docs/tutorial/injection/jndi.properties	2008-12-19 17:28:47 UTC (rev 82450)
@@ -0,0 +1,3 @@
+java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
+java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
+java.naming.provider.url=localhost


Property changes on: projects/ejb3/trunk/docs/tutorial/injection/jndi.properties
___________________________________________________________________
Name: svn:executable
   + *

Added: projects/ejb3/trunk/docs/tutorial/injection/log4j.xml
===================================================================
--- projects/ejb3/trunk/docs/tutorial/injection/log4j.xml	                        (rev 0)
+++ projects/ejb3/trunk/docs/tutorial/injection/log4j.xml	2008-12-19 17:28:47 UTC (rev 82450)
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
+
+<!-- ===================================================================== -->
+<!--                                                                       -->
+<!--  Log4j Configuration                                                  -->
+<!--                                                                       -->
+<!-- ===================================================================== -->
+
+<!-- $Id: log4j.xml 32809 2005-06-24 04:49:29Z bill $ -->
+
+<!--
+   | For more configuration infromation and examples see the Jakarta Log4j
+   | owebsite: http://jakarta.apache.org/log4j
+ -->
+
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
+   
+<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
+      <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
+      <param name="Target" value="System.out"/>
+      <param name="Threshold" value="INFO"/>
+
+      <layout class="org.apache.log4j.PatternLayout">
+         <!-- The default pattern: Date Priority [Category] Messagen -->
+         <!--
+         <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
+         -->
+         <param name="ConversionPattern" value="%-5p %d{dd-MM HH:mm:ss,SSS} (%F:%M:%L)  -%m%n"/>
+      </layout>
+</appender>
+
+   <root>
+      <appender-ref ref="CONSOLE"/>
+   </root>
+
+</log4j:configuration>


Property changes on: projects/ejb3/trunk/docs/tutorial/injection/log4j.xml
___________________________________________________________________
Name: svn:executable
   + *

Added: projects/ejb3/trunk/docs/tutorial/injection/pom.xml
===================================================================
--- projects/ejb3/trunk/docs/tutorial/injection/pom.xml	                        (rev 0)
+++ projects/ejb3/trunk/docs/tutorial/injection/pom.xml	2008-12-19 17:28:47 UTC (rev 82450)
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+  
+
+  
+   
+  <!-- Model Version -->
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.jboss.ejb3</groupId>
+    <artifactId>jboss-ejb3-tutorial-common</artifactId>
+    <version>0.1.0-SNAPSHOT</version>
+    <relativePath>../common/</relativePath>
+  </parent>
+
+  <properties>
+    <ejb3.tutorial.client>org.jboss.tutorial.injection.client.Client</ejb3.tutorial.client>
+  </properties>
+
+
+  <artifactId>jboss-ejb3-tutorial-injection</artifactId>
+  <version>0.1.0-SNAPSHOT</version>
+  <packaging>jar</packaging>
+  <name>Injection in EJB3</name>
+  <url>http://labs.jboss.com/jbossejb3/</url>
+  <description>
+    Tutorial explaining how to use injection in EJB3
+  </description>
+  
+  
+  
+  
+
+
+</project>


Property changes on: projects/ejb3/trunk/docs/tutorial/injection/pom.xml
___________________________________________________________________
Name: svn:executable
   + *

Added: projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/bean/Calculator.java
===================================================================
--- projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/bean/Calculator.java	                        (rev 0)
+++ projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/bean/Calculator.java	2008-12-19 17:28:47 UTC (rev 82450)
@@ -0,0 +1,29 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.tutorial.injection.bean;
+
+public interface Calculator
+{
+   int add(int x, int y);
+
+   int subtract(int x, int y);
+}


Property changes on: projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/bean/Calculator.java
___________________________________________________________________
Name: svn:executable
   + *

Added: projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/bean/CalculatorBean.java
===================================================================
--- projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/bean/CalculatorBean.java	                        (rev 0)
+++ projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/bean/CalculatorBean.java	2008-12-19 17:28:47 UTC (rev 82450)
@@ -0,0 +1,38 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.tutorial.injection.bean;
+
+import javax.ejb.Stateless;
+
+ at Stateless
+public class CalculatorBean implements Calculator
+{
+   public int add(int x, int y)
+   {
+      return x + y;
+   }
+
+   public int subtract(int x, int y)
+   {
+      return x - y;
+   }
+}


Property changes on: projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/bean/CalculatorBean.java
___________________________________________________________________
Name: svn:executable
   + *

Added: projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/bean/ShoppingCart.java
===================================================================
--- projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/bean/ShoppingCart.java	                        (rev 0)
+++ projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/bean/ShoppingCart.java	2008-12-19 17:28:47 UTC (rev 82450)
@@ -0,0 +1,34 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.tutorial.injection.bean;
+
+import java.util.HashMap;
+import javax.ejb.Remove;
+
+public interface ShoppingCart
+{
+   void buy(String product, int quantity);
+
+   HashMap<String, Integer> getCartContents();
+
+   @Remove void checkout();
+}


Property changes on: projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/bean/ShoppingCart.java
___________________________________________________________________
Name: svn:executable
   + *

Added: projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/bean/ShoppingCartBean.java
===================================================================
--- projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/bean/ShoppingCartBean.java	                        (rev 0)
+++ projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/bean/ShoppingCartBean.java	2008-12-19 17:28:47 UTC (rev 82450)
@@ -0,0 +1,74 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.tutorial.injection.bean;
+
+import java.util.HashMap;
+import javax.ejb.EJB;
+import javax.ejb.Remote;
+import javax.ejb.Remove;
+import javax.ejb.Stateful;
+
+
+ at Stateful
+ at Remote(ShoppingCart.class)
+public class ShoppingCartBean implements ShoppingCart, java.io.Serializable
+{
+   private HashMap<String, Integer> cart = new HashMap<String, Integer>();
+
+   @EJB
+   private Calculator calculator;
+
+
+   private Calculator set;
+
+   @EJB(beanName="CalculatorBean")
+   public void setCalculator(Calculator c)
+   {
+      set = c;
+   }
+
+
+   public void buy(String product, int quantity)
+   {
+      if (cart.containsKey(product))
+      {
+         int currq = cart.get(product);
+         currq = calculator.add(currq, quantity);
+         cart.put(product, currq);
+      }
+      else
+      {
+         cart.put(product, quantity);
+      }
+   }
+
+   public HashMap<String, Integer> getCartContents()
+   {
+      return cart;
+   }
+
+   @Remove
+   public void checkout()
+   {
+      System.out.println("To be implemented");
+   }
+}


Property changes on: projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/bean/ShoppingCartBean.java
___________________________________________________________________
Name: svn:executable
   + *

Added: projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/client/Client.java
===================================================================
--- projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/client/Client.java	                        (rev 0)
+++ projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/client/Client.java	2008-12-19 17:28:47 UTC (rev 82450)
@@ -0,0 +1,63 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.tutorial.injection.client;
+
+
+import org.jboss.tutorial.injection.bean.ShoppingCart;
+
+import javax.naming.InitialContext;
+
+import java.util.HashMap;
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
+ * @version $Revision: 61136 $
+ */
+public class Client
+{
+   public static void main(String[] args) throws Exception
+   {
+      InitialContext ctx = new InitialContext();
+      ShoppingCart cart = (ShoppingCart) ctx.lookup("ShoppingCartBean/remote");
+
+      System.out.println("Buying 1 memory stick");
+      cart.buy("Memory stick", 1);
+      System.out.println("Buying another memory stick");
+      cart.buy("Memory stick", 1);
+
+      System.out.println("Buying a laptop");
+      cart.buy("Laptop", 1);
+
+      System.out.println("Print cart:");
+      HashMap<String, Integer> fullCart = cart.getCartContents();
+      for (String product : fullCart.keySet())
+      {
+         System.out.println(fullCart.get(product) + "     " + product);
+      }
+
+      System.out.println("Checkout");
+      cart.checkout();
+
+   }
+}


Property changes on: projects/ejb3/trunk/docs/tutorial/injection/src/org/jboss/tutorial/injection/client/Client.java
___________________________________________________________________
Name: svn:executable
   + *




More information about the jboss-cvs-commits mailing list