7 Replies Latest reply on Sep 20, 2004 7:26 AM by sanjuthomas

    Accessing business methods from EJB

    sanjuthomas

      hi all,

      this is very simple problem, how can i access the business methods in a SessionBean if i dont have permission to create object, i mean i dont have the right to access the create() method, on the very same time. i have right to access other methods. in this condition with out creating a ejbRemote object how can i access these methods. i think i can access already created ejb instance from the server. that is pooled sessionbean instance i can use to access business methods. how can i query for those objects from the JBoss server. if some one knows the solution please help me. thanx in advance.

      sanju.

        • 1. Re: Accessing business methods from EJB
          jamesstrachan

          Sanju,

          You need to get the remote interface to execute any business methods.

          So, if you don't have permission to execute the ejbCreate() method, you can't get the remote interface and you can't execute a business method.

          Don't forget that stateless and stateful session beans behave in a different way.

          On a stateless bean, ejbCreate() doesn't create a Session Bean but gives you a reference to a pool of beans that the server created earlier. Each method call will be allocated by the server to any instance within the pool.

          On a stateful bean, ejbCreate() does give you a dedicated Session Bean that is dedicated to your client.

          James

          • 2. Re: Accessing business methods from EJB
            sanjuthomas

            hi James,
            thank you, but still i have a doubt, if we cant access business methods with out remote object, then what for this security role and method permission. see this is the scenario....

            <assembly-descriptor>
            <security-role>
            <role-name>programmer</role-name>
            </security-role>
            <security-role>
            <role-name>designer</role-name>
            </security-role>
            <security-role>
            <role-name>system</role-name>
            </security-role>

            <method-permission>
            <role-name>system</role-name>

            <ejb-name>HelloAssyst</ejb-name>
            <method-name>*</method-name>

            </method-permission>

            <method-permission>
            <role-name>designer</role-name>

            <ejb-name>HelloAssyst</ejb-name>
            <method-name>sayHello</method-name>



            <ejb-name>HelloAssyst</ejb-name>
            <method-name>remove</method-name>

            </method-permission>

            <method-permission>
            <role-name>programmer</role-name>

            <ejb-name>HelloAssyst</ejb-name>
            <method-name>sayHello</method-name>

            </method-permission>
            </assembly-descriptor>

            here only role system have the permission to access the create method, the designer and programmer cant access the create method, if it is so then how can people with these role make use of this SessionBean. if they cant access then what for this security is like this... please give me a detailed replay..
            thanx in advance.

            with regards,
            sanju

            • 3. Re: Accessing business methods from EJB
              jamesstrachan

              Sanju,

              If the deployment script is for a Session Bean, then it will prevent anybody who does not have a role of "system" from accessing the bean.

              If "HelloAssyt" was an entity bean representing persistent data (which seems unlikely from the name of the EJB and the method name), it would allow access as below :-

              Role create sayHello remove

              system X X X
              designer X X
              programmer X

              which would make some sort of sense. For an entity bean, the ejbCreate() operation actually constructs a new object, so it can make sense to restrict access.

              Without seeing the code for the EJB, it's difficult to say more. I suggest that you expirement with changing the descriptor.

              James


              • 4. Re: Accessing business methods from EJB
                sanjuthomas

                hi James,
                Thank you for the replay, it is a session bean. here i wanted to give create permission only for the system, and on the same time programmer has to access the sayHello method, and the designer has to access the sayHello and remove too. now you got my problem. right ? I may be wrong in my concept about security. i am very much new in EJB. please help me.

                with regards
                sanju

                • 5. Re: Accessing business methods from EJB
                  jamesstrachan

                  Sanju,

                  I think that I see where you are having a problem.

                  The create() method on an entity bean does real work - it creates an object and persists it.

                  The create() method on a session bean creates the session bean (which may initialse variables etc.) but doesn't create the underlying object.

                  Here are two sample pieces of code :-

                  Session Bean SalesMaintenanceBean is a session bean that allows a user to maintain sales products stored in Entity Beans. It needs a createNew() method to actually create a new Sales Product :-

                  //Package: Demonstration
                  //Class: SalesProductMaintenanceBean
                  //Version: 0.b
                  //Copyright: Copyright (c) 2002
                  //Author: James Strachan
                  //Company: Strachan Software Components
                  //Description: Use Case Session Bean for maintenance of Sales Products.
                  //Amendment History:-
                  //Version Date Author Modification
                  //0.a 17/06/2002 JAS Conception
                  //0.b 01/01/2003 JAS Convert to Log4J Error Reporting.
                  
                  package co.uk.miltonsoftware.demonstration.maintenance;
                  
                  import java.util.Collection;
                  import java.util.ArrayList;
                  import java.util.Iterator;
                  import javax.naming.Context;
                  import javax.rmi.PortableRemoteObject;
                  import javax.naming.NamingException;
                  import javax.naming.InitialContext;
                  import java.rmi.RemoteException;
                  import javax.ejb.*;
                  
                  import org.apache.log4j.Logger;
                  import co.uk.miltonsoftware.utilities.general.InvalidDataException;
                  
                  import co.uk.miltonsoftware.demonstration.common.DemoEJBNameFactory;
                  
                  /**
                   * A Use Case Session Bean used to mantain Sales Products.
                   * @author J.A.Strachan
                   * @version 0.b
                   */
                  public class SalesProductMaintenanceBean implements SessionBean {
                  
                   private static final String ENTITY_ADDRESS = DemoEJBNameFactory.buildName(DemoEJBNameFactory.SALES_PRODUCT);
                  
                   /**
                   * Context for the Session Bean.
                   */
                   private SessionContext context;
                  
                   /**
                   * Log4J Logger for error logging.
                   */
                   private static Logger logger;
                  
                  
                   /**
                   * Empty Constructor.
                   */
                   public SalesProductMaintenanceBean() {
                  
                   if ( logger == null ) {
                   logger = Logger.getLogger( this.getClass() );
                   }
                  
                   }
                  
                   /**
                   * Create a new Sales Product.
                   * @param newSalesProduct a SalesProductLightweight object.
                   * @throws NamingException
                   * @throws RemoteException
                   * @throws CreateException
                   */
                   public void createNew( SalesProductLightweight newSalesProduct )
                   throws NamingException, RemoteException, CreateException {
                  
                   SalesProductRemote temp = getHome().create( newSalesProduct );
                  
                   }
                  
                   /**
                   * Check for an existing Sales Product with the same Product Code.
                   * @param productCode
                   * @throws RemoteException
                   * @throws NamingException
                   * @throws InvalidDataException
                   */
                   public void checkDuplicate( String productCode ) throws RemoteException,
                   NamingException, InvalidDataException {
                  
                   try {
                   getHome().findByPrimaryKey( productCode );
                   throw new InvalidDataException( "Sales Product with code "
                   + productCode + " already exists." );
                   }
                   catch ( ObjectNotFoundException onfe ) {}
                   catch ( FinderException fe ) {}
                  
                   }
                  
                   /**
                   * Remove a Sales Product no longer required.
                   * @param productCode
                   * @throws RemoteException
                   * @throws RemoveException
                   * @throws NamingException
                   * @throws FinderException
                   * @throws ObjectNotFoundException
                   */
                   public void remove( String productCode ) throws RemoteException,
                   NamingException, RemoveException, FinderException, ObjectNotFoundException {
                  
                   SalesProductRemote temp = getHome().findByPrimaryKey( productCode );
                   temp.remove();
                  
                   }
                  
                   /**
                   * Get details of a Sales Product wrapped in a <code>SalesProductLightweight</code> Value Object.
                   * @param productCode
                   * @return SalesProductLightweight
                   * @throws RemoteException
                   * @throws NamingException
                   * @throws ObjectNotFoundException
                   * @throws FinderException
                   */
                   public SalesProductLightweight getSalesProduct( String productCode )
                   throws RemoteException, NamingException, ObjectNotFoundException,
                   FinderException {
                  
                   logger.info( "getSalesProduct : " + productCode );
                  
                   SalesProductRemote temp = getRemote( productCode );
                  
                   return temp.getSalesProduct();
                  
                   }
                  
                   /**
                   * Set the attributes of a Sales Product bean.
                   * @param salesProduct - a SalesProductLightweight Value Object.
                   * @throws RemoteException
                   * @throws NamingException
                   * @throws ObjectNotFoundException
                   * @throws FinderException
                   */
                   public void setSalesProduct( SalesProductLightweight salesProduct )
                   throws RemoteException, NamingException, ObjectNotFoundException,
                   FinderException {
                  
                   String tempKey = salesProduct.getProductCode();
                  
                   logger.info( "setSalesProduct : " + tempKey );
                  
                   SalesProductRemote temp = getRemote( tempKey );
                   temp.setSalesProduct( salesProduct );
                  
                   }
                  
                   /**
                   * Get the home interface of the Entity Bean.
                   * @return SalesProductHome
                   * @throws RemoteException
                   * @throws NamingException
                   */
                   private SalesProductHome getHome() throws RemoteException,
                   NamingException {
                  
                   SalesProductHome home;
                   Context ctx = new InitialContext();
                  
                   home = (SalesProductHome)
                   PortableRemoteObject.narrow( ctx.lookup( ENTITY_ADDRESS ),
                   SalesProductHome.class ) ;
                  
                   return home;
                  
                   }
                  
                   /**
                   * Get the remote interface corresponding to the requested Sales Product.
                   * @param productCode
                   * @return SalesProductRemote
                   * @throws RemoteException
                   * @throws NamingException
                   * @throws ObjectNotFoundException
                   * @throws FinderException
                   */
                   private SalesProductRemote getRemote( String productCode ) throws RemoteException,
                   NamingException, ObjectNotFoundException, FinderException {
                  
                   return getHome().findByPrimaryKey( productCode );
                  
                   }
                  
                   // Session Bean utility methods below.
                  
                   /**
                   * Called by the Container when an instance is required.
                   * @throws RemoteException
                   * @throws CreateException
                   */
                   public void ejbCreate() throws RemoteException, CreateException {
                   }
                  
                   public void ejbActivate() throws RemoteException {
                   }
                  
                   public void ejbPassivate() throws RemoteException {
                   }
                  
                   public void ejbRemove() throws RemoteException {
                   }
                  
                   public void setSessionContext(SessionContext ctx) throws RemoteException {
                   this.context = ctx;
                   }
                  
                  }
                  


                  The associated Entity Bean does use the create() method(translated to ejbCreate() ) to create a Sales Product.

                  //Package: Demonstration
                  //Class: SalesProductBean
                  //Version: 0.b
                  //Copyright: Copyright (c) 2002
                  //Author: James Strachan
                  //Company: Strachan Software Components
                  //Description: The EJB Component holding an instance of a Sales Product.
                  //Amendment History:-
                  //Version Date Author Modification
                  //0.a 17/06/2002 JAS Conception
                  //0.b 01/01/2003 JAS Convert to Log4J Error Reporting.
                  
                  package co.uk.miltonsoftware.demonstration.maintenance;
                  
                  // Classes required to return summary list of parameters.
                  import java.util.Collection;
                  import java.util.ArrayList;
                  
                  // Database imports
                  import java.sql.Date;
                  import java.sql.Connection;
                  import java.sql.SQLException;
                  import java.sql.PreparedStatement;
                  import java.sql.ResultSet;
                  import javax.sql.*;
                  
                  // EJB imports.
                  import java.rmi.RemoteException;
                  import javax.ejb.RemoveException;
                  import javax.ejb.EJBException;
                  import javax.ejb.CreateException;
                  import javax.ejb.EntityBean;
                  import javax.ejb.EntityContext;
                  import javax.naming.InitialContext;
                  import javax.naming.NamingException;
                  import javax.ejb.ObjectNotFoundException;
                  import javax.ejb.FinderException;
                  import javax.rmi.PortableRemoteObject;
                  
                  
                  import java.rmi.RemoteException;
                  import javax.naming.Context;
                  import java.io.*;
                  
                  // Money classes.
                  import co.uk.miltonsoftware.services.money.MoneyControlBean;
                  import co.uk.miltonsoftware.services.money.MoneyControlHome;
                  import co.uk.miltonsoftware.services.money.MoneyControlRemote;
                  import co.uk.miltonsoftware.services.money.ImmutableMoney;
                  
                  // Error Log Helper.
                  import org.apache.log4j.Logger;
                  
                  import co.uk.miltonsoftware.utilities.general.InvalidDataException;
                  
                  import co.uk.miltonsoftware.demonstration.common.DemoEJBNameFactory;
                  import co.uk.miltonsoftware.services.common.EJBNameFactory;
                  
                  /**
                   * This class defines an Entity Bean used to implement a Sales Product.
                   * <p>
                   * The class has specialised finder methods and therefore uses Bean Managed Persistence.
                   * @author J.A.Strachan
                   * @version 0.b
                   */
                  public class SalesProductBean implements EntityBean {
                  
                   private static final String DATASTORE_ADDRESS = DemoEJBNameFactory.DEMO_DATASTORE_ADDRESS;
                  
                   private EntityContext ctx;
                  
                   /**
                   * The identifier for a Sales Product.
                   * Must be public to be available for use by the EJB container.
                   */
                   public String productCode;
                  
                   /**
                   * Data representing a Sales Product in a lightweight, behaviourless bean.
                   */
                   private SalesProductLightweight salesProduct;
                  
                   /**
                   * Log4J Logger for error logging.
                   */
                   private static Logger logger;
                  
                   /**
                   * Parameterless constructor as required by EJB.
                   */
                   public SalesProductBean() {
                  
                   if ( logger == null ) {
                   logger = Logger.getLogger( this.getClass() );
                   }
                  
                   }
                  
                  
                   /**
                   * Get the identifier for a Product Code.
                   * @return String
                   */
                   public String getProductCode() {
                   return productCode;
                   }
                  
                   /**
                   * Get the Value Object for a Product Code.
                   * @return SalesProductLightweight
                   */
                   public SalesProductLightweight getSalesProduct() {
                   return salesProduct;
                   }
                  
                   /**
                   * Set the identifier for a sequence.
                   * @param salesProduct
                   */
                   public void setSalesProduct(SalesProductLightweight salesProduct) {
                  
                   this.salesProduct = salesProduct;
                   productCode = salesProduct.getProductCode();
                  
                   }
                  
                   /**
                   * Standard create method.
                   * @param newSalesProduct
                   * @return String sequenceId - primary key of created object.
                   * @throws RemoteException
                   * @throws CreateException
                   */
                   public String ejbCreate( SalesProductLightweight newSalesProduct )
                   throws RemoteException, CreateException {
                  
                   // Set the primary key and the contained data.
                   this.productCode = newSalesProduct.getProductCode();
                   this.salesProduct = newSalesProduct;
                  
                   logger.info( "Create called : " + productCode );
                  
                   Connection conn = null;
                   PreparedStatement statement = null;
                   InitialContext context = null;
                   DataSource dataSource;
                  
                   String sqlStatement = "INSERT into salesProducts ( productCode, " +
                   "productCategory, title, author, leadTime, currencyCode, netPrice, vatCode ) " +
                   "values ( ?, ?, ?, ?, ?, ?, ?, ? )";
                  
                   try {
                  
                   // Get a database connection.
                   context = new InitialContext();
                   dataSource = (DataSource) context.lookup( DATASTORE_ADDRESS );
                   conn = dataSource.getConnection();
                  
                   // Prepare a statement and set parameters.
                   statement = conn.prepareStatement( sqlStatement );
                   statement.setString( 1, productCode );
                   statement.setString( 2, salesProduct.getProductCategory() );
                   statement.setString( 3, salesProduct.getTitle() );
                   statement.setString( 4, salesProduct.getAuthor() );
                   statement.setInt( 5, salesProduct.getLeadTime() );
                   statement.setString( 6, salesProduct.getNetPrice().getCurrencyCode() );
                   statement.setBigDecimal( 7, salesProduct.getNetPrice().getValue() );
                   statement.setString( 8, salesProduct.getVatCode() );
                  
                   // Execute the statement.
                   statement.execute();
                   }
                   // Error in lookup ?
                   catch( NamingException ne ) {
                   logger.error( ne.getMessage(), ne );
                   throw new CreateException( ne.getMessage() );
                   }
                   // Error in SQL statement ?
                   catch( SQLException sqe ) {
                   try {
                   // Check if error is caused by duplicate primary key.
                   ejbFindByPrimaryKey( productCode );
                   throw new CreateException( "Sales Product " + productCode + "already exists." );
                   }
                   catch ( Exception ignore ) {};
                   logger.error( sqe.getMessage(), sqe );
                   throw new CreateException( sqe.getMessage() );
                   }
                   finally {
                   try {
                   if ( statement != null ) {
                   statement.close();
                   }
                   if ( conn != null ) {
                   conn.close();
                   }
                   }
                   catch ( SQLException e1 ) {}
                   }
                  
                   return productCode;
                  
                   }
                  
                   /**
                   * No work required here - but skeleton is required by EJB.
                   * @param newSalesProduct
                   */
                   public void ejbPostCreate( SalesProductLightweight newSalesProduct ) {}
                  
                   /**
                   * This standard EJB method locates a Sales Product using the Primary Key.
                   * @param productCode - the Key of the requested Sales Product.
                   * @return String productCode - the Product Code if the Sales Product was located.
                   * @throws RemoteException
                   * @throws ObjectNotFoundException
                   * @throws FinderException
                   */
                   public String ejbFindByPrimaryKey( String productCode ) throws RemoteException,
                   ObjectNotFoundException, FinderException {
                  
                   logger.info( "Find by primary key >" + productCode + "<" );
                  
                   Connection conn = null;
                   PreparedStatement statement = null;
                   InitialContext context = null;
                   DataSource dataSource;
                   ResultSet rs;
                  
                   String sqlStatement = "SELECT productCode from SalesProducts " +
                   "where productCode = ?";
                  
                   try {
                  
                   // Get a database connection.
                   context = new InitialContext();
                   dataSource = (DataSource) context.lookup( DATASTORE_ADDRESS );
                   conn = dataSource.getConnection();
                  
                   // Prepare and execute SQL statement.
                   statement = conn.prepareStatement( sqlStatement );
                   statement.setString( 1, productCode );
                   statement.execute();
                  
                   // Check the result set. If there is one row, we have successfully located
                   // the Parameter.
                   rs = statement.getResultSet();
                   if ( rs.next() ) {
                   return productCode;
                   }
                   else {
                   throw new ObjectNotFoundException( "Sales Product " + productCode + "not found." );
                   }
                   }
                   // Lookup Service failed.
                   catch( NamingException ne ) {
                   logger.error( ne.getMessage(), ne );
                   throw new ObjectNotFoundException( ne.getMessage() );
                   }
                   // SQL failed somewhere.
                   catch( SQLException sqe ) {
                   logger.error( sqe.getMessage(), sqe );
                   throw new ObjectNotFoundException( sqe.getMessage() );
                   }
                   finally {
                   try {
                   if ( statement != null ) {
                   statement.close();
                   }
                   if ( conn != null ) {
                   conn.close();
                   }
                   }
                   catch ( SQLException e1 ) {}
                   }
                  
                   }
                  
                   /**
                   * This standard EJB method loads a Sales Product. To fit EJB standards, the
                   * primary key - productCode - is read from the Context.
                   * <p>
                   * @throws ObjectNotFoundException
                   * @throws RemoteException
                   */
                   public void ejbLoad() throws RemoteException {
                  
                   Connection conn = null;
                   PreparedStatement statement = null;
                   InitialContext context = null;
                   DataSource dataSource;
                   ResultSet rs;
                  
                   MoneyControlRemote moneyControl = getMoneyRemote();
                  
                   String sqlStatement = "SELECT productCategory, title, author, " +
                   "leadTime, currencyCode, netPrice, vatCode from salesProducts " +
                   "where productCode = ?";
                  
                   productCode = (String) ctx.getPrimaryKey();
                  
                   logger.info( "Load called : " + productCode );
                  
                   try {
                  
                   // Get a database connection.
                   context = new InitialContext();
                   dataSource = (DataSource) context.lookup( DATASTORE_ADDRESS );
                   conn = dataSource.getConnection();
                  
                   // Prepare and execute the statement.
                   statement = conn.prepareStatement( sqlStatement );
                   statement.setString( 1, productCode );
                   statement.execute();
                  
                   // Get the result set - if there is a row, create an instance of a Parameter.
                   rs = statement.getResultSet();
                   if ( rs.next() ) {
                   salesProduct = new SalesProductLightweight();
                   salesProduct.setProductCode( productCode );
                   salesProduct.setProductCategory( rs.getString(1).trim() );
                   salesProduct.setTitle( rs.getString(2).trim() );
                   salesProduct.setAuthor( rs.getString(3).trim() );
                   salesProduct.setLeadTime( rs.getInt(4) );
                   ImmutableMoney temp = moneyControl.createImmutableMoney( rs.getString(5).trim(),
                   rs.getBigDecimal(6) );
                   salesProduct.setNetPrice( temp );
                   salesProduct.setVatCode( rs.getString(7).trim() );
                   }
                   else {
                   throw new RemoteException( "Sales Product " + productCode + "not found." );
                   }
                   }
                   // Lookup failed ?
                   catch( InvalidDataException ive ) {
                   logger.error( ive.getMessage(), ive );
                   throw new RemoteException( ive.getMessage() );
                   }
                   // Lookup failed ?
                   catch( NamingException ne ) {
                   logger.error( ne.getMessage(), ne );
                   throw new RemoteException( ne.getMessage() );
                   }
                   // Something nasty in the woodshed ?
                   catch( SQLException sqe ) {
                   logger.error( sqe.getMessage(), sqe );
                   throw new RemoteException( sqe.getMessage() );
                   }
                   finally {
                   try {
                   if ( statement != null ) {
                   statement.close();
                   }
                   if ( conn != null ) {
                   conn.close();
                   }
                   }
                   catch ( SQLException e1 ) {}
                   }
                  
                   }
                  
                   /**
                   * This standard EJB method physically deletes a Sales Product from the database.
                   * The key of the Sales Product is obtained from the context.
                   * @throws RemoteException
                   * @throws RemoveException
                   */
                   public void ejbRemove() throws RemoteException, RemoveException {
                  
                   Connection conn = null;
                   PreparedStatement statement = null;
                   InitialContext context = null;
                   DataSource dataSource;
                  
                   String sqlStatement = "DELETE from SalesProducts where productCode = ?";
                   int rowCount;
                  
                   productCode = (String) ctx.getPrimaryKey();
                  
                   logger.info( "Remove called : " + productCode );
                  
                   try {
                  
                   context = new InitialContext();
                   dataSource = (DataSource) context.lookup( DATASTORE_ADDRESS );
                   conn = dataSource.getConnection();
                  
                   statement = conn.prepareStatement( sqlStatement );
                   statement.setString( 1, productCode );
                   rowCount = statement.executeUpdate();
                   if ( rowCount != 1 ) {
                   throw new RemoveException( "Sales product " + productCode + " already deleted." );
                   }
                   }
                   catch( NamingException ne ) {
                   logger.error( ne.getMessage(), ne );
                   throw new RemoveException( ne.getMessage() );
                   }
                   catch( SQLException sqe ) {
                   logger.error( sqe.getMessage(), sqe );
                   throw new RemoveException( sqe.getMessage() );
                   }
                   finally {
                   try {
                   if ( statement != null ) {
                   statement.close();
                   }
                   if ( conn != null ) {
                   conn.close();
                   }
                   }
                   catch ( SQLException e1 ) {}
                   }
                  
                   }
                  
                   /**
                   * Standard EJB method to store a Sales Product in persistent data.
                   * @throws RemoteException
                   */
                   public void ejbStore() throws RemoteException {
                  
                   Connection conn = null;
                   PreparedStatement statement = null;
                   InitialContext context = null;
                   DataSource dataSource;
                  
                   String sqlStatement = "UPDATE SalesProducts set ProductCategory = ?, " +
                   "author = ?, title = ?, leadTime = ?, currencyCode = ?, " +
                   "netPrice = ?, vatCode = ? " +
                   "WHERE productCode = ?";
                  
                   int rowCount;
                  
                   logger.info( "Store : " + productCode );
                  
                   try {
                  
                   // Get a database connection.
                   context = new InitialContext();
                   dataSource = (DataSource) context.lookup( DATASTORE_ADDRESS );
                   conn = dataSource.getConnection();
                  
                   // Prepare statement and parameters.
                   statement = conn.prepareStatement( sqlStatement );
                   statement.setString( 1, salesProduct.getProductCategory() );
                   statement.setString( 2, salesProduct.getTitle() );
                   statement.setString( 3, salesProduct.getAuthor() );
                   statement.setInt( 4, salesProduct.getLeadTime() );
                   statement.setString( 5, salesProduct.getNetPrice().getCurrencyCode() );
                   statement.setBigDecimal( 6, salesProduct.getNetPrice().getValue() );
                   statement.setString( 7, salesProduct.getVatCode() );
                   statement.setString( 8, productCode );
                  
                   // Execute and check results.
                   rowCount = statement.executeUpdate();
                   logger.debug( "rowCount = " + rowCount );
                   if ( rowCount != 1 ) {
                   logger.warn( "Sales Product " + productCode + "not found." );
                   throw new RemoteException( "Sales Product " + productCode + "not found." );
                   }
                   }
                   // Lookup failed.
                   catch( NamingException ne ) {
                   logger.error( ne.getMessage(), ne );
                   throw new RemoteException( ne.getMessage() );
                   }
                   // Database error.
                   catch( SQLException sqe ) {
                   logger.error( sqe.getMessage(), sqe );
                   throw new RemoteException( sqe.getMessage() );
                   }
                   finally {
                   try {
                   if ( statement != null ) {
                   statement.close();
                   }
                   if ( conn != null ) {
                   conn.close();
                   }
                   }
                   catch ( SQLException e1 ) {}
                   }
                  
                   }
                  
                  
                  
                   /**
                   * This method is a customised EJB finder method that returns a list of all
                   * Sales Products stored in persistent data. The data returned is contained
                   * in a Collection of objects which are Primary Keys of the class. These
                   * are then encapsulated as EJBObjects by the container.
                   * <p>
                   * The primary key for this class is a String so we build an ArrayList of
                   * Strings.
                   * @return Collection of String primary keys.
                   * @throws RemoteException
                   * @throws FinderException
                   */
                   public Collection ejbFindAll() throws RemoteException, FinderException {
                  
                   Connection conn = null;
                   PreparedStatement statement = null;
                   InitialContext context = null;
                   DataSource dataSource;
                   ResultSet rs;
                   ArrayList result = new ArrayList();
                  
                   String sqlStatement = "SELECT productCode from SalesProducts " +
                   "order by productCode";
                  
                   logger.info( "FindAll called");
                  
                   try {
                  
                   // Get a connection/
                   context = new InitialContext();
                   dataSource = (DataSource) context.lookup( DATASTORE_ADDRESS );
                   conn = dataSource.getConnection();
                  
                   // Execute the statement.
                   statement = conn.prepareStatement( sqlStatement );
                   statement.execute();
                   rs = statement.getResultSet();
                  
                   // Loop through the SQL result set, building the Collection to be returned.
                   // Trim the strings so that the key definition is consistent.
                   while( rs.next() ) {
                   result.add( rs.getString(1).trim() );
                   }
                   }
                   // Lookup Servicefailed ?
                   catch( NamingException ne ) {
                   logger.error( ne.getMessage(), ne );
                   throw new RemoteException( ne.getMessage() );
                   }
                   // Database service failed ?
                   catch( SQLException sqe ) {
                   logger.error( sqe.getMessage(), sqe );
                   throw new RemoteException( sqe.getMessage() );
                   }
                   finally {
                   try {
                   if ( statement != null ) {
                   statement.close();
                   }
                   if ( conn != null ) {
                   conn.close();
                   }
                   }
                   catch ( SQLException e1 ) {}
                   }
                  
                   return result;
                  
                   }
                  
                  
                   /**
                   * This method is a customised EJB finder method that returns a list of all
                   * Sales Products in a Product Category.
                   * @see comments on ejbFindAll
                   * @param productCategory
                   * @return Collection of String primary keys.
                   * @throws RemoteException
                   * @throws FinderException
                   */
                   public Collection ejbFindByCategory( String productCategory )
                   throws RemoteException, FinderException {
                  
                   Connection conn = null;
                   PreparedStatement statement = null;
                   InitialContext context = null;
                   DataSource dataSource;
                   ResultSet rs;
                   ArrayList result = new ArrayList();
                  
                   String sqlStatement = "SELECT productCode from SalesProducts " +
                   "where productCategory = ? " +
                   "order by productCode";
                  
                   logger.info( "FindByCategory called");
                  
                   try {
                  
                   // Get a connection/
                   context = new InitialContext();
                   dataSource = (DataSource) context.lookup( DATASTORE_ADDRESS );
                   conn = dataSource.getConnection();
                  
                   // Execute the statement.
                   statement = conn.prepareStatement( sqlStatement );
                   statement.setString( 1, productCategory );
                   statement.execute();
                   rs = statement.getResultSet();
                  
                   // Loop through the SQL result set, building the Collection to be returned.
                   // Trim the strings so that the key definition is consistent.
                   while( rs.next() ) {
                   result.add( rs.getString(1).trim() );
                   }
                   }
                   // Lookup Servicefailed ?
                   catch( NamingException ne ) {
                   logger.error( ne.getMessage(), ne );
                   throw new RemoteException( ne.getMessage() );
                   }
                   // Database service failed ?
                   catch( SQLException sqe ) {
                   logger.error( sqe.getMessage(), sqe );
                   throw new RemoteException( sqe.getMessage() );
                   }
                   finally {
                   try {
                   if ( statement != null ) {
                   statement.close();
                   }
                   if ( conn != null ) {
                   conn.close();
                   }
                   }
                   catch ( SQLException e1 ) {}
                   }
                  
                   return result;
                  
                   }
                  
                   /**
                   * Get the remote interface to the MoneyControlBean in the Application Server.
                   * @return MoneyControlRemote
                   * @throws RemoteException
                   */
                   private MoneyControlRemote getMoneyRemote() throws RemoteException {
                  
                   MoneyControlHome home;
                   MoneyControlRemote remote = null;
                  
                  
                   try {
                   Context context = new InitialContext();
                   home = (MoneyControlHome) PortableRemoteObject.narrow(
                   context.lookup(EJBNameFactory.buildName(EJBNameFactory.MONEY_CONTROLS)),
                   MoneyControlHome.class );
                   remote = home.findByPrimaryKey( MoneyControlBean.SINGLETON_NAME );
                   }
                   catch ( Exception e ) {
                   logger.error( "Finding money control", e );
                   throw new RemoteException( "Finding Money Control", e );
                   }
                  
                   return remote;
                  
                   }
                  
                   public void setEntityContext(EntityContext ctx) throws RemoteException {
                  
                   this.ctx = ctx;
                  
                   }
                  
                   public void unsetEntityContext() throws RemoteException {
                  
                   ctx = null;
                  
                   }
                  
                   public void ejbActivate() throws RemoteException {
                   }
                  
                   public void ejbPassivate() throws RemoteException {
                   }
                  
                  }
                  

                  I assume that your user "system" is creating objects for later use by "designer" and "programmer". So it is the equivalent of the createNew() method that will require protection.

                  James

                  • 6. Re: Accessing business methods from EJB
                    sanjuthomas

                    h james,
                    your assumption is right, the system is responsable to create object and the designer and programmer will use in the future. thank you very much for your comments and code. now i am reading the code. i hope i can understand it. if i have doubts in the code then i need your help in the future too.
                    now thank you very much

                    with regards
                    sanju

                    • 7. Re: Accessing business methods from EJB
                      sanjuthomas

                      hi james,
                      thank you very much , i got it, really thanx

                      sanju