PropertyLoader.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Sat May 7 12:56:52 CEST 2005 PropertyLoader.cxx
3 
4  PropertyLoader.cxx - description
5  -------------------
6  begin : Sat May 07 2005
7  copyright : (C) 2005 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 
40 #include "PropertyLoader.hpp"
41 #include "rtt-config.h"
42 #ifdef OROPKG_CORELIB_PROPERTIES_MARSHALLING
43 #include ORODAT_CORELIB_PROPERTIES_MARSHALLING_INCLUDE
44 #include ORODAT_CORELIB_PROPERTIES_DEMARSHALLING_INCLUDE
45 #endif
46 #include "../Logger.hpp"
47 #include "../TaskContext.hpp"
49 #include "../types/PropertyComposition.hpp"
50 #include <fstream>
51 
52 using namespace std;
53 using namespace RTT;
54 using namespace RTT::detail;
55 
56 PropertyLoader::PropertyLoader(TaskContext *task)
57  : target(task->provides().get())
58 {}
59 
61  : target(service)
62 {}
63 
64 bool PropertyLoader::load(const std::string& filename) const
65 {
66  Logger::In in("PropertyLoader:load");
67 #ifndef OROPKG_CORELIB_PROPERTIES_MARSHALLING
68  log(Error) << "No Property DemarshallInterface configured !" << endlog();
69  return false;
70 
71 #else
72  if ( target->properties() == 0) {
73  log(Error) << "Service " <<target->getName()<<" has no Properties to configure." << endlog();
74  return false;
75  }
76 
77  log(Info) << "Loading properties into Service '" <<target->getName()
78  <<"' with '"<<filename<<"'."<< endlog();
79  bool failure = false;
80  OROCLS_CORELIB_PROPERTIES_DEMARSHALLING_DRIVER* demarshaller = 0;
81  try
82  {
83  demarshaller = new OROCLS_CORELIB_PROPERTIES_DEMARSHALLING_DRIVER (filename);
84  } catch (...) {
85  log(Error) << "Could not open file "<< filename << endlog();
86  return false;
87  }
88  try {
89  PropertyBag propbag;
90  vector<ActionInterface*> assignComs;
91 
92  if ( demarshaller->deserialize( propbag ) )
93  {
94  // compose propbag:
95  PropertyBag composed_props;
96  if ( composePropertyBag(propbag, composed_props) == false) {
97  delete demarshaller;
98  return false;
99  }
100  // take restore-copy;
101  PropertyBag backup;
102  copyProperties( backup, *target->properties() );
103  // First test if the updateProperties will succeed:
104  if ( refreshProperties( *target->properties(), composed_props, false) ) { // not strict
105  // this just adds the new properties, *should* never fail, but
106  // let's record failure to be sure.
107  failure = !updateProperties( *target->properties(), composed_props );
108  } else {
109  // restore backup in case of failure:
110  refreshProperties( *target->properties(), backup, false ); // not strict
111  failure = true;
112  }
113  // cleanup
114  deletePropertyBag( backup );
115  }
116  else
117  {
118  log(Error) << "Some error occured while parsing "<< filename.c_str() <<endlog();
119  failure = true;
120  }
121  } catch (...)
122  {
123  log(Error)
124  << "Uncaught exception in deserialise !"<< endlog();
125  failure = true;
126  }
127  delete demarshaller;
128  return !failure;
129 #endif // OROPKG_CORELIB_PROPERTIES_MARSHALLING
130 
131 }
132 
133 bool PropertyLoader::configure(const std::string& filename, bool all ) const
134 {
135  Logger::In in("PropertyLoader:configure");
136 #ifndef OROPKG_CORELIB_PROPERTIES_MARSHALLING
137  log(Error) << "No Property DemarshallInterface configured !" << endlog();
138  return false;
139 
140 #else
141  if ( target->properties() == 0) {
142  log(Error) << "Service " <<target->getName()<<" has no Properties to configure." << endlog();
143  return false;
144  }
145 
146  log(Info) << "Configuring Service '" <<target->getName()
147  <<"' with '"<<filename<<"'."<< endlog();
148  bool failure = false;
149  OROCLS_CORELIB_PROPERTIES_DEMARSHALLING_DRIVER* demarshaller = 0;
150  try
151  {
152  demarshaller = new OROCLS_CORELIB_PROPERTIES_DEMARSHALLING_DRIVER (filename);
153  } catch (...) {
154  log(Error) << "Could not open file "<< filename << endlog();
155  return false;
156  }
157  try {
158  PropertyBag propbag;
159 
160  if ( demarshaller->deserialize( propbag ) )
161  {
162  // compose propbag:
163  PropertyBag composed_props;
164  if ( composePropertyBag(propbag, composed_props) == false) {
165  delete demarshaller;
166  return false;
167  }
168  // take restore-copy;
169  PropertyBag backup;
170  copyProperties( backup, *target->properties() );
171  if ( refreshProperties( *target->properties(), composed_props, all ) == false ) {
172  // restore backup:
173  refreshProperties( *target->properties(), backup );
174  failure = true;
175  }
176  // cleanup
177  deletePropertyBag( backup );
178  }
179  else
180  {
181  log(Error) << "Some error occured while parsing "<< filename.c_str() <<endlog();
182  failure = true;
183  }
184  deletePropertyBag( propbag );
185  } catch (...)
186  {
187  log(Error)
188  << "Uncaught exception in deserialise !"<< endlog();
189  failure = true;
190  }
191  delete demarshaller;
192  return !failure;
193 #endif // OROPKG_CORELIB_PROPERTIES_MARSHALLING
194 
195 }
196 
197 bool PropertyLoader::store(const std::string& filename) const
198 {
199  Logger::In in("PropertyLoader::store");
200 #ifndef OROPKG_CORELIB_PROPERTIES_MARSHALLING
201  log(Error) << "No Property Marshaller configured !" << endlog();
202  return false;
203 #else
204  std::ofstream file( filename.c_str() );
205  if ( file )
206  {
207  // Write results
208  PropertyBag* compProps = target->properties();
209  PropertyBag allProps;
210 
211  // decompose repos into primitive property types.
212  PropertyBagIntrospector pbi( allProps );
213  pbi.introspect( *compProps );
214 
215  OROCLS_CORELIB_PROPERTIES_MARSHALLING_DRIVER<std::ostream> marshaller( file );
216  marshaller.serialize( allProps );
217  deletePropertyBag( allProps );
218  log(Info) << "Wrote "<< filename <<endlog();
219  }
220  else {
221  log(Error) << "Could not open file "<< filename <<" for writing."<<endlog();
222  return false;
223  }
224  return true;
225 #endif
226 }
227 
228 bool PropertyLoader::save(const std::string& filename, bool all) const
229 {
230  Logger::In in("PropertyLoader::save");
231 #ifndef OROPKG_CORELIB_PROPERTIES_MARSHALLING
232  log(Error) << "No Property MarshallInterface configured !" << endlog();
233  return false;
234 
235 #else
236  if ( target->properties() == 0 ) {
237  log(Error) << "Service "<< target->getName()
238  << " does not have Properties to save." << endlog();
239  return false;
240  }
241  PropertyBag allProps;
242  PropertyBag decompProps;
243 
244  // first check if the target file exists.
245  std::ifstream ifile( filename.c_str() );
246  // if target file does not exist, skip this step.
247  if ( ifile ) {
248  ifile.close();
249  log(Info) << target->getName()<<" updating of file "<< filename << endlog();
250  // The demarshaller itself will open the file.
251  OROCLS_CORELIB_PROPERTIES_DEMARSHALLING_DRIVER demarshaller( filename );
252  if ( demarshaller.deserialize( allProps ) == false ) {
253  // Parse error, abort writing of this file.
254  log(Error) << "While updating "<< target->getName() <<" : Failed to read "<< filename << endlog();
255  return false;
256  }
257  }
258  else {
259  log(Info) << "Creating "<< filename << endlog();
260  return store(filename);
261  }
262 
263  // Write results
264  PropertyBag* compProps = target->properties();
265 
266  // decompose repos into primitive property types.
267  PropertyBagIntrospector pbi( decompProps );
268  pbi.introspect( *compProps );
269 
270  //Add target properties to existing properties
271  bool updater = false;
272  if (all) {
273  log(Info) << "Writing all properties of "<<target->getName()<<" to file "<< filename << endlog();
274  updater = updateProperties( allProps, decompProps ); // add new.
275  }
276  else {
277  log(Info) << "Refreshing properties in file "<< filename << " with values of properties of "<<target->getName() << endlog();
278  updater = refreshProperties( allProps, decompProps ); // only refresh existing.
279  }
280  if (updater == false) {
281  log(Error) << "Could not update properties of file "<< filename <<"."<<endlog();
282  deletePropertyBag( allProps );
283  deletePropertyBag( decompProps );
284  return false;
285  }
286  // ok, finish.
287  // serialize and cleanup
288  std::ofstream file( filename.c_str() );
289  if ( file )
290  {
291  OROCLS_CORELIB_PROPERTIES_MARSHALLING_DRIVER<std::ostream> marshaller( file );
292  marshaller.serialize( allProps );
293  log(Info) << "Wrote "<< filename <<endlog();
294  }
295  else {
296  log(Error) << "Could not open file "<< filename <<" for writing."<<endlog();
297  deletePropertyBag( allProps );
298  return false;
299  }
300  // allProps contains copies (clone()), thus may be safely deleted :
301  deletePropertyBag( allProps );
302  deletePropertyBag( decompProps );
303  return true;
304 #endif
305 }
306 
307 bool PropertyLoader::configure(const std::string& filename, const std::string& name ) const
308 {
309  Logger::In in("PropertyLoader:configure");
310 #ifndef OROPKG_CORELIB_PROPERTIES_MARSHALLING
311  log(Error) << "No Property DemarshallInterface configured !" << endlog();
312  return false;
313 
314 #else
315  log(Info) << "Reading Property '" <<name
316  <<"' from file '"<<filename<<"'."<< endlog();
317  bool failure = false;
318  OROCLS_CORELIB_PROPERTIES_DEMARSHALLING_DRIVER* demarshaller = 0;
319  try
320  {
321  demarshaller = new OROCLS_CORELIB_PROPERTIES_DEMARSHALLING_DRIVER (filename);
322  } catch (...) {
323  log(Error) << "Could not open file "<< filename << endlog();
324  return false;
325  }
326  try {
327  PropertyBag propbag;
328  if ( demarshaller->deserialize( propbag ) )
329  {
330  // compose propbag:
331  PropertyBag composed_props;
332  if ( composePropertyBag(propbag, composed_props) == false) {
333  deletePropertyBag( propbag );
334  delete demarshaller;
335  return false;
336  }
337  failure = !refreshProperty( *(target->properties()), composed_props, name );
338  }
339  else
340  {
341  log(Error) << "Some error occured while parsing "<< filename.c_str() <<endlog();
342  failure = true;
343  }
344  deletePropertyBag( propbag );
345  } catch (...)
346  {
347  log(Error) << "Uncaught exception in deserialise !"<< endlog();
348  failure = true;
349  }
350  delete demarshaller;
351  return !failure;
352 #endif // OROPKG_CORELIB_PROPERTIES_MARSHALLING
353 }
354 
355 bool PropertyLoader::save(const std::string& filename, const std::string& name) const
356 {
357  Logger::In in("PropertyLoader::save");
358 #ifndef OROPKG_CORELIB_PROPERTIES_MARSHALLING
359  log(Error) << "No Property MarshallInterface configured !" << endlog();
360  return false;
361 
362 #else
363  PropertyBag fileProps;
364  // Update exising file ?
365  {
366  // first check if the target file exists.
367  std::ifstream ifile( filename.c_str() );
368  // if target file does not exist, skip this step.
369  if ( ifile ) {
370  ifile.close();
371  log(Info) << "Updating file "<< filename << " with properties of "<<target->getName()<<endlog();
372  // The demarshaller itself will open the file.
373  OROCLS_CORELIB_PROPERTIES_DEMARSHALLING_DRIVER demarshaller( filename );
374  if ( demarshaller.deserialize( fileProps ) == false ) {
375  // Parse error, abort writing of this file.
376  log(Error) << "Failed to read "<< filename << endlog();
377  return false;
378  }
379  }
380  else
381  log(Info) << "Creating "<< filename << endlog();
382  }
383 
384  // decompose service properties into primitive property types.
385  PropertyBag serviceProps;
386  PropertyBagIntrospector pbi( serviceProps );
387  pbi.introspect( *(target->properties()) );
388 
389  bool failure;
390  failure = ! updateProperty( fileProps, serviceProps, name );
391 
392  deletePropertyBag( serviceProps );
393 
394  if ( failure ) {
395  log(Error) << "Could not update properties of file "<< filename <<"."<<endlog();
396  deletePropertyBag( fileProps );
397  return false;
398  }
399  // serialize and cleanup
400  std::ofstream file( filename.c_str() );
401  if ( file )
402  {
403  OROCLS_CORELIB_PROPERTIES_MARSHALLING_DRIVER<std::ostream> marshaller( file );
404  marshaller.serialize( fileProps );
405  log(Info) << "Wrote Property "<<name <<" to "<< filename <<endlog();
406  }
407  else {
408  log(Error) << "Could not open file "<< filename <<" for writing."<<endlog();
409  deletePropertyBag( fileProps );
410  return false;
411  }
412  // fileProps contains copies (clone()), thus may be safely deleted :
413  deletePropertyBag( fileProps );
414  return true;
415 #endif
416 }
417 
const std::string & getName() const
Definition: Service.hpp:139
bool updateProperty(PropertyBag &target, const PropertyBag &source, const std::string &name, const std::string &separator)
PropertyLoader(TaskContext *task)
bool updateProperties(PropertyBag &target, const PropertyBag &source)
bool configure(const std::string &filename, bool all=true) const
bool refreshProperties(const PropertyBag &target, const PropertyBag &source, bool allprops)
bool copyProperties(PropertyBag &target, const PropertyBag &source)
This class fills up a given bag with the results of an introspection.
bool load(const std::string &filename) const
Definition: mystd.hpp:163
bool RTT_API composePropertyBag(PropertyBag const &sourcebag, PropertyBag &target)
A container for holding references to properties.
Definition: PropertyBag.hpp:96
bool save(const std::string &filename, bool all=true) const
void deletePropertyBag(PropertyBag &target)
bool refreshProperty(const PropertyBag &target, const PropertyBase &source)
bool store(const std::string &filename) const
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
static Logger & log()
Definition: Logger.hpp:350
static Logger::LogFunction endlog()
Definition: Logger.hpp:362


rtt
Author(s): RTT Developers
autogenerated on Tue Jun 25 2019 19:33:27