1 #include "Database_i.h"
3 #include "tao/DynamicInterface/Server_Request.h"
5 #include "tao/AnyTypeCode/NVList.h"
6 #include "tao/AnyTypeCode/TypeCode.h"
7 #include "ace/Null_Mutex.h"
9 DatabaseImpl::Simpler_Database_Malloc::Simpler_Database_Malloc ()
10 // : DATABASE_MALLOC ()
14 DatabaseImpl::Simpler_Database_Malloc::~Simpler_Database_Malloc ()
19 DatabaseImpl::Entry::Entry (CORBA::ORB_ptr orb
,
20 PortableServer::POA_ptr poa
)
21 : orb_ (CORBA::ORB::_duplicate (orb
)),
22 poa_ (PortableServer::POA::_duplicate (poa
))
24 // Get the POA Current object reference
25 CORBA::Object_var obj
=
26 this->orb_
->resolve_initial_references ("POACurrent");
28 // Narrow the object reference to a POA Current reference
30 PortableServer::Current::_narrow (obj
.in ());
33 DatabaseImpl::Entry::~Entry ()
38 DatabaseImpl::Entry::invoke (CORBA::ServerRequest_ptr request
)
40 // The servant determines the key associated with the database
41 // entry represented by self.
42 PortableServer::ObjectId_var oid
=
43 this->poa_current_
->get_object_id ();
45 // Now convert the id into a string
46 CORBA::String_var key
=
47 PortableServer::ObjectId_to_string (oid
.in ());
49 // Get the operation name for this request
50 const char *operation
=
51 request
->operation ();
53 if (ACE_OS::strcmp (operation
,
59 throw CORBA::NO_IMPLEMENT ();
63 DatabaseImpl::Entry::is_a (CORBA::ServerRequest_ptr request
)
65 CORBA::NVList_ptr list
;
66 this->orb_
->create_list (0, list
);
69 any_1
._tao_set_typecode (CORBA::_tc_string
);
71 list
->add_value ("value",
75 request
->arguments (list
);
77 CORBA::NamedValue_ptr nv
= list
->item (0);
79 CORBA::Any_ptr ap
= nv
->value ();
83 const char *object_id
=
84 CORBA::_tc_Object
->id ();
86 CORBA::Boolean result
;
87 if (ACE_OS::strcmp (value
, "IDL:Database/Employee:1.0") == 0
88 || ACE_OS::strcmp (value
, "IDL:Database/Entry:1.0") == 0
89 || ACE_OS::strcmp (value
, object_id
) == 0)
94 CORBA::Any result_any
;
95 CORBA::Any::from_boolean
from_boolean (result
);
96 result_any
<<= from_boolean
;
98 request
->set_result (result_any
);
102 DatabaseImpl::Entry::_primary_interface (const PortableServer::ObjectId
&/*oid*/,
103 PortableServer::POA_ptr
)
108 PortableServer::POA_ptr
109 DatabaseImpl::Entry::_default_POA ()
111 return PortableServer::POA::_duplicate (this->poa_
.in ());
114 DatabaseImpl::Agent::Agent (CORBA::ORB_ptr orb
,
115 PortableServer::POA_ptr poa
)
116 : orb_ (CORBA::ORB::_duplicate (orb
)),
117 poa_ (PortableServer::POA::_duplicate (poa
)),
118 common_servant_ (orb
,
121 this->poa_
->set_servant (&this->common_servant_
);
124 DatabaseImpl::Agent::~Agent ()
129 DatabaseImpl::Agent::create_entry (const char *key
,
130 const char *entry_type
,
131 const Database::NVPairSequence
&initial_attributes
)
133 // Create a new entry in the database:
134 if (ACE_OS::strcmp (entry_type
, "Employee") != 0
135 || initial_attributes
.length () != 2)
136 throw Database::Unknown_Type ();
138 const char *name
= 0;
141 const Database::NamedValue
&first
=
142 initial_attributes
[0];
143 const Database::NamedValue
&second
=
144 initial_attributes
[1];
146 if (ACE_OS::strcmp (first
.name
.in (), "name") != 0
147 || ACE_OS::strcmp (second
.name
.in (), "id") != 0)
148 throw Database::Unknown_Type ();
150 first
.value
>>= name
;
153 Employee
*new_employee
;
154 // This attempts to create a new Employee and throws an exception
155 // and returns a null value if it fails
156 ACE_NEW_THROW_EX (new_employee
,
158 CORBA::NO_MEMORY ());
160 // @@ Should check the return value here and throw an exception if
162 DATABASE::instance ()->bind (key
,
165 ACE_DEBUG ((LM_DEBUG
,
166 "New employee created with name = %C and id = %d\n",
170 // Creates a reference to the CORBA object used to encapsulate
171 // access to the new entry in the database. There is an interface
172 // for each entry type:
173 PortableServer::ObjectId_var obj_id
=
174 PortableServer::string_to_ObjectId (key
);
175 CORBA::String_var repository_id
=
176 DatabaseImpl::entry_type_to_repository_id ("Entry");
178 CORBA::Object_var obj
=
179 this->poa_
->create_reference_with_id (obj_id
.in (),
180 repository_id
.in ());
182 Database::Entry_var entry
= Database::Entry::_narrow (obj
.in ());
184 return entry
._retn ();
188 DatabaseImpl::Agent::find_entry (const char *key
,
189 const char *entry_type
)
191 if (ACE_OS::strcmp (entry_type
,
193 throw Database::Unknown_Type ();
196 Database::Entry_var entry
;
197 if (DATABASE::instance ()->find (key
, temp
) == 0)
200 reinterpret_cast<Employee
*> (temp
);
201 ACE_DEBUG ((LM_DEBUG
,
202 "Employee with key = %C found: name = %C and id = %d\n",
207 // Creates a reference to the CORBA object used to encapsulate
208 // access to the new entry in the database. There is an
209 // interface for each entry type:
210 PortableServer::ObjectId_var obj_id
=
211 PortableServer::string_to_ObjectId (key
);
212 CORBA::String_var repository_id
=
213 DatabaseImpl::entry_type_to_repository_id ("Entry");
214 CORBA::Object_var obj
=
215 this->poa_
->create_reference_with_id (obj_id
.in (),
216 repository_id
.in ());
218 entry
= Database::Entry::_narrow (obj
.in ());
222 ACE_DEBUG ((LM_DEBUG
,
223 "Employee with key = %C not found\n",
226 throw Database::Not_Found ();
229 return entry
._retn ();
233 DatabaseImpl::Agent::destroy_entry (const char *key
,
234 const char *entry_type
)
236 if (ACE_OS::strcmp (entry_type
, "Employee") != 0)
237 throw Database::Unknown_Type ();
240 if (DATABASE::instance ()->unbind (key
, temp
) == 0)
243 reinterpret_cast<Employee
*> (temp
);
244 ACE_DEBUG ((LM_DEBUG
,
245 "Employee with key = %C will be removed from the database: "
246 "name = %C and id = %d\n",
255 ACE_DEBUG ((LM_DEBUG
,
256 "Employee with key = %C not found\n",
259 throw Database::Unknown_Key ();
264 DatabaseImpl::Agent::shutdown ()
266 this->orb_
->shutdown ();
269 PortableServer::POA_ptr
270 DatabaseImpl::Agent::_default_POA ()
272 return PortableServer::POA::_duplicate (this->poa_
.in ());
276 DatabaseImpl::entry_type_to_repository_id (const char *entry_type
)
278 static const char *prefix
= "IDL:Database/";
279 static int prefix_length
= ACE_OS::strlen (prefix
);
280 static const char *suffix
= ":1.0";
281 static int suffix_length
= ACE_OS::strlen (prefix
);
285 ACE_OS::strlen (entry_type
) +
289 char *result
= CORBA::string_alloc (len
);
290 ACE_OS::sprintf (result
,
298 DatabaseImpl::Employee::Employee (const char* name
,
306 DatabaseImpl::Employee::~Employee ()
308 DATABASE::instance ()->free (this->name_
);
312 DatabaseImpl::Employee::name () const
318 DatabaseImpl::Employee::name (const char* name
)
320 DATABASE::instance ()->free (this->name_
);
323 (char *) DATABASE::instance ()->malloc (ACE_OS::strlen (name
) + 1);
325 ACE_OS::strcpy (this->name_
,
330 DatabaseImpl::Employee::id () const
336 DatabaseImpl::Employee::id (CORBA::Long id
)
342 DatabaseImpl::Employee::operator new (size_t size
)
344 return DATABASE::instance ()->malloc (size
);
348 DatabaseImpl::Employee::operator delete (void *pointer
)
350 DATABASE::instance ()->free (pointer
);
353 /// Overloaded new operator, nothrow_t variant.
355 DatabaseImpl::Employee::operator new (size_t size
, const std::nothrow_t
&)
357 return DATABASE::instance ()->malloc (size
);
361 DatabaseImpl::Employee::operator delete (void *ptr
, const std::nothrow_t
&) noexcept
363 DATABASE::instance ()->free (ptr
);
366 ACE_SINGLETON_TEMPLATE_INSTANTIATE(ACE_Singleton
, DatabaseImpl::Simpler_Database_Malloc
, ACE_Null_Mutex
);