KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DiningPhilosophers > cif > Deployment_PDA


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.cif;
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 Deployment_PDA
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 ns =
80             org.omg.CosNaming.NamingContextHelper.narrow(obj);
81         obj = orb.resolve_initial_references("CommonNameService");
82         org.omg.CosNaming.NamingContext JavaDoc cns =
83             org.omg.CosNaming.NamingContextHelper.narrow(obj);
84
85         org.omg.CosTransactions.Current current = null;
86         if (_OTS.equals("yes"))
87         {
88             System.out.println("Obtaining the Transaction Service...");
89             org.omg.CORBA.Object JavaDoc objOTS = orb.resolve_initial_references("TransactionCurrent");
90             current = org.omg.CosTransactions.CurrentHelper.narrow( objOTS );
91         }
92
93         try
94         {
95             if ((current!=null)&&(_OTS.equals("yes")))
96             {
97                 System.out.println("Beginning the transaction...");
98                 current.begin();
99             }
100
101             // Obtain the component servers.
102
System.out.println("Obtaining Component Servers...");
103             String JavaDoc cs1_name = System.getProperty("org.objectweb.ccm.DiningPhilosophers.CS1");
104             String JavaDoc cs2_name = System.getProperty("org.objectweb.ccm.DiningPhilosophers.CS2");
105
106             org.omg.CosNaming.NameComponent JavaDoc[] ncomp =
107                 new org.omg.CosNaming.NameComponent JavaDoc[1];
108             ncomp[0] = new org.omg.CosNaming.NameComponent JavaDoc(cs1_name, "");
109             obj = ns.resolve(ncomp);
110             org.objectweb.openccm.Deployment.Server server1 =
111                 org.objectweb.openccm.Deployment.ServerHelper.narrow(obj);
112             ncomp[0].id = cs2_name;
113             obj = ns.resolve(ncomp);
114             org.objectweb.openccm.Deployment.Server server2 =
115                 org.objectweb.openccm.Deployment.ServerHelper.narrow(obj);
116
117             // Obtain the home factories and archive installators.
118
org.omg.Components.Deployment.ComponentServer server1_cs =
119                 server1.provide_component_server();
120             org.omg.Components.Deployment.ComponentInstallation server1_inst =
121                 server1.provide_install();
122             org.omg.Components.Deployment.ComponentServer server2_cs =
123                 server2.provide_component_server();
124             org.omg.Components.Deployment.ComponentInstallation server2_inst =
125                 server2.provide_install();
126
127             // Install archives.
128
System.out.println("Installing archives...");
129             String JavaDoc http_server = System.getProperty("org.objectweb.ccm.DiningPhilosophers.http");
130             server1_inst.install("dinner", http_server + "DiningPhilosophers.jar");
131             server2_inst.install("dinner", http_server + "DiningPhilosophers.jar");
132             // install some plugins already provided by OpenCCM.
133
server1_inst.install("openccm_plugins", http_server + "DiningPhilosophers.jar");
134             server2_inst.install("openccm_plugins", http_server + "DiningPhilosophers.jar");
135
136             // declaring the container types
137
String JavaDoc cont_config =
138                 "container = CONTAINER.container ; " +
139                 "config_home = container.create_system_home("+
140                     "\"openccm_plugins\", " +
141                     "\"EmptyConfig\", "+
142                     "\"org.objectweb.openccm.Containers.Plugins.EmptyConfigurationHome.create\") ;"+
143                 "config = config_home.create_component(JAVA.null) ;" +
144                 "container.create_system_home("+
145                     "\"openccm_plugins\", " +
146                     "\"EmptyCoordinatorHome\", "+
147                     "\"org.objectweb.openccm.Containers.Plugins.EmptyCoordinatorHome.create\") ;"+
148                 "container.create_system_home("+
149                     "\"openccm_plugins\", " +
150                     "\"EmptyControllerHome\", "+
151                     "\"org.objectweb.openccm.Containers.Plugins.EmptyControllerHome.create\") ;"+
152                 "container.set_container_configuration(config) ;";
153
154             // instantiate a container on each server.
155
org.omg.Components.ConfigValue[] config = new org.omg.Components.ConfigValue[1];
156             config[0] = new org.objectweb.openccm.Components.ConfigValueImpl();
157             config[0].name = "container_script";
158             org.omg.CORBA.TypeCode JavaDoc string_tc =
159                 org.objectweb.openccm.corba.TheORB.getORB().get_primitive_tc(org.omg.CORBA.TCKind.tk_string);
160             org.omg.CORBA.Any JavaDoc value =
161                 org.objectweb.openccm.corba.TheDynamicAnyFactory.getFactory().
162                 create_dyn_any_from_type_code(string_tc).to_any();
163             value.insert_string(cont_config);
164             config[0].value = value;
165
166
167             org.omg.Components.Deployment.Container server1_cont =
168                 server1_cs.create_container(config);
169             org.omg.Components.Deployment.Container server2_cont =
170                 server2_cs.create_container(config);
171
172             // Create homes.
173
System.out.println("Instantiating homes...");
174
175             // define a container configuration.
176
java.lang.String JavaDoc home_config =
177                 "container.set_home_configuration(container.get_container_configuration()) ;" +
178                 "container.set_component_configuration(container.get_container_configuration()) ;";
179
180             config[0].name = "home_script";
181             value.insert_string(home_config);
182             config[0].value = value;
183
184             // install homes with this container configuration.
185

186             // On tof's PDA
187
org.omg.Components.CCMHome h =
188                 server1_cont.install_home("dinner",
189                                           "DiningPhilosophers.cif.ObserverHomePdaImpl.create_home",
190                                           config);
191             ObserverHome oh_pda = ObserverHomeHelper.narrow(h);
192             h = server1_cont.install_home("dinner",
193                                           "DiningPhilosophers.cif.PhilosopherHomePdaImpl.create_home",
194                                           config);
195             PhilosopherHome ph_pda = PhilosopherHomeHelper.narrow(h);
196
197             // On sly laptop
198
h = server2_cont.install_home("dinner",
199                                           "DiningPhilosophers.cif.ObserverHomeImpl.create_home",
200                                           config);
201             ObserverHome oh = ObserverHomeHelper.narrow(h);
202
203             h = server2_cont.install_home("dinner",
204                                           "DiningPhilosophers.cif.PhilosopherHomeImpl.create_home",
205                                           config);
206             PhilosopherHome ph = PhilosopherHomeHelper.narrow(h);
207
208             h = server2_cont.install_home("dinner",
209                                           "DiningPhilosophers.cif.ForkHomeImpl.create_home",
210                                           config);
211             ForkHome fh = ForkHomeHelper.narrow(h);
212
213             // Register homes in the name service
214

215             // creating OpenCCM Context
216
org.omg.CosNaming.NameComponent JavaDoc[] nc =
217                 new org.omg.CosNaming.NameComponent JavaDoc[1];
218             nc[0] = new org.omg.CosNaming.NameComponent JavaDoc();
219             nc[0].id = "OpenCCM_PDA";
220             nc[0].kind = "";
221
222             try {
223                 ns = cns.bind_new_context(nc);
224             } catch (org.omg.CosNaming.NamingContextPackage.AlreadyBound JavaDoc ex) {
225                 ns = org.omg.CosNaming.NamingContextHelper.narrow(cns.resolve(nc));
226             }
227
228             nc[0].id = "ObserverHomePC";
229             ns.rebind(nc, oh);
230
231             nc[0].id = "PhilosopherHome";
232             ns.rebind(nc, ph);
233
234             nc[0].id = "ForkHome";
235             ns.rebind(nc, fh);
236
237             nc[0].id = "ObserverHome";
238             ns.rebind(nc, oh_pda);
239
240             nc[0].id = "PhilosopherHomePDA";
241             ns.rebind(nc, ph_pda);
242
243         } catch (Exception JavaDoc e) {
244             if ((current!=null)&&(_OTS.equals("yes")))
245             {
246                 System.out.println("Error during deployment :");
247                 e.printStackTrace();
248                 System.out.print("Rolling Back ... ");
249                 current.rollback();
250                 System.out.println("Done");
251                 // Force to exit.
252
System.exit(0);
253             }
254             throw e;
255         }
256
257         if ((current!=null)&&(_OTS.equals("yes")))
258         {
259             System.out.print("Do you want to commit the Deployment ? [Y/n] ");
260
261             java.io.BufferedReader JavaDoc _buffer
262                 = new java.io.BufferedReader JavaDoc(new java.io.InputStreamReader JavaDoc(System.in));
263             char _answer = (char) _buffer.read();
264
265             if ((_answer=='n')||(_answer=='N'))
266             {
267                 System.out.print("Rolling Back ... ");
268                 current.rollback();
269                 System.out.println("Done");
270             } else {
271                 System.out.print("Committing ... ");
272                 current.commit(false);
273                 System.out.println("Done");
274             }
275         }
276
277         // Force to exit.
278
System.exit(0);
279         }
280     }
281
Popular Tags