TAO_Notify::XML_Saver Class Reference

Save Notification Service Topology to an XML file. More...

#include <XML_Saver.h>

Inheritance diagram for TAO_Notify::XML_Saver:

Inheritance graph
[legend]
Collaboration diagram for TAO_Notify::XML_Saver:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 XML_Saver (bool timestamp=true)
virtual ~XML_Saver ()
bool open (const ACE_CString &file_name, size_t backup_count)
virtual bool begin_object (CORBA::Long id, const ACE_CString &type, const NVPList &attrs, bool changed)
 Begin the storage of an object.

virtual void end_object (CORBA::Long id, const ACE_CString &type)
 End the storage of an object.

virtual void close ()
 Close the saver.


Private Member Functions

void backup_file_name (char *file_path, int nfile)

Private Attributes

FILE * output_
 A stream representing our current output.

bool close_out_
ACE_CString base_name_
 the name of the output file

size_t backup_count_
bool timestamp_
 true to enable timestamping

ACE_CString indent_
 A string consisting of spaces that is our current indentation level.


Detailed Description

Save Notification Service Topology to an XML file.

Definition at line 33 of file XML_Saver.h.


Constructor & Destructor Documentation

TAO_Notify::XML_Saver::XML_Saver bool  timestamp = true  ) 
 

Construct an XML_Saver. Initialization is deferred to "open()"

Definition at line 17 of file XML_Saver.cpp.

00018     : output_ (0)
00019     , close_out_ (false)
00020     , backup_count_ (1)
00021     , timestamp_ (timestamp)
00022   {
00023   }

TAO_Notify::XML_Saver::~XML_Saver  )  [virtual]
 

Definition at line 25 of file XML_Saver.cpp.

References ACE_ERROR, ACE_TEXT(), LM_ERROR, and output_.

00026   {
00027     if (this->output_ != 0)
00028     {
00029       ACE_ERROR ((LM_ERROR,
00030         ACE_TEXT ("(%P|%t) XML_Saver warning close not called or failed\n")
00031         ));
00032     }
00033   }


Member Function Documentation

void TAO_Notify::XML_Saver::backup_file_name char *  file_path,
int  nfile
[private]
 

Definition at line 36 of file XML_Saver.cpp.

References MAXPATHLEN, and ACE_OS::snprintf().

Referenced by close().

00037   {
00038     ACE_OS::snprintf(file_path, MAXPATHLEN, "%s.%3.3d",
00039       this->base_name_.c_str (),
00040       nfile);
00041   }

bool TAO_Notify::XML_Saver::begin_object CORBA::Long  id,
const ACE_CString type,
const NVPList attrs,
bool  changed
[virtual]
 

Begin the storage of an object.

Call this function with the type and ID of an object to be stored. This object may have children, and this will necessitate nested calls to begin_object. Design principle: Names should be descriptive enough to allow the objects' parent to create an instance of the desired class. This instance will be registered with the poa using the id. The instance itself should will load its own attributes. Example <proxy type="push_supplier" events="any" id="20"...> is not a good design because the name "proxy" is not descriptive enough. "<structured_proxy_push_supplier id="20"...> is better because this allows the parent to create the correct type of object without decoding attributes.

Parameters:
id numeric id for this object
type string containing the unique type name for this class of objects
attrs a collection of name/value attributes
change true if this object's attributes have changed.
Returns:
bool want_all_children. If true even changed children should be saved.

Implements TAO_Notify::Topology_Saver.

Definition at line 157 of file XML_Saver.cpp.

References ACE_ASSERT, ACE_CString, ACE_OS::fprintf(), indent_, output_, TAO_Notify::NVPList::size(), and TAO_Notify::TOPOLOGY_ID_NAME.

Referenced by open().

