Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / LifeCycle_Service / Factory_Trader.cpp
blobf9daa66dd22bb0e6dcfe4f09ec5eed9aa0e22926
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);
141 catch (const CORBA::Exception& ex)
143 ex._tao_print_exception (
144 "LifeCycle Server (Factory_Trader::export): ""Failed to export factory.\n");
146 // @@ ACE_CHECK*
150 CORBA::Object_ptr
151 Factory_Trader::query (const char* constraint)
155 CosTrading::Lookup::SpecifiedProps specifiedProps;
156 specifiedProps._d(CosTrading::Lookup::all);
158 // Get some pointers for the out parameters of the call.
159 CosTrading::OfferSeq *offerSeq_ptr = 0;
160 CosTrading::OfferIterator_ptr offerIterator_ptr = 0;
161 CosTrading::PolicyNameSeq *policyNameSeq_ptr = 0;
163 // An empty policy sequence
164 CosTrading::PolicySeq policySeq;
166 // Get a reference to the lookup interface
167 CosTrading::Lookup_ptr lookup_ptr = this->trading_Components_ptr_->lookup_if ();
168 // this pointer is deleted when the trader_ptr is deleted
170 // Invoke the query method on the Lookup Interface.
171 lookup_ptr->query ("GenericFactory", // Type name
172 constraint, // Constraint, very important
173 "", // Preferences
174 policySeq, // Policy
175 specifiedProps, // Specified Properties
176 1, // Number of wanted results
177 CosTrading::OfferSeq_out(offerSeq_ptr), // results
178 CosTrading::OfferIterator_out(offerIterator_ptr), // more results
179 CosTrading::PolicyNameSeq_out(policyNameSeq_ptr) // Policies
182 // Initialize
183 CORBA::Object_ptr object_ptr = 0;
185 // Check if an offer was made
186 if (offerSeq_ptr != 0)
188 // Insert the pointer into the out class
189 CosTrading::OfferSeq_var offerSeq_var(offerSeq_ptr);
191 // We need at least one offer.
192 if (offerSeq_var->length() >= 1)
194 // now we are all set to read from the sequence the result
195 object_ptr = CORBA::Object::_duplicate (offerSeq_var[0u].reference.in());
197 if (CORBA::is_nil (object_ptr))
198 ORBSVCS_ERROR_RETURN ((LM_ERROR,"Factory_Trader::query: Object reference is nil.\n"), 0);
199 else
200 if (this->debug_level_ >= 2)
201 ORBSVCS_DEBUG ((LM_DEBUG, "Factory_Trader::query: Received a proper object reference.\n"));
203 else
204 ORBSVCS_ERROR ((LM_ERROR, "Factory_Trader::query: OfferSequence.length is smaller than 1.\n"));
206 return object_ptr;
208 catch (const CORBA::Exception& ex)
210 ex._tao_print_exception (
211 "Factory_Trader::query: Failed.\n");
213 // @@ ACE_CHECK_RETURN (?)
214 return 0;