User: starksm 
  Date: 01/06/20 13:59:56

  Added:       src/examples/org/jboss/docs/interest Interest.java
                        InterestBean.java InterestClient.java
                        InterestHome.java InterestServlet.java
                        application.xml build.xml ejb-jar.xml home.html
                        jboss-web.xml jboss.xml web.xml
  Log:
  Add first steps interest example to manual examples src tree
  
  Revision  Changes    Path
  1.1                  manual/src/examples/org/jboss/docs/interest/Interest.java
  
  Index: Interest.java
  ===================================================================
  package org.jboss.docs.interest;
  
  import javax.ejb.EJBObject;
  import java.rmi.RemoteException;
  
  /**
  This interface defines the `Remote' interface for the `Interest' EJB. Its
  single method is the only method exposed to the outside world. The class
  InterestBean implements the method.
  */
  public interface Interest extends EJBObject 
  {
     /**
     Calulates the compound interest on the sum `principle', with interest rate per
     period `rate' over `periods' time periods. This method also prints a message to
     standard output; this is picked up by the EJB server and logged. In this way we
     can demonstrate that the method is actually being executed on the server,
     rather than the client.
     */
     public double calculateCompoundInterest(double principle, 
                                double rate, double periods) throws RemoteException;
  }
  
  
  
  1.1                  manual/src/examples/org/jboss/docs/interest/InterestBean.java
  
  Index: InterestBean.java
  ===================================================================
  package org.jboss.docs.interest;
  
  import java.rmi.RemoteException;
  import javax.ejb.SessionBean;
  import javax.ejb.SessionContext;
  
  /**
   This class contains the implementation for the `calculateCompoundInterest'
   method exposed by this Bean. It includes empty method bodies for the methods
   prescribe by the SessionBean interface; these don't need to do anything in this
   simple example.
   */
  public class InterestBean implements SessionBean
  {
     /**
     Calulates the compound interest on the sum `principle', with interest rate per
     period `rate' over `periods' time periods. This method also prints a message to
     standard output; this is picked up by the EJB server and logged. In this way we
     can demonstrate that the method is actually being executed on the server,
     rather than the client.
      */
     public double calculateCompoundInterest(double principle,
        double rate, double periods)
     {
        System.out.println("Someone called `calculateCompoundInterest!'");
        return principle * Math.pow(1+rate, periods) - principle;
     }
  
     /** Empty method body
      */
     public void ejbCreate()
     {}
     /** Empty method body
      */
     public void ejbRemove()
     {}
     /** Empty method body
      */
     public void ejbActivate()
     {}
     /** Empty method body
      */
     public void ejbPassivate()
     {}
     /** Empty method body
      */
     public void setSessionContext(SessionContext sc)
     {}
  }
  
  
  
  
  
  1.1                  manual/src/examples/org/jboss/docs/interest/InterestClient.java
  
  Index: InterestClient.java
  ===================================================================
  package org.jboss.docs.interest;
  
  import java.util.Properties;
  import javax.naming.InitialContext;
  import javax.rmi.PortableRemoteObject;
  
  import org.jboss.docs.interest.Interest;
  import org.jboss.docs.interest.InterestHome;
  
  /** This simple application tests the 'Interest' Enterprise JavaBean which is
   implemented in the package 'com.web_tomorrow.interest'. For this to work, the
   Bean must be deployed on an EJB server.
   
   IMPORTANT: If you want to test this in a real client-server
   configuration, this class goes on the client; the URL of the naming provider
   specifed in the class must be changed from 'localhost:1099' to the URL of the
   naming service on the server
   */
  class InterestClient
  {
     /** This method does all the work. It creates an instance of the Interest EJB on
      the EJB server, and calls its `calculateCompoundInterest()' method, then prints
      the result of the calculation.
      */
     public static void main(String[] args)
     {
        // Set up the naming provider; this may not always be necessary, depending
        // on how your Java system is configured.
        Properties env = new Properties();
        env.setProperty("java.naming.factory.initial",  
"org.jnp.interfaces.NamingContextFactory");
        env.setProperty("java.naming.provider.url",  "localhost:1099");
        env.setProperty("java.naming.factory.url.pkgs",  
"org.jboss.naming,org.jnp.interfaces");
        
        // Enclosing the whole process in a single `try' block is not an ideal way
        // to do exception handling, but I don't want to clutter the program up
        // with catch blocks
        try
        {
           // Get a naming context
           InitialContext jndiContext = new InitialContext(env);
           System.out.println("Got context");
           
           // Get a reference to the Interest Bean
           Object ref  = jndiContext.lookup("interest/Interest");
           System.out.println("Got reference");
           
           // Get a reference from this to the Bean's Home interface
           InterestHome home = (InterestHome)
           PortableRemoteObject.narrow(ref, InterestHome.class);
           
           // Create an Interest object from the Home interface
           Interest interest = home.create();
           
           // call the calculateCompoundInterest() method to do the calculation
           System.out.println("Interest on 1000 units, at 10% per period, compounded 
over 2 periods is:");
           System.out.println(interest.calculateCompoundInterest(1000, 0.10, 2));
        }
        catch(Exception e)
        {
           System.out.println(e.toString());
        }
     }
  }
  
  
  
  1.1                  manual/src/examples/org/jboss/docs/interest/InterestHome.java
  
  Index: InterestHome.java
  ===================================================================
  package org.jboss.docs.interest;
  
  import java.io.Serializable;
  import java.rmi.RemoteException;
  import javax.ejb.CreateException;
  import javax.ejb.EJBHome;
  
  /**
  This interface defines the `home' interface for the `Interest' EJB. 
  */
  public interface InterestHome extends EJBHome 
  {
     /**
     Creates an instance of the `InterestBean' class on the server, and returns a
     remote reference to an Interest interface on the client. 
     */
     Interest create() throws RemoteException, CreateException;
  }
  
  
  
  
  1.1                  manual/src/examples/org/jboss/docs/interest/InterestServlet.java
  
  Index: InterestServlet.java
  ===================================================================
  package org.jboss.docs.interest;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.util.Hashtable;
  import javax.naming.InitialContext;
  import javax.rmi.PortableRemoteObject;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse; 
  
  
  import org.jboss.docs.interest.Interest;
  import org.jboss.docs.interest.InterestHome;
  
  /**
   * This Servlet provides a user interface to an Enterprise Java Bean.
   * The example EJB described in the jBoss documentation at
   * http://jboss.org/ calculates compound interest.
   * This servlet will call the Interest EJB, passing it a few
   * parameters. The EJB will return a result, which the servlet
   * will display to the Web client.
   */
  public class InterestServlet extends HttpServlet
  {
     private InterestHome interestHome = null;
     
     /** Looks up the InterestHome interface and saves it for use in
      doGet().
      */
     public void init() throws ServletException
     {
        try
        {
           // Get a naming context
           InitialContext jndiContext = new InitialContext();
           // Get a reference to the Interest Bean
           Object ref  = jndiContext.lookup("java:comp/env/ejb/Interest");      
           // Get a reference from this to the Bean's Home interface
           interestHome = (InterestHome) PortableRemoteObject.narrow(ref, 
InterestHome.class);
        }
        catch(Exception e)
        {
           throw new ServletException("Failed to lookup java:comp/env/ejb/Interest", 
e);
        }
     }
  
     /**
      */
     public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
     {
        String title = "Servlet interface to EJB";
        double principal = getValue("principal", 1000.0, request);
        double rate = getValue("rate", 10.0, request);
        double periods = getValue("periods", 2.0, request);
        // set content type and other response header fields first
        response.setContentType("text/html");
        // then write the data of the response
        PrintWriter out = response.getWriter();
        
        out.println("<HTML><HEAD><TITLE>");
        out.println(title);
        out.println("</TITLE></HEAD><BODY>");
        out.println("<H1>" + title + "</H1>");     
        out.println("<H2>Calling EJB...</H2>");
  
        try
        {
           // Create an Interest object from the Home interface
           Interest bean = interestHome.create();
           // call the calculateCompoundInterest() method to do the calculation
           out.print("Interest on "+principal);
           out.print(" units, at "+rate);
           out.print("% per period, compounded over "+periods);
           out.println(" periods is: ");
           out.println(bean.calculateCompoundInterest(principal, rate/100, periods));
           bean.remove();
        }
        catch(Exception e)
        {
           out.println(e.toString());
        }
        finally
        {
           out.println("</BODY></HTML>");
           out.close();
        }
     }
  
     private double getValue(String name, double defaultValue, HttpServletRequest 
request)
     {
        double value = defaultValue;
        String pvalue = request.getParameter(name);
        if( pvalue != null )
        {
           try
           {
              value = Double.valueOf(pvalue).doubleValue();
           }
           catch(NumberFormatException e)
           {
           }
        }
        return value;
     }
  }
  
  
  
  1.1                  manual/src/examples/org/jboss/docs/interest/application.xml
  
  Index: application.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  
  <application>
      <display-name>Interest Tutorial</display-name>
  
      <module>
      <web>
          <web-uri>interest.war</web-uri>
          <context-root>/interest</context-root>
      </web>
      </module>
  
      <module>
          <ejb>interest.jar</ejb>
      </module>
  
  </application>
  
  
  
  1.1                  manual/src/examples/org/jboss/docs/interest/build.xml
  
  Index: build.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8" ?>
  <!-- An Ant build file for the Interest EJB example
  -->
  
  <project name="Interest Build Script" default="ejb-jar" basedir=".">
  
      <!-- Override with your JBoss server dist location -->
      <property name="jboss.dist" value="G:/tmp/JBoss-2.2.2_Tomcat-3.2.2/jboss"/>
      <!-- Override with your web server servlet jar location -->
      <property name="servlet.jar" 
value="G:/tmp/JBoss-2.2.2_Tomcat-3.2.2/tomcat/lib/servlet.jar"/>
  
      <property name="src.dir" value="${basedir}/src/org/jboss/docs/interest"/>
      <property name="build.dir" value="${basedir}/build"/>
      <property name="build.classes.dir" value="${build.dir}/classes"/>
  
      <path id="base.path_22">
          <pathelement location="${jboss.dist}/client/ejb.jar"/>
          <pathelement location="${jboss.dist}/client/jaas.jar"/>
          <pathelement location="${jboss.dist}/client/jbosssx-client.jar"/>
          <pathelement location="${jboss.dist}/client/jboss-client.jar"/>
          <pathelement location="${jboss.dist}/client/jnp-client.jar"/>
          <pathelement location="${servlet.jar}"/>
          <pathelement location="${build.classes.dir}"/>
      </path>
      <path id="base.path_23">
          <pathelement location="${jboss.dist}/client/jboss-j2ee.jar"/>
          <pathelement location="${jboss.dist}/client/jaas.jar"/>
          <pathelement location="${jboss.dist}/client/jbosssx-client.jar"/>
          <pathelement location="${jboss.dist}/client/jboss-client.jar"/>
          <pathelement location="${jboss.dist}/client/jnp-client.jar"/>
          <pathelement location="${servlet.jar}"/>
          <pathelement location="${build.classes.dir}"/>
      </path>
  
      <target name="validate">
          <available property="classpath_id" value="base.path_22" 
file="${jboss.dist}/client/ejb.jar" />
          <available property="classpath_id" value="base.path_23" 
file="${jboss.dist}/client/jboss-j2ee.jar" />
      </target>
      <target name="fail_if_not_valid" unless="classpath_id">
          <fail message="jboss.dist=${jboss.dist} is not a valid JBoss dist 
directory"/>
      </target>
      <target name="init" depends="validate,fail_if_not_valid">
          <property name="classpath" refid="${classpath_id}" />
          <echo message="Using jboss.dist=${jboss.dist}" />
          <echo message="Using classpath=${classpath}" />
      </target>
  
      <target name="compile" depends="init">
      <mkdir dir="${build.classes.dir}"/>
      <javac srcdir="${src.dir}"
             destdir="${build.classes.dir}"
             classpathref="${classpath_id}"
             debug="on"
             deprecation="on"
             optimize="off"
         includes="*.java"
      />
      </target>
  
      <!-- Tutorial ejb jar -->
      <target name="ejb-jar" depends="compile">
          <delete dir="${build.dir}/META-INF"/>
          <mkdir dir="${build.dir}/META-INF"/>
          <copy file="${src.dir}/ejb-jar.xml" todir="${build.dir}/META-INF" />
          <copy file="${src.dir}/jboss.xml" todir="${build.dir}/META-INF" />
          <jar jarfile="${build.dir}/interest.jar">
              <fileset dir="${build.classes.dir}">
                  <include name="**/Interest.class" />
                  <include name="**/InterestHome.class" />
                  <include name="**/InterestBean.class" />
              </fileset>
              <fileset dir="${build.dir}">
                  <include name="META-INF/ejb-jar.xml" />
                  <include name="META-INF/jboss.xml" />
              </fileset>
          </jar>
      </target>
      <!-- Tutorial web app war -->
      <target name="war" depends="compile">
          <delete dir="${build.dir}/WEB-INF"/>
          <mkdir dir="${build.dir}/WEB-INF/classes/org/jboss/docs/interest"/>
          <copy file="${src.dir}/web.xml" todir="${build.dir}/WEB-INF" />
          <copy file="${src.dir}/jboss-web.xml" todir="${build.dir}/WEB-INF" />
          <copy file="${src.dir}/home.html" todir="${build.dir}" />
          <copy 
file="${build.dir}/classes/org/jboss/docs/interest/InterestServlet.class" 
todir="${build.dir}/WEB-INF/classes/org/jboss/docs/interest" />
          <jar jarfile="${build.dir}/interest.war">
              <fileset dir="${build.dir}">
                  <include name="WEB-INF/**"/>
                  <include name="home.html"/>
              </fileset>
          </jar>
      </target>
      <!-- Create the tutorial ear that uses the properties based security info -->
      <target name="ear" depends="ejb-jar,war">
          <copy file="${src.dir}/application.xml" todir="${build.dir}/META-INF" />
          <jar jarfile="${build.dir}/interest.ear">
              <fileset dir="${build.dir}">
                  <include name="META-INF/application.xml" />
                  <include name="interest.jar" />
                  <include name="interest.war" />
              </fileset>
          </jar>
      </target>
  
      <target name="deploy-ejb-jar" depends="ejb-jar">
          <copy file="${build.dir}/interest.jar" todir="${jboss.dist}/deploy" />
      </target>
      <target name="deploy-ear" depends="ear">
          <copy file="${build.dir}/interest.ear" todir="${jboss.dist}/deploy" />
      </target>
  
      <target name="interest-client" depends="compile">
          <java classname="org.jboss.docs.interest.InterestClient" fork="yes">
              <classpath>
                  <path refid="${classpath_id}"/>
                  <pathelement location="${build.classes.dir}"/>
              </classpath>
          </java>
      </target>
  </project>
  
  
  
  1.1                  manual/src/examples/org/jboss/docs/interest/ejb-jar.xml
  
  Index: ejb-jar.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  
  <ejb-jar>
       <description>JBoss Interest Sample Application</description>
       <display-name>Interest EJB</display-name>
       <enterprise-beans>
         <session>
           <ejb-name>Interest</ejb-name>
           <home>org.jboss.docs.interest.InterestHome</home>
           <remote>org.jboss.docs.interest.Interest</remote>
           <ejb-class>org.jboss.docs.interest.InterestBean</ejb-class>
           <session-type>Stateless</session-type>
           <transaction-type>Bean</transaction-type>
         </session>
       </enterprise-beans>
  </ejb-jar>
  
  
  
  1.1                  manual/src/examples/org/jboss/docs/interest/home.html
  
  Index: home.html
  ===================================================================
  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <html>
     <head>
        <title>
           Interest EJB Form
        </title>
     </head>
     <body>
        <h1>Interest EJB Form</h1>
        <form action="/interest/InterestServlet" method="POST" >
           <table cellspacing="2" cellpadding="2" border="0">
              <tr>
                 <td>
                    Principal:
                 </td>
                 <td>
                    <input type="text" name="principle" value="1000">
                 </td>
              </tr>
              <tr>
                 <td>
                    Rate(%):
                 </td>
                 <td>
                    <input type="text" name="rate" value="10">
                 </td>
              </tr>
              <tr>
                 <td>
                    Periods:
                 </td>
                 <td>
                    <input type="text" name="periods" value="2">
                 </td>
              </tr>
              <tr>
                 <td>
                    <input type="submit" name="Calculate" value="Calculate">
                 </td>
                 <td>
                    <input type="Reset">
                 </td>
              </tr>
           </table>
        </form>
     </body>
  </html>
  
  
  
  
  1.1                  manual/src/examples/org/jboss/docs/interest/jboss-web.xml
  
  Index: jboss-web.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  
  <jboss-web>
      <ejb-ref>
          <ejb-ref-name>ejb/Interest</ejb-ref-name>
          <jndi-name>interest/Interest</jndi-name>
      </ejb-ref>
  </jboss-web>
  
  
  
  1.1                  manual/src/examples/org/jboss/docs/interest/jboss.xml
  
  Index: jboss.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  <jboss>
    <enterprise-beans>
      <session>
        <ejb-name>Interest</ejb-name>
        <jndi-name>interest/Interest</jndi-name>
      </session>
    </enterprise-beans>
  </jboss>
  
  
  
  1.1                  manual/src/examples/org/jboss/docs/interest/web.xml
  
  Index: web.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  
  <web-app>
  <!-- ### Servlets -->
      <servlet>
          <servlet-name>InterestServlet</servlet-name>
          <servlet-class>org.jboss.docs.interest.InterestServlet</servlet-class>
      </servlet>
      <servlet-mapping>
          <servlet-name>InterestServlet</servlet-name>
          <url-pattern>/InterestServlet</url-pattern>
      </servlet-mapping>
  
      <welcome-file-list>
        <welcome-file>home.html</welcome-file>
      </welcome-file-list>
  
  <!-- ### EJB References (java:comp/env/ejb) -->
      <ejb-ref>
          <ejb-ref-name>ejb/Interest</ejb-ref-name>
          <ejb-ref-type>Session</ejb-ref-type>
          <home>org.jboss.docs.interest.InterestHome</home>
          <remote>org.jboss.docs.interest.Interest</remote>
      </ejb-ref>
  
  </web-app>
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to