00162   {
00163     ACE_ASSERT(this->output_ != 0);
00164 
00165     FILE *out = this->output_;
00166 
00167     ACE_OS::fprintf (out, "%s%s%s", indent_.c_str(), "<", type.c_str());
00168     if (id != 0)
00169     {
00170       // not all ostreams know what to do with a CORBA::Long
00171       long lid = id;
00172       ACE_OS::fprintf (out, " %s%s%ld%s", TOPOLOGY_ID_NAME, "=\"", lid, "\"");
00173     }
00174 
00175     const size_t BUF_SIZE = 512;
00176     ACE_CString tmp(BUF_SIZE);
00177     for (size_t idx = 0; idx < attrs.size(); idx++)
00178     {
00179       ACEXML_escape_string(attrs[idx].value, tmp);
00180       ACE_OS::fprintf (out, "%s%s%s%s%s", " ",
00181         attrs[idx].name.c_str (), "=\"", tmp.c_str(), "\"");
00182     }
00183     ACE_OS::fprintf (out, ">\n");
00184     this->indent_ += "  ";
00185     return true;
00186   }

void TAO_Notify::XML_Saver::close  )  [virtual]
 

Close the saver.

This is not pure virtual. The default implementation does nothing.

There should be a corresponding open, but the signature may vary based on the type of saver, so we can't include it in the interface.

Reimplemented from TAO_Notify::Topology_Saver.

Definition at line 44 of file XML_Saver.cpp.

References ACE_CHECK, ACE_CString, ACE_ENV_ARG_PARAMETER, backup_count_, backup_file_name(), base_name_, close_out_, end_object(), ACE_OS::fclose(), MAXPATHLEN, output_, ACE_OS::rename(), ACE_OS::strcpy(), and ACE_OS::unlink().

00045   {
00046     if (this->close_out_ && this->output_ != 0)
00047     {
00048       this->end_object(0, "notification_service" ACE_ENV_ARG_PARAMETER);
00049       ACE_CHECK;
00050 
00051       ACE_OS::fclose(this->output_);
00052       this->output_ = 0;
00053 
00054       // delete the oldest backup file (if it exists)
00055       size_t nfile = this->backup_count_ - 1;
00056       char old_path [MAXPATHLEN + 1];
00057       backup_file_name (old_path, nfile);
00058       ACE_OS::unlink (old_path);
00059 
00060       while (nfile != 0)
00061       {
00062         char new_path [MAXPATHLEN + 1];
00063         nfile -= 1;
00064         backup_file_name (new_path, nfile);
00065         // this may fail, we don't care
00066         ACE_OS::rename (new_path, old_path);
00067         ACE_OS::strcpy (old_path, new_path);
00068       }
00069       // old_path now contains the name of the backup file
00070       ACE_CString xml_name = this->base_name_;
00071       xml_name += ".xml";
00072 
00073       ACE_OS::rename (xml_name.c_str (), old_path);
00074 
00075       ACE_CString new_name = this->base_name_;
00076       new_name += ".new";
00077       ACE_OS::rename (new_name.c_str (), xml_name.c_str ());
00078     }
00079     this->output_ = 0;
00080   }

void TAO_Notify::XML_Saver::end_object CORBA::Long  id,
const ACE_CString type
[virtual]
 

End the storage of an object.

This function should be called to end the scope of the current object and commit it to the persistent store.

Implements TAO_Notify::Topology_Saver.

Definition at line 188 of file XML_Saver.cpp.

References ACE_ASSERT, ACE_OS::fprintf(), indent_, and output_.

Referenced by close().

00191   {
00192     ACE_ASSERT(this->output_ != 0);
00193     FILE *out = this->output_;
00194     if (this->indent_.length() >= 2)
00195     {
00196       this->indent_ = this->indent_.substr(2);
00197     }
00198     ACE_OS::fprintf (out, "%s%s%s%s", indent_.c_str(), "</",
00199                      type.c_str(), ">\n");
00200   }

bool TAO_Notify::XML_Saver::open const ACE_CString file_name,
size_t  backup_count
 

Open the output file.

Parameters:
file_name the fully qualified file name
Returns:
true if successful

Definition at line 83 of file XML_Saver.cpp.

References ACE_ANY_EXCEPTION, ACE_CATCHANY, ACE_CString, ACE_DECLARE_NEW_CORBA_ENV, ACE_ENDTRY, ACE_ENV_ARG_PARAMETER, ACE_ERROR, ACE_PRINT_EXCEPTION, ACE_TEXT, ACE_TRY, ACE_TRY_CHECK, ACE_UINT64, ACE_UINT64_FORMAT_SPECIFIER, backup_count_, base_name_, begin_object(), close_out_, ACE_OS::fopen(), ACE_OS::fprintf(), ACE_High_Res_Timer::gettimeofday(), LM_ERROR, output_, TAO_Notify::NVPList::push_back(), ACE_Time_Value::sec(), ACE_OS::sprintf(), and ACE_Time_Value::usec().

