2 //=============================================================================
4 * @file Generic_Factory_i.cpp
6 * The implementation of a LifeCycle Service GenericFactory for the
9 * @author Michael Kircher (mk1@cs.wustl.edu)
11 //=============================================================================
14 #include "Generic_Factory_i.h"
17 #include "tao/ORB_Core.h"
19 #include "ace/Get_Opt.h"
22 Quoter_Generic_Factory_i::Quoter_Generic_Factory_i (int debug_level
)
23 : debug_level_ (debug_level
)
28 Quoter_Generic_Factory_i::~Quoter_Generic_Factory_i (void)
33 Quoter_Generic_Factory_i::supports (const CosLifeCycle::Key
&)
40 CosNaming::NamingContext_ptr
41 Quoter_Generic_Factory_i::get_naming_context (const CosLifeCycle::Key
&factory_key
)
43 CosNaming::NamingContext_var quoterNamingContext_var
;
46 // @@ FIXME Get a reference to the ORB.
47 CORBA::ORB_ptr orb_ptr
=
48 TAO_ORB_Core_instance ()->orb ();
50 // Get the Naming Service object reference.
51 CORBA::Object_var namingObj_var
=
52 orb_ptr
->resolve_initial_references ("NameService");
54 if (CORBA::is_nil (namingObj_var
.in ()))
56 "(%P|%t) Unable get the Naming Service.\n"));
58 // Narrow the object reference to a Naming Context.
59 CosNaming::NamingContext_var namingContext_var
=
60 CosNaming::NamingContext::_narrow (namingObj_var
.in ());
62 CosNaming::Name
quoterContextName (1); // max = 1
63 quoterContextName
.length (1);
64 quoterContextName
[0].id
= CORBA::string_dup ("IDL_Quoter");
66 // Get the IDL_Quoter naming context.
67 CORBA::Object_var quoterNamingObj_var
=
68 namingContext_var
->resolve (quoterContextName
);
70 quoterNamingContext_var
=
71 CosNaming::NamingContext::_narrow (quoterNamingObj_var
.in ());
73 catch (const CORBA::Exception
&)
75 throw CosLifeCycle::NoFactory (factory_key
);
77 return quoterNamingContext_var
._retn ();
83 Quoter_Generic_Factory_i::create_object (const CosLifeCycle::Key
&factory_key
,
84 const CosLifeCycle::Criteria
&)
86 Stock::Quoter_var quoter_var
;
89 CosNaming::NamingContext_var quoterNamingContext_var
=
90 this->get_naming_context (factory_key
);
92 // ** now a proper reference to the quoter naming context is
95 // Fill in the name of the Quoter Factory.
96 // Take the key supplied to search for a Quoter Factory
97 CosNaming::Name factory_Name
= (CosNaming::Name
) factory_key
;
99 // Try to get a reference to a Quoter Factory
100 CORBA::Object_var quoterFactoryObject_var
=
101 quoterNamingContext_var
->resolve (factory_Name
);
103 // We were able to get a reference to Quoter Factory.
104 // Check if it is a valid Quoter Factory reference
105 if (CORBA::is_nil (quoterFactoryObject_var
.in()))
106 { // throw a NoFactory exception
107 throw CosLifeCycle::NoFactory (factory_key
);
110 Stock::Quoter_Factory_var factory_var
=
111 Stock::Quoter_Factory::_narrow (quoterFactoryObject_var
.in ());
113 if (CORBA::is_nil (factory_var
.in ()))
115 ACE_ERROR ((LM_ERROR
,
116 "invalid factory.\n"));
117 return CORBA::Object::_nil ();
120 if (this->debug_level_
> 1)
121 ACE_DEBUG ((LM_DEBUG
, "Generic Factory: Factory reference OK.\n"));
123 // Now retrieve the Quoter obj ref corresponding to the key.
125 factory_var
->create_quoter ("test");
127 if (this->debug_level_
> 1)
128 ACE_DEBUG ((LM_DEBUG
, "Generic_Factory: Quoter Created\n"));
130 if (CORBA::is_nil (quoter_var
.in ()))
132 ACE_ERROR ((LM_ERROR
,
133 "null quoter objref returned by factory\n"));
134 return CORBA::Object::_nil ();
137 if (this->debug_level_
> 1)
138 ACE_DEBUG ((LM_DEBUG
, "Generic_Factory: Return an object reference to a new object.\n"));
141 catch (const CORBA::Exception
&)
143 throw CosLifeCycle::NoFactory (factory_key
);
145 return quoter_var
._retn ();