KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > demo3 > monolithic > Demo3


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): Philippe Merle, Mathieu Vadet.
23 Contributor(s): Christophe Demarey, Christophe Contreras.
24
25 ====================================================================*/

26
27 package org.objectweb.ccm.demo3.monolithic;
28
29 import org.objectweb.ccm.demo3.*;
30
31 /**
32  * The demo3 application deployment.
33  *
34  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</A>
35  * <a HREF="mailto:Mathieu.Vadet@lifl.fr">Mathieu Vadet</A>
36  * <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</A>
37  */

38
39 public class Demo3
40 {
41     /**
42      * The bootstrap of the demo3 application.
43      */

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

59         // Init the OpenCCM Components Runtime.
60
org.omg.CORBA.ORB JavaDoc orb =
61             org.objectweb.openccm.Components.Runtime.init(args);
62
63         // Obtain the Name Service.
64
System.out.println("Obtaining the Name Service...");
65         org.omg.CORBA.Object JavaDoc obj =
66             orb.resolve_initial_references("NameService");
67         org.omg.CosNaming.NamingContext JavaDoc nc =
68             org.omg.CosNaming.NamingContextHelper.narrow(obj);
69
70         org.omg.CosTransactions.Current current = null;
71         if (_OTS.equals("yes"))
72         {
73             System.out.println("Obtaining the Transaction Service...");
74             org.omg.CORBA.Object JavaDoc objOTS = orb.resolve_initial_references("TransactionCurrent");
75             current = org.omg.CosTransactions.CurrentHelper.narrow( objOTS );
76         }
77
78         try
79         {
80             if ((current!=null)&&(_OTS.equals("yes")))
81             {
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 =
89                 new org.omg.CosNaming.NameComponent JavaDoc[1];
90             ncomp[0] = new org.omg.CosNaming.NameComponent JavaDoc("ComponentServer1", "");
91             obj = nc.resolve(ncomp);
92             org.objectweb.openccm.Deployment.Server server1 =
93                 org.objectweb.openccm.Deployment.ServerHelper.narrow(obj);
94             ncomp[0].id = "ComponentServer2";
95             obj = nc.resolve(ncomp);
96             org.objectweb.openccm.Deployment.Server server2 =
97                 org.objectweb.openccm.Deployment.ServerHelper.narrow(obj);
98
99             // Obtain the container homes and archive installators.
100
org.omg.Components.Deployment.ComponentServer server1_cs = server1.provide_component_server();
101             org.omg.Components.Deployment.ComponentInstallation server1_inst = server1.provide_install();
102             org.omg.Components.Deployment.ComponentServer server2_cs = server2.provide_component_server();
103             org.omg.Components.Deployment.ComponentInstallation server2_inst = server2.provide_install();
104
105             // Install archives.
106
// get the current directory and store it as a String
107
String JavaDoc demoPath = null;
108             try {
109                 demoPath = new java.io.File JavaDoc(".").getCanonicalPath()+ java.io.File.separator ;
110             }catch(Exception JavaDoc e) {
111                 e.printStackTrace();
112             }
113
114             server1_inst.install("demo3","file:"+ demoPath + "./archives/demo3.jar");
115             server2_inst.install("demo3","file:"+ demoPath + "./archives/demo3.jar");
116
117             // install some plugins already provided by OpenCCM.
118
server1_inst.install("openccm_plugins","file:"+ demoPath + "./archives/OpenCCM_Plugins.jar");
119             server2_inst.install("openccm_plugins","file:"+ demoPath + "./archives/OpenCCM_Plugins.jar");
120
121             // declaring the container types
122
String JavaDoc cont_config =
123                 // retrieve the container reference.
124
"container = CONTAINER.container ; " +
125                 // create aliases on PropertySetImpl and StringPropertyImpl class.
126
"JAVA.declare(\"org.objectweb.openccm.Containers\") ; " +
127                 "PropertySet = org.objectweb.openccm.Containers.PropertySetImpl ;" +
128                 "StringProperty = org.objectweb.openccm.Containers.StringPropertyImpl ;" +
129                 // create an empty configuration with it's alias.
130
"config_home = container.create_system_home("+
131                 "\"openccm_plugins\", " +
132                 "\"EmptyConfig\", "+
133                 "\"org.objectweb.openccm.Containers.Plugins.EmptyConfigurationHome.create\") ;"+
134                 "config = config_home.create_component(JAVA.null) ;" +
135                 // create an empty coordinator system home with it's alias
136
"container.create_system_home("+
137                 "\"openccm_plugins\", " +
138                 "\"EmptyCoordinatorHome\", "+
139                 "\"org.objectweb.openccm.Containers.Plugins.EmptyCoordinatorHome.create\") ;"+
140                 // create an empty controller system home with it's alias
141
"container.create_system_home("+
142                 "\"openccm_plugins\", " +
143                 "\"EmptyControllerHome\", "+
144                 "\"org.objectweb.openccm.Containers.Plugins.EmptyControllerHome.create\") ;"+
145                 // create a trace controller system home with it's alias.
146
"container.create_system_home("+
147                 "\"openccm_plugins\", " +
148                 "\"Trace\", " +
149                 "\"org.objectweb.openccm.Containers.Plugins.TraceControllerHome.create\") ; "+
150                 // create a list coordinator system home with it's alias.
151
"container.create_system_home("+
152                 "\"openccm_plugins\", " +
153                 "\"ListCoord\", " +
154                 "\"org.objectweb.openccm.Containers.Plugins.ListCoordinatorHome.create\") ; "+
155                 // create an port specific config.
156
"container.create_system_home("+
157                 "\"openccm_plugins\", " +
158                 "\"PortConfig\", " +
159                 "\"org.objectweb.openccm.Containers.Plugins.PortSpecificConfigurationHome.create\") ; " +
160                 // set this configuration as the container default configuration.
161
"container.set_container_configuration(config) ;";
162
163
164             // instantiate a container on each server.
165
org.omg.Components.ConfigValue[] config = new org.omg.Components.ConfigValue[1];
166             config[0] = new org.objectweb.openccm.Components.ConfigValueImpl();
167             config[0].name = "container_script";
168             org.omg.CORBA.TypeCode JavaDoc string_tc =
169                 org.objectweb.openccm.corba.TheORB.getORB().get_primitive_tc(org.omg.CORBA.TCKind.tk_string);
170             org.omg.CORBA.Any JavaDoc value =
171                 org.objectweb.openccm.corba.TheDynamicAnyFactory.getFactory().
172                 create_dyn_any_from_type_code(string_tc).to_any();
173             value.insert_string(cont_config);
174             config[0].value = value;
175
176
177             org.omg.Components.Deployment.Container server1_cont =
178                 server1_cs.create_container(config);
179             org.omg.Components.Deployment.Container server2_cont =
180                 server2_cs.create_container(config);
181
182             // Install homes.
183
System.out.println("Instantiating homes...");
184
185             // define a container configuration.
186
config[0].name = "home_script";
187             String JavaDoc home_config =
188                 // set the container config as the home config
189
// i.e. an empty configuration.
190
"container.set_home_configuration(container.get_container_configuration()) ;" +
191                 // create a port specific config
192
"config = container.find_system_home(\"PortConfig\").create_component(JAVA.null) ;" +
193                 // set the component ::CORBA::Repository of the component.
194
"config.setComponentUId(\"IDL:ccm.objectweb.org/demo3/Client:1.0\") ;" +
195                 // set a list coordinator for the "the_service" receptacle of the "Client" component
196
"config.setCallCoordinator(\"the_service\", \"ListCoord\") ;" +
197                 // set a trace controller for the "the_service" receptacle of the "Client" component
198
"config.setCallControllers(\"the_service\", [ \"Trace\" ]) ;" +
199                 // create and set the trace configuration for this receptacle
200
"trace_config = PropertySet(\"trace\")" +
201                 "trace_config.add(StringProperty(\"filename\", \"./trace_Clients.txt\"))" +
202                 "trace_config.add(StringProperty(\"singleton\", \"the_service\"))" +
203                 "config.setCallControllersConfig(\"the_service\", [ trace_config ])" +
204                 // set the container config as the parent config.
205
"config.set_parent_configuration(container.get_container_configuration()) ;" +
206                 // set this config as the component config.
207
"container.set_component_configuration(config) ;";
208
209             // install the Client home with this container configuration.
210
value.insert_string(home_config);
211             config[0].value = value;
212
213             org.omg.Components.CCMHome h = null;
214             h = server1_cont.install_home("demo3",
215                                           "org.objectweb.ccm.demo3.monolithic.ClientHomeImpl.create_home",
216                                           config);
217             ClientHome ch = ClientHomeHelper.narrow(h);
218
219             // define a container configuration.
220
home_config =
221                 // set the container config as the home config
222
// i.e. an empty configuration.
223
"container.set_home_configuration(container.get_container_configuration()) ;" +
224                 // create a port specific config
225
"config = container.find_system_home(\"PortConfig\").create_component(JAVA.null) ;" +
226                 // set the component ::CORBA::Repository of the component.
227
"config.setComponentUId(\"IDL:ccm.objectweb.org/demo3/Server:1.0\") ;" +
228                 // set a list coordinator for the "the_service" facet of the "Server" component
229
"config.setCallCoordinator(\"the_service\", \"ListCoord\") ;" +
230                 // set a trace controller for the "the_service" facet of the "Server" component
231
"config.setCallControllers(\"the_service\", [ \"Trace\" ]) ;" +
232                 // create and set the trace configuration for this facet
233
"trace_config = PropertySet(\"trace\")" +
234                 "trace_config.add(StringProperty(\"filename\", \"./trace_Server_Service.txt\"))" +
235                 "config.setCallControllersConfig(\"the_service\", [ trace_config ])" +
236                 // set a list coordinator for the "to_consumers" event source of the "Server" component
237
"config.setCallCoordinator(\"to_consumers\", \"ListCoord\") ;" +
238                 // set a trace controller for the "to_consumers" event source of the "Server" component
239
"config.setCallControllers(\"to_consumers\", [ \"Trace\" ]) ;" +
240                 // create and set the trace configuration for this event source
241
"trace_config = PropertySet(\"trace\")" +
242                 "trace_config.add(StringProperty(\"filename\", \"./trace_Server_TextEvent.txt\"))" +
243                 "config.setCallControllersConfig(\"to_consumers\", [ trace_config ])" +
244                 // set the container config as the parent config.
245
"config.set_parent_configuration(container.get_container_configuration()) ;" +
246                 // set this config as the component config.
247
"container.set_component_configuration(config) ;";
248
249             // install the Server home with this container configuration.
250
value.insert_string(home_config);
251             config[0].value = value;
252
253             h = server2_cont.install_home("demo3",
254                                           "org.objectweb.ccm.demo3.monolithic.ServerHomeImpl.create_home",
255                                           config);
256             ServerHome sh = ServerHomeHelper.narrow(h);
257
258             // define a container configuration.
259
home_config =
260                 // set the container config as the home config
261
// i.e. an empty configuration.
262
"container.set_home_configuration(container.get_container_configuration()) ;" +
263                 // create a port specific config
264
"config = container.find_system_home(\"PortConfig\").create_component(JAVA.null) ;" +
265                 // set the component ::CORBA::Repository of the component.
266
"config.setComponentUId(\"IDL:ccm.objectweb.org/demo3/Consumer:1.0\") ;" +
267                 // set a list coordinator for the "from_servers" event sink of the "Consumer" component
268
"config.setCallCoordinator(\"from_servers\", \"ListCoord\") ;" +
269                 // set a trace controller for the "from_servers" event sink of the "Consumer" component
270
"config.setCallControllers(\"from_servers\", [ \"Trace\" ]) ;" +
271                 // create and set the trace configuration for this event sink
272
"trace_config = PropertySet(\"trace\")" +
273                 "trace_config.add(StringProperty(\"filename\", \"./trace_Consumers.txt\"))" +
274                 "trace_config.add(StringProperty(\"singleton\", \"from_servers\"))" +
275                 "config.setCallControllersConfig(\"from_servers\", [ trace_config ])" +
276                 // set the container config as the parent config.
277
"config.set_parent_configuration(container.get_container_configuration()) ;" +
278                 // set this config as the component config.
279
"container.set_component_configuration(config) ;";
280
281             // install the Server home with this container configuration.
282
value.insert_string(home_config);
283             config[0].value = value;
284
285             h = server1_cont.install_home("demo3",
286                                           "org.objectweb.ccm.demo3.monolithic.ConsumerHomeImpl.create_home",
287                                           config);
288             ConsumerHome csh = ConsumerHomeHelper.narrow(h);
289
290             // Create components.
291
System.out.println("Creating components...");
292             Server s = sh.create();
293             Client c1 = ch.create();
294             Client c2 = ch.create();
295             Client c3 = ch.create();
296             Consumer cs1 = csh.create();
297             Consumer cs2 = csh.create();
298             Consumer cs3 = csh.create();
299
300             // Configure components.
301
System.out.println("Configuring components...");
302             c1.name("Mathieu");
303             c2.name("Raphael");
304             c3.name("Philippe");
305             s.name("The Server");
306             cs1.name("Mathieu");
307             cs2.name("Raphael");
308             cs3.name("Philippe");
309
310             // Connecting clients and consumers to server.
311
System.out.println("Interconnecting components...");
312             Service the_service = s.provide_the_service();
313             c1.connect_the_service(the_service);
314             c2.connect_the_service(the_service);
315             c3.connect_the_service(the_service);
316             s.subscribe_to_consumers(cs1.get_consumer_from_servers());
317             s.subscribe_to_consumers(cs2.get_consumer_from_servers());
318             s.subscribe_to_consumers(cs3.get_consumer_from_servers());
319
320             // Configuration completion.
321
System.out.println("Configuration completion...");
322             cs1.configuration_complete();
323             cs2.configuration_complete();
324             cs3.configuration_complete();
325             s.configuration_complete();
326             c1.configuration_complete();
327             c2.configuration_complete();
328             c3.configuration_complete();
329
330         } catch (Exception JavaDoc e) {
331             if ((current!=null)&&(_OTS.equals("yes")))
332                 {
333                 System.out.println("Error during deployment :");
334                 e.printStackTrace();
335                 System.out.print("Rolling Back ... ");
336                 current.rollback();
337                 System.out.println("Done");
338                 // Force to exit.
339
System.exit(0);
340             }
341         }
342
343         if ((current!=null)&&(_OTS.equals("yes")))
344         {
345             System.out.print("Do you want to commit the Deployment ? [Y/n] ");
346
347             java.io.BufferedReader JavaDoc _buffer
348                 = new java.io.BufferedReader JavaDoc(new java.io.InputStreamReader JavaDoc(System.in));
349             char _answer = (char) _buffer.read();
350
351             if ((_answer=='n')||(_answer=='N'))
352             {
353                 System.out.print("Rolling Back ... ");
354                 current.rollback();
355                 System.out.println("Done");
356             } else {
357                 System.out.print("Committing ... ");
358                 current.commit(false);
359                 System.out.println("Done");
360             }
361         }
362
363         // force to exit
364
System.exit(0);
365         }
366 }
367
368
369
Popular Tags