Package org.ow2.easybeans.examples.statefulbean

Source Code of org.ow2.easybeans.examples.statefulbean.ClientStateful

/**
* EasyBeans
* Copyright (C) 2006 Bull S.A.S.
* Contact: easybeans@ow2.org
*
* This library 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 any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
* USA
*
* --------------------------------------------------------------------------
* $Id: ClientStateful.java 5369 2010-02-24 14:58:19Z benoitf $
* --------------------------------------------------------------------------
*/

package org.ow2.easybeans.examples.statefulbean;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.transaction.UserTransaction;

/**
* Simple client of the stateless.
* @author Florent Benoit
*/
public final class ClientStateful {

    /**
     * First amount to buy.
     */
    private static final int FIRST_BUY_AMOUNT = 10;

    /**
     * Second amount to buy.
     */
    private static final int SECOND_BUY_AMOUNT = 20;

    /**
     * Third amount to buy (will be rollback).
     */
    private static final int THIRD_BUY_AMOUNT = 50;

    /**
     * Default InitialContextFactory to use.
     */
    private static final String DEFAULT_INITIAL_CONTEXT_FACTORY = "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory";

    /**
     * Client class, no public constructor.
     */
    private ClientStateful() {

    }

    /**
     * Main method.
     * @param args the arguments (not required)
     */
    public static void main(final String[] args) {

        Context initialContext = null;

        try {
            initialContext = getInitialContext();
        } catch (NamingException e) {
            System.err.println("Cannot get InitialContext: " + e);
            System.exit(2);
        }
        StatefulRemote statefulBean = null;
        try {
            statefulBean = (StatefulRemote) initialContext
                    .lookup("org.ow2.easybeans.examples.statefulbean.StatefulBean" + "_"
                            + StatefulRemote.class.getName() + "@Remote");
        } catch (NamingException e) {
            System.err.println("Cannot get statefulBean: " + e);
            System.exit(2);
        }

        // We want to start transactions from client: get UserTransaction
        UserTransaction utx = null;
        try {
            utx = (UserTransaction) initialContext.lookup("javax.transaction.UserTransaction");
        } catch (NamingException e) {
            System.err.println("Cannot lookup UserTransaction: " + e);
            System.exit(2);
        }

        // First transaction (committed)
        try {
            System.out.println("Start a first transaction");
            utx.begin();
            System.out.println("First request on the new bean");
            statefulBean.buy(FIRST_BUY_AMOUNT);
            System.out.println("Second request on the bean");
            statefulBean.buy(SECOND_BUY_AMOUNT);
            System.out.println("Commit the transaction");
            utx.commit();
        } catch (Exception e) {
            System.err.println("exception during 1st Tx: " + e);
            System.exit(2);
        }

        // Start another transaction (rolled back)
        try {
            System.out.println("Start a second transaction");
            utx.begin();
            System.out.println("Buy " + THIRD_BUY_AMOUNT + " amount.");
            statefulBean.buy(THIRD_BUY_AMOUNT);
            System.out.println("Rollback the transaction");
            utx.rollback();
        } catch (Exception e) {
            System.err.println("exception during 2nd Tx: " + e);
            System.exit(2);
        }

        System.out.println("after rollback, value = " + statefulBean.read());

        // Get the total bought, outside the transaction
        int val = 0;
        try {
            System.out.println("Request outside any transaction");
            val = statefulBean.read();
        } catch (Exception e) {
            System.err.println("Cannot read value on t1 : " + e);
            System.exit(2);
        }

        System.out.println("Check that value = " + (FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT));
        if (val != FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT) {
            System.err.println("Bad value read: " + val);
            System.exit(2);
        }

        System.out.println("ClientStateful OK. Exiting.");
    }

    /**
     * @return Returns the InitialContext.
     * @throws NamingException If the Context cannot be created.
     */
    private static Context getInitialContext() throws NamingException {

        // if user don't use jclient/client container
        // we can specify the InitialContextFactory to use
        // But this is *not recommended*.
        Hashtable<String, Object> env = new Hashtable<String, Object>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, getInitialContextFactory());

        // Usually a simple new InitialContext() without any parameters is sufficent.
        // return new InitialContext();

        return new InitialContext(env);
    }

    /**
     * Returns a configurable InitialContextFactory classname.<br/>
     * Can be configured with the <code>easybeans.client.initial-context-factory</code> System property.
     * @return Returns a configurable InitialContextFactory classname.
     */
    private static String getInitialContextFactory() {
        String prop = System.getProperty("easybeans.client.initial-context-factory");
        // If not found, use the default
        if (prop == null) {
            prop = DEFAULT_INITIAL_CONTEXT_FACTORY;
        }
        return prop;
    }

}
TOP

Related Classes of org.ow2.easybeans.examples.statefulbean.ClientStateful

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.