Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / orbsvcs / LifeCycle_Service / Factory_Trader.cpp
blobd0585c80483453371b0f166573172b5621c25017
2 //=============================================================================
3 /**
4 * @file Factory_Trader.cpp
6 * A colocated instance of the Trading Service, only part of
7 * the functionality provided is used. This class serves
8 * as Wrapper around the Trading Service and provides
9 * smaller interfaces.
10 * TRADER_AVAILABLE is defined via compiler switch in the Makefile
12 * @author Michael Kircher (mk1@cs.wustl.edu)
14 //=============================================================================
17 #include "orbsvcs/Log_Macros.h"
18 #include "Factory_Trader.h"
19 #include "orbsvcs/CosTradingC.h"
21 // This const char * is used for adding a new type to the service repository
22 // the added types will be subclasses of this.
23 const char * Factory_Trader::GENERIC_FACTORY_INTERFACE_REPOSITORY_ID =
24 "IDL:omg.org/CosLifeCycle/GenericFactory:1.0";
27 Factory_Trader::Factory_Trader (int debug_level)
28 : trader_ptr_(0),
29 trading_Components_ptr_ (0),
30 support_Attributes_ptr_(0),
31 debug_level_ (debug_level)
33 try
35 int argc = 0;
36 // create the trader
37 this->trader_ptr_ = TAO_Trader_Factory::create_trader (argc, 0);
38 this->support_Attributes_ptr_ = &(trader_ptr_->support_attributes ());
39 // this pointer is deleted when the trader_ptr is deleted
40 this->trading_Components_ptr_ = &(trader_ptr_->trading_components ());
41 // this pointer is deleted when the trader_ptr is deleted
43 // Set the service type repository
44 support_Attributes_ptr_->type_repos
45 (this->repository_._this ());
47 // Add the "Factory" type to the repository
48 this->add_type ();
50 catch (const CORBA::Exception& ex)
52 ex._tao_print_exception (
53 "LifeCycle Server: (Factory_Trader::Factory_Trader) Failed adding a new type.\n");
55 // @@ ACE_CHECK? No way to pass back any exceptions.
58 Factory_Trader::~Factory_Trader ()
60 delete this->trader_ptr_;
64 void
65 Factory_Trader::add_type ()
67 try
69 // define the new type
70 CosTradingRepos::ServiceTypeRepository::PropStruct propStruct_name;
71 propStruct_name.name = CORBA::string_dup ("name");
72 propStruct_name.value_type = CORBA::_tc_string;
73 propStruct_name.mode = CosTradingRepos::ServiceTypeRepository::PROP_MANDATORY;
75 CosTradingRepos::ServiceTypeRepository::PropStruct propStruct_location;
76 propStruct_location.name = CORBA::string_dup ("location");
77 propStruct_location.value_type = CORBA::_tc_string;
78 propStruct_location.mode = CosTradingRepos::ServiceTypeRepository::PROP_NORMAL;
80 CosTradingRepos::ServiceTypeRepository::PropStruct propStruct_description;
81 propStruct_description.name = CORBA::string_dup ("description");
82 propStruct_description.value_type = CORBA::_tc_string;
83 propStruct_description.mode = CosTradingRepos::ServiceTypeRepository::PROP_NORMAL;
85 CosTradingRepos::ServiceTypeRepository::PropStructSeq propStructSeq(3);
86 propStructSeq.length (3);
87 propStructSeq[0] = propStruct_name;
88 propStructSeq[1] = propStruct_location;
89 propStructSeq[2] = propStruct_description;
91 CosTradingRepos::ServiceTypeRepository::ServiceTypeNameSeq superTypeSeq;
93 // Add the new type
94 this->repository_.add_type (CORBA::string_dup("GenericFactory"),
95 GENERIC_FACTORY_INTERFACE_REPOSITORY_ID,
96 propStructSeq,
97 superTypeSeq);
99 catch (const CORBA::Exception& ex)
101 ex._tao_print_exception ("LifeCycle Server: (Factory_Trader::init).\n");
103 // @@ ACE_CHECK
107 void
108 Factory_Trader::_cxx_export (const char * name,
109 const char * location,
110 const char * description,
111 const CORBA::Object_ptr object_ptr)
115 if (CORBA::is_nil(object_ptr))
117 ORBSVCS_ERROR ((LM_ERROR, "LifeCycle Server (Factory_Trader::export): "
118 "Object pointer is nil, cannot export!\n"));
119 return;
122 CosTrading::PropertySeq propertySeq(3);
123 propertySeq.length (3);
124 propertySeq[0].name = CORBA::string_dup("name");
125 propertySeq[0].value <<= CORBA::string_dup (name);
126 propertySeq[1].name = CORBA::string_dup("location");
127 propertySeq[1].value <<= CORBA::string_dup (location);
128 propertySeq[2].name = CORBA::string_dup("description");
129 propertySeq[2].value <<= CORBA::string_dup (description);
131 // get the register interface
132 CosTrading::Register_ptr register_ptr = this->trading_Components_ptr_->register_if ();
133 // this pointer is deleted when the trader_ptr is deleted
135 // invoke the export method on the Register interface of the Trading Service
136 register_ptr->_cxx_export (CORBA::Object::_duplicate (object_ptr),
137 CORBA::string_dup("GenericFactory"),
138 propertySeq);
140 catch (const CORBA::Exception& ex)
142 ex._tao_print_exception (
143 "LifeCycle Server (Factory_Trader::export): ""Failed to export factory.\n");
145 // @@ ACE_CHECK*
149 CORBA::Object_ptr
150 Factory_Trader::query (const char* constraint)
154 CosTrading::Lookup::SpecifiedProps specifiedProps;
155 specifiedProps._d(CosTrading::Lookup::all);
157 // Get some pointers for the out parameters of the call.
158 CosTrading::OfferSeq *offerSeq_ptr = 0;
159 CosTrading::OfferIterator_ptr offerIterator_ptr = 0;
160 CosTrading::PolicyNameSeq *policyNameSeq_ptr = 0;
162 // An empty policy sequence
163 CosTrading::PolicySeq policySeq;
165 // Get a reference to the lookup interface
166 CosTrading::Lookup_ptr lookup_ptr = this->trading_Components_ptr_->lookup_if ();
167 // this pointer is deleted when the trader_ptr is deleted
169 // Invoke the query method on the Lookup Interface.
170 lookup_ptr->query ("GenericFactory", // Type name
171 constraint, // Constraint, very important
172 "", // Preferences
173 policySeq, // Policy
174 specifiedProps, // Specified Properties
175 1, // Number of wanted results
176 CosTrading::OfferSeq_out(offerSeq_ptr), // results
177 CosTrading::OfferIterator_out(offerIterator_ptr), // more results
178 CosTrading::PolicyNameSeq_out(policyNameSeq_ptr) // Policies
181 // Initialize
182 CORBA::Object_ptr object_ptr = 0;
184 // Check if an offer was made
185 if (offerSeq_ptr != 0)
187 // Insert the pointer into the out class
188 CosTrading::OfferSeq_var offerSeq_var(offerSeq_ptr);
190 // We need at least one offer.
191 if (offerSeq_var->length() >= 1)
193 // now we are all set to read from the sequence the result
194 object_ptr = CORBA::Object::_duplicate (offerSeq_var[0u].reference.in());
196 if (CORBA::is_nil (object_ptr))
197 ORBSVCS_ERROR_RETURN ((LM_ERROR,"Factory_Trader::query: Object reference is nil.\n"), 0);
198 else
199 if (this->debug_level_ >= 2)
200 ORBSVCS_DEBUG ((LM_DEBUG, "Factory_Trader::query: Received a proper object reference.\n"));
202 else
203 ORBSVCS_ERROR ((LM_ERROR, "Factory_Trader::query: OfferSequence.length is smaller than 1.\n"));
205 return object_ptr;
207 catch (const CORBA::Exception& ex)
209 ex._tao_print_exception (
210 "Factory_Trader::query: Failed.\n");
212 // @@ ACE_CHECK_RETURN (?)
213 return 0;