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: Client.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.examples.migrationejb21;
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 Client {
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 Client() {
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: // Call EJB3 bean
060: EJB3RemoteBusinessInterface ejb3Bean = (EJB3RemoteBusinessInterface) initialContext
061: .lookup("org.ow2.easybeans.examples.migrationejb21.EJB2And3Bean"
062: + "_"
063: + EJB3RemoteBusinessInterface.class.getName()
064: + "@Remote");
065: System.out
066: .println("Calling hello() method on EJB 3.0 view of the Bean...");
067: ejb3Bean.hello();
068:
069: // Call EJB2.1 bean
070: EJB2RemoteHome ejb2Home = (EJB2RemoteHome) initialContext
071: .lookup("org.ow2.easybeans.examples.migrationejb21.EJB2And3Bean"
072: + "_" + EJB2RemoteHome.class.getName());
073: // Create bean
074: EJB2RemoteInterface ejb2Bean = ejb2Home.create();
075: System.out
076: .println("Calling hello() method on Remote EJB 2.1 view of the Bean...");
077: ejb2Bean.hello21Remote();
078:
079: }
080:
081: /**
082: * @return Returns the InitialContext.
083: * @throws NamingException If the Context cannot be created.
084: */
085: private static Context getInitialContext() throws NamingException {
086:
087: // if user don't use jclient/client container
088: // we can specify the InitialContextFactory to use
089: // But this is *not recommended*.
090: Hashtable<String, Object> env = new Hashtable<String, Object>();
091: env.put(Context.INITIAL_CONTEXT_FACTORY,
092: getInitialContextFactory());
093:
094: // Usually a simple new InitialContext() without any parameters is sufficent.
095: // return new InitialContext();
096:
097: return new InitialContext(env);
098: }
099:
100: /**
101: * Returns a configurable InitialContextFactory classname.<br/>
102: * Can be configured with the <code>easybeans.client.initial-context-factory</code> System property.
103: * @return Returns a configurable InitialContextFactory classname.
104: */
105: private static String getInitialContextFactory() {
106: String prop = System
107: .getProperty("easybeans.client.initial-context-factory");
108: // If not found, use the default
109: if (prop == null) {
110: prop = DEFAULT_INITIAL_CONTEXT_FACTORY;
111: }
112: return prop;
113: }
114:
115: }
|