2 //=============================================================================
6 * @author Darrell Brunsch <brunsch@cs.wustl.edu>
8 //=============================================================================
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
)
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_
],
46 for (size_t i
= 0; i
< this->quoter_num_
; i
++)
48 ACE_NEW_RETURN (this->my_quoters_
[i
],
50 0, // don't use the LifeCycle_Service
51 this->poa_ptr_
), // a reference to the poa
54 const char *location_string
= "POA::activate";
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
);
79 // Return the quoter by the id <name>.
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 ();
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
);
107 Quoter_i::~Quoter_i (void)
113 // Returns the current quote for the stock <stock_name>.
114 // For now, just return 42. It was a good day on Wall Street.
117 Quoter_i::get_quote (char const *)
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");
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);
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
,
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
);
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.
211 // if succeeded in creating a new Quoter over there, then stop trying
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>
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.
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 ();