#include <XML_Saver.h>
Inheritance diagram for TAO_Notify::XML_Saver:
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 (void) |
Close the saver. | |
Private Member Functions | |
void | backup_file_name (char *file_path, size_t 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. |
Definition at line 33 of file XML_Saver.h.
|
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 } |
|
Definition at line 25 of file XML_Saver.cpp. References ACE_ERROR, ACE_TEXT(), LM_ERROR, and output_.
|
|
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 } |
|
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.
Implements TAO_Notify::Topology_Saver. Definition at line 154 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().
00158 { 00159 ACE_ASSERT(this->output_ != 0); 00160 00161 FILE *out = this->output_; 00162 00163 ACE_OS::fprintf (out, "%s%s%s", indent_.c_str(), "<", type.c_str()); 00164 if (id != 0) 00165 { 00166 // not all ostreams know what to do with a CORBA::Long 00167 long lid = id; 00168 ACE_OS::fprintf (out, " %s%s%ld%s", TOPOLOGY_ID_NAME, "=\"", lid, "\""); 00169 } 00170 00171 const size_t BUF_SIZE = 512; 00172 ACE_CString tmp(BUF_SIZE); 00173 for (size_t idx = 0; idx < attrs.size(); idx++) 00174 { 00175 ACEXML_escape_string(attrs[idx].value, tmp); 00176 ACE_OS::fprintf (out, "%s%s%s%s%s", " ", 00177 attrs[idx].name.c_str (), "=\"", tmp.c_str(), "\""); 00178 } 00179 ACE_OS::fprintf (out, ">\n"); 00180 this->indent_ += " "; 00181 return true; 00182 } |
|
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_CString, 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"); 00049 00050 ACE_OS::fclose(this->output_); 00051 this->output_ = 0; 00052 00053 // delete the oldest backup file (if it exists) 00054 size_t nfile = this->backup_count_ - 1; 00055 char old_path [MAXPATHLEN + 1]; 00056 backup_file_name (old_path, nfile); 00057 ACE_OS::unlink (old_path); 00058 00059 while (nfile != 0) 00060 { 00061 char new_path [MAXPATHLEN + 1]; 00062 nfile -= 1; 00063 backup_file_name (new_path, nfile); 00064 // this may fail, we don't care 00065 ACE_OS::rename (new_path, old_path); 00066 ACE_OS::strcpy (old_path, new_path); 00067 } 00068 // old_path now contains the name of the backup file 00069 ACE_CString xml_name = this->base_name_; 00070 xml_name += ".xml"; 00071 00072 ACE_OS::rename (xml_name.c_str (), old_path); 00073 00074 ACE_CString new_name = this->base_name_; 00075 new_name += ".new"; 00076 ACE_OS::rename (new_name.c_str (), xml_name.c_str ()); 00077 } 00078 this->output_ = 0; 00079 } |
|
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 184 of file XML_Saver.cpp. References ACE_ASSERT, ACE_OS::fprintf(), indent_, and output_. Referenced by close().
00186 { 00187 ACE_ASSERT(this->output_ != 0); 00188 FILE *out = this->output_; 00189 if (this->indent_.length() >= 2) 00190 { 00191 this->indent_ = this->indent_.substr(2); 00192 } 00193 ACE_OS::fprintf (out, "%s%s%s%s", indent_.c_str(), "</", 00194 type.c_str(), ">\n"); 00195 } |
|
Open the output file.
Definition at line 82 of file XML_Saver.cpp. References ACE_CString, ACE_ERROR, ACE_TEXT, 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().
00083 { 00084 this->base_name_ = base_name; 00085 this->backup_count_ = backup_count; 00086 if (base_name == "cout") 00087 { 00088 this->output_ = stdout; 00089 this->close_out_ = false; 00090 } 00091 else if (base_name == "cerr") 00092 { 00093 this->output_ = stderr; 00094 this->close_out_ = false; 00095 } 00096 else 00097 { 00098 ACE_CString file_name = base_name; 00099 file_name += ".new"; 00100 00101 this->output_ = ACE_OS::fopen (file_name.c_str(), ACE_TEXT("wb")); 00102 if (this->output_) { 00103 this->close_out_ = true; 00104 } else { 00105 ACE_ERROR ((LM_ERROR, 00106 ACE_TEXT ("(%P|%t) XML_Saver unable to open %s\n"), 00107 base_name.c_str())); 00108 } 00109 } 00110 if (this->output_ != 0) 00111 { 00112 FILE *out = this->output_; 00113 00114 ACE_OS::fprintf (out, "<?xml version=\"1.0\"?>\n"); 00115 00116 try 00117 { 00118 bool changed = true; 00119 NVPList attrs; 00120 00121 ACE_Time_Value now = ACE_High_Res_Timer::gettimeofday(); 00122 00123 ACE_UINT64 nowus = now.usec(); 00124 static const ACE_UINT64 USECSPERSEC = 1000 * 1000; 00125 ACE_UINT64 tmpus = now.sec(); 00126 nowus += tmpus * USECSPERSEC; 00127 00128 char nowusstr[128]; 00129 #ifdef ACE_LACKS_LONGLONG_T 00130 nowus.as_string(nowusstr); 00131 #else 00132 ACE_OS::sprintf(nowusstr, ACE_UINT64_FORMAT_SPECIFIER, nowus); 00133 #endif /* ACE_LACKS_LONGLONG_T */ 00134 00135 attrs.push_back(NVP("version", "1.0")); 00136 if (this->timestamp_) 00137 { 00138 attrs.push_back(NVP("timestamp", nowusstr)); 00139 } 00140 this->begin_object(0, "notification_service", attrs, changed); 00141 } 00142 catch (const CORBA::Exception& ex) 00143 { 00144 ex._tao_print_exception ( 00145 ACE_TEXT ( 00146 "(%P|%t) XML_Saver Unknown exception\n")); 00147 delete this->output_; 00148 this->output_ = 0; 00149 } 00150 } 00151 return this->output_ != 0; 00152 } |
|
Definition at line 70 of file XML_Saver.h. |
|
the name of the output file
Definition at line 69 of file XML_Saver.h. |
|
Definition at line 66 of file XML_Saver.h. |
|
A string consisting of spaces that is our current indentation level.
Definition at line 76 of file XML_Saver.h. Referenced by begin_object(), and end_object(). |
|
A stream representing our current output.
Definition at line 65 of file XML_Saver.h. Referenced by begin_object(), close(), end_object(), open(), and ~XML_Saver(). |
|
true to enable timestamping
Definition at line 73 of file XML_Saver.h. |