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.entitybean;
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: // Lookup the Remote Bean interface through JNDI
058: Context initialContext = getInitialContext();
059: SessionFacadeRemote facadeBean = (SessionFacadeRemote) initialContext
060: .lookup("org.ow2.easybeans.examples.entitybean.SessionFacade"
061: + "_"
062: + SessionFacadeRemote.class.getName()
063: + "@Remote");
064:
065: // Adds some employees
066: Employee florent = facadeBean.findEmployee(1);
067: if (florent == null) {
068: facadeBean.addEmployee(1, "Florent");
069: }
070: Employee whale = facadeBean.findEmployee(2);
071: if (whale == null) {
072: facadeBean.addEmployee(2, "Whale");
073: }
074:
075: // Try to get employee with id 1
076: Employee employee = facadeBean.findEmployee(1);
077: System.out
078: .println("Employee with id 1 = " + employee.getName());
079:
080: // Try to get employee with id 2
081: employee = facadeBean.findEmployee(2);
082: System.out
083: .println("Employee with id 2 = " + employee.getName());
084:
085: }
086:
087: /**
088: * @return Returns the InitialContext.
089: * @throws NamingException If the Context cannot be created.
090: */
091: private static Context getInitialContext() throws NamingException {
092:
093: // if user don't use jclient/client container
094: // we can specify the InitialContextFactory to use
095: // But this is *not recommended*.
096: Hashtable<String, Object> env = new Hashtable<String, Object>();
097: env.put(Context.INITIAL_CONTEXT_FACTORY,
098: getInitialContextFactory());
099:
100: // Usually a simple new InitialContext() without any parameters is sufficent.
101: // return new InitialContext();
102:
103: return new InitialContext(env);
104: }
105:
106: /**
107: * Returns a configurable InitialContextFactory classname.<br/>
108: * Can be configured with the <code>easybeans.client.initial-context-factory</code> System property.
109: * @return Returns a configurable InitialContextFactory classname.
110: */
111: private static String getInitialContextFactory() {
112: String prop = System
113: .getProperty("easybeans.client.initial-context-factory");
114: // If not found, use the default
115: if (prop == null) {
116: prop = DEFAULT_INITIAL_CONTEXT_FACTORY;
117: }
118: return prop;
119: }
120:
121: }
|