KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > examples > statelessbean > ClientStateless


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: ClientStateless.java 1078 2006-08-10 09:28:57Z sauthieg $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.examples.statelessbean;
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 ClientStateless {
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 ClientStateless() {
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         Context JavaDoc initialContext = getInitialContext();
60
61         StatelessRemote statelessBean = (StatelessRemote) initialContext
62                 .lookup("org.objectweb.easybeans.examples.statelessbean.StatelessBean"
63                         + "_" + StatelessRemote.class.getName() + "@Remote");
64
65         System.out.println("Calling helloWorld method...");
66         statelessBean.helloWorld();
67
68         System.out.println("Add 1 + 2...");
69         int resultAdd = statelessBean.add(1, 2);
70         System.out.println("Sum = '" + resultAdd + "'.");
71
72         // System.out.println("Div 1 / 0 (expect exception)...");
73
// statelessBean.div(1, 0);
74

75     }
76
77     /**
78      * @return Returns the InitialContext.
79      * @throws NamingException If the Context cannot be created.
80      */

81     private static Context JavaDoc getInitialContext() throws NamingException JavaDoc {
82
83         // if user don't use jclient/client container
84
// we can specify the InitialContextFactory to use
85
// But this is *not recommended*.
86
Hashtable JavaDoc<String JavaDoc, Object JavaDoc> env = new Hashtable JavaDoc<String JavaDoc, Object JavaDoc>();
87         env.put(Context.INITIAL_CONTEXT_FACTORY, getInitialContextFactory());
88
89         // Usually a simple new InitialContext() without any parameters is sufficent.
90
// return new InitialContext();
91

92         return new InitialContext JavaDoc(env);
93     }
94
95     /**
96      * Returns a configurable InitialContextFactory classname.<br/>
97      * Can be configured with the <code>easybeans.client.initial-context-factory</code> System property.
98      * @return Returns a configurable InitialContextFactory classname.
99      */

100     private static String JavaDoc getInitialContextFactory() {
101         String JavaDoc prop = System.getProperty("easybeans.client.initial-context-factory");
102         // If not found, use the default
103
if (prop == null) {
104             prop = DEFAULT_INITIAL_CONTEXT_FACTORY;
105         }
106         return prop;
107     }
108
109
110 }
111
Popular Tags