KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > demo3 > cif > 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.cif;
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             // instantiate a container on each server.
164
org.omg.Components.ConfigValue[] config = new org.omg.Components.ConfigValue[1];
165             config[0] = new org.objectweb.openccm.Components.ConfigValueImpl();
166             config[0].name = "container_script";
167             org.omg.CORBA.TypeCode JavaDoc string_tc =
168                 org.objectweb.openccm.corba.TheORB.getORB().get_primitive_tc(org.omg.CORBA.TCKind.tk_string);
169             org.omg.CORBA.Any JavaDoc value =
170                 org.objectweb.openccm.corba.TheDynamicAnyFactory.getFactory().
171                 create_dyn_any_from_type_code(string_tc).to_any();
172             value.insert_string(cont_config);
173             config[0].value = value;
174
175
176             org.omg.Components.Deployment.Container server1_cont =
177                 server1_cs.create_container(config);
178             org.omg.Components.Deployment.Container server2_cont =
179                 server2_cs.create_container(config);
180
181             // Install homes.
182
System.out.println("Instantiating homes...");
183
184             // define a container configuration.
185
config[0].name = "home_script";
186             String JavaDoc home_config =
187                 // set the container config as the home config
188
// i.e. an empty configuration.
189
"container.set_home_configuration(container.get_container_configuration()) ;" +
190                 // create a port specific config
191
"config = container.find_system_home(\"PortConfig\").create_component(JAVA.null) ;" +
192                 // set the component ::CORBA::Repository of the component.
193
"config.setComponentUId(\"IDL:ccm.objectweb.org/demo3/Client:1.0\") ;" +
194                 // set a list coordinator for the "the_service" receptacle of the "Client" component
195
"config.setCallCoordinator(\"the_service\", \"ListCoord\") ;" +
196                 // set a trace controller for the "the_service" receptacle of the "Client" component
197
"config.setCallControllers(\"the_service\", [ \"Trace\" ]) ;" +
198                 // create and set the trace configuration for this receptacle
199
"trace_config = PropertySet(\"trace\")" +
200                 "trace_config.add(StringProperty(\"filename\", \"./trace_Clients.txt\"))" +
201                 "trace_config.add(StringProperty(\"singleton\", \"the_service\"))" +
202                 "config.setCallControllersConfig(\"the_service\", [ trace_config ])" +
203                 // set the container config as the parent config.
204
"config.set_parent_configuration(container.get_container_configuration()) ;" +
205                 // set this config as the component config.
206
"container.set_component_configuration(config) ;";
207
208             // install the Client home with this container configuration.
209
value.insert_string(home_config);
210             config[0].value = value;
211
212             org.omg.Components.CCMHome h = null;
213             h = server1_cont.install_home("demo3",
214                                           "org.objectweb.ccm.demo3.cif.ClientHomeImpl.create_home",
215                                           config);
216             ClientHome ch = ClientHomeHelper.narrow(h);
217
218             // define a container configuration.
219
home_config =
220                 // set the container config as the home config
221
// i.e. an empty configuration.
222
"container.set_home_configuration(container.get_container_configuration()) ;" +
223                 // create a port specific config
224
"config = container.find_system_home(\"PortConfig\").create_component(JAVA.null) ;" +
225                 // set the component ::CORBA::Repository of the component.
226
"config.setComponentUId(\"IDL:ccm.objectweb.org/demo3/Server:1.0\") ;" +
227                 // set a list coordinator for the "the_service" facet of the "Server" component
228
"config.setCallCoordinator(\"the_service\", \"ListCoord\") ;" +
229                 // set a trace controller for the "the_service" facet of the "Server" component
230
"config.setCallControllers(\"the_service\", [ \"Trace\" ]) ;" +
231                 // create and set the trace configuration for this facet
232
"trace_config = PropertySet(\"trace\")" +
233                 "trace_config.add(StringProperty(\"filename\", \"./trace_Server_Service.txt\"))" +
234                 "config.setCallControllersConfig(\"the_service\", [ trace_config ])" +
235                 // set a list coordinator for the "to_consumers" event source of the "Server" component
236
"config.setCallCoordinator(\"to_consumers\", \"ListCoord\") ;" +
237                 // set a trace controller for the "to_consumers" event source of the "Server" component
238
"config.setCallControllers(\"to_consumers\", [ \"Trace\" ]) ;" +
239                 // create and set the trace configuration for this event source
240
"trace_config = PropertySet(\"trace\")" +
241                 "trace_config.add(StringProperty(\"filename\", \"./trace_Server_TextEvent.txt\"))" +
242                 "config.setCallControllersConfig(\"to_consumers\", [ trace_config ])" +
243                 // set the container config as the parent config.
244
"config.set_parent_configuration(container.get_container_configuration()) ;" +
245                 // set this config as the component config.
246
"container.set_component_configuration(config) ;";
247
248             // install the Server home with this container configuration.
249
value.insert_string(home_config);
250             config[0].value = value;
251
252             h = server2_cont.install_home("demo3",
253                                           "org.objectweb.ccm.demo3.cif.ServerHomeImpl.create_home",
254                                           config);
255             ServerHome sh = ServerHomeHelper.narrow(h);
256
257             // define a container configuration.
258
home_config =
259                 // set the container config as the home config
260
// i.e. an empty configuration.
261
"container.set_home_configuration(container.get_container_configuration()) ;" +
262                 // create a port specific config
263
"config = container.find_system_home(\"PortConfig\").create_component(JAVA.null) ;" +
264                 // set the component ::CORBA::Repository of the component.
265
"config.setComponentUId(\"IDL:ccm.objectweb.org/demo3/Consumer:1.0\") ;" +
266                 // set a list coordinator for the "from_servers" event sink of the "Consumer" component
267
"config.setCallCoordinator(\"from_servers\", \"ListCoord\") ;" +
268                 // set a trace controller for the "from_servers" event sink of the "Consumer" component
269
"config.setCallControllers(\"from_servers\", [ \"Trace\" ]) ;" +
270                 // create and set the trace configuration for this event sink
271
"trace_config = PropertySet(\"trace\")" +
272                 "trace_config.add(StringProperty(\"filename\", \"./trace_Consumers.txt\"))" +
273                 "trace_config.add(StringProperty(\"singleton\", \"from_servers\"))" +
274                 "config.setCallControllersConfig(\"from_servers\", [ trace_config ])" +
275                 // set the container config as the parent config.
276
"config.set_parent_configuration(container.get_container_configuration()) ;" +
277                 // set this config as the component config.
278
"container.set_component_configuration(config) ;";
279
280             // install the Server home with this container configuration.
281
value.insert_string(home_config);
282             config[0].value = value;
283
284             h = server1_cont.install_home("demo3",
285                                           "org.objectweb.ccm.demo3.cif.ConsumerHomeImpl.create_home",
286                                           config);
287             ConsumerHome csh = ConsumerHomeHelper.narrow(h);
288
289             // Create components.
290
System.out.println("Creating components...");
291             Server s = sh.create();
292             Client c1 = ch.create();
293             Client c2 = ch.create();
294             Client c3 = ch.create();
295             Consumer cs1 = csh.create();
296             Consumer cs2 = csh.create();
297             Consumer cs3 = csh.create();
298
299             // Configure components.
300
System.out.println("Configuring components...");
301             c1.name("Romain");
302             c2.name("Christophe");
303             c3.name("Philippe");
304             s.name("The Server");
305             cs1.name("Romain");
306             cs2.name("Christophe");
307             cs3.name("Philippe");
308
309             // Connecting clients and consumers to server.
310
System.out.println("Interconnecting components...");
311             Service the_service = s.provide_the_service();
312             c1.connect_the_service(the_service);
313             c2.connect_the_service(the_service);
314             c3.connect_the_service(the_service);
315             s.subscribe_to_consumers(cs1.get_consumer_from_servers());
316             s.subscribe_to_consumers(cs2.get_consumer_from_servers());
317             s.subscribe_to_consumers(cs3.get_consumer_from_servers());
318
319             // Configuration completion.
320
System.out.println("Configuration completion...");
321             cs1.configuration_complete();
322             cs2.configuration_complete();
323             cs3.configuration_complete();
324             s.configuration_complete();
325             c1.configuration_complete();
326             c2.configuration_complete();
327             c3.configuration_complete();
328
329         } catch (Exception JavaDoc e) {
330             if ((current!=null)&&(_OTS.equals("yes")))
331                 {
332                 System.out.println("Error during deployment :");
333                 e.printStackTrace();
334                 System.out.print("Rolling Back ... ");
335                 current.rollback();
336                 System.out.println("Done");
337                 // Force to exit.
338
System.exit(0);
339             }
340         }
341
342         if ((current!=null)&&(_OTS.equals("yes")))
343         {
344             System.out.print("Do you want to commit the Deployment ? [Y/n] ");
345
346             java.io.BufferedReader JavaDoc _buffer
347                 = new java.io.BufferedReader JavaDoc(new java.io.InputStreamReader JavaDoc(System.in));
348             char _answer = (char) _buffer.read();
349
350             if ((_answer=='n')||(_answer=='N'))
351             {
352                 System.out.print("Rolling Back ... ");
353                 current.rollback();
354                 System.out.println("Done");
355             } else {
356                 System.out.print("Committing ... ");
357                 current.commit(false);
358                 System.out.println("Done");
359             }
360         }
361
362         // force to exit
363
System.exit(0);
364         }
365 }
366
367
368
Popular Tags