KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > demo1 > cif > Demo1


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): Romain Rouvoy, Christophe Demarey, Christophe Contreras.
24
25 ====================================================================*/

26
27 package org.objectweb.ccm.demo1.cif;
28
29 import org.objectweb.ccm.demo1.*;
30
31 /**
32  * The demo1 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:Romain.Rouvoy@lifl.fr">Romain Rouvoy</A>
37  * <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</A>
38  */

39
40 public class Demo1
41 {
42     /**
43      * The bootstrap of the demo1 application.
44      */

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

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

72         org.omg.CosTransactions.Current current = null;
73         if (_OTS.equals("yes"))
74         {
75             System.out.println("Obtaining the Transaction Service...");
76             org.omg.CORBA.Object JavaDoc objOTS = orb.resolve_initial_references("TransactionCurrent");
77             current = org.omg.CosTransactions.CurrentHelper.narrow( objOTS );
78         }
79
80         try
81         {
82             if ((current!=null)&&(_OTS.equals("yes")))
83             {
84                 System.out.println("Status = "+current.get_status());
85                 System.out.println("Beginning the transaction...");
86                 current.begin();
87             }
88
89             // Obtain the component servers.
90
System.out.println("Obtaining Component Servers...");
91             org.omg.CosNaming.NameComponent JavaDoc[] ncomp = new org.omg.CosNaming.NameComponent JavaDoc[1];
92             ncomp[0] = new org.omg.CosNaming.NameComponent JavaDoc("ComponentServer1", "");
93             obj = nc.resolve(ncomp);
94             org.objectweb.openccm.Deployment.Server server1 = org.objectweb.openccm.Deployment.ServerHelper.narrow(obj);
95             ncomp[0].id = "ComponentServer2";
96             obj = nc.resolve(ncomp);
97             org.objectweb.openccm.Deployment.Server server2 = 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
System.out.println("Installing archives...");
107             // get the current directory and store it as a String
108
String JavaDoc demoPath = null;
109             try {
110                 demoPath = new java.io.File JavaDoc(".").getCanonicalPath()+ java.io.File.separator ;
111             }catch(Exception JavaDoc e) {
112                 e.printStackTrace();
113             }
114
115             server1_inst.install("demo1", "file:"+demoPath + "./archives/demo1.jar");
116             server2_inst.install("demo1", "file:"+demoPath + "./archives/demo1.jar");
117
118             // install some plugins already provided by OpenCCM.
119
server1_inst.install("openccm_plugins", "file:"+demoPath + "./archives/OpenCCM_Plugins.jar");
120             server2_inst.install("openccm_plugins", "file:"+demoPath + "./archives/OpenCCM_Plugins.jar");
121
122             // declaring the container types
123
//
124
// Uncomment this part for a minimal configuration
125
// and comment the following one.
126
//
127
/**
128             String cont_config =
129             "container = CONTAINER.container ; " +
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             "container.create_system_home("+
136             "\"openccm_plugins\", " +
137             "\"EmptyCoordinatorHome\", "+
138             "\"org.objectweb.openccm.Containers.Plugins.EmptyCoordinatorHome.create\") ;"+
139             "container.create_system_home("+
140             "\"openccm_plugins\", " +
141             "\"EmptyControllerHome\", "+
142             "\"org.objectweb.openccm.Containers.Plugins.EmptyControllerHome.create\") ;"+
143             "container.set_container_configuration(config) ;";
144             **/

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

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

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