summaryrefslogtreecommitdiff
path: root/TAO/CIAO/DAnCE/new_RepositoryManager/new_RepositoryManager_Impl.cpp
blob: 47ec43b39726e31338648eb8176fc5ea5d9f69c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// $Id$

// -*- C++ -*-
//
// $Id$

// ****  Code generated by the The ACE ORB (TAO) IDL Compiler ****
// TAO and the TAO IDL Compiler have been developed by:
//       Center for Distributed Object Computing
//       Washington University
//       St. Louis, MO
//       USA
//       http://www.cs.wustl.edu/~schmidt/doc-center.html
// and
//       Distributed Object Computing Laboratory
//       University of California at Irvine
//       Irvine, CA
//       USA
//       http://doc.ece.uci.edu/
// and
//       Institute for Software Integrated Systems
//       Vanderbilt University
//       Nashville, TN
//       USA
//       http://www.isis.vanderbilt.edu/
//
// Information about TAO is available at:
//     http://www.cs.wustl.edu/~schmidt/TAO.html

// TAO_IDL - Generated from 
// .\be\be_codegen.cpp:939

#include "new_RepositoryManager_Impl.h"

#include "ace/OS_NS_fcntl.h"	  //for open
#include "ace/OS_NS_unistd.h"	  //for close
#include "ace/OS_NS_sys_stat.h"   //for filesize and fstat and mkdir
#include "ace/OS_NS_string.h"	  //for ACE_CString


//to remove a file or dir from the local filesystem need remove () from stdio.h
// ---> need to include ace/OS_NS_stdio.h which would include the correct file for any OS!
#include "ace/OS_NS_stdio.h"

#include "ZIP_Wrapper.h"
#include "ace/Containers_T.h"			//for ACE_Double_Linked_List
#include "ace/Malloc_Allocator.h"		//for ACE_New_Allocator needed by the doubly link list

#include "Config_Handlers/cdp.hpp"
#include "Config_Handlers/Deployment.hpp"
#include "Config_Handlers/DP_Handler.h"
#include "ciao/Deployment_DataC.h"
#include "Config_Handlers/XML_Helper.h"

#include "ace/Thread.h"					//for obtaining the ID of the current thread
#include "ace/OS_NS_stdlib.h"			//for itoa ()


#include <iostream>
using namespace std;


// Implementation skeleton constructor
CIAO_new_RepositoryManagerDaemon_i::CIAO_new_RepositoryManagerDaemon_i (CORBA::ORB_ptr the_orb)
: the_orb_ (CORBA::ORB::_duplicate (the_orb))
{
	//create directory in which the packages will be stored

	ACE_OS::mkdir(RM_STORAGE_PATH);	
	//if dir already exists a -1 is returned
	//we ignore this, just need to make sure the directory exists
}

// Implementation skeleton destructor
CIAO_new_RepositoryManagerDaemon_i::~CIAO_new_RepositoryManagerDaemon_i (void)
{
	this->packages_.unbind_all ();
}

void CIAO_new_RepositoryManagerDaemon_i::shutdown (
    
  )
  ACE_THROW_SPEC ((
    CORBA::SystemException
  ))
{
  // Add your implementation here
	
	this->packages_.unbind_all ();
	
	this->the_orb_->shutdown (1);

	ACE_OS::exit (0);
}

void CIAO_new_RepositoryManagerDaemon_i::installPackage (
    const char * installationName,
    const ::Deployment::Package & the_package,
    ::CORBA::Boolean replace
  )
  ACE_THROW_SPEC ((
    CORBA::SystemException,
    ::Deployment::NameExists,
    ::Deployment::PackageError
  ))
{
  // Add your implementation here

	ACE_Hash_Map_Entry <ACE_CString, ACE_CString> *entry;

	if (this->packages_.find (ACE_CString (installationName), entry) == 0)
		ACE_THROW (Deployment::NameExists ());

	ACE_CString path (RM_STORAGE_PATH);
	path += "/";
	path += installationName;
	path += ".cpk";

	int retn_code = this->write_to_disk (path.c_str (), the_package.get_buffer (), the_package.length ());

	if (retn_code == -1)
		ACE_THROW (Deployment::PackageError ());

	//insert the package into the database
	this->packages_.bind (ACE_CString (installationName), path);
}

