KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > remoting > SampleMain


1 package org.bsf.remoting;
2
3 import org.bsf.remoting.EJBDefinition;
4 import org.bsf.remoting.http.HttpServiceFactory;
5
6 /**
7  * Shows the use of the Http Invocation to communication with an EJB.
8  *
9  */

10 public class SampleMain {
11
12     /**
13      * The factory
14      */

15     private static HttpServiceFactory factory =
16         new HttpServiceFactory("localhost", 8080,"remoting");
17
18         //For a secured communication we would have use this constructor :
19
//new HttpServiceFactory("https","www.bs-factory.com", 443,"remoting");
20

21     /**
22      * Every access to a stateless EJB is defined through its EJBDefinition.
23      * All the used EJB should be placed in a constant interface to facilitate
24      * the view of all the services.
25      */

26     private static final EJBDefinition TEST_SESSION = new EJBDefinition(
27         "ejb/TestSession","org.bsf.remoting.StatelessTestHome",
28             "org.bsf.remoting.StatelessTest"
29     );
30
31
32
33     public static void main(String JavaDoc[] args) {
34
35         //The retrieving of a stateless EJB is straightforward
36
StatelessTest myService = (StatelessTest)
37                 factory.getService(TEST_SESSION);
38
39         try {
40             //Calls are made like any normal calls on an EJB.
41
String JavaDoc upperWord = myService.upper("word");
42             System.out.println("Result : " + upperWord);
43
44             //The pattern to retrieve a statefull bean : we use a stateless Bean to
45
//create it.
46
StatefulTest myStateful = myService.createStateful();
47             String JavaDoc name = myStateful.getEJBName();
48             System.out.println("Statefull EJB Name : " + name);
49
50
51             //We know want to know use the authentication
52
factory.setLogin("myLogin");
53             factory.setPassword("myPass");
54
55             //All the following calls will use HTTP basic authentication
56
if ("myLogin".equals(myService.getCallerPrincipal())){
57                 System.out.println("That's me");
58             }
59
60
61         } catch (java.rmi.RemoteException JavaDoc e) {
62             e.printStackTrace();
63         }
64     }
65 }
66
Popular Tags