KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > chat > monolithic > Chat


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2002 USTL - LIFL - GOAL
5 Contact: openccm@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): Areski Flissi.
23 Contributor(s): Christophe Contreras.
24
25 ====================================================================*/

26
27 package org.objectweb.ccm.chat.monolithic;
28
29 import org.objectweb.ccm.chat.*;
30
31 /**
32  * The chat application deployment.
33  *
34 * @author <a HREF="mailto:Areski.Flissi@lifl.fr">Areski Flissi</A>
35  */

36
37 public class Chat
38 {
39     /**
40      * The bootstrap of the chat application.
41      */

42     public static void
43     main(String JavaDoc[] args)
44     throws Exception JavaDoc
45     {
46        String JavaDoc _OTS = System.getProperties().getProperty("TRANSACTIONAL_PLUGIN","no").toLowerCase();
47
48        // Init the ORB.
49
System.out.println("Initializing the ORB...");
50
51        // TODO: Need to report this ORB.init() problem to OpenORB developers!
52
// Due to an OpenORB problem, the following line:
53
// org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
54
// are replaced by the next line.
55

56        // Init the OpenCCM Components Runtime.
57
org.omg.CORBA.ORB JavaDoc orb = org.objectweb.openccm.Components.Runtime.init(args);
58
59        // Obtain the Name Service.
60
System.out.println("Obtaining the Name Service...");
61        org.omg.CORBA.Object JavaDoc obj = orb.resolve_initial_references("NameService");
62        org.omg.CosNaming.NamingContext JavaDoc nc = org.omg.CosNaming.NamingContextHelper.narrow(obj);
63
64 // String[] ids = orb.list_initial_services();
65
// System.out.println("R‰f‰rences initiales disponibles : ");
66
// for (int i=0;i<ids.length;i++)
67
// System.out.println("-> "+ids[i]);
68

69         org.omg.CosTransactions.Current current = null;
70         if (_OTS.equals("yes"))
71         {
72             System.out.println("Obtaining the Transaction Service...");
73             org.omg.CORBA.Object JavaDoc objOTS = orb.resolve_initial_references("TransactionCurrent");
74             current = org.omg.CosTransactions.CurrentHelper.narrow( objOTS );
75         }
76
77         try
78         {
79             if ((current!=null)&&(_OTS.equals("yes")))
80             {
81                 System.out.println("Status = "+current.get_status());
82                 System.out.println("Beginning the transaction...");
83                 current.begin();
84             }
85
86             // Obtain the component servers.
87
System.out.println("Obtaining Component Servers...");
88             org.omg.CosNaming.NameComponent JavaDoc[] ncomp = new org.omg.CosNaming.NameComponent JavaDoc[1];
89             ncomp[0] = new org.omg.CosNaming.NameComponent JavaDoc("ComponentServer1", "");
90             obj = nc.resolve(ncomp);
91             org.objectweb.openccm.Deployment.Server server1 = org.objectweb.openccm.Deployment.ServerHelper.narrow(obj);
92             ncomp[0].id = "ComponentServer2";
93             obj = nc.resolve(ncomp);
94             org.objectweb.openccm.Deployment.Server server2 = org.objectweb.openccm.Deployment.ServerHelper.narrow(obj);
95
96             // Obtain the container homes and archive installators.
97
org.omg.Components.Deployment.ComponentServer server1_cs = server1.provide_component_server();
98             org.omg.Components.Deployment.ComponentInstallation server1_inst = server1.provide_install();
99             org.omg.Components.Deployment.ComponentServer server2_cs = server2.provide_component_server();
100             org.omg.Components.Deployment.ComponentInstallation server2_inst = server2.provide_install();
101
102             // Install archives.
103
System.out.println("Installing archives...");
104             // get the current directory and store it as a String
105
String JavaDoc demoPath = null;
106             try {
107                 demoPath = new java.io.File JavaDoc(".").getCanonicalPath()+ java.io.File.separator ;
108             }catch(Exception JavaDoc e) {
109                 e.printStackTrace();
110             }
111
112             server1_inst.install("chat", "file:"+demoPath + "./archives/chat.jar");
113             server2_inst.install("chat","file:"+ demoPath + "./archives/chat.jar");
114
115             // install some plugins already provided by OpenCCM.
116
server1_inst.install("openccm_plugins","file:"+ demoPath + "./archives/OpenCCM_Plugins.jar");
117             server2_inst.install("openccm_plugins","file:"+ demoPath + "./archives/OpenCCM_Plugins.jar");
118
119             // declaring the container types
120
//
121
// Uncomment this part for a minimal configuration
122
// and comment the following one.
123
//
124
/**
125             String cont_config =
126             "container = CONTAINER.container ; " +
127             "config_home = container.create_system_home("+
128             "\"openccm_plugins\", " +
129             "\"EmptyConfig\", "+
130             "\"org.objectweb.openccm.Containers.Plugins.EmptyConfigurationHome.create\") ;"+
131             "config = config_home.create_component(JAVA.null) ;" +
132             "container.create_system_home("+
133             "\"openccm_plugins\", " +
134             "\"EmptyCoordinatorHome\", "+
135             "\"org.objectweb.openccm.Containers.Plugins.EmptyCoordinatorHome.create\") ;"+
136             "container.create_system_home("+
137             "\"openccm_plugins\", " +
138             "\"EmptyControllerHome\", "+
139             "\"org.objectweb.openccm.Containers.Plugins.EmptyControllerHome.create\") ;"+
140             "container.set_container_configuration(config) ;";
141             **/

142
143             String JavaDoc cont_config =
144             // retrieve the container reference.
145
"container = CONTAINER.container ; " +
146             // create aliases on PropertySetImpl and StringPropertyImpl class.
147
"JAVA.declare(\"org.objectweb.openccm.Containers\") ; " +
148             "PropertySet = org.objectweb.openccm.Containers.PropertySetImpl ;" +
149             "StringProperty = org.objectweb.openccm.Containers.StringPropertyImpl ;" +
150             // create an empty configuration with it's alias.
151
"config_home = container.create_system_home("+
152             "\"openccm_plugins\", " +
153             "\"EmptyConfig\", "+
154             "\"org.objectweb.openccm.Containers.Plugins.EmptyConfigurationHome.create\") ;"+
155             "config = config_home.create_component(JAVA.null) ;" +
156             // create an empty coordinator system home with it's alias
157
"container.create_system_home("+
158             "\"openccm_plugins\", " +
159             "\"EmptyCoordinatorHome\", "+
160             "\"org.objectweb.openccm.Containers.Plugins.EmptyCoordinatorHome.create\") ;"+
161             // create an empty controller system home with it's alias
162
"container.create_system_home("+
163             "\"openccm_plugins\", " +
164             "\"EmptyControllerHome\", "+
165             "\"org.objectweb.openccm.Containers.Plugins.EmptyControllerHome.create\") ;"+
166             // create a trace controller system home with it's alias.
167
"container.create_system_home("+
168             "\"openccm_plugins\", " +
169             "\"Trace\", " +
170             "\"org.objectweb.openccm.Containers.Plugins.TraceControllerHome.create\") ; "+
171             // create a list coordinator system home with it's alias.
172
"container.create_system_home("+
173             "\"openccm_plugins\", " +
174             "\"ListCoord\", " +
175             "\"org.objectweb.openccm.Containers.Plugins.ListCoordinatorHome.create\") ; "+
176             // create an port specific config.
177
"container.create_system_home("+
178             "\"openccm_plugins\", " +
179             "\"PortConfig\", " +
180             "\"org.objectweb.openccm.Containers.Plugins.PortSpecificConfigurationHome.create\") ; " +
181             // set this configuration as the container default configuration.
182
"container.set_container_configuration(config) ;";
183
184             // instantiate a container on each server.
185
org.omg.Components.ConfigValue[] config = new org.omg.Components.ConfigValue[1];
186             config[0] = new org.objectweb.openccm.Components.ConfigValueImpl();
187             config[0].name = "container_script";
188             org.omg.CORBA.TypeCode JavaDoc string_tc =
189             org.objectweb.openccm.corba.TheORB.getORB().get_primitive_tc(org.omg.CORBA.TCKind.tk_string);
190             org.omg.CORBA.Any JavaDoc value =
191                     org.objectweb.openccm.corba.TheDynamicAnyFactory.getFactory().
192                     create_dyn_any_from_type_code(string_tc).to_any();
193             value.insert_string(cont_config);
194             config[0].value = value;
195
196
197             org.omg.Components.Deployment.Container server1_cont =
198                 // server1_cs.create_container(config);
199
server1_cs.create_container(new org.omg.Components.ConfigValue[0]);
200             org.omg.Components.Deployment.Container server2_cont =
201                 // server2_cs.create_container(config);
202
server2_cs.create_container(new org.omg.Components.ConfigValue[0]);
203
204
205             // Install homes.
206
System.out.println("Instantiating homes...");
207
208             // define a container configuration.
209
config[0].name = "home_script";
210
211             //
212
// Uncomment this part for a minimal configuration
213
// and comment the following one.
214
//
215
/**
216             java.lang.String home_config =
217             "container.set_home_configuration(container.get_container_configuration()) ;" +
218             "container.set_component_configuration(container.get_container_configuration()) ;";
219             **/

220
221             java.lang.String JavaDoc home_config =
222             // set the container config as the home config
223
// i.e. an empty configuration.
224
"container.set_home_configuration(container.get_container_configuration()) ;" +
225             // create a port specific config
226
"config = container.find_system_home(\"PortConfig\").create_component(JAVA.null) ;" +
227             // set the component ::CORBA::Repository of the component.
228
"config.setComponentUId(\"IDL:ccm.objectweb.org/chat/Server:1.0\") ;" +
229             // set a list coordinator for the "for_clients" facet of the "Server" component
230
"config.setCallCoordinator(\"for_clients\", \"ListCoord\") ;" +
231             // set a trace controller for the "for_clients" facet of the "Server" component
232
"config.setCallControllers(\"for_clients\", [ \"Trace\" ]) ;" +
233             // create and set the trace configuration for this facet
234
"trace_config = PropertySet(\"trace\")" +
235             "trace_config.add(StringProperty(\"filename\", \"./trace_Server.txt\"))" +
236             "config.setCallControllersConfig(\"for_clients\", [ trace_config ])" +
237             // set the container config as the parent config.
238
"config.set_parent_configuration(container.get_container_configuration()) ;" +
239             // set this config as the component config.
240
"container.set_component_configuration(config) ;";
241
242             // install the Server home with this container configuration.
243
value.insert_string(home_config);
244             config[0].value = value;
245
246             // org.omg.Components.CCMHome h = server1_cont.install_home("chat", "org.objectweb.ccm.chat.monolithic.ServerHomeImpl.create_home", config);
247
org.omg.Components.CCMHome h =
248                     server1_cont.install_home("chat",
249                                               "org.objectweb.ccm.chat.monolithic.ServerHomeImpl.create_home",
250                                                new org.omg.Components.ConfigValue[0]);
251             ServerHome sh = ServerHomeHelper.narrow(h);
252
253             // define a container configuration.
254
//
255
// Uncomment this part for a minimal configuration
256
// and comment the following one.
257
//
258
/**
259             **/

260
261             home_config =
262             // set the container config as the home config
263
// i.e. an empty configuration.
264
"container.set_home_configuration(container.get_container_configuration()) ;" +
265             // create a port specific config
266
"config = container.find_system_home(\"PortConfig\").create_component(JAVA.null) ;" +
267             // set the component ::CORBA::Repository of the component.
268
"config.setComponentUId(\"IDL:ccm.objectweb.org/chat/Client:1.0\") ;" +
269             // set a list coordinator for the "to_server" receptacle of the "Client" component
270
"config.setCallCoordinator(\"to_server\", \"ListCoord\") ;" +
271             // set a trace controller for the "to_server" receptacle of the "Client" component
272
"config.setCallControllers(\"to_server\", [ \"Trace\" ]) ;" +
273             // create and set the trace configuration for this receptacle
274
"trace_config = PropertySet(\"trace\")" +
275             "trace_config.add(StringProperty(\"filename\", \"./trace_Clients.txt\"))" +
276             "trace_config.add(StringProperty(\"singleton\", \"to_server\"))" +
277             "config.setCallControllersConfig(\"to_server\", [ trace_config ])" +
278             // set the container config as the parent config.
279
"config.set_parent_configuration(container.get_container_configuration()) ;" +
280             // set this config as the component config.
281
"container.set_component_configuration(config) ;";
282
283             // install the Client home with this container configuration.
284
value.insert_string(home_config);
285             config[0].value = value;
286
287             // h = server2_cont.install_home("chat", "org.objectweb.ccm.chat.monolithic.ClientHomeImpl.create_home", config);
288
h = server2_cont.install_home("chat",
289                                           "org.objectweb.ccm.chat.monolithic.ClientHomeImpl.create_home",
290                                            new org.omg.Components.ConfigValue[0]);
291             ClientHome ch = ClientHomeHelper.narrow(h);
292
293
294             // Create components.
295
System.out.println("Instantiating components...");
296             Server s = sh.create();
297             Client c1 = ch.create();
298             Client c2 = ch.create();
299             
300
301             // Configure components.
302
System.out.println("Configuring components...");
303             s.name("The Server");
304             c1.name("Cosette");
305             c2.name("Gavroche");
306             
307
308             // Connect clients and server.
309
System.out.println("Interconnecting components...");
310             Service for_clients = s.provide_the_service();
311             c1.connect_the_service(for_clients);
312             c2.connect_the_service(for_clients);
313             s.subscribe_to_consumers(c1.get_consumer_from_servers());
314             s.subscribe_to_consumers(c2.get_consumer_from_servers());
315             
316
317             // Configuration completion.
318
System.out.println("Configuration completion...");
319             s.configuration_complete();
320             c1.configuration_complete();
321             c2.configuration_complete();
322             
323
324         } catch (Exception JavaDoc e) {
325             if ((current!=null)&&(_OTS.equals("yes")))
326             {
327                 System.out.println("Error during deployment :");
328                 e.printStackTrace();
329                 System.out.print("Rolling Back ... ");
330                 current.rollback();
331                 System.out.println("Done");
332                 // Force to exit.
333
System.exit(0);
334             }
335             else
336             {
337                 System.out.println("Error during deployment :");
338                 e.printStackTrace();
339                 // Force to exit.
340
System.exit(0);
341             }
342         }
343
344         if ((current!=null)&&(_OTS.equals("yes")))
345         {
346             System.out.print("Do you want to commit the Deployment ? [Y/n] ");
347
348             java.io.BufferedReader JavaDoc _buffer
349                 = new java.io.BufferedReader JavaDoc(new java.io.InputStreamReader JavaDoc(System.in));
350             char _answer = (char) _buffer.read();
351
352             if ((_answer=='n')||(_answer=='N'))
353             {
354                 System.out.print("Rolling Back ... ");
355                 current.rollback();
356                 System.out.println("Done");
357             }
358             else
359             {
360                 System.out.print("Committing ... ");
361                 current.commit(false);
362                 System.out.println("Done");
363             }
364         }
365
366        // Force to exit.
367
System.exit(0);
368     }
369 }
370
371
372
373
374
375
376
377
Popular Tags