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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
|
// **********************************************************************
//
// Copyright (c) 2001
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************
#include <Ice/Ice.h>
#include <IcePack/ComponentBuilder.h>
#include <Yellow/Yellow.h>
#include <xercesc/parsers/SAXParser.hpp>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#include <stack>
#include <iterator>
#include <fstream>
using namespace std;
using namespace IcePack;
namespace IcePack
{
static string
toString(const XMLCh* ch)
{
char* t = XMLString::transcode(ch);
string s(t);
delete[] t;
return s;
}
//
// Create a directory.
//
// TODO: IcePatch implements portable version of filesystem primitives
// in the Util.cpp file. We should move these primitives in IceUtil
// and use them here.
//
class CreateDirectory : public Task
{
public:
CreateDirectory(const string& name, bool force) :
_name(name),
_force(force)
{
}
virtual void
execute()
{
if(mkdir(_name.c_str(), 0755) != 0)
{
DeploymentException ex;
ex.reason = "couldn't create directory " + _name + ": " + strerror(getSystemErrno());
throw ex;
}
}
virtual void
undo()
{
if(_force)
{
//
// Only remove files inside the directory, don't remove
// directories (deployed directory should only be created
// through this task so other directories should be
// removed by another task).
//
struct dirent **namelist;
int n = ::scandir(_name.c_str(), &namelist, 0, alphasort);
if(n > 0)
{
Ice::StringSeq entries;
entries.reserve(n);
for(int i = 0; i < n; ++i)
{
string name = namelist[i]->d_name;
free(namelist[i]);
entries.push_back(_name + "/" + name);
}
free(namelist);
for(Ice::StringSeq::iterator p = entries.begin(); p != entries.end(); ++p)
{
struct stat buf;
if(::stat(p->c_str(), &buf) != 0)
{
if(errno != ENOENT)
{
//
// TODO: log error
//
}
}
else if(S_ISREG(buf.st_mode))
{
if(unlink(p->c_str()) != 0)
{
//
// TODO: log error
//
}
}
}
}
else if(n < 0)
{
//
// TODO: something seems to be wrong if we can't scan
// the directory. Print a warning.
//
}
}
if(rmdir(_name.c_str()) != 0)
{
DeploymentException ex;
ex.reason = "couldn't remove directory " + _name + ": " + strerror(getSystemErrno());
throw ex;
}
}
private:
string _name;
bool _force;
};
//
// Generate a configuration file from a property set.
//
class GenerateConfiguration : public Task
{
class WriteConfigProperty : public unary_function<Ice::PropertyDict::value_type, string>
{
public:
string
operator()(const Ice::PropertyDict::value_type& p) const
{
return p.first + "=" + p.second;
}
};
public:
GenerateConfiguration(const string& file, const Ice::PropertiesPtr& properties) :
_file(file),
_properties(properties)
{
}
virtual void
execute()
{
ofstream configfile;
configfile.open(_file.c_str(), ios::out);
if(!configfile)
{
DeploymentException ex;
ex.reason = "couldn't create configuration file: " + _file;
throw ex;
}
Ice::PropertyDict props = _properties->getPropertiesForPrefix("");
transform(props.begin(), props.end(), ostream_iterator<string>(configfile,"\n"), WriteConfigProperty());
configfile.close();
}
virtual void
undo()
{
if(unlink(_file.c_str()) != 0)
{
DeploymentException ex;
ex.reason = "couldn't remove configuration file: " + _file;
throw ex;
}
}
private:
string _file;
Ice::PropertiesPtr _properties;
};
//
// Register an offer with the yellow page service.
//
class RegisterOffer : public Task
{
public:
RegisterOffer(const Yellow::AdminPrx& admin, const string& offer, const Ice::ObjectPrx& proxy) :
_admin(admin),
_offer(offer),
_proxy(proxy)
{
}
virtual void
execute()
{
try
{
_admin->add(_offer, _proxy);
}
catch(const Ice::LocalException& lex)
{
ostringstream os;
os << "couldn't contact Yellow to add offer:\n" << lex << ends;
OfferDeploymentException ex;
ex.reason = os.str();
ex.intf = _offer;
ex.proxy = _proxy;
throw ex;
}
}
virtual void
undo()
{
assert(_admin);
try
{
_admin->remove(_offer, _proxy);
}
catch(const Yellow::NoSuchOfferException& lex)
{
ostringstream os;
os << "couldn't remove offer:\n" << lex << ends;
OfferDeploymentException ex;
ex.reason = os.str();
ex.intf = _offer;
ex.proxy = _proxy;
throw ex;
}
catch(const Ice::LocalException& lex)
{
ostringstream os;
os << "couldn't contact Yellow to remove offer:\n" << lex << ends;
OfferDeploymentException ex;
ex.reason = os.str();
ex.intf = _offer;
ex.proxy = _proxy;
throw ex;
}
}
private:
Yellow::AdminPrx _admin;
string _offer;
Ice::ObjectPrx _proxy;
};
}
IcePack::DeploySAXParseException::DeploySAXParseException(const string& msg, const Locator*const locator)
: SAXParseException(XMLString::transcode(msg.c_str()), *locator)
{
}
IcePack::ParserDeploymentWrapperException::ParserDeploymentWrapperException(const ParserDeploymentException& ex)
: _exception(ex)
{
}
void
IcePack::ParserDeploymentWrapperException::throwParserDeploymentException() const
{
throw _exception;
}
IcePack::ComponentErrorHandler::ComponentErrorHandler(ComponentBuilder& builder) :
_builder(builder)
{
}
void
IcePack::ComponentErrorHandler::warning(const SAXParseException& exception)
{
string s = toString(exception.getMessage());
cerr << "warning: " << s << endl;
}
void
IcePack::ComponentErrorHandler::error(const SAXParseException& exception)
{
throw exception;
// string s = toString(exception.getMessage());
// cerr << "error: " << s << endl;
}
void
IcePack::ComponentErrorHandler::fatalError(const SAXParseException& exception)
{
throw exception;
// string s = toString(exception.getMessage());
// cerr << "fatal:" << s << endl;
}
void
IcePack::ComponentErrorHandler::resetErrors()
{
}
IcePack::ComponentHandler::ComponentHandler(ComponentBuilder& builder) :
_builder(builder),
_isCurrentTargetDeployable(true)
{
}
void
IcePack::ComponentHandler::characters(const XMLCh *const chars, const unsigned int length)
{
_elements.top().assign(toString(chars));
}
void
IcePack::ComponentHandler::startElement(const XMLCh *const name, AttributeList &attrs)
{
_elements.push("");
if(!isCurrentTargetDeployable())
{
return;
}
string str = toString(name);
if(str == "property")
{
string value = getAttributeValueWithDefault(attrs, "value", "");
if(value.empty())
{
value = _builder.toLocation(getAttributeValueWithDefault(attrs, "location", ""));
}
_builder.addProperty(getAttributeValue(attrs, "name"), value);
}
else if(str == "adapter")
{
if(!_currentAdapterId.empty())
{
throw DeploySAXParseException("Adapter element enclosed in an adapter element is not allowed", _locator);
}
//
// If the id is not specified, we ask the builder to generate
// an id for us based on the adapter name.
//
string name = getAttributeValue(attrs, "name");
if(name.empty())
{
throw DeploySAXParseException("empty adapter name", _locator);
}
_currentAdapterId = getAttributeValueWithDefault(attrs, "id", _builder.getDefaultAdapterId(name));
}
else if(str == "offer")
{
_builder.addOffer(getAttributeValue(attrs, "interface"),
_currentAdapterId,
getAttributeValue(attrs, "identity"));
}
else if(str == "target")
{
if(!_currentTarget.empty())
{
throw DeploySAXParseException("Target element enclosed in a target element is not allowed", _locator);
}
_isCurrentTargetDeployable = _builder.isTargetDeployable(getAttributeValue(attrs, "name"));
}
}
void
IcePack::ComponentHandler::endElement(const XMLCh *const name)
{
_elements.pop();
string str = toString(name);
if(str == "target")
{
_isCurrentTargetDeployable = true;
}
if(isCurrentTargetDeployable())
{
if(str == "adapter")
{
_currentAdapterId = "";
}
}
}
void
IcePack::ComponentHandler::ignorableWhitespace(const XMLCh*const, const unsigned int)
{
}
void
IcePack::ComponentHandler::processingInstruction(const XMLCh*const, const XMLCh*const)
{
}
void
IcePack::ComponentHandler::resetDocument()
{
}
void
IcePack::ComponentHandler::startDocument()
{
}
void
IcePack::ComponentHandler::endDocument()
{
}
void
IcePack::ComponentHandler::setDocumentLocator(const Locator *const locator)
{
_builder.setDocumentLocator(locator);
_locator = locator;
}
string
IcePack::ComponentHandler::getAttributeValue(const AttributeList& attrs, const string& name) const
{
XMLCh* n = XMLString::transcode(name.c_str());
const XMLCh* value = attrs.getValue(n);
delete[] n;
if(value == 0)
{
throw DeploySAXParseException("missing attribute '" + name + "'", _locator);
}
return _builder.substitute(toString(value));
}
string
IcePack::ComponentHandler::getAttributeValueWithDefault(const AttributeList& attrs, const string& name,
const string& def) const
{
XMLCh* n = XMLString::transcode(name.c_str());
const XMLCh* value = attrs.getValue(n);
delete[] n;
if(value == 0)
{
return _builder.substitute(def);
}
else
{
return _builder.substitute(toString(value));
}
}
string
IcePack::ComponentHandler::toString(const XMLCh* ch) const
{
return IcePack::toString(ch);
}
string
IcePack::ComponentHandler::elementValue() const
{
return _elements.top();
}
bool
IcePack::ComponentHandler::isCurrentTargetDeployable() const
{
return _isCurrentTargetDeployable;
}
IcePack::ComponentBuilder::ComponentBuilder(const Ice::CommunicatorPtr& communicator,
const map<string, string>& variables,
const vector<string>& targets) :
_communicator(communicator),
_properties(Ice::createProperties()),
_variables(variables),
_targets(targets)
{
}
void
IcePack::ComponentBuilder::parse(const string& xmlFile, ComponentHandler& handler)
{
//
// Setup the base directory for this deploment descriptor to the
// location of the desciptor file.
//
string::size_type end = xmlFile.find_last_of('/');
if(end != string::npos)
{
_variables["basedir"] = xmlFile.substr(0, end);
}
if(_variables["basedir"].empty())
{
_variables["basedir"] = ".";
}
SAXParser* parser = new SAXParser;
try
{
parser->setValidationScheme(SAXParser::Val_Never);
ComponentErrorHandler err(*this);
parser->setDocumentHandler(&handler);
parser->setErrorHandler(&err);
parser->parse(xmlFile.c_str());
}
catch(const ParserDeploymentWrapperException& ex)
{
//
// Throw the exception wrapped in ex.
//
ex.throwParserDeploymentException();
}
catch(const SAXParseException& e)
{
delete parser;
ostringstream os;
os << xmlFile << ":" << e.getLineNumber() << ": " << toString(e.getMessage());
ParserDeploymentException ex;
ex.component = _variables["fqn"];
ex.reason = os.str();
throw ex;
}
catch(const SAXException& e)
{
delete parser;
ostringstream os;
os << xmlFile << ": SAXException: " << toString(e.getMessage());
ParserDeploymentException ex;
ex.component = _variables["fqn"];
ex.reason = os.str();
throw ex;
}
catch(const XMLException& e)
{
delete parser;
ostringstream os;
os << xmlFile << ": XMLException: " << toString(e.getMessage());
ParserDeploymentException ex;
ex.component = _variables["fqn"];
ex.reason = os.str();
throw ex;
}
catch(...)
{
delete parser;
ostringstream os;
os << xmlFile << ": unknown exception while parsing file";
ParserDeploymentException ex;
ex.component = _variables["fqn"];
ex.reason = os.str();
throw ex;
}
int rc = parser->getErrorCount();
delete parser;
if(rc > 0)
{
ParserDeploymentException ex;
ex.component = _variables["fqn"];
ex.reason = xmlFile + ": parse error";
throw ex;
}
}
void
IcePack::ComponentBuilder::setDocumentLocator(const Locator* locator)
{
_locator = locator;
}
bool
IcePack::ComponentBuilder::isTargetDeployable(const string& target) const
{
map<string, string>::const_iterator p = _variables.find("fqn");
assert(p != _variables.end());
const string fqn = p->second;
for(vector<string>::const_iterator p = _targets.begin(); p != _targets.end(); ++p)
{
if((*p) == target)
{
return true;
}
else
{
string componentTarget;
string::size_type end = 0;
while(end != string::npos)
{
//
// Add the first component name from the component
// fully qualified name to the target and see if
// matches.
//
end = fqn.find('.', end);
if(end == string::npos)
{
componentTarget = fqn + "." + target;
}
else
{
componentTarget = fqn.substr(0, end) + "." + target;
++end;
}
if((*p) == componentTarget)
{
return true;
}
}
}
}
return false;
}
void
IcePack::ComponentBuilder::execute()
{
vector<TaskPtr>::iterator p;
for(p = _tasks.begin(); p != _tasks.end(); p++)
{
try
{
(*p)->execute();
}
catch(DeploymentException& ex)
{
if(ex.component.empty())
{
ex.component = _variables["fqn"];
}
undoFrom(p);
throw;
}
}
}
void
IcePack::ComponentBuilder::undo()
{
for(vector<TaskPtr>::reverse_iterator p = _tasks.rbegin(); p != _tasks.rend(); p++)
{
try
{
(*p)->undo();
}
catch(const DeploymentException& ex)
{
ostringstream os;
os << "exception while removing component " << (ex.component.empty() ? _variables["fqn"] : ex.component);
os << ":\n" << ex << ": " << ex.reason;
_communicator->getLogger()->warning(os.str());
}
}
}
void
IcePack::ComponentBuilder::createDirectory(const string& name, bool force)
{
string path = _variables["datadir"] + (name.empty() || name[0] == '/' ? name : "/" + name);
_tasks.push_back(new CreateDirectory(path, force));
}
void
IcePack::ComponentBuilder::createConfigFile(const string& name)
{
assert(!name.empty());
_configFile = _variables["datadir"] + (name[0] == '/' ? name : "/" + name);
_tasks.push_back(new GenerateConfiguration(_configFile, _properties));
}
void
IcePack::ComponentBuilder::addProperty(const string& name, const string& value)
{
_properties->setProperty(name, value);
}
void
IcePack::ComponentBuilder::addOffer(const string& offer, const string& adapterId, const string& identity)
{
assert(!adapterId.empty());
if(!_yellowAdmin)
{
string msg = "Can't find a running Yellow service to deploy offers";
throw DeploySAXParseException(msg, _locator);
}
Ice::ObjectPrx object = _communicator->stringToProxy(identity + "@" + adapterId);
assert(object);
_tasks.push_back(new RegisterOffer(_yellowAdmin, offer, object));
}
void
IcePack::ComponentBuilder::overrideBaseDir(const string& basedir)
{
if(basedir[0] == '/')
{
_variables["basedir"] = basedir;
}
else
{
_variables["basedir"] += "/" + basedir;
}
}
//
// Compute an adapter id for a given adapter name.
//
string
IcePack::ComponentBuilder::getDefaultAdapterId(const string& name)
{
//
// Concatenate the component name to the adapter name.
//
return name + "-" + _variables["name"];
}
//
// Returns a path including the base directory if path is a relative
// path.
//
string
IcePack::ComponentBuilder::toLocation(const string& path) const
{
if(path.empty())
{
return "";
}
map<string, string>::const_iterator p = _variables.find("basedir");
assert(p != _variables.end());
return path[0] != '/' ? p->second + "/" + path : path;
}
//
// Substitute variables with their values.
//
string
IcePack::ComponentBuilder::substitute(const string& v) const
{
string value(v);
string::size_type beg;
string::size_type end = 0;
while((beg = value.find("${")) != string::npos)
{
end = value.find("}", beg);
if(end == string::npos)
{
throw DeploySAXParseException("malformed variable name in the '" + value + "' value", _locator);
}
string name = value.substr(beg + 2, end - beg - 2);
map<string, string>::const_iterator p = _variables.find(name);
if(p == _variables.end())
{
throw DeploySAXParseException("unknown variable name in the '" + value + "' value", _locator);
}
value.replace(beg, end - beg + 1, p->second);
}
return value;
}
vector<string>
IcePack::ComponentBuilder::toTargets(const string& targets) const
{
vector<string> result;
if(!targets.empty())
{
const string delim = " \t\n\r";
string::size_type beg = 0;
string::size_type end = 0;
do
{
end = targets.find_first_of(delim, end);
if(end == string::npos)
{
end = targets.size();
}
result.push_back(targets.substr(beg, end - beg));
beg = ++end;
}
while(end < targets.size());
}
return result;
}
void
IcePack::ComponentBuilder::undoFrom(vector<TaskPtr>::iterator p)
{
if(p != _tasks.begin())
{
for(vector<TaskPtr>::reverse_iterator q(p); q != _tasks.rend(); q++)
{
try
{
(*q)->undo();
}
catch(const DeploymentException& ex)
{
ostringstream os;
os << "exception while removing component " << _variables["fqn"] << ": " << ex.reason << ":" << endl;
os << ex;
_communicator->getLogger()->warning(os.str());
}
}
}
}
|