::Deployment::DeploymentPlan * CIAO_new_RepositoryManagerDaemon_i::retrievePlan (
    const char * packageName
  )
  ACE_THROW_SPEC ((
    CORBA::SystemException
  ))
{
  // Add your implementation here

	cout << "in retrieve Plan!" << endl;

	//form the path to the archive in the local filesystem
	ACE_CString path (RM_STORAGE_PATH);
	path += "/";
	path += packageName;
	path += ".cpk";

	//create a ZIP wrapper
	ZIP_Wrapper zip;

	//create a doubly link list
	ACE_New_Allocator allocator;
	ACE_Double_Linked_List<ZIP_File_Info> list (&allocator);


	cout << "Getting the list ..." <<endl;

	//get the list of files in the package and figure out the name of the Deployment Plan
	zip.file_list_info (const_cast<char*> (path.c_str ()), list);
	ACE_CString plan_name;
	CORBA::Boolean found = false;

	while (!list.is_empty ())
	{
		ZIP_File_Info* inf = list.delete_head ();
		if (!found && ACE_OS::strstr (inf->name_.c_str (), ".cdp"))
		{
			cout << inf->name_.c_str () << endl;
			plan_name = inf->name_.c_str ();
			found = true;
		}
		delete inf;
	}

	//there is not deployment plan in the package
	if (!found)
		ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%p\n"),
						   ACE_TEXT ("[RM::retrievePlan] no DeploymentPlan in package!\n")),
                           0);

	//read the Deployment plan from the package
	ACE_Message_Block file (0,0);
	if (!zip.get_file( const_cast<char*> (path.c_str ()), const_cast<char*> (plan_name.c_str ()), file))
		ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%p\n"),
						   ACE_TEXT ("[RM::retrievePlan] Unable to retrieve file!\n")),
                           0);


	//if file is 0 length XERCESC Fails!!!

	//We are using Xercesc in the Config_Handlers and unfortunately its API only
	//takes a file in the local file system as an argument, thus need to
	//write out the contents of the deployent plan to a file
	//in the current directory. I use the thread id to guarrantee 
	//lack of race conditions if multithreading is enabled
	unsigned int thread_id = ACE_Thread::self ();
	char temp[128];

	ACE_CString local_file ("_dp_temp_");
	local_file += ACE_OS::itoa (thread_id, temp, 10);

	//write the file to disk
	if(!this->write_to_disk (const_cast<char*> (local_file.c_str ()), file))
		ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%p\n"),
						   ACE_TEXT ("[RM::retrievePlan] DeploymentPlan preparation failule!\n")),
                           0);

	//parse the Deployment plan and return the resulting data structure
	CIAO::Config_Handlers::XML_Helper the_helper;
	xercesc::DOMDocument *doc;

	if (!(doc = the_helper.create_dom (local_file.c_str ())))
		return 0;

	CIAO::Config_Handlers::DeploymentPlan dp = CIAO::Config_Handlers::deploymentPlan (doc);
	CIAO::Config_Handlers::DP_Handler dp_handler (dp);

	//we no longer need the file with the deployment plan, so lets erase it!
	remove (local_file.c_str ());

	//the non-const version of this function releases the ownership of the plan
	//before it returns it so we are safe to return the return value
	return dp_handler.plan ();
}

