Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "PropertyLoader.hpp"
00041 #include "rtt-config.h"
00042 #ifdef OROPKG_CORELIB_PROPERTIES_MARSHALLING
00043 #include ORODAT_CORELIB_PROPERTIES_MARSHALLING_INCLUDE
00044 #include ORODAT_CORELIB_PROPERTIES_DEMARSHALLING_INCLUDE
00045 #endif
00046 #include "../Logger.hpp"
00047 #include "../TaskContext.hpp"
00048 #include "PropertyBagIntrospector.hpp"
00049 #include "../types/PropertyComposition.hpp"
00050 #include <fstream>
00051
00052 using namespace std;
00053 using namespace RTT;
00054 using namespace RTT::detail;
00055
00056 PropertyLoader::PropertyLoader(TaskContext *task)
00057 : target(task->provides().get())
00058 {}
00059
00060 PropertyLoader::PropertyLoader(Service *service)
00061 : target(service)
00062 {}
00063
00064 bool PropertyLoader::load(const std::string& filename) const
00065 {
00066 Logger::In in("PropertyLoader:load");
00067 #ifndef OROPKG_CORELIB_PROPERTIES_MARSHALLING
00068 log(Error) << "No Property DemarshallInterface configured !" << endlog();
00069 return false;
00070
00071 #else
00072 if ( target->properties() == 0) {
00073 log(Error) << "Service " <<target->getName()<<" has no Properties to configure." << endlog();
00074 return false;
00075 }
00076
00077 log(Info) << "Loading properties into Service '" <<target->getName()
00078 <<"' with '"<<filename<<"'."<< endlog();
00079 bool failure = false;
00080 OROCLS_CORELIB_PROPERTIES_DEMARSHALLING_DRIVER* demarshaller = 0;
00081 try
00082 {
00083 demarshaller = new OROCLS_CORELIB_PROPERTIES_DEMARSHALLING_DRIVER (filename);
00084 } catch (...) {
00085 log(Error) << "Could not open file "<< filename << endlog();
00086 return false;
00087 }
00088 try {
00089 PropertyBag propbag;
00090 vector<ActionInterface*> assignComs;
00091
00092 if ( demarshaller->deserialize( propbag ) )
00093 {
00094
00095 PropertyBag composed_props;
00096 if ( composePropertyBag(propbag, composed_props) == false) {
00097 delete demarshaller;
00098 return false;
00099 }
00100
00101 PropertyBag backup;
00102 copyProperties( backup, *target->properties() );
00103
00104 if ( refreshProperties( *target->properties(), composed_props, false) ) {
00105
00106
00107 failure = !updateProperties( *target->properties(), composed_props );
00108 } else {
00109
00110 refreshProperties( *target->properties(), backup, false );
00111 failure = true;
00112 }
00113
00114 deletePropertyBag( backup );
00115 }
00116 else
00117 {
00118 log(Error) << "Some error occured while parsing "<< filename.c_str() <<endlog();
00119 failure = true;
00120 }
00121 } catch (...)
00122 {
00123 log(Error)
00124 << "Uncaught exception in deserialise !"<< endlog();
00125 failure = true;
00126 }
00127 delete demarshaller;
00128 return !failure;
00129 #endif // OROPKG_CORELIB_PROPERTIES_MARSHALLING
00130
00131 }
00132
00133 bool PropertyLoader::configure(const std::string& filename, bool all ) const
00134 {
00135 Logger::In in("PropertyLoader:configure");
00136 #ifndef OROPKG_CORELIB_PROPERTIES_MARSHALLING
00137 log(Error) << "No Property DemarshallInterface configured !" << endlog();
00138 return false;
00139
00140 #else
00141 if ( target->properties() == 0) {
00142 log(Error) << "Service " <<target->getName()<<" has no Properties to configure." << endlog();
00143 return false;
00144 }
00145
00146 log(Info) << "Configuring Service '" <<target->getName()
00147 <<"' with '"<<filename<<"'."<< endlog();
00148 bool failure = false;
00149 OROCLS_CORELIB_PROPERTIES_DEMARSHALLING_DRIVER* demarshaller = 0;
00150 try
00151 {
00152 demarshaller = new OROCLS_CORELIB_PROPERTIES_DEMARSHALLING_DRIVER (filename);
00153 } catch (...) {
00154 log(Error) << "Could not open file "<< filename << endlog();
00155 return false;
00156 }
00157 try {
00158 PropertyBag propbag;
00159
00160 if ( demarshaller->deserialize( propbag ) )
00161 {
00162
00163 PropertyBag composed_props;
00164 if ( composePropertyBag(propbag, composed_props) == false) {
00165 delete demarshaller;
00166 return false;
00167 }
00168
00169 PropertyBag backup;
00170 copyProperties( backup, *target->properties() );
00171 if ( refreshProperties( *target->properties(), composed_props, all ) == false ) {
00172
00173 refreshProperties( *target->properties(), backup );
00174 failure = true;
00175 }
00176
00177 deletePropertyBag( backup );
00178 }
00179 else
00180 {
00181 log(Error) << "Some error occured while parsing "<< filename.c_str() <<endlog();
00182 failure = true;
00183 }
00184 deletePropertyBag( propbag );
00185 } catch (...)
00186 {
00187 log(Error)
00188 << "Uncaught exception in deserialise !"<< endlog();
00189 failure = true;
00190 }
00191 delete demarshaller;
00192 return !failure;
00193 #endif // OROPKG_CORELIB_PROPERTIES_MARSHALLING
00194
00195 }
00196
00197 bool PropertyLoader::store(const std::string& filename) const
00198 {
00199 Logger::In in("PropertyLoader::store");
00200 #ifndef OROPKG_CORELIB_PROPERTIES_MARSHALLING
00201 log(Error) << "No Property Marshaller configured !" << endlog();
00202 return false;
00203 #else
00204 std::ofstream file( filename.c_str() );
00205 if ( file )
00206 {
00207
00208 PropertyBag* compProps = target->properties();
00209 PropertyBag allProps;
00210
00211
00212 PropertyBagIntrospector pbi( allProps );
00213 pbi.introspect( *compProps );
00214
00215 OROCLS_CORELIB_PROPERTIES_MARSHALLING_DRIVER<std::ostream> marshaller( file );
00216 marshaller.serialize( allProps );
00217 deletePropertyBag( allProps );
00218 log(Info) << "Wrote "<< filename <<endlog();
00219 }
00220 else {
00221 log(Error) << "Could not open file "<< filename <<" for writing."<<endlog();
00222 return false;
00223 }
00224 return true;
00225 #endif
00226 }
00227
00228 bool PropertyLoader::save(const std::string& filename, bool all) const
00229 {
00230 Logger::In in("PropertyLoader::save");
00231 #ifndef OROPKG_CORELIB_PROPERTIES_MARSHALLING
00232 log(Error) << "No Property MarshallInterface configured !" << endlog();
00233 return false;
00234
00235 #else
00236 if ( target->properties() == 0 ) {
00237 log(Error) << "Service "<< target->getName()
00238 << " does not have Properties to save." << endlog();
00239 return false;
00240 }
00241 PropertyBag allProps;
00242 PropertyBag decompProps;
00243
00244
00245 std::ifstream ifile( filename.c_str() );
00246
00247 if ( ifile ) {
00248 ifile.close();
00249 log(Info) << target->getName()<<" updating of file "<< filename << endlog();
00250
00251 OROCLS_CORELIB_PROPERTIES_DEMARSHALLING_DRIVER demarshaller( filename );
00252 if ( demarshaller.deserialize( allProps ) == false ) {
00253
00254 log(Error) << "While updating "<< target->getName() <<" : Failed to read "<< filename << endlog();
00255 return false;
00256 }
00257 }
00258 else {
00259 log(Info) << "Creating "<< filename << endlog();
00260 return store(filename);
00261 }
00262
00263
00264 PropertyBag* compProps = target->properties();
00265
00266
00267 PropertyBagIntrospector pbi( decompProps );
00268 pbi.introspect( *compProps );
00269
00270
00271 bool updater = false;
00272 if (all) {
00273 log(Info) << "Writing all properties of "<<target->getName()<<" to file "<< filename << endlog();
00274 updater = updateProperties( allProps, decompProps );
00275 }
00276 else {
00277 log(Info) << "Refreshing properties in file "<< filename << " with values of properties of "<<target->getName() << endlog();
00278 updater = refreshProperties( allProps, decompProps );
00279 }
00280 if (updater == false) {
00281 log(Error) << "Could not update properties of file "<< filename <<"."<<endlog();
00282 deletePropertyBag( allProps );
00283 deletePropertyBag( decompProps );
00284 return false;
00285 }
00286
00287
00288 std::ofstream file( filename.c_str() );
00289 if ( file )
00290 {
00291 OROCLS_CORELIB_PROPERTIES_MARSHALLING_DRIVER<std::ostream> marshaller( file );
00292 marshaller.serialize( allProps );
00293 log(Info) << "Wrote "<< filename <<endlog();
00294 }
00295 else {
00296 log(Error) << "Could not open file "<< filename <<" for writing."<<endlog();
00297 deletePropertyBag( allProps );
00298 return false;
00299 }
00300
00301 deletePropertyBag( allProps );
00302 deletePropertyBag( decompProps );
00303 return true;
00304 #endif
00305 }
00306
00307 bool PropertyLoader::configure(const std::string& filename, const std::string& name ) const
00308 {
00309 Logger::In in("PropertyLoader:configure");
00310 #ifndef OROPKG_CORELIB_PROPERTIES_MARSHALLING
00311 log(Error) << "No Property DemarshallInterface configured !" << endlog();
00312 return false;
00313
00314 #else
00315 log(Info) << "Reading Property '" <<name
00316 <<"' from file '"<<filename<<"'."<< endlog();
00317 bool failure = false;
00318 OROCLS_CORELIB_PROPERTIES_DEMARSHALLING_DRIVER* demarshaller = 0;
00319 try
00320 {
00321 demarshaller = new OROCLS_CORELIB_PROPERTIES_DEMARSHALLING_DRIVER (filename);
00322 } catch (...) {
00323 log(Error) << "Could not open file "<< filename << endlog();
00324 return false;
00325 }
00326 try {
00327 PropertyBag propbag;
00328 if ( demarshaller->deserialize( propbag ) )
00329 {
00330
00331 PropertyBag composed_props;
00332 if ( composePropertyBag(propbag, composed_props) == false) {
00333 deletePropertyBag( propbag );
00334 delete demarshaller;
00335 return false;
00336 }
00337 failure = !refreshProperty( *(target->properties()), composed_props, name );
00338 }
00339 else
00340 {
00341 log(Error) << "Some error occured while parsing "<< filename.c_str() <<endlog();
00342 failure = true;
00343 }
00344 deletePropertyBag( propbag );
00345 } catch (...)
00346 {
00347 log(Error) << "Uncaught exception in deserialise !"<< endlog();
00348 failure = true;
00349 }
00350 delete demarshaller;
00351 return !failure;
00352 #endif // OROPKG_CORELIB_PROPERTIES_MARSHALLING
00353 }
00354
00355 bool PropertyLoader::save(const std::string& filename, const std::string& name) const
00356 {
00357 Logger::In in("PropertyLoader::save");
00358 #ifndef OROPKG_CORELIB_PROPERTIES_MARSHALLING
00359 log(Error) << "No Property MarshallInterface configured !" << endlog();
00360 return false;
00361
00362 #else
00363 PropertyBag fileProps;
00364
00365 {
00366
00367 std::ifstream ifile( filename.c_str() );
00368
00369 if ( ifile ) {
00370 ifile.close();
00371 log(Info) << "Updating file "<< filename << " with properties of "<<target->getName()<<endlog();
00372
00373 OROCLS_CORELIB_PROPERTIES_DEMARSHALLING_DRIVER demarshaller( filename );
00374 if ( demarshaller.deserialize( fileProps ) == false ) {
00375
00376 log(Error) << "Failed to read "<< filename << endlog();
00377 return false;
00378 }
00379 }
00380 else
00381 log(Info) << "Creating "<< filename << endlog();
00382 }
00383
00384
00385 PropertyBag serviceProps;
00386 PropertyBagIntrospector pbi( serviceProps );
00387 pbi.introspect( *(target->properties()) );
00388
00389 bool failure;
00390 failure = ! updateProperty( fileProps, serviceProps, name );
00391
00392 deletePropertyBag( serviceProps );
00393
00394 if ( failure ) {
00395 log(Error) << "Could not update properties of file "<< filename <<"."<<endlog();
00396 deletePropertyBag( fileProps );
00397 return false;
00398 }
00399
00400 std::ofstream file( filename.c_str() );
00401 if ( file )
00402 {
00403 OROCLS_CORELIB_PROPERTIES_MARSHALLING_DRIVER<std::ostream> marshaller( file );
00404 marshaller.serialize( fileProps );
00405 log(Info) << "Wrote Property "<<name <<" to "<< filename <<endlog();
00406 }
00407 else {
00408 log(Error) << "Could not open file "<< filename <<" for writing."<<endlog();
00409 deletePropertyBag( fileProps );
00410 return false;
00411 }
00412
00413 deletePropertyBag( fileProps );
00414 return true;
00415 #endif
00416 }
00417