KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > examples > entitybean > Client


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: Client.java 1078 2006-08-10 09:28:57Z sauthieg $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.examples.entitybean;
27
28 import java.util.Hashtable JavaDoc;
29
30 import javax.naming.Context JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.naming.NamingException JavaDoc;
33
34 /**
35  * Simple client of the stateless.
36  * @author Florent Benoit
37  */

38 public final class Client {
39
40     /**
41      * Default InitialContextFactory to use.
42      */

43     private static final String JavaDoc DEFAULT_INITIAL_CONTEXT_FACTORY = "org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory";
44
45     /**
46      * Utility class.
47      */

48     private Client() {
49
50     }
51
52     /**
53      * Main method.
54      * @param args the arguments (not required)
55      * @throws Exception if exception is found.
56      */

57     public static void main(final String JavaDoc[] args) throws Exception JavaDoc {
58
59         // Lookup the Remote Bean interface through JNDI
60
Context JavaDoc initialContext = getInitialContext();
61         SessionFacadeRemote facadeBean = (SessionFacadeRemote) initialContext.lookup(
62                 "org.objectweb.easybeans.examples.entitybean.SessionFacade"
63                 + "_" + SessionFacadeRemote.class.getName() + "@Remote");
64
65         // Adds some employees
66
Employee florent = facadeBean.findEmployee(1);
67         if (florent == null) {
68             facadeBean.addEmployee(1, "Florent");
69         }
70         Employee whale = facadeBean.findEmployee(2);
71         if (whale == null) {
72             facadeBean.addEmployee(2, "Whale");
73         }
74
75         // Try to get employee with id 1
76
Employee employee = facadeBean.findEmployee(1);
77         System.out.println("Employee with id 1 = " + employee.getName());
78
79         // Try to get employee with id 2
80
employee = facadeBean.findEmployee(2);
81         System.out.println("Employee with id 2 = " + employee.getName());
82
83     }
84
85     /**
86      * @return Returns the InitialContext.
87      * @throws NamingException If the Context cannot be created.
88      */

89     private static Context JavaDoc getInitialContext() throws NamingException JavaDoc {
90
91         // if user don't use jclient/client container
92
// we can specify the InitialContextFactory to use
93
// But this is *not recommended*.
94
Hashtable JavaDoc<String JavaDoc, Object JavaDoc> env = new Hashtable JavaDoc<String JavaDoc, Object JavaDoc>();
95         env.put(Context.INITIAL_CONTEXT_FACTORY, getInitialContextFactory());
96
97         // Usually a simple new InitialContext() without any parameters is sufficent.
98
// return new InitialContext();
99

100         return new InitialContext JavaDoc(env);
101     }
102
103     /**
104      * Returns a configurable InitialContextFactory classname.<br/>
105      * Can be configured with the <code>easybeans.client.initial-context-factory</code> System property.
106      * @return Returns a configurable InitialContextFactory classname.
107      */

108     private static String JavaDoc getInitialContextFactory() {
109         String JavaDoc prop = System.getProperty("easybeans.client.initial-context-factory");
110         // If not found, use the default
111
if (prop == null) {
112             prop = DEFAULT_INITIAL_CONTEXT_FACTORY;
113         }
114         return prop;
115     }
116
117 }
118
Popular Tags