Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / examples / Quoter / Quoter_i.cpp
blob7c25746e4a413f690abcf17a7f0e54885958750c
2 //=============================================================================
3 /**
4 * @file Quoter_i.cpp
6 * @author Darrell Brunsch <brunsch@cs.wustl.edu>
7 */
8 //=============================================================================
11 #include "Quoter_i.h"
13 #include "tao/debug.h"
14 #include "tao/ORB_Core.h"
16 // Constructor. Create all the quoter factories.
18 Quoter_Factory_i::Quoter_Factory_i (size_t num, PortableServer::POA_ptr poa_ptr)
19 : poa_ptr_ (poa_ptr),
20 my_quoters_ (0),
21 quoter_num_ (num),
22 next_quoter_ (0)
24 // Nothing
28 // Destructor
30 Quoter_Factory_i::~Quoter_Factory_i (void)
32 for (size_t i = 0; i < this->quoter_num_; i++)
33 delete this->my_quoters_[i];
34 delete [] this->my_quoters_;
38 // Initialize everything in the factory
40 int Quoter_Factory_i::init (void)
42 ACE_NEW_RETURN (this->my_quoters_,
43 Quoter_i *[this->quoter_num_],
44 -1);
46 for (size_t i = 0; i < this->quoter_num_; i++)
48 ACE_NEW_RETURN (this->my_quoters_[i],
49 Quoter_i("x", // name
50 0, // don't use the LifeCycle_Service
51 this->poa_ptr_), // a reference to the poa
52 -1);
54 const char *location_string = "POA::activate";
55 try
57 this->poa_ptr_->activate_object (this->my_quoters_[i]);
59 location_string = "_this";
60 Stock::Quoter_var quoter_var = this->my_quoters_[i]->_this();
62 location_string = "CORBA::ORB::object_to_string";
63 // Stringify the object reference and print it out.
64 CORBA::String_var quoter_ior =
65 TAO_ORB_Core_instance()->orb()->object_to_string (quoter_var.in ());
67 catch (const CORBA::Exception& ex)
69 ex._tao_print_exception (location_string);
70 return -1;
74 // Everything is ok
75 return 0;
79 // Return the quoter by the id <name>.
81 Stock::Quoter_ptr
82 Quoter_Factory_i::create_quoter (const char *)
84 this->next_quoter_ = (this->next_quoter_ + 1) % this->quoter_num_;
86 if (TAO_debug_level > 0)
87 ACE_DEBUG ((LM_DEBUG, "Quoter %d Created\n", this->next_quoter_));
89 return my_quoters_[this->next_quoter_]->_this ();
93 // Constructor
95 Quoter_i::Quoter_i (const char *name,
96 const unsigned char use_LifeCycle_Service,
97 PortableServer::POA_ptr poa_ptr)
98 : use_LifeCycle_Service_ (use_LifeCycle_Service),
99 poa_var_ (PortableServer::POA::_duplicate (poa_ptr))
101 ACE_UNUSED_ARG (name);
105 // Destructor
107 Quoter_i::~Quoter_i (void)
109 // Nothing
113 // Returns the current quote for the stock <stock_name>.
114 // For now, just return 42. It was a good day on Wall Street.
116 CORBA::Long
117 Quoter_i::get_quote (char const *)
119 return 42;
123 // Make a copy of this object
125 CosLifeCycle::LifeCycleObject_ptr
126 Quoter_i::copy (CosLifeCycle::FactoryFinder_ptr there,
127 const CosLifeCycle::Criteria &/*the_criteria*/)
129 const char *exception_message = "Null message";
130 CosLifeCycle::LifeCycleObject_ptr lifeCycleObject_ptr =
131 CosLifeCycle::LifeCycleObject::_nil ();
134 // The name of the Generic Factory
135 CosLifeCycle::Key factoryKey (2); // max = 2
137 if (this->use_LifeCycle_Service_ == 1)
139 // use the LifeCycle Service
140 factoryKey.length(1);
141 factoryKey[0].id = CORBA::string_dup ("Life_Cycle_Service");
143 else
145 // use a Generic Factory
146 factoryKey.length(2);
147 factoryKey[0].id = CORBA::string_dup ("IDL_Quoter");
148 factoryKey[1].id = CORBA::string_dup ("Quoter_Generic_Factory");
151 // Find an appropriate factory over there.
152 exception_message = "While trying to find a factory.\n";
153 CosLifeCycle::Factories *factories_ptr =
154 there->find_factories (factoryKey);
156 // Now it is known that there is at least one factory.
157 Stock::Quoter_var quoter_var;
159 for (u_int i = 0; i < factories_ptr->length (); i++)
161 // Get the first object reference to a factory.
162 CORBA::Object_ptr generic_FactoryObj_ptr = (*factories_ptr)[i];
164 // Narrow it to a Quoter Factory.
165 exception_message = "While narrowing.\n";
166 CosLifeCycle::GenericFactory_var generic_Factory_var =
167 CosLifeCycle::GenericFactory::_narrow (generic_FactoryObj_ptr);
169 if (CORBA::is_nil (generic_Factory_var.in ()))
171 ACE_ERROR ((LM_ERROR,
172 "Quoter::copy: Narrow failed. Generic Factory is not valid.\n"));
173 return CosLifeCycle::LifeCycleObject::_nil();
176 CosLifeCycle::Key genericFactoryName (1); // max = 1
177 genericFactoryName.length(1);
178 genericFactoryName[0].id = CORBA::string_dup ("Quoter_Factory");
180 CosLifeCycle::Criteria criteria(1);
181 criteria.length (1);
182 criteria[0].name = CORBA::string_dup ("filter");
183 criteria[0].value <<= CORBA::string_dup ("name=='Quoter_Generic_Factory'");
185 exception_message = "While creating an object";
186 CORBA::Object_var quoterObject_var =
187 generic_Factory_var->create_object (genericFactoryName,
188 criteria);
190 exception_message = "While narrowing object";
191 quoter_var = Stock::Quoter::_narrow (quoterObject_var.in());
193 if (CORBA::is_nil (quoter_var.in ()))
195 // If we had already our last chance, then give up.
196 if (i == factories_ptr->length ())
198 ACE_ERROR ((LM_ERROR,
199 "Quoter::copy: Last factory did not work.\n"
200 "No more factories are available. I give up.\n"));
201 throw CosLifeCycle::NoFactory (factoryKey);
203 else
205 ACE_ERROR ((LM_ERROR,
206 "Quoter::copy: Factory did not create the Quoter properly.\n"));
207 // Tell what's wrong and try the next factory.
210 else
211 // if succeeded in creating a new Quoter over there, then stop trying
212 break;
215 if (TAO_debug_level > 0)
216 ACE_DEBUG ((LM_DEBUG, "Quoter copied\n"));
218 lifeCycleObject_ptr =
219 CosLifeCycle::LifeCycleObject::_duplicate ((CosLifeCycle::LifeCycleObject_ptr) quoter_var.in());
221 catch (const CORBA::Exception& ex)
223 ACE_ERROR ((LM_ERROR, "Quoter_i::copy - %C\n", exception_message));
224 ex._tao_print_exception ("Exception");
226 // Report a NoFactory exception back to the caller
227 throw CosLifeCycle::NoFactory ();
231 return lifeCycleObject_ptr;
235 // Move this object using <there> and <the_criteria>
237 void
238 Quoter_i::move (CosLifeCycle::FactoryFinder_ptr /* there */,
239 const CosLifeCycle::Criteria & /* the_criteria */)
241 ACE_ERROR ((LM_ERROR,
242 "Quoter_i::move: The Quoter object is not movable!"));
244 throw CosLifeCycle::NotMovable();
248 // Removes the object. Once we shut down the ORB we can call it a day.
250 void
251 Quoter_i::remove (void)
253 if (TAO_debug_level > 0)
254 ACE_DEBUG ((LM_DEBUG, "I have been asked to shut down.\n"));
256 TAO_ORB_Core_instance ()->orb ()->shutdown ();