::Deployment::Package * CIAO_new_RepositoryManagerDaemon_i::findPackageByName (
    const char * name
  )
  ACE_THROW_SPEC ((
    CORBA::SystemException,
    ::Deployment::NoSuchName
  ))
{
  // Add your implementation here

	// Find out if the package was installed in the repository,
	// return it if found or throw and exception otherwise

    ACE_Hash_Map_Entry <ACE_CString, ACE_CString> *entry = 0;

      if (this->packages_.find (ACE_CString (name), entry) != 0)
		  ACE_THROW (Deployment::NoSuchName ());							// package not found

 
	  size_t length = 0;	 
	  CORBA::Octet* buffer = this->read_from_disk (entry->int_id_.c_str (), length);

	  Deployment::Package* package = new Deployment::Package (
		length,				//max of the sequence
		length,				//length of the sequence
		buffer,				//data to be stored within the sequence
		true				//take ownership of the data
		);

	  return package;
}

::Deployment::Implementation * CIAO_new_RepositoryManagerDaemon_i::findImplementationByName (
     const char * implementation_name, 
	 const char * package_name
  )
  ACE_THROW_SPEC ((
    CORBA::SystemException,
    ::Deployment::NoSuchName
  ))
{
  // Add your implementation here

	//form the path to the archive in the local filesystem
	ACE_CString path (RM_STORAGE_PATH);
	path += "/";
	path += package_name;
	path += ".cpk";


	ACE_CString artifact ("implementations/");
	artifact += implementation_name;

	//create a ZIP wrapper
	ZIP_Wrapper zip;

	//read in artifact
	ACE_Message_Block file (0,0);
	if (!zip.get_file( const_cast<char*> (path.c_str ()), const_cast<char*> (artifact.c_str ()), file))
	{
		cout << "Unable to retrieve file!" << endl;
		ACE_THROW_RETURN (Deployment::NoSuchName (), 0);
	}

	Deployment::Implementation* impl = new Deployment::Implementation ();

	//initialize the Implementation struct and allocate the buffer to hold the data
	impl->name = implementation_name;
	impl->the_implementation.replace (
		file.total_length (),									//max of the sequence
		file.total_length (),									//length of the sequence
		Deployment::Artifact::allocbuf (file.total_length ()),	//data to be stored within the sequence
		true													//take ownership of the data
		);

	//impl->the_platform = UNDEF;		//Deployment::Platform

	CORBA::Octet* ptr = impl->the_implementation.get_buffer ();

	//place the Artifact in the Implementation struct
	for (ACE_Message_Block * curr = &file; curr != 0; curr = curr->cont ())
	{
		if (!ACE_OS::memcpy (ptr, curr->rd_ptr(), curr->length()))
			ACE_THROW_RETURN (CORBA::INTERNAL (), 0);

		ptr += curr->length ();
	}

	return impl;
}

::Deployment::Package * CIAO_new_RepositoryManagerDaemon_i::findPackageByUUID (
    const char * UUID
  )
  ACE_THROW_SPEC ((
    CORBA::SystemException,
    ::Deployment::NoSuchName
  ))
{
  // Add your implementation here
  ACE_THROW_RETURN (CORBA::NO_IMPLEMENT (), 0);
}

::Deployment::Implementation * CIAO_new_RepositoryManagerDaemon_i::findImplementationByUUID (
    const char * UUID
  )
  ACE_THROW_SPEC ((
    CORBA::SystemException,
    ::Deployment::NoSuchName
  ))
{
  // Add your implementation here
	ACE_THROW_RETURN (CORBA::NO_IMPLEMENT (), 0);
}

::CORBA::StringSeq * CIAO_new_RepositoryManagerDaemon_i::getAllPackageNames (
    
  )
  ACE_THROW_SPEC ((
    CORBA::SystemException
  ))
{
  // Add your implementation here

	CORBA::ULong num_entries = this->packages_.current_size (); 
	CORBA::StringSeq_var seq = new CORBA::StringSeq (num_entries);

	CORBA::ULong index = 0;
	for (RepositoryDatabase_Iterator iter = this->packages_.begin ();
									 iter != this->packages_.end () && index < num_entries;
									 iter++)

		seq[index] = const_cast<char*> (((*iter).int_id_).c_str ());	//this looks hideous, but as lond as it works!


	return seq._retn ();		//release the underlying CORBA::StringSeq
}



