KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > hello > Run


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2002 USTL - LIFL - GOAL
5 Contact: openccm-team@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle, Sylvain Leblanc.
23 Contributor(s): Christophe Demarey, Christophe Contreras.
24
25 ====================================================================*/

26
27 package org.objectweb.ccm.hello;
28
29 /**
30  * The hello client script.
31  */

32
33 public class Run
34 {
35   
36   /**
37    * The usage message.
38    */

39   public static void
40   usage()
41   {
42     System.err.println("usage: Run [NS_name]");
43     System.err.println("");
44     System.err.println(" <NS_name> " +
45                        "The name of the registered ServerHome " +
46                        "instance.");
47   }
48   
49   /**
50    * The bootstrap of the demo1 client application.
51    */

52   public static void
53   main(String JavaDoc[] args)
54     throws Exception JavaDoc
55   {
56     String JavaDoc _OTS = System.getProperties().getProperty("TRANSACTIONAL_PLUGIN","no").toLowerCase();
57     
58     // Init the ORB.
59
System.out.println("Initializing the ORB...");
60     
61     // TODO: Need to report this ORB.init() problem to OpenORB developers!
62
// Due to an OpenORB problem, the following line:
63
// org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
64
// are replaced by the next line.
65

66      // Init the ORB.
67
args = org.objectweb.openccm.corba.TheORB.initialize(args);
68
69     // Init the OpenCCM runtime.
70
org.objectweb.openccm.Components.Runtime.init();
71
72     // Obtain the ORB.
73
org.omg.CORBA.ORB JavaDoc orb = org.objectweb.openccm.corba.TheORB.getORB();
74
75
76     if (args.length > 1) {
77       usage();
78       System.exit(1);
79     }
80
81     String JavaDoc ns_name = (args.length >= 1)?args[0]:"MyHelloServerHome";
82
83     // Obtain the Name Service.
84
System.out.println("Obtaining the Name Service...");
85     org.omg.CORBA.Object JavaDoc obj = orb.resolve_initial_references("NameService");
86     org.omg.CosNaming.NamingContext JavaDoc nc = org.omg.CosNaming.NamingContextHelper.narrow(obj);
87     
88     // Resolving the hello::ServerHome instance
89
System.out.println("Obtaining the " + ns_name + " ServerHome instance...");
90     org.omg.CosNaming.NameComponent JavaDoc[] ncomp = new org.omg.CosNaming.NameComponent JavaDoc[1];
91     ncomp[0] = new org.omg.CosNaming.NameComponent JavaDoc(ns_name, "");
92     obj = nc.resolve(ncomp);
93     ServerHome home = ServerHomeHelper.narrow(obj);
94     
95     org.omg.CosTransactions.Current current = null;
96     if (_OTS.equals("yes")) {
97       System.out.println("Obtaining the Transaction Service...");
98       org.omg.CORBA.Object JavaDoc objOTS = orb.resolve_initial_references("TransactionCurrent");
99       current = org.omg.CosTransactions.CurrentHelper.narrow( objOTS );
100     }
101     
102     try {
103       if ((current!=null)&&(_OTS.equals("yes"))) {
104         System.out.println("Beginning the transaction...");
105         current.begin();
106       }
107       
108       // Creates a new Server component instance
109
System.out.println("Creating a new Server component instance...");
110       Server server = home.create();
111
112       System.out.println("Completing the server configuration...");
113       server.configuration_complete();
114
115       // Retrieve the hello::HelloWorld facet
116
System.out.println("Retrieving the facet 'for_clients'...");
117       HelloWorld for_clients = server.provide_for_clients();
118       
119       // Call the 'say_hello' method
120
System.out.println("Calling the 'say_hello' method...");
121       System.out.println( for_clients.say_hello() );
122       
123       // Destroy the server component
124
System.out.println("Destroying the server component instance...");
125       server.remove();
126       
127       System.out.println("Bye !!");
128     } catch (Exception JavaDoc e) {
129       if ((current!=null)&&(_OTS.equals("yes"))) {
130         System.out.println("Error during deployment :");
131         e.printStackTrace();
132         System.out.print("Rolling Back ... ");
133         current.rollback();
134         System.out.println("Done");
135         // Force to exit.
136
System.exit(0);
137       }
138     }
139     
140     if ((current!=null)&&(_OTS.equals("yes"))) {
141       System.out.print("Do you want to commit the Deployment ? [Y/n] ");
142       
143       java.io.BufferedReader JavaDoc _buffer
144         = new java.io.BufferedReader JavaDoc(new java.io.InputStreamReader JavaDoc(System.in));
145       char _answer = (char) _buffer.read();
146       
147       if ((_answer=='n')||(_answer=='N'))
148       {
149         System.out.print("Rolling Back ... ");
150         current.rollback();
151         System.out.println("Done");
152       } else {
153         System.out.print("Committing ... ");
154         current.commit(false);
155         System.out.println("Done");
156         System.out.println(ns_name + " is ready to be used...");
157       }
158     } else {
159       System.out.println(ns_name + " is ready to be used...");
160     }
161     
162     System.exit(0);
163   }
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
Popular Tags