Referenced by TAO_Notify::XML_Topology_Factory::create_saver().

00084   {
00085     this->base_name_ = base_name;
00086     this->backup_count_ = backup_count;
00087     if (base_name ==  "cout")
00088     {
00089       this->output_ = stdout;
00090       this->close_out_ = false;
00091     }
00092     else if (base_name ==  "cerr")
00093     {
00094       this->output_ = stderr;
00095       this->close_out_ = false;
00096     }
00097     else
00098     {
00099       ACE_CString file_name = base_name;
00100       file_name += ".new";
00101 
00102       this->output_ = ACE_OS::fopen (file_name.c_str(), ACE_TEXT("wb"));
00103       if (this->output_) {
00104         this->close_out_ = true;
00105       } else {
00106         ACE_ERROR ((LM_ERROR,
00107           ACE_TEXT ("(%P|%t) XML_Saver unable to open %s\n"),
00108             base_name.c_str()));
00109       }
00110     }
00111     if (this->output_ != 0)
00112     {
00113       FILE *out = this->output_;
00114 
00115       ACE_OS::fprintf (out, "<?xml version=\"1.0\"?>\n");
00116 
00117       ACE_DECLARE_NEW_CORBA_ENV;
00118       ACE_TRY
00119       {
00120         bool changed = true;
00121         NVPList attrs;
00122 
00123         ACE_Time_Value now = ACE_High_Res_Timer::gettimeofday();
00124 
00125         ACE_UINT64 nowus = now.usec();
00126         static const ACE_UINT64 USECSPERSEC = 1000 * 1000;
00127         ACE_UINT64 tmpus = now.sec();
00128         nowus += tmpus * USECSPERSEC;
00129 
00130         char nowusstr[128];
00131 #ifdef ACE_LACKS_LONGLONG_T
00132         nowus.as_string(nowusstr);
00133 #else
00134         ACE_OS::sprintf(nowusstr, ACE_UINT64_FORMAT_SPECIFIER, nowus);
00135 #endif /* ACE_LACKS_LONGLONG_T */
00136 
00137         attrs.push_back(NVP("version", "1.0"));
00138         if (this->timestamp_)
00139         {
00140           attrs.push_back(NVP("timestamp", nowusstr));
00141         }
00142         this->begin_object(0, "notification_service", attrs, changed ACE_ENV_ARG_PARAMETER);
00143         ACE_TRY_CHECK;
00144       }
00145       ACE_CATCHANY
00146       {
00147         ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
00148           ACE_TEXT ("(%P|%t) XML_Saver Unknown exception\n"));
00149         delete this->output_;
00150         this->output_ = 0;
00151       }
00152       ACE_ENDTRY;
00153     }
00154     return this->output_ != 0;
00155   }


Member Data Documentation

size_t TAO_Notify::XML_Saver::backup_count_ [private]
 

Definition at line 72 of file XML_Saver.h.

Referenced by close(), and open().

ACE_CString TAO_Notify::XML_Saver::base_name_ [private]
 

the name of the output file

Definition at line 71 of file XML_Saver.h.

Referenced by close(), and open().

bool TAO_Notify::XML_Saver::close_out_ [private]
 

Definition at line 68 of file XML_Saver.h.

Referenced by close(), and open().

ACE_CString TAO_Notify::XML_Saver::indent_ [private]
 

A string consisting of spaces that is our current indentation level.

Definition at line 78 of file XML_Saver.h.

Referenced by begin_object(), and end_object().

FILE* TAO_Notify::XML_Saver::output_ [private]
 

A stream representing our current output.

Definition at line 67 of file XML_Saver.h.

Referenced by begin_object(), close(), end_object(), open(), and ~XML_Saver().

bool TAO_Notify::XML_Saver::timestamp_ [private]
 

true to enable timestamping

Definition at line 75 of file XML_Saver.h.


The documentation for this class was generated from the following files:
Generated on Thu Nov 9 13:34:42 2006 for TAO_CosNotification by doxygen 1.3.6