KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DiningPhilosophers > monolithic > Dinner


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, Sylvain Leblanc.
23 Contributor(s): Christophe Demarey, Christophe Contreras.
24
25 ====================================================================*/

26
27 package DiningPhilosophers.monolithic;
28
29 import DiningPhilosophers.*;
30
31 /**
32  * The philosopher dinner application deployment.
33  *
34  * <p>
35  * This application is composed of four philosopher
36  * components, four fork manager components and one observer
37  * component distributed on two servers.
38  * These servers must be named "ComponentServer1" and "ComponentServer2".
39  * The OpenCCM_plugins.jar archive must be
40  * in the current directory and DinningPhilosophers.jar archive
41  * must be in <code>./archives</code> directory.
42  * </p>
43  *
44  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
45  * @author <a HREF="mailto:Sylvain.Leblanc@lifl.fr">Sylvain Leblanc</a>
46  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
47  *
48  * @version 2.0
49  */

50
51 public class Dinner
52 {
53     /**
54      * The bootstrap of the dinner application.
55      */

56     public static void
57     main(String JavaDoc[] args)
58     throws Exception JavaDoc
59     {
60         String JavaDoc _OTS = System.getProperties().getProperty("TRANSACTIONAL_PLUGIN","no").toLowerCase();
61
62         // Init the ORB.
63
System.out.println("Initializing the ORB...");
64
65         // TODO: Need to report this ORB.init() problem to OpenORB developers!
66
// Due to an OpenORB problem, the following lines:
67
// org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
68
// fr.lifl.goal.OpenCCM.Components.Runtime.init(orb);
69
// are replaced by the next line.
70

71         // Init the OpenCCM Components Runtime.
72
org.omg.CORBA.ORB JavaDoc orb =
73             org.objectweb.openccm.Components.Runtime.init(args);
74
75         // Obtain the Name Service.
76
System.out.println("Obtaining the Name Service...");
77         org.omg.CORBA.Object JavaDoc obj =
78             orb.resolve_initial_references("NameService");
79         org.omg.CosNaming.NamingContext JavaDoc nc =
80             org.omg.CosNaming.NamingContextHelper.narrow(obj);
81
82         org.omg.CosTransactions.Current current = null;
83         if (_OTS.equals("yes"))
84         {
85             System.out.println("Obtaining the Transaction Service...");
86             org.omg.CORBA.Object JavaDoc objOTS = orb.resolve_initial_references("TransactionCurrent");
87             current = org.omg.CosTransactions.CurrentHelper.narrow( objOTS );
88         }
89
90         try
91         {
92             if ((current!=null)&&(_OTS.equals("yes")))
93             {
94                 System.out.println("Beginning the transaction...");
95                 current.begin();
96             }
97
98             // Obtain the component servers.
99
System.out.println("Obtaining Component Servers...");
100             org.omg.CosNaming.NameComponent JavaDoc[] ncomp =
101                 new org.omg.CosNaming.NameComponent JavaDoc[1];
102             ncomp[0] = new org.omg.CosNaming.NameComponent JavaDoc("ComponentServer1", "");
103             obj = nc.resolve(ncomp);
104             org.objectweb.openccm.Deployment.Server server1 =
105                 org.objectweb.openccm.Deployment.ServerHelper.narrow(obj);
106             ncomp[0].id = "ComponentServer2";
107             obj = nc.resolve(ncomp);
108             org.objectweb.openccm.Deployment.Server server2 =
109                 org.objectweb.openccm.Deployment.ServerHelper.narrow(obj);
110
111             // Obtain the home factories and archive installators.
112
org.omg.Components.Deployment.ComponentServer server1_cs =
113                 server1.provide_component_server();
114             org.omg.Components.Deployment.ComponentInstallation server1_inst =
115                 server1.provide_install();
116             org.omg.Components.Deployment.ComponentServer server2_cs =
117                 server2.provide_component_server();
118             org.omg.Components.Deployment.ComponentInstallation server2_inst =
119                 server2.provide_install();
120
121             // Install archives.
122
System.out.println("Installing archives...");
123             // get the current directory and store it as a String
124
String JavaDoc demoPath = null;
125             try {
126                 demoPath = new java.io.File JavaDoc(".").getCanonicalPath()+ java.io.File.separator ;
127             }catch(Exception JavaDoc e) {
128                 e.printStackTrace();
129             }
130
131             server1_inst.install("dinner","file:"+ demoPath + "./archives/DiningPhilosophers.jar");
132             server2_inst.install("dinner","file:"+ demoPath + "./archives/DiningPhilosophers.jar");
133
134             // install some plugins already provided by OpenCCM.
135
server1_inst.install("openccm_plugins","file:"+ demoPath + "./archives/OpenCCM_Plugins.jar");
136             server2_inst.install("openccm_plugins","file:"+ demoPath + "./archives/OpenCCM_Plugins.jar");
137
138             // declaring the container types
139
String JavaDoc cont_config =
140                 "container = CONTAINER.container ; " +
141                 "config_home = container.create_system_home("+
142                 "\"openccm_plugins\", " +
143                 "\"EmptyConfig\", "+
144                 "\"org.objectweb.openccm.Containers.Plugins.EmptyConfigurationHome.create\") ;"+
145                 "config = config_home.create_component(JAVA.null) ;" +
146                 "container.create_system_home("+
147                 "\"openccm_plugins\", " +
148                 "\"EmptyCoordinatorHome\", "+
149                 "\"org.objectweb.openccm.Containers.Plugins.EmptyCoordinatorHome.create\") ;"+
150                 "container.create_system_home("+
151                 "\"openccm_plugins\", " +
152                 "\"EmptyControllerHome\", "+
153                 "\"org.objectweb.openccm.Containers.Plugins.EmptyControllerHome.create\") ;"+
154                 "container.set_container_configuration(config) ;";
155
156             // instantiate a container on each server.
157
org.omg.Components.ConfigValue[] config = new org.omg.Components.ConfigValue[1];
158             config[0] = new org.objectweb.openccm.Components.ConfigValueImpl();
159             config[0].name = "container_script";
160             org.omg.CORBA.TypeCode JavaDoc string_tc =
161                 org.objectweb.openccm.corba.TheORB.getORB().get_primitive_tc(org.omg.CORBA.TCKind.tk_string);
162             org.omg.CORBA.Any JavaDoc value =
163                 org.objectweb.openccm.corba.TheDynamicAnyFactory.getFactory().
164                 create_dyn_any_from_type_code(string_tc).to_any();
165             value.insert_string(cont_config);
166             config[0].value = value;
167
168
169             org.omg.Components.Deployment.Container server1_cont =
170                 server1_cs.create_container(config);
171             org.omg.Components.Deployment.Container server2_cont =
172                 server2_cs.create_container(config);
173
174             // Create homes.
175
System.out.println("Instantiating homes...");
176
177             // define a container configuration.
178
java.lang.String JavaDoc home_config =
179                 "container.set_home_configuration(container.get_container_configuration()) ;" +
180                 "container.set_component_configuration(container.get_container_configuration()) ;";
181
182             config[0].name = "home_script";
183             value.insert_string(home_config);
184             config[0].value = value;
185
186             // install homes with this container configuration.
187
org.omg.Components.CCMHome h =
188                     server1_cont.install_home("dinner",
189                                               "DiningPhilosophers.monolithic.PhilosopherHomeImpl.create_home",
190                                               config);
191             PhilosopherHome ph = PhilosopherHomeHelper.narrow(h);
192
193             h = server2_cont.install_home("dinner",
194                                           "DiningPhilosophers.monolithic.ForkHomeImpl.create_home",
195                                           config);
196             ForkHome fh = ForkHomeHelper.narrow(h);
197             h = server2_cont.install_home("dinner",
198                                           "DiningPhilosophers.monolithic.ObserverHomeImpl.create_home",
199                                           config);
200             ObserverHome oh = ObserverHomeHelper.narrow(h);
201
202             // Create components.
203
System.out.println("Creating components...");
204             Philosopher p1 = ph._new("Mathieu");
205             Philosopher p2 = ph._new("Raphael");
206             Philosopher p3 = ph._new("Philippe");
207             Philosopher p4 = ph._new("Sylvain");
208             ForkManager f1 = fh.create();
209             ForkManager f2 = fh.create();
210             ForkManager f3 = fh.create();
211             ForkManager f4 = fh.create();
212             Observer o = oh.create();
213
214             // Configure components.
215
System.out.println("Configuring components...");
216
217             // Connecting components.
218
System.out.println("Interconnecting components...");
219
220             Fork fork = f1.provide_the_fork();
221             p1.connect_right(fork);
222             p2.connect_left(fork);
223
224             fork = f2.provide_the_fork();
225             p2.connect_right(fork);
226             p3.connect_left(fork);
227
228             fork = f3.provide_the_fork();
229             p3.connect_right(fork);
230             p4.connect_left(fork);
231
232             fork = f4.provide_the_fork();
233             p4.connect_right(fork);
234             p1.connect_left(fork);
235
236             StatusInfoConsumer consumer = o.get_consumer_info();
237             p1.subscribe_info(consumer);
238             p2.subscribe_info(consumer);
239             p3.subscribe_info(consumer);
240             p4.subscribe_info(consumer);
241
242             // Configuration completion.
243
System.out.println("Configuration completion...");
244             f1.configuration_complete();
245             f2.configuration_complete();
246             f3.configuration_complete();
247             f4.configuration_complete();
248             o.configuration_complete();
249             p1.configuration_complete();
250             p2.configuration_complete();
251             p3.configuration_complete();
252             p4.configuration_complete();
253
254         } catch (Exception JavaDoc e) {
255             if ((current!=null)&&(_OTS.equals("yes")))
256             {
257                 System.out.println("Error during deployment :");
258                 e.printStackTrace();
259                 System.out.print("Rolling Back ... ");
260                 current.rollback();
261                 System.out.println("Done");
262                 // Force to exit.
263
System.exit(0);
264             }
265         }
266
267         if ((current!=null)&&(_OTS.equals("yes")))
268         {
269             System.out.print("Do you want to commit the Deployment ? [Y/n] ");
270
271             java.io.BufferedReader JavaDoc _buffer
272                 = new java.io.BufferedReader JavaDoc(new java.io.InputStreamReader JavaDoc(System.in));
273             char _answer = (char) _buffer.read();
274
275             if ((_answer=='n')||(_answer=='N'))
276             {
277                 System.out.print("Rolling Back ... ");
278                 current.rollback();
279                 System.out.println("Done");
280             } else {
281                 System.out.print("Committing ... ");
282                 current.commit(false);
283                 System.out.println("Done");
284             }
285         }
286
287         System.exit(0);
288     }
289 }
290
Popular Tags