Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / examples / Simulator / DOVEBrowser / PushConsumerFactory.java
blob6a27c986a398708853e83a10b3168bfc3d4cc1e5
1 //
2 // ============================================================================
3 //
4 // = FILENAME
5 // PushConsumerFactory.java
6 //
7 // = AUTHOR
8 // Michael Kircher (mk1@cs.wustl.edu)
9 //
10 // = DESCRIPTION
11 // This is the administor/factory for a PushConsumer.
13 // ============================================================================
18 public class PushConsumerFactory {
20 private org.omg.CORBA.ORB orb_;
21 private org.omg.PortableServer.POA poa_;
22 private org.omg.CORBA.Object naming_service_object_;
24 private DataHandler dataHandler_;
25 private Navigation navigation_;
26 private Weapons weapons_;
28 private String[] ec_names_ = null;
29 private int ec_names_count_ = 0;
31 private String[] ss_names_ = null;
32 private int ss_names_count_ = 0;
34 private boolean use_queueing_ = false;
36 public PushConsumerFactory (DataHandler dataHandler,
37 String nameServiceIOR,
38 String nameServicePort,
39 String[] args,
40 boolean use_queueing,
41 java.applet.Applet applet)
43 try
45 use_queueing_ = use_queueing;
46 dataHandler_ = dataHandler;
48 // if the DOVE Browser is running as an Applet
49 if (applet != null) {
50 orb_ = org.omg.CORBA.ORB.init (applet, null);
52 else { // not running as an Applet, but as an normal Application
53 orb_ = org.omg.CORBA.ORB.init (args, null);
56 System.out.println ("Initialized ORB");
58 org.omg.CORBA.Object obj =
59 orb_.resolve_initial_references ("RootPOA");
61 System.out.println ("Obtained RootPOA");
63 poa_ = org.omg.PortableServer.POAHelper.narrow (obj);
65 System.out.println ("Narrowed RootPOA");
67 // Get the Naming Service initial reference
69 // Name Service Lookup cannot be used when running as an Applet
70 if (nameServiceIOR == null && applet != null) {
71 System.out.println (" Name Service Lookup cannot be used when running as an Applet! Quit!");
72 System.exit (1);
75 if (nameServiceIOR == null) { // only used when running via "java" or "vbj"
76 System.out.println ("Using the lookup protocol!");
77 NS_Resolve ns_resolve_ = new NS_Resolve (nameServicePort);
78 naming_service_object_ = ns_resolve_.resolve_name_service (orb_);
80 else {
81 System.out.println ("Using the following IOR: " + nameServiceIOR);
82 naming_service_object_ = orb_.string_to_object (nameServiceIOR);
85 // Walk through args and count EC and Scheduling Service names.
86 int arg_index = 0;
87 while (args.length > arg_index)
89 // Count an event service name
90 if ((args[arg_index].equals ("-eventservicename")) &&
91 (args.length > arg_index + 1))
93 System.out.println ("switch [" + args[arg_index] + "]");
94 System.out.println ("value [" + args[arg_index + 1] + "]");
95 arg_index += 2;
96 ++ec_names_count_;
98 // Count a scheduling service name
99 else if ((args[arg_index].equals ("-scheduleservicename")) &&
100 (args.length > arg_index + 1))
102 System.out.println ("switch [" + args[arg_index] + "]");
103 System.out.println ("value [" + args[arg_index + 1] + "]");
104 arg_index += 2;
105 ++ss_names_count_;
107 else if (args[arg_index].equals ("-dualECdemo"))
109 System.out.println ("switch [" + args[arg_index] + "]");
110 ++arg_index;
111 ec_names_count_ += 2;
112 ss_names_count_ += 2;
114 else if ((args[arg_index].equals ("-dualECdemo1")) ||
115 (args[arg_index].equals ("-dualECdemo2")))
117 System.out.println ("switch [" + args[arg_index] + "]");
118 ++arg_index;
119 ++ec_names_count_;
120 ++ss_names_count_;
122 // Skip over anything else
123 else
125 arg_index ++;
129 // Allocate arrays for the event and schedule service names (if any)
130 if (ec_names_count_ > 0)
132 ec_names_ = new String [ec_names_count_];
134 if (ss_names_count_ > 0)
136 ss_names_ = new String [ss_names_count_];
139 // Then traverse the args again to allocate and fill in a list
140 // of EC and Scheduling Service names.
141 arg_index = 0;
142 ec_names_count_ = 0;
143 ss_names_count_ = 0;
144 while (args.length > arg_index)
146 // Set an event service name.
147 if ((args[arg_index].equals ("-eventservicename")) &&
148 (args.length > arg_index + 1))
150 ec_names_[ec_names_count_] = args[arg_index + 1];
151 ++ec_names_count_;
152 arg_index += 2;
154 // Set a schedule service name.
155 else if ((args[arg_index].equals ("-scheduleservicename")) &&
156 (args.length > arg_index + 1))
158 ss_names_[ss_names_count_] = args[arg_index + 1];
159 ++ss_names_count_;
160 arg_index += 2;
162 else if (args[arg_index].equals ("-dualECdemo"))
164 ss_names_[ss_names_count_] = "DUAL_SCHED_HI";
165 ss_names_[ss_names_count_ + 1] = "DUAL_SCHED_LO";
166 ec_names_[ec_names_count_] = "DUAL_EC_HI";
167 ec_names_[ec_names_count_ + 1] = "DUAL_EC_LO";
168 ++arg_index;
169 ec_names_count_ += 2;
170 ss_names_count_ += 2;
172 else if (args[arg_index].equals ("-dualECdemo1"))
174 ss_names_[ss_names_count_] = "DUAL_SCHED_HI";
175 ec_names_[ec_names_count_] = "DUAL_EC_HI";
176 ++arg_index;
177 ++ec_names_count_;
178 ++ss_names_count_;
180 else if (args[arg_index].equals ("-dualECdemo2"))
182 ss_names_[ss_names_count_] = "DUAL_SCHED_LO";
183 ec_names_[ec_names_count_] = "DUAL_EC_LO";
184 ++arg_index;
185 ++ec_names_count_;
186 ++ss_names_count_;
188 // Skip over anything else.
189 else
191 arg_index ++;
195 // If names for event or scheduling services have not been provided
196 // already, provide the default names used by the service executables.
197 if (ec_names_count_ == 0)
199 ec_names_count_ = 1;
200 ec_names_ = new String [1];
201 ec_names_ [0] = "EventService";
203 if (ss_names_count_ == 0)
205 ss_names_count_ = 1;
206 ss_names_ = new String [1];
207 ss_names_ [0] = "ScheduleService";
210 catch (org.omg.CORBA.ORBPackage.InvalidName e)
212 System.err.println ("CosNaming.NamingContextPackage.InvalidName");
213 System.err.println (e);
215 catch(org.omg.CORBA.SystemException e) {
216 System.err.println ("PushConsumerFactory constructor: ORB and Name Service initialization");
217 System.err.println(e);
222 public class Object_is_null_exception extends Exception
224 Object_is_null_exception (String s)
226 super (s);
230 public void run ()
235 // Get the Naming Context to allow resolving the EventService and
236 // ScheduleService
237 CosNaming.NamingContext naming_context_ =
238 CosNaming.NamingContextHelper.narrow (naming_service_object_);
240 if (naming_context_ == null)
242 System.err.println ("The Naming Context is null");
243 System.exit (1);
245 System.out.println ("Reference to the Naming Service is ok.");
247 // Create Consumers connected to the EventService
248 int ss_number = 0;
249 RtecScheduler.Scheduler scheduler_ = null;
250 for (int ec_number = 0; ec_number < ec_names_count_; ++ec_number)
252 // Get a reference for the next Scheduling Service if there is one.
253 // Invariant: we will always execute this code at least once.
254 if (ss_number < ss_names_count_)
256 CosNaming.NameComponent[] ss_name_components_ = new CosNaming.NameComponent[1];
257 ss_name_components_[0] = new CosNaming.NameComponent (ss_names_[ss_number],"");
258 org.omg.CORBA.Object scheduler_object_ = naming_context_.resolve (ss_name_components_);
260 if (scheduler_object_ == null)
262 throw new Object_is_null_exception(ss_names_[ss_number] +
263 " Object is null");
266 scheduler_ = RtecScheduler.SchedulerHelper.narrow (scheduler_object_);
268 if (scheduler_ == null)
270 throw new Object_is_null_exception(ss_names_[ss_number] +
271 " narrowed ref is null");
274 System.out.println ("Reference to " +
275 ss_names_[ss_number] + " is ok.");
277 ++ss_number;
280 // Get a reference for the next Event Service
282 CosNaming.NameComponent[] ec_name_components_ = new CosNaming.NameComponent[1];
283 ec_name_components_[0] = new CosNaming.NameComponent (ec_names_[ec_number],"");
284 org.omg.CORBA.Object event_channel_object_ = naming_context_.resolve (ec_name_components_);
286 if (event_channel_object_ == null)
288 throw new Object_is_null_exception(ec_names_[ec_number] +
289 " Object is null");
292 RtecEventChannelAdmin.EventChannel event_channel_ =
293 RtecEventChannelAdmin.EventChannelHelper.narrow (event_channel_object_);
295 if (event_channel_ == null)
297 throw new Object_is_null_exception(ec_names_[ec_number] +
298 " narrowed ref is null");
301 System.out.println ("Reference to " +
302 ec_names_[ec_number] + " is ok.");
304 // Start the consumer
305 System.out.println ("Instantiating the Push Consumer for " +
306 ec_names_[ec_number] + ": demo_consumer_" +
307 ec_number + ".");
309 PushConsumer pushConsumer_ = new PushConsumer (orb_, poa_,
310 dataHandler_,
311 use_queueing_);
312 System.out.println ("Initializing the Push Consumer for " +
313 ec_names_[ec_number] + ": demo_consumer_" +
314 ec_number + ".");
316 pushConsumer_.open_consumer (event_channel_, scheduler_,
317 "demo_consumer_" + ec_number);
321 // Tell the CORBA environment that we are ready
323 System.out.println ("PushConsumer registered with POA");
324 poa_.the_POAManager ().activate ();
325 System.out.println ("POA Activated");
327 catch (CosNaming.NamingContextPackage.CannotProceed e)
329 System.err.println ("CosNaming.NamingContextPackage.CannotProceed");
330 System.err.println (e);
332 catch (CosNaming.NamingContextPackage.InvalidName e)
334 System.err.println ("CosNaming.NamingContextPackage.InvalidName");
335 System.err.println (e);
337 catch (CosNaming.NamingContextPackage.NotFound e)
339 System.err.println ("CosNaming.NamingContextPackage.NotFound");
340 System.err.println (e);
343 catch (org.omg.PortableServer.POAManagerPackage.AdapterInactive e)
345 System.err.println ("org.omg.PortableServer.POAManagerPackage.AdapterInactive");
346 System.err.println (e);
348 catch (Object_is_null_exception e)
350 System.err.println (e);
352 catch(org.omg.CORBA.SystemException e)
354 System.err.println ("PushConsumerFactory.run: Failure");
355 System.err.println(e);
360 } // public class PushConsumerFactory