// Find out if the package was installed in the repository,
// delete it if found or throw and exception otherwise

void CIAO_new_RepositoryManagerDaemon_i::deletePackage (
    const char * installationName
  )
  ACE_THROW_SPEC ((
    CORBA::SystemException,
    ::Deployment::NoSuchName
  ))
{
  // Add your implementation here

    ACE_Hash_Map_Entry <ACE_CString, ACE_CString> *entry;

    if (this->packages_.find (ACE_CString (installationName), entry) != 0)
		ACE_THROW (Deployment::NoSuchName ());
	else
		this->packages_.unbind (installationName);

	//actually delete the package here!

	ACE_CString path (RM_STORAGE_PATH);
	path += "/";
	path += installationName;
	path += ".cpk";

	remove (path.c_str ());
}


int CIAO_new_RepositoryManagerDaemon_i::write_to_disk (
	const char* full_path, 
	const CORBA::Octet* buffer, 
	size_t length
	)
{
	
	// Open a file handle to the local filesystem
    ACE_HANDLE handle = ACE_OS::open (full_path, _O_CREAT | _O_TRUNC | O_WRONLY);
    if (handle == ACE_INVALID_HANDLE)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%p\n"),
                           ACE_TEXT ("[RM::write_to_disk] file creation error")),
                           -1);

	//write the data to the file
	if (ACE_OS::write (handle, buffer, length) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%p\n"),
						   ACE_TEXT ("[RM::write_to_disk] file write error")),
                           -1);

	// Close the file handle
    ACE_OS::close (handle);

	return 1;
}


int CIAO_new_RepositoryManagerDaemon_i::write_to_disk 
	(const char* full_path, 
	 ACE_Message_Block& mb,
	 bool replace
	 )
{

	ACE_stat stat;

	if (ACE_OS::stat(full_path, &stat) != -1 && !replace)
		return 0;
	
	// Open a file handle to the local filesystem
    ACE_HANDLE handle = ACE_OS::open (full_path, _O_CREAT | _O_TRUNC | O_WRONLY);
    if (handle == ACE_INVALID_HANDLE)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%p\n"),
                           ACE_TEXT ("[RM::write_to_disk] file creation error")),
                           -1);

	//write the data to the file
	for (ACE_Message_Block * curr = &mb; curr != 0; curr = curr->cont ())
		if (ACE_OS::write_n (handle, curr->rd_ptr(), curr->length()) == -1)
		    ACE_ERROR_RETURN ((LM_ERROR,
		                       ACE_TEXT ("%p\n"),
		                       ACE_TEXT ("write error")),
		                      -1);
		
	// Close the file handle
    ACE_OS::close (handle);

	return 1;
}


CORBA::Octet* CIAO_new_RepositoryManagerDaemon_i::read_from_disk (
	const char* full_path,
	size_t &length
	)
{
	//open the file

	ACE_HANDLE handle = ACE_OS::open (full_path, O_RDONLY);
    if (handle == ACE_INVALID_HANDLE)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%p\n"),
                           ACE_TEXT ("[RM::read_from_disk] file open error")),
                           0);

	ACE_stat file_info;

	ACE_OS::fstat (handle, &file_info);

	CORBA::Octet* buffer = new CORBA::Octet[file_info.st_size];

	if (buffer == 0)
		return 0;

	//read the contents of the file into the buffer
	if (ACE_OS::read_n (handle, buffer, file_info.st_size) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT ("%p\n"),
						   ACE_TEXT ("[RM::write_to_disk] file write error")),
                           0);

	// Close the file handle
    ACE_OS::close (handle);

	length = file_info.st_size;
	return buffer;
}