KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > demo1 > monolithic > 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 Contreras.
24
25 ====================================================================*/

26
27 package org.objectweb.ccm.demo1.monolithic;
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  */

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

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

57        // Init the OpenCCM Components Runtime.
58
org.omg.CORBA.ORB JavaDoc orb = org.objectweb.openccm.Components.Runtime.init(args);
59
60        // Obtain the Name Service.
61
System.out.println("Obtaining the Name Service...");
62        org.omg.CORBA.Object JavaDoc obj = orb.resolve_initial_references("NameService");
63        org.omg.CosNaming.NamingContext JavaDoc nc = org.omg.CosNaming.NamingContextHelper.narrow(obj);
64        
65 // String[] ids = orb.list_initial_services();
66
// System.out.println("R‰f‰rences initiales disponibles : ");
67
// for (int i=0;i<ids.length;i++)
68
// System.out.println("-> "+ids[i]);
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("Status = "+current.get_status());
83                  System.out.println("Beginning the transaction...");
84                  current.begin();
85               }
86             
87             // Obtain the component servers.
88
System.out.println("Obtaining Component Servers...");
89             org.omg.CosNaming.NameComponent JavaDoc[] ncomp = 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 = org.objectweb.openccm.Deployment.ServerHelper.narrow(obj);
93             ncomp[0].id = "ComponentServer2";
94             obj = nc.resolve(ncomp);
95             org.objectweb.openccm.Deployment.Server server2 = org.objectweb.openccm.Deployment.ServerHelper.narrow(obj);
96
97             // Obtain the container homes and archive installators.
98
org.omg.Components.Deployment.ComponentServer server1_cs = server1.provide_component_server();
99             org.omg.Components.Deployment.ComponentInstallation server1_inst = server1.provide_install();
100             org.omg.Components.Deployment.ComponentServer server2_cs = server2.provide_component_server();
101             org.omg.Components.Deployment.ComponentInstallation server2_inst = server2.provide_install();
102
103             // Install archives.
104
System.out.println("Installing archives...");
105             // get the current directory and store it as a String
106
String JavaDoc demoPath = null;
107             try {
108                 demoPath = new java.io.File JavaDoc(".").getCanonicalPath()+ java.io.File.separator ;
109             }catch(Exception JavaDoc e) {
110                 e.printStackTrace();
111             }
112
113             server1_inst.install("demo1","file:"+ demoPath + "./archives/demo1.jar");
114             server2_inst.install("demo1", "file:"+demoPath + "./archives/demo1.jar");
115
116             // install some plugins already provided by OpenCCM.
117
server1_inst.install("openccm_plugins","file:"+ demoPath + "./archives/OpenCCM_Plugins.jar");
118             server2_inst.install("openccm_plugins","file:"+ demoPath + "./archives/OpenCCM_Plugins.jar");
119
120             // declaring the container types
121
//
122
// Uncomment this part for a minimal configuration
123
// and comment the following one.
124
//
125
/**
126              String cont_config =
127              "container = CONTAINER.container ; " +
128              "config_home = container.create_system_home("+
129              "\"openccm_plugins\", " +
130              "\"EmptyConfig\", "+
131              "\"org.objectweb.openccm.Containers.Plugins.EmptyConfigurationHome.create\") ;"+
132              "config = config_home.create_component(JAVA.null) ;" +
133              "container.create_system_home("+
134              "\"openccm_plugins\", " +
135              "\"EmptyCoordinatorHome\", "+
136              "\"org.objectweb.openccm.Containers.Plugins.EmptyCoordinatorHome.create\") ;"+
137              "container.create_system_home("+
138              "\"openccm_plugins\", " +
139              "\"EmptyControllerHome\", "+
140              "\"org.objectweb.openccm.Containers.Plugins.EmptyControllerHome.create\") ;"+
141              "container.set_container_configuration(config) ;";
142              **/

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

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

258
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/demo1/Client:1.0\") ;" +
267               // set a list coordinator for the "to_server" receptacle of the "Client" component
268
"config.setCallCoordinator(\"to_server\", \"ListCoord\") ;" +
269               // set a trace controller for the "to_server" receptacle of the "Client" component
270
"config.setCallControllers(\"to_server\", [ \"Trace\" ]) ;" +
271               // create and set the trace configuration for this receptacle
272
"trace_config = PropertySet(\"trace\")" +
273               "trace_config.add(StringProperty(\"filename\", \"./trace_Clients.txt\"))" +
274               "trace_config.add(StringProperty(\"singleton\", \"to_server\"))" +
275               "config.setCallControllersConfig(\"to_server\", [ 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 Client home with this container configuration.
282
value.insert_string(home_config);
283             config[0].value = value;
284
285             // h = server2_cont.install_home("demo1", "org.objectweb.ccm.demo1.monolithic.ClientHomeImpl.create_home", config);
286
h = server2_cont.install_home("demo1", "org.objectweb.ccm.demo1.monolithic.ClientHomeImpl.create_home", new org.omg.Components.ConfigValue[0]);
287             ClientHome ch = ClientHomeHelper.narrow(h);
288
289             
290             // Create components.
291
System.out.println("Instantiating components...");
292             Server s = sh.create();
293             Client c1 = ch.create();
294             Client c2 = ch.create();
295             Client c3 = ch.create();
296             
297             // Configure components.
298
System.out.println("Configuring components...");
299             s.the_name("The Server");
300             c1.the_name("Mathieu");
301             c2.the_name("Raphael");
302             c3.the_name("Philippe");
303             
304             // Connect clients and server.
305
System.out.println("Interconnecting components...");
306             Display for_clients = s.provide_for_clients();
307             c1.connect_to_server(for_clients);
308             c2.connect_to_server(for_clients);
309             c3.connect_to_server(for_clients);
310             
311             // Configuration completion.
312
System.out.println("Configuration completion...");
313             s.configuration_complete();
314             c1.configuration_complete();
315             c2.configuration_complete();
316             c3.configuration_complete();
317             
318          } catch (Exception JavaDoc e) {
319             if ((current!=null)&&(_OTS.equals("yes")))
320               {
321                  System.out.println("Error during deployment :");
322                  e.printStackTrace();
323                  System.out.print("Rolling Back ... ");
324                  current.rollback();
325                  System.out.println("Done");
326                  // Force to exit.
327
System.exit(0);
328               }
329          }
330        
331        if ((current!=null)&&(_OTS.equals("yes")))
332          {
333             System.out.print("Do you want to commit the Deployment ? [Y/n] ");
334             
335             java.io.BufferedReader JavaDoc _buffer
336               = new java.io.BufferedReader JavaDoc(new java.io.InputStreamReader JavaDoc(System.in));
337             char _answer = (char) _buffer.read();
338             
339             if ((_answer=='n')||(_answer=='N'))
340               {
341                  System.out.print("Rolling Back ... ");
342                  current.rollback();
343                  System.out.println("Done");
344               } else {
345                  System.out.print("Committing ... ");
346                  current.commit(false);
347                  System.out.println("Done");
348               }
349          }
350        
351        // Force to exit.
352
System.exit(0);
353     }
354 }
355
356
357
358
359
360
361
362
Popular Tags