Package org.ow2.easybeans.mavenplugin.examples.client

Source Code of org.ow2.easybeans.mavenplugin.examples.client.Client

/**
* EasyBeans
* Copyright (C) 2007 Bull S.A.S.
* Contact: easybeans@objectweb.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 (at your option) 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
* USA
*
* --------------------------------------------------------------------------
* $Id: Client.java 5369 2010-02-24 14:58:19Z benoitf $
* --------------------------------------------------------------------------
*/

package org.ow2.easybeans.mavenplugin.examples.client;

import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.ow2.easybeans.mavenplugin.examples.helloworld.HelloInterface;
import org.ow2.easybeans.mavenplugin.examples.calculator.StatefulCalcRemote;




/**
* Test client for unbound project.
* @author Julien Blais
*/
public final class Client {

    /**
    * JNDI name of the calculator bean.
    */
    private static final String CALC_JNDI_NAME = "org.ow2.easybeans.mavenplugin.examples.calculator.StatefulCalcBean"
                                               + "_" + StatefulCalcRemote.class.getName() + "@Remote";


   /**
    * JNDI name of the calculator bean.
    */
    private static final String HELLO_JNDI_NAME = "org.ow2.easybeans.mavenplugin.examples.helloworld.HelloBean"
                                                + "_" + HelloInterface.class.getName() + "@Remote";
  


    /**
     * Calculator initial for tests.
     */
    private static final int INITIAL_CALC_VALUE = 12;

    /**
     * First add'value for tests.
     */
    private static final int VALUE_TEST_ADD = 28;



    /**
     * Utility class : no public constructor.
     */
    private Client() {
    }


    /**
     * Main method.
     * @param args the arguments (not required)
     * @throws Exception if exception is found.
     */
    public static void main(final String[] args) throws Exception {

        try {
            System.out.println("Testing unbound project with Maven EasyBeans Plugin...");
            Context initialContext = getInitialContext();
            StatefulCalcRemote calc = getCalcEJB(initialContext);
            HelloInterface hr = getHelloEJB(initialContext);

            System.out.println("Calling Calculator methods : x = " + INITIAL_CALC_VALUE + " + " + VALUE_TEST_ADD);
            calc.initial(INITIAL_CALC_VALUE);
            calc.add(VALUE_TEST_ADD);
            Integer x = calc.result();

            System.out.println("Calling HelloWorld method : sayHello(x)");
            String result = hr.sayHello(x.toString());

            System.out.println("Result : " + result);

        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(-1);
        }
    }




    /**
     * Get Initial Context.
     * @return smartFactory initialised context
     * @throws NamingException naming exception
     */
    private static Context getInitialContext() throws NamingException {

        Hashtable<String, Object> env = new Hashtable<String, Object>();
        System.setProperty("java.rmi.server.useCodebaseOnly", "true");
        env.put(Context.INITIAL_CONTEXT_FACTORY, "org.ow2.easybeans.component.smartclient.spi.SmartContextFactory");
        env.put(Context.PROVIDER_URL, "smart://localhost:2503");
        return new InitialContext(env);
    }


    /**
     * Get the StatefulCalcRemote EJB interface.
     * @param context Initial Context
     * @return Interface of EJB
     */
    private static StatefulCalcRemote getCalcEJB(final Context context) {
        StatefulCalcRemote c = null;

        try {
            c = (StatefulCalcRemote) context.lookup(CALC_JNDI_NAME);
        } catch (NamingException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, "Unable to found StatefulCalcRemote EJB.", ex);
        }
        return c;
    }


        /**
     * Get the StatefulCalcRemote EJB interface.
     * @param context Initial Context
     * @return Interface of EJB
     */
    private static HelloInterface getHelloEJB(final Context context) {
        HelloInterface h = null;

        try {
            h = (HelloInterface) context.lookup(HELLO_JNDI_NAME);
        } catch (NamingException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, "Unable to found HelloInterface EJB.", ex);
        }
        return h;
    }
}
TOP

Related Classes of org.ow2.easybeans.mavenplugin.examples.client.Client

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.