KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > demo2 > monolithic > Demo2


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.demo2.monolithic;
28
29 import org.objectweb.ccm.demo2.*;
30
31 /**
32  * The demo2 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 Demo2
39 {
40     /**
41      * The bootstrap of the demo2 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 lines:
54
// org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
55
// org.objectweb.openccm.Components.Runtime.init(orb);
56
// are replaced by the next line.
57

58         // Init the OpenCCM Components Runtime.
59
org.omg.CORBA.ORB JavaDoc orb =
60             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 =
65             orb.resolve_initial_references("NameService");
66         org.omg.CosNaming.NamingContext JavaDoc nc =
67             org.omg.CosNaming.NamingContextHelper.narrow(obj);
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("Beginning the transaction...");
82                 current.begin();
83             }
84
85             // Obtain the component servers.
86
System.out.println("Obtaining Component Servers...");
87             org.omg.CosNaming.NameComponent JavaDoc[] ncomp = new org.omg.CosNaming.NameComponent JavaDoc[1];
88             ncomp[0] = new org.omg.CosNaming.NameComponent JavaDoc("ComponentServer1", "");
89             obj = nc.resolve(ncomp);
90             org.objectweb.openccm.Deployment.Server server1 = org.objectweb.openccm.Deployment.ServerHelper.narrow(obj);
91             ncomp[0].id = "ComponentServer2";
92             obj = nc.resolve(ncomp);
93             org.objectweb.openccm.Deployment.Server server2 = org.objectweb.openccm.Deployment.ServerHelper.narrow(obj);
94
95             // Obtain the container homes and archive installators.
96
org.omg.Components.Deployment.ComponentServer server1_cs = server1.provide_component_server();
97             org.omg.Components.Deployment.ComponentInstallation server1_inst = server1.provide_install();
98             org.omg.Components.Deployment.ComponentServer server2_cs = server2.provide_component_server();
99             org.omg.Components.Deployment.ComponentInstallation server2_inst = server2.provide_install();
100
101             // Install archives.
102
System.out.println("Installing archives...");
103             // get the current directory and store it as a String
104
String JavaDoc demoPath = null;
105             try {
106                 demoPath = new java.io.File JavaDoc(".").getCanonicalPath()+ java.io.File.separator ;
107             }catch(Exception JavaDoc e) {
108                 e.printStackTrace();
109             }
110
111             server1_inst.install("demo2","file:"+ demoPath + "./archives/demo2.jar");
112             server2_inst.install("demo2","file:"+ demoPath + "./archives/demo2.jar");
113
114             // install some plugins already provided by OpenCCM.
115
server1_inst.install("openccm_plugins","file:"+ demoPath + "./archives/OpenCCM_Plugins.jar");
116             server2_inst.install("openccm_plugins","file:"+ demoPath + "./archives/OpenCCM_Plugins.jar");
117
118             // declaring the container types
119
String JavaDoc cont_config =
120                 // retrieve the container reference.
121
"container = CONTAINER.container ; " +
122                 // create aliases on PropertySetImpl and StringPropertyImpl class.
123
"JAVA.declare(\"org.objectweb.openccm.Containers\") ; " +
124                 "PropertySet = org.objectweb.openccm.Containers.PropertySetImpl ;" +
125                 "StringProperty = org.objectweb.openccm.Containers.StringPropertyImpl ;" +
126                 // create an empty configuration with it's alias.
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                 // create an empty coordinator system home with it's alias
133
"container.create_system_home("+
134                 "\"openccm_plugins\", " +
135                 "\"EmptyCoordinatorHome\", "+
136                 "\"org.objectweb.openccm.Containers.Plugins.EmptyCoordinatorHome.create\") ;"+
137                 // create an empty controller system home with it's alias
138
"container.create_system_home("+
139                 "\"openccm_plugins\", " +
140                 "\"EmptyControllerHome\", "+
141                 "\"org.objectweb.openccm.Containers.Plugins.EmptyControllerHome.create\") ;"+
142                 // create a trace controller system home with it's alias.
143
"container.create_system_home("+
144                 "\"openccm_plugins\", " +
145                 "\"Trace\", " +
146                 "\"org.objectweb.openccm.Containers.Plugins.TraceControllerHome.create\") ; "+
147                 // create a list coordinator system home with it's alias.
148
"container.create_system_home("+
149                 "\"openccm_plugins\", " +
150                 "\"ListCoord\", " +
151                 "\"org.objectweb.openccm.Containers.Plugins.ListCoordinatorHome.create\") ; "+
152                 // create an port specific config.
153
"container.create_system_home("+
154                 "\"openccm_plugins\", " +
155                 "\"PortConfig\", " +
156                 "\"org.objectweb.openccm.Containers.Plugins.PortSpecificConfigurationHome.create\") ; " +
157                 // set this configuration as the container default configuration.
158
"container.set_container_configuration(config) ;";
159
160
161             // instantiate a container on each server.
162
org.omg.Components.ConfigValue[] config = new org.omg.Components.ConfigValue[1];
163             config[0] = new org.objectweb.openccm.Components.ConfigValueImpl();
164             config[0].name = "container_script";
165             org.omg.CORBA.TypeCode JavaDoc string_tc =
166                 org.objectweb.openccm.corba.TheORB.getORB().get_primitive_tc(org.omg.CORBA.TCKind.tk_string);
167             org.omg.CORBA.Any JavaDoc value =
168                 org.objectweb.openccm.corba.TheDynamicAnyFactory.getFactory().
169                 create_dyn_any_from_type_code(string_tc).to_any();
170             value.insert_string(cont_config);
171             config[0].value = value;
172
173             org.omg.Components.Deployment.Container server1_cont =
174                 server1_cs.create_container(config);
175             org.omg.Components.Deployment.Container server2_cont =
176                 server2_cs.create_container(config);
177
178
179             // Install homes.
180
System.out.println("Instantiating homes...");
181
182             // define a container configuration.
183
config[0].name = "home_script";
184             String JavaDoc home_config =
185                 // set the container config as the home config
186
// i.e. an empty configuration.
187
"container.set_home_configuration(container.get_container_configuration()) ;" +
188                 // create a port specific config
189
"config = container.find_system_home(\"PortConfig\").create_component(JAVA.null) ;" +
190                 // set the component ::CORBA::Repository of the component.
191
"config.setComponentUId(\"IDL:ccm.objectweb.org/demo2/Producer:1.0\") ;" +
192                 // set a list coordinator for the "crop" event source of the "Producer" component
193
"config.setCallCoordinator(\"crop\", \"ListCoord\") ;" +
194                 // set a trace controller for the "crop" event source of the "Producer" component
195
"config.setCallControllers(\"crop\", [ \"Trace\" ]) ;" +
196                 // create and set the trace configuration for this event source
197
"trace_config = PropertySet(\"trace\")" +
198                 "trace_config.add(StringProperty(\"filename\", \"./trace_Producer.txt\"))" +
199                 "config.setCallControllersConfig(\"crop\", [ trace_config ])" +
200                 // set the container config as the parent config.
201
"config.set_parent_configuration(container.get_container_configuration()) ;" +
202                 // set this config as the component config.
203
"container.set_component_configuration(config) ;";
204
205             // install the Producer home with this container configuration.
206
value.insert_string(home_config);
207             config[0].value = value;
208
209             org.omg.Components.CCMHome h =
210                 server2_cont.install_home("demo2",
211                                           "org.objectweb.ccm.demo2.monolithic.ProducerHomeImpl.create_home",
212                                           config);
213             ProducerHome ph = ProducerHomeHelper.narrow(h);
214
215             // define a container configuration.
216
home_config =
217                 // set the container config as the home config
218
// i.e. an empty configuration.
219
"container.set_home_configuration(container.get_container_configuration()) ;" +
220                 // create a port specific config
221
"config = container.find_system_home(\"PortConfig\").create_component(JAVA.null) ;" +
222                 // set the component ::CORBA::Repository of the component.
223
"config.setComponentUId(\"IDL:ccm.objectweb.org/demo2/Consumer:1.0\") ;" +
224                 // set a list coordinator for the "crop" event source of the "Producer" component
225
"config.setCallCoordinator(\"eat\", \"ListCoord\") ;" +
226                 // set a trace controller for the "crop" event source of the "Producer" component
227
"config.setCallControllers(\"eat\", [ \"Trace\" ]) ;" +
228                 // create and set the trace configuration for this event source
229
"trace_config = PropertySet(\"trace\")" +
230                 "trace_config.add(StringProperty(\"filename\", \"./trace_Consumers.txt\"))" +
231                 "trace_config.add(StringProperty(\"singleton\", \"eat\"))" +
232                 "config.setCallControllersConfig(\"eat\", [ trace_config ])" +
233                 // set the container config as the parent config.
234
"config.set_parent_configuration(container.get_container_configuration()) ;" +
235                 // set this config as the component config.
236
"container.set_component_configuration(config) ;";
237
238             // install the Producer home with this container configuration.
239
value.insert_string(home_config);
240             config[0].value = value;
241
242             h = server1_cont.install_home("demo2",
243                                           "org.objectweb.ccm.demo2.monolithic.ConsumerHomeImpl.create_home",
244                                           config);
245             ConsumerHome ch = ConsumerHomeHelper.narrow(h);
246
247
248             // Create components.
249
System.out.println("Instantiating components...");
250             Consumer c1 = ch.create();
251             Consumer c2 = ch.create();
252             Consumer c3 = ch.create();
253             Producer p = ph.create();
254
255             // Configuring components.
256
System.out.println("Configuring components...");
257             p.name("The Producer");
258             c1.name("Mathieu");
259             c2.name("Raphael");
260             c3.name("Philippe");
261
262             // Connect consumers and producer.
263
System.out.println("Interconnecting components...");
264             p.subscribe_crop(c1.get_consumer_eat());
265             p.subscribe_crop(c2.get_consumer_eat());
266             p.subscribe_crop(c3.get_consumer_eat());
267
268             // Configuration completion.
269
System.out.println("Configuration completion...");
270             c1.configuration_complete();
271             c2.configuration_complete();
272             c3.configuration_complete();
273             p.configuration_complete();
274
275         } catch (Exception JavaDoc e) {
276             if ((current!=null)&&(_OTS.equals("yes")))
277             {
278                 System.out.println("Error during deployment :");
279                 e.printStackTrace();
280                 System.out.print("Rolling Back ... ");
281                 current.rollback();
282                 System.out.println("Done");
283                 // Force to exit.
284
System.exit(0);
285             }
286         }
287
288         if ((current!=null)&&(_OTS.equals("yes")))
289         {
290             System.out.print("Do you want to commit the Deployment ? [Y/n] ");
291
292             java.io.BufferedReader JavaDoc _buffer
293                 = new java.io.BufferedReader JavaDoc(new java.io.InputStreamReader JavaDoc(System.in));
294             char _answer = (char) _buffer.read();
295
296             if ((_answer=='n')||(_answer=='N'))
297             {
298                 System.out.print("Rolling Back ... ");
299                 current.rollback();
300                 System.out.println("Done");
301             } else {
302                 System.out.print("Committing ... ");
303                 current.commit(false);
304                 System.out.println("Done");
305             }
306         }
307
308         // Force to exit.
309
System.exit(0);
310     }
311 }
312
313
Popular Tags