KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2002 USTL - LIFL - GOAL
5 Contact: openccm-team@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.cif;
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  * <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
37  */

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