2 //=============================================================================
4 * @file Generic_Factory.cpp
6 * The server for the GenericFactory for the quoter example.
8 * @author Michael Kircher (mk1@cs.wustl.edu)
10 //=============================================================================
13 #include "Generic_Factory.h"
15 #include "orbsvcs/LifeCycleServiceC.h"
17 Quoter_Generic_Factory_Server::Quoter_Generic_Factory_Server ()
18 : use_LifeCycle_Service_ (0),
23 Quoter_Generic_Factory_Server::~Quoter_Generic_Factory_Server ()
27 // Unbind the Quoter Factory Finder.
28 CosNaming::Name
generic_Factory_Name (2);
29 generic_Factory_Name
.length (2);
30 generic_Factory_Name
[0].id
= CORBA::string_dup ("IDL_Quoter");
31 generic_Factory_Name
[1].id
= CORBA::string_dup ("Quoter_Generic_Factory");
32 if (!CORBA::is_nil (this->quoterNamingContext_var_
.in ()))
33 this->quoterNamingContext_var_
->unbind (generic_Factory_Name
);
35 catch (const CORBA::SystemException
& sysex
)
37 sysex
._tao_print_exception ("System Exception");
39 catch (const CORBA::UserException
& userex
)
41 userex
._tao_print_exception ("User Exception");
46 Quoter_Generic_Factory_Server::init (int argc
,
49 const char *exception_message
= "Null Message";
54 // Initialize the ORB Manager
55 exception_message
= "While initing the orb_manager";
56 result
= this->orb_manager_
.init (argc
, argv
);
59 ACE_ERROR_RETURN ((LM_ERROR
, "%p\n", "init"), -1);
61 // Activate the POA manager
62 exception_message
= "While activating the POA manager";
63 result
= this->orb_manager_
.activate_poa_manager ();
66 ACE_ERROR_RETURN ((LM_ERROR
, "%p\n", "activate_poa_manager"), -1);
68 // Copy them, because parse_args expects them there.
73 ACE_NEW_RETURN (this->quoter_Generic_Factory_i_ptr_
,
74 Quoter_Generic_Factory_i(this->debug_level_
),
77 // Activate the object.
78 exception_message
= "While activating the Generic Factory";
79 CORBA::String_var str
=
80 this->orb_manager_
.activate (this->quoter_Generic_Factory_i_ptr_
);
83 if (this->debug_level_
>= 2)
84 ACE_DEBUG ((LM_DEBUG
, "Generic_Factory: IOR is: <%C>\n", str
.in ()));
86 // Register the Quoter GenericFactory with the Naming Service.
87 if (this->debug_level_
>= 2)
89 "Generic_Factory: Trying to get a reference to the Naming Service.\n"));
91 // Get the Naming Service object reference.
92 exception_message
= "While getting the Naming Service Reference";
93 CORBA::Object_var namingObj_var
=
94 orb_manager_
.orb()->resolve_initial_references ("NameService");
96 if (CORBA::is_nil (namingObj_var
.in ()))
98 " (%P|%t) Unable get the Naming Service.\n"));
100 // Narrow the object reference to a Naming Context.
101 exception_message
= "While narrowing the Naming Context";
102 CosNaming::NamingContext_var namingContext_var
=
103 CosNaming::NamingContext::_narrow (namingObj_var
.in ());
105 if (CORBA::is_nil (namingContext_var
.in ()))
106 ACE_ERROR ((LM_ERROR
,
107 " (%P|%t) Unable get the Naming Service.\n"));
110 if (this->debug_level_
>= 2)
111 ACE_DEBUG ((LM_DEBUG
,
112 "Generic_Factory: Have a proper reference to the Naming Service.\n"));
114 // Get the IDL_Quoter naming context.
115 CosNaming::Name
quoterContextName (1); // max = 1
116 quoterContextName
.length (1);
117 quoterContextName
[0].id
= CORBA::string_dup ("IDL_Quoter");
119 exception_message
= "While resolving the Quoter";
120 CORBA::Object_var quoterNamingObj_var
=
121 namingContext_var
->resolve (quoterContextName
);
123 exception_message
= "While narrowing the Quoter";
124 quoterNamingContext_var_
=
125 CosNaming::NamingContext::_narrow (quoterNamingObj_var
.in ());
127 if (this->debug_level_
>= 2)
128 ACE_DEBUG ((LM_DEBUG
,
129 "Generic_Factory: Have a proper reference to the Quoter Naming Context.\n"));
131 // Bind the Quoter GenericFactory to the IDL_Quoter naming
133 CosNaming::Name
quoter_Generic_Factory_Name (1);
134 quoter_Generic_Factory_Name
.length (1);
135 quoter_Generic_Factory_Name
[0].id
= CORBA::string_dup ("Quoter_Generic_Factory");
137 exception_message
= "Generic_Factory::_this";
138 CORBA::Object_var gf_obj
= this->quoter_Generic_Factory_i_ptr_
->_this();
140 exception_message
= "While binding the Generic Factory";
141 quoterNamingContext_var_
->bind (quoter_Generic_Factory_Name
,
144 if (this->debug_level_
>= 2)
145 ACE_DEBUG ((LM_DEBUG
,
146 "Generic_Factory: Bound the Quoter GenericFactory to the Quoter Naming Context.\n"));
148 // now the Quoter GenericFactory is bound to the Naming Context
149 // the Generic Factory should try to register itself to the closest
150 // Life Cycle Service is order to be called.
152 if (this->use_LifeCycle_Service_
)
154 // get the Quoter_Life_Cycle_Service
155 CosNaming::Name
life_Cycle_Service_Name (1);
156 life_Cycle_Service_Name
.length (1);
157 life_Cycle_Service_Name
[0].id
= CORBA::string_dup ("Life_Cycle_Service");
159 exception_message
= "While resolving the Life Cycle Service";
160 CORBA::Object_var life_Cycle_Service_Obj_var
=
161 namingContext_var
->resolve (life_Cycle_Service_Name
);
163 exception_message
= "While narrowing the Life Cycle Service";
164 LifeCycleService::Life_Cycle_Service_var life_Cycle_Service_var
=
165 LifeCycleService::Life_Cycle_Service::_narrow (life_Cycle_Service_Obj_var
.in ());
167 if (this->debug_level_
>= 2)
168 ACE_DEBUG ((LM_DEBUG
, "Generic_Factory: Have a proper reference to Life Cycle Service.\n"));
170 exception_message
= "While _this on Generic Factory";
171 CORBA::Object_var object_var
= this->quoter_Generic_Factory_i_ptr_
->_this();
173 exception_message
= "While registering the generic factory";
174 life_Cycle_Service_var
->register_factory ("Quoter_Generic_Factory", // name
175 "Bryan 503", // location
176 "Generic Factory", // description
179 if (this->debug_level_
>= 2)
180 ACE_DEBUG ((LM_DEBUG
,
181 "Generic_Factory: Registered the Quoter GenericFactory to the Life Cycle Service.\n"));
185 catch (const CORBA::Exception
& ex
)
187 ACE_ERROR ((LM_ERROR
, "Quoter_Generic_Factory_Server::init - %C\n", exception_message
));
188 ex
._tao_print_exception (
189 "Quoter_Generic_Factory_Server::init");
198 Quoter_Generic_Factory_Server::run ()
200 if (this->debug_level_
>= 1)
201 ACE_DEBUG ((LM_DEBUG
,
202 "\nQuoter Example: Quoter_Generic_Factory_Server is running\n"));
204 orb_manager_
.orb()->run ();
209 // Function get_options.
212 Quoter_Generic_Factory_Server::parse_args ()
214 ACE_Get_Opt
get_opt (this->argc_
, this->argv_
, ACE_TEXT("l?d:"));
218 while ((opt
= get_opt ()) != EOF
)
221 case 'd': // debug flag.
222 this->debug_level_
= ACE_OS::atoi (get_opt
.opt_arg ());
225 this->use_LifeCycle_Service_
= 1;
229 ACE_ERROR ((LM_ERROR
,
230 "%s: unknown arg, -%c\n",
231 this->argv_
[0], char(opt
)));
234 ACE_DEBUG ((LM_DEBUG
,
236 " [-d] <debug level> - Set the debug level\n"
237 " [-?] - Prints this message\n"
238 " [-l] - Use the lifecycle service\n"
241 ACE_OS::exit (exit_code
);
250 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
252 Quoter_Generic_Factory_Server quoter_Generic_Factory_Server
;
256 if (quoter_Generic_Factory_Server
.init (argc
,
261 quoter_Generic_Factory_Server
.run ();
264 catch (const CORBA::SystemException
& sysex
)
266 sysex
._tao_print_exception ("System Exception");
269 catch (const CORBA::UserException
& userex
)
271 userex
._tao_print_exception ("User Exception");