Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / examples / Quoter / Factory_Finder.cpp
blobdd925c6cedc931700766b813b4781b58593a1564
2 //=============================================================================
3 /**
4 * @file Factory_Finder.cpp
6 * A Factory Finder for the Quoter example. This example conforms
7 * to the CosLifeCycle Factory Finder notion.
9 * @author Michael Kircher (mk1@cs.wustl.edu)
11 //=============================================================================
14 #include "Factory_Finder.h"
16 Quoter_Factory_Finder_Server::Quoter_Factory_Finder_Server ()
17 : debug_level_ (1)
19 // Nothing
22 Quoter_Factory_Finder_Server::~Quoter_Factory_Finder_Server ()
24 try
26 // Unbind the Quoter Factory Finder.
27 CosNaming::Name factory_Finder_Name (2);
28 factory_Finder_Name.length (2);
29 factory_Finder_Name[0].id = CORBA::string_dup ("IDL_Quoter");
30 factory_Finder_Name[1].id = CORBA::string_dup ("Quoter_Factory_Finder");
31 if (!CORBA::is_nil (this->quoterNamingContext_var_.in ()))
32 this->quoterNamingContext_var_->unbind (factory_Finder_Name);
34 catch (const CORBA::Exception& ex)
36 ACE_ERROR ((LM_ERROR, "Could not unbind the Factor Finder from the Name Service\n"));
37 ex._tao_print_exception ("~Quoter_Factor_Finder_Server");
41 int
42 Quoter_Factory_Finder_Server::init (int argc,
43 ACE_TCHAR *argv[])
45 const char *exception_message = "Null Message";
47 try
49 exception_message = "While ORB_Manager::init";
50 int initvalue = this->orb_manager_.init (argc,
51 argv);
53 if (initvalue == -1)
54 ACE_ERROR_RETURN ((LM_ERROR,
55 "%p\n",
56 "init"),
57 -1);
59 // Activate the POA manager
60 exception_message = "While activating the POA manager";
61 int result = this->orb_manager_.activate_poa_manager ();
63 if (result == -1)
64 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "activate_poa_manager"), -1);
66 // Copy them, because parse_args expects them there.
67 this->argc_ = argc;
68 this->argv_ = argv;
70 this->parse_args ();
73 ACE_NEW_RETURN (this->quoter_Factory_Finder_i_ptr_,
74 Quoter_Factory_Finder_i(this->debug_level_),
75 -1);
77 // Activate the object.
78 exception_message = "Failure while activating the Quoter Factory Finder Impl";
79 CORBA::String_var str =
80 this->orb_manager_.activate (this->quoter_Factory_Finder_i_ptr_);
82 // Print the IOR.
83 if (this->debug_level_ >= 2)
84 ACE_DEBUG ((LM_DEBUG, "Factory Finder: IOR is: <%C>\n", str.in ()));
86 // Register the Quoter Factory Finder with the Naming Service
87 if (this->debug_level_ >= 2)
88 ACE_DEBUG ((LM_DEBUG,"Factory Finder: Trying to get a reference to the Naming Service.\n"));
90 // Get the Naming Service object reference.
91 exception_message = "While resolving the Name Service";
92 CORBA::Object_var namingObj_var =
93 orb_manager_.orb()->resolve_initial_references ("NameService");
95 if (CORBA::is_nil (namingObj_var.in ()))
96 ACE_ERROR ((LM_ERROR,
97 " (%P|%t) Unable get the Naming Service.\n"));
99 // Narrow the object reference to a Naming Context.
100 exception_message = "While narrowing the Naming Context";
101 CosNaming::NamingContext_var namingContext_var =
102 CosNaming::NamingContext::_narrow (namingObj_var.in ());
104 // Get the IDL_Quoter naming context.
105 CosNaming::Name quoterContextName (1); // max = 1
106 quoterContextName.length (1);
107 quoterContextName[0].id = CORBA::string_dup ("IDL_Quoter");
109 exception_message = "While resolving the Quoter Naming Context";
110 CORBA::Object_var quoterNamingObj_var =
111 namingContext_var->resolve (quoterContextName);
113 exception_message = "While narrowing the Quoter Naming Context";
114 quoterNamingContext_var_ =
115 CosNaming::NamingContext::_narrow (quoterNamingObj_var.in ());
117 if (this->debug_level_ >= 2)
118 ACE_DEBUG ((LM_DEBUG,
119 "Factory Finder: Have a proper reference to the Quoter Naming Context.\n"));
121 // Bind the QuoterFactory Finder to the IDL_Quoter naming
122 // context.
123 CosNaming::Name quoter_Factory_Finder_Name_ (1);
124 quoter_Factory_Finder_Name_.length (1);
125 quoter_Factory_Finder_Name_[0].id = CORBA::string_dup ("Quoter_Factory_Finder");
127 exception_message = "Factory_Factory::_this";
128 CORBA::Object_var ff_obj = this->quoter_Factory_Finder_i_ptr_->_this();
130 exception_message = "While binding the Factory Finder";
131 quoterNamingContext_var_->bind (quoter_Factory_Finder_Name_,
132 ff_obj.in ());
134 if (this->debug_level_ >= 2)
135 ACE_DEBUG ((LM_DEBUG,
136 "Factory_Finder: Bound the Quoter Factory Finder to the Quoter Naming Context.\n"));
138 catch (const CORBA::Exception& ex)
140 ACE_ERROR ((LM_ERROR, "Quoter_Factor_Finder_Server::init - %C\n", exception_message));
141 ex._tao_print_exception ("SYS_EX");
142 return -1;
145 return 0;
149 Quoter_Factory_Finder_Server::run ()
151 if (this->debug_level_ >= 1)
152 ACE_DEBUG ((LM_DEBUG,
153 "\nQuoter Example: Quoter_Factory_Finder_Server is running\n"));
155 orb_manager_.orb()->run ();
157 return 0;
161 // Function get_options.
163 u_int
164 Quoter_Factory_Finder_Server::parse_args ()
166 ACE_Get_Opt get_opt (this->argc_, this->argv_, ACE_TEXT("?d:"));
167 int opt;
168 int exit_code = 0;
170 while ((opt = get_opt ()) != EOF)
171 switch (opt)
173 case 'd': // debug flag.
174 this->debug_level_ = ACE_OS::atoi (get_opt.opt_arg ());
175 break;
176 default:
177 exit_code = 1;
178 ACE_ERROR ((LM_ERROR,
179 "%s: unknown arg, -%c\n",
180 this->argv_[0], char(opt)));
181 ACE_FALLTHROUGH;
182 case '?':
183 ACE_DEBUG ((LM_DEBUG,
184 "usage: %s"
185 " [-d] <debug level> - Set the debug level\n"
186 " [-?] - Prints this message\n"
187 "\n",
188 this->argv_[0]));
189 ACE_OS::exit (exit_code);
190 break;
192 return 0;
195 // function main
198 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
200 Quoter_Factory_Finder_Server quoter_Factory_Finder_Server;
204 int result = quoter_Factory_Finder_Server.init (argc, argv);
206 if (result == -1)
207 return 1;
208 else
210 quoter_Factory_Finder_Server.run ();
213 catch (const CORBA::SystemException& sysex)
215 sysex._tao_print_exception ("System Exception");
216 return -1;
218 catch (const CORBA::UserException& userex)
220 userex._tao_print_exception ("User Exception");
221 return -1;
223 return 0;