001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ClientStateless.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.examples.statelessbean;
025:
026: import java.util.Hashtable;
027:
028: import javax.naming.Context;
029: import javax.naming.InitialContext;
030: import javax.naming.NamingException;
031:
032: /**
033: * Simple client of the stateless.
034: * @author Florent Benoit
035: */
036: public final class ClientStateless {
037:
038: /**
039: * Default InitialContextFactory to use.
040: */
041: private static final String DEFAULT_INITIAL_CONTEXT_FACTORY = "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory";
042:
043: /**
044: * Utility class.
045: */
046: private ClientStateless() {
047:
048: }
049:
050: /**
051: * Main method.
052: * @param args the arguments (not required)
053: * @throws Exception if exception is found.
054: */
055: public static void main(final String[] args) throws Exception {
056:
057: Context initialContext = getInitialContext();
058:
059: StatelessRemote statelessBean = (StatelessRemote) initialContext
060: .lookup("org.ow2.easybeans.examples.statelessbean.StatelessBean"
061: + "_"
062: + StatelessRemote.class.getName()
063: + "@Remote");
064:
065: System.out.println("Calling helloWorld method...");
066: statelessBean.helloWorld();
067:
068: System.out.println("Add 1 + 2...");
069: int resultAdd = statelessBean.add(1, 2);
070: System.out.println("Sum = '" + resultAdd + "'.");
071:
072: // System.out.println("Div 1 / 0 (expect exception)...");
073: // statelessBean.div(1, 0);
074:
075: }
076:
077: /**
078: * @return Returns the InitialContext.
079: * @throws NamingException If the Context cannot be created.
080: */
081: private static Context getInitialContext() throws NamingException {
082:
083: // if user don't use jclient/client container
084: // we can specify the InitialContextFactory to use
085: // But this is *not recommended*.
086: Hashtable<String, Object> env = new Hashtable<String, Object>();
087: env.put(Context.INITIAL_CONTEXT_FACTORY,
088: getInitialContextFactory());
089:
090: // Usually a simple new InitialContext() without any parameters is sufficent.
091: // return new InitialContext();
092:
093: return new InitialContext(env);
094: }
095:
096: /**
097: * Returns a configurable InitialContextFactory classname.<br/>
098: * Can be configured with the <code>easybeans.client.initial-context-factory</code> System property.
099: * @return Returns a configurable InitialContextFactory classname.
100: */
101: private static String getInitialContextFactory() {
102: String prop = System
103: .getProperty("easybeans.client.initial-context-factory");
104: // If not found, use the default
105: if (prop == null) {
106: prop = DEFAULT_INITIAL_CONTEXT_FACTORY;
107: }
108: return prop;
109: }
110:
111: }
|