KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DiningPhilosophers > cif > Run


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 Run
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().
61                       getProperty("TRANSACTIONAL_PLUGIN","no").toLowerCase();
62
63         // Init the ORB.
64
System.out.println("Initializing the ORB...");
65
66         // TODO: Need to report this ORB.init() problem to OpenORB developers!
67
// Due to an OpenORB problem, the following lines:
68
// org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);
69
// fr.lifl.goal.OpenCCM.Components.Runtime.init(orb);
70
// are replaced by the next line.
71

72         // Init the OpenCCM Components Runtime.
73
org.omg.CORBA.ORB JavaDoc orb =
74                 org.objectweb.openccm.Components.Runtime.init(args);
75
76         // Obtain the Name Service.
77
System.out.println("Obtaining the Name Service...");
78         org.omg.CORBA.Object JavaDoc obj =
79             orb.resolve_initial_references("CommonNameService");
80         org.omg.CosNaming.NamingContext JavaDoc ns =
81             org.omg.CosNaming.NamingContextHelper.narrow(obj);
82
83         org.omg.CosTransactions.Current current = null;
84         if (_OTS.equals("yes"))
85         {
86             System.out.println("Obtaining the Transaction Service...");
87             org.omg.CORBA.Object JavaDoc objOTS = orb.resolve_initial_references("TransactionCurrent");
88             current = org.omg.CosTransactions.CurrentHelper.narrow( objOTS );
89         }
90
91         try
92         {
93             if ((current!=null)&&(_OTS.equals("yes")))
94             {
95                 System.out.println("Beginning the transaction...");
96                 current.begin();
97             }
98
99          // resolving OpenCCM Context
100
org.omg.CosNaming.NameComponent JavaDoc[] nc =
101             new org.omg.CosNaming.NameComponent JavaDoc[1];
102         nc[0] = new org.omg.CosNaming.NameComponent JavaDoc();
103         nc[0].id = System.getProperty("org.objectweb.ccm.DiningPhilosophers.name");;
104         nc[0].kind = "";
105
106         ns = org.omg.CosNaming.NamingContextHelper.narrow(ns.resolve(nc));
107
108         // resolving homes
109
nc[0].id = "PhilosopherHome";
110         obj = ns.resolve(nc);
111         PhilosopherHome ph = PhilosopherHomeHelper.narrow(obj);
112
113         nc[0].id = "ForkHome";
114         obj = ns.resolve(nc);
115         ForkHome fh = ForkHomeHelper.narrow(obj);
116
117         nc[0].id = "ObserverHome";
118         obj = ns.resolve(nc);
119         ObserverHome oh = ObserverHomeHelper.narrow(obj);
120
121         // Create components.
122
System.out.println("Creating components...");
123         Philosopher p1 = ph._new("Mathieu");
124         Philosopher p2 = ph._new("Raphael");
125         Philosopher p3 = ph._new("Philippe");
126         Philosopher p4 = ph._new("Sylvain");
127         ForkManager f1 = fh.create();
128         ForkManager f2 = fh.create();
129         ForkManager f3 = fh.create();
130         ForkManager f4 = fh.create();
131         Observer o = oh.create();
132
133         // Configure components.
134
System.out.println("Configuring components...");
135
136         // Connecting components.
137
System.out.println("Interconnecting components...");
138
139         Fork fork = f1.provide_the_fork();
140         p1.connect_right(fork);
141         p2.connect_left(fork);
142
143         fork = f2.provide_the_fork();
144         p2.connect_right(fork);
145         p3.connect_left(fork);
146
147         fork = f3.provide_the_fork();
148         p3.connect_right(fork);
149         p4.connect_left(fork);
150
151         fork = f4.provide_the_fork();
152         p4.connect_right(fork);
153         p1.connect_left(fork);
154
155         StatusInfoConsumer consumer = o.get_consumer_info();
156         p1.subscribe_info(consumer);
157         p2.subscribe_info(consumer);
158         p3.subscribe_info(consumer);
159         p4.subscribe_info(consumer);
160
161         // Configuration completion.
162
System.out.println("Configuration completion...");
163         f1.configuration_complete();
164         f2.configuration_complete();
165         f3.configuration_complete();
166         f4.configuration_complete();
167         o.configuration_complete();
168         p1.configuration_complete();
169         p2.configuration_complete();
170         p3.configuration_complete();
171         p4.configuration_complete();
172
173         } catch (Exception JavaDoc e) {
174             if ((current!=null)&&(_OTS.equals("yes")))
175             {
176                 System.out.println("Error during deployment :");
177                 e.printStackTrace();
178                 System.out.print("Rolling Back ... ");
179                 current.rollback();
180                 System.out.println("Done");
181                 // Force to exit.
182
System.exit(0);
183             }
184             throw e;
185         }
186
187         if ((current!=null)&&(_OTS.equals("yes")))
188         {
189             System.out.print("Do you want to commit the Deployment ? [Y/n] ");
190
191             java.io.BufferedReader JavaDoc _buffer
192                 = new java.io.BufferedReader JavaDoc(new java.io.InputStreamReader JavaDoc(System.in));
193             char _answer = (char) _buffer.read();
194
195             if ((_answer=='n')||(_answer=='N'))
196             {
197                 System.out.print("Rolling Back ... ");
198                 current.rollback();
199                 System.out.println("Done");
200             } else {
201                 System.out.print("Committing ... ");
202                 current.commit(false);
203                 System.out.println("Done");
204             }
205         }
206
207         System.exit(0);
208     }
209 }
210
Popular Tags