1 #include "orbsvcs/Log_Macros.h"
2 #include "XML_Backing_Store.h"
3 #include "Server_Info.h"
4 #include "Activator_Info.h"
6 #include "Locator_XMLHandler.h"
7 #include "ace/OS_NS_stdio.h"
8 #include "ace/OS_NS_strings.h"
9 #include "ace/OS_NS_ctype.h"
10 #include "ace/OS_NS_unistd.h"
11 #include "ACEXML/parser/parser/Parser.h"
12 #include "ACEXML/common/FileCharStream.h"
13 #include "ACEXML/common/XML_Util.h"
15 XML_Backing_Store::XML_Backing_Store(const Options
& opts
,
18 : Locator_Repository(opts
, orb
),
19 filename_(opts
.persist_file_name())
21 if (opts
.repository_erase() && !suppress_erase
)
23 ACE_OS::unlink ( this->filename_
.c_str () );
27 XML_Backing_Store::~XML_Backing_Store()
32 XML_Backing_Store::persistent_remove (const ACE_CString
& , bool )
34 // one big XML file, need to persist everything
39 XML_Backing_Store::persistent_update(const Server_Info_Ptr
& , bool )
41 // one big XML file, need to persist everything
46 XML_Backing_Store::persistent_update(const Activator_Info_Ptr
& , bool )
48 // one big XML file, need to persist everything
53 XML_Backing_Store::persist ()
55 FILE* fp
= ACE_OS::fopen (this->filename_
.c_str (), "w");
58 ORBSVCS_ERROR ((LM_ERROR
, ACE_TEXT ("Couldn't write to file %C\n"),
59 this->filename_
.c_str()));
62 ACE_OS::fprintf (fp
,"<?xml version=\"1.0\"?>\n");
63 ACE_OS::fprintf (fp
,"<%s>\n", Locator_XMLHandler::ROOT_TAG
);
66 Locator_Repository::SIMap::ENTRY
* sientry
= 0;
67 Locator_Repository::SIMap::ITERATOR
siit (this->servers ());
68 for (; siit
.next (sientry
); siit
.advance() )
70 persist(fp
, *sientry
->int_id_
, "\t");
74 Locator_Repository::AIMap::ENTRY
* aientry
= 0;
75 Locator_Repository::AIMap::ITERATOR
aiit (this->activators ());
76 for (; aiit
.next (aientry
); aiit
.advance ())
78 ACE_CString aname
= aientry
->ext_id_
;
79 persist(fp
, *aientry
->int_id_
, "\t");
82 ACE_OS::fprintf (fp
,"</%s>\n", Locator_XMLHandler::ROOT_TAG
);
89 ACE_CString
ACEXML_escape_cstring (const ACE_CString
&str
)
91 return ACE_TEXT_ALWAYS_CHAR (ACEXML_escape_string (
92 ACE_TEXT_CHAR_TO_TCHAR (str
.c_str ())).c_str ());
97 XML_Backing_Store::persist (FILE* fp
,
98 const Server_Info
& info
,
99 const char* tag_prepend
,
100 const NameValues
& name_values
)
102 ACE_CString server_id
= ACEXML_escape_cstring (info
.server_id
);
103 ACE_CString pname
= ACEXML_escape_cstring (info
.poa_name
);
104 ACE_CString keyname
= ACEXML_escape_cstring (info
.key_name_
);
105 ACE_CString altkey
= "";
106 if (!info
.alt_info_
.null())
108 altkey
= ACEXML_escape_cstring (info
.alt_info_
->key_name_
);
111 ACE_CString activator
= ACEXML_escape_cstring (info
.activator
);
112 ACE_CString cmdline
= ACEXML_escape_cstring (info
.cmdline
);
113 ACE_CString wdir
= ACEXML_escape_cstring (info
.dir
);
114 ACE_CString partial_ior
= ACEXML_escape_cstring (info
.partial_ior
);
115 ACE_CString ior
= ACEXML_escape_cstring (info
.ior
);
116 ACE_CString amodestr
=
117 ImR_Utils::activationModeToString (info
.activation_mode_
);
119 ACE_OS::fprintf (fp
,"%s<%s", tag_prepend
,
120 Locator_XMLHandler::SERVER_INFO_TAG
);
121 ACE_OS::fprintf (fp
, " %s=\"%s\"", Locator_XMLHandler::SERVER_TAG
, server_id
.c_str ());
122 ACE_OS::fprintf (fp
, " %s=\"%s\"", Locator_XMLHandler::POANAME_TAG
, pname
.c_str ());
123 ACE_OS::fprintf (fp
, " %s=\"%s\"", Locator_XMLHandler::ACTNAME_TAG
, activator
.c_str ());
124 ACE_OS::fprintf (fp
, " %s=\"%s\"", Locator_XMLHandler::CMDLINE_TAG
, cmdline
.c_str ());
125 ACE_OS::fprintf (fp
, " %s=\"%s\"", Locator_XMLHandler::DIR_TAG
, wdir
.c_str ());
126 ACE_OS::fprintf (fp
, " %s=\"%s\"", Locator_XMLHandler::MODE_TAG
, amodestr
.c_str ());
127 ACE_OS::fprintf (fp
, " %s=\"%d\"", Locator_XMLHandler::LIMIT_TAG
, info
.start_limit_
);
128 ACE_OS::fprintf (fp
, " %s=\"%s\"", Locator_XMLHandler::PARTIOR_TAG
, partial_ior
.c_str ());
129 ACE_OS::fprintf (fp
, " %s=\"%s\"", Locator_XMLHandler::IOR_TAG
, ior
.c_str ());
130 ACE_OS::fprintf (fp
, " %s=\"%d\"", Locator_XMLHandler::STARTED_TAG
, !CORBA::is_nil(info
.server
.in()));
131 ACE_OS::fprintf (fp
, " %s=\"%d\"", Locator_XMLHandler::JACORB_TAG
, info
.is_jacorb
);
132 ACE_OS::fprintf (fp
, " %s=\"%d\"", Locator_XMLHandler::PID_TAG
, info
.pid
);
133 ACE_OS::fprintf (fp
, " %s= \"%s\"", Locator_XMLHandler::KEYNAME_TAG
, keyname
.c_str ());
134 ACE_OS::fprintf (fp
, " %s= \"%s\"", Locator_XMLHandler::ALTKEY_TAG
, altkey
.c_str ());
136 NameValues::const_iterator name_value
;
137 for (name_value
= name_values
.begin(); name_value
!= name_values
.end(); ++name_value
)
139 ACE_OS::fprintf (fp
," %s=\"%s\"",
140 name_value
->first
.c_str(), name_value
->second
.c_str());
143 CORBA::ULong elen
= info
.env_vars
.length ();
144 CORBA::ULong plen
= info
.peers
.length ();
148 ACE_OS::fprintf (fp
,">\n");
149 for (CORBA::ULong i
= 0; i
< elen
; ++i
)
151 ACE_OS::fprintf (fp
,"%s\t<%s", tag_prepend
,
152 Locator_XMLHandler::ENVIRONMENT_TAG
);
153 ACE_OS::fprintf (fp
," name=\"%s\"", info
.env_vars
[i
].name
.in ());
154 ACE_CString val
= ACEXML_escape_cstring(info
.env_vars
[i
].value
.in());
155 ACE_OS::fprintf (fp
," value=\"%s\"", val
.c_str());
156 ACE_OS::fprintf (fp
,"/>\n");
158 for (CORBA::ULong i
= 0; i
< plen
; ++i
)
160 ACE_OS::fprintf (fp
,"%s\t<%s", tag_prepend
,
161 Locator_XMLHandler::PEER_TAG
);
162 ACE_CString name
= ACEXML_escape_cstring(info
.peers
[i
].in());
163 ACE_OS::fprintf (fp
," name=\"%s\"", name
.c_str());
164 ACE_OS::fprintf (fp
,"/>\n");
167 ACE_OS::fprintf (fp
,"%s</%s>\n", tag_prepend
, Locator_XMLHandler::SERVER_INFO_TAG
);
171 ACE_OS::fprintf (fp
,"/>\n");
176 XML_Backing_Store::persist (FILE* fp
,
177 const Activator_Info
& info
,
178 const char* tag_prepend
,
179 const NameValues
& name_values
)
181 ACE_OS::fprintf (fp
,"%s<%s", tag_prepend
,
182 Locator_XMLHandler::ACTIVATOR_INFO_TAG
);
183 ACE_OS::fprintf( fp
," name=\"%s\"", info
.name
.c_str ());
184 ACE_OS::fprintf (fp
," token=\"%d\"", info
.token
);
185 ACE_OS::fprintf (fp
," ior=\"%s\"", info
.ior
.c_str ());
187 NameValues::const_iterator name_value
;
188 for (name_value
= name_values
.begin(); name_value
!= name_values
.end(); ++name_value
)
190 ACE_OS::fprintf (fp
," %s=\"%s\"",
191 name_value
->first
.c_str(), name_value
->second
.c_str());
194 ACE_OS::fprintf (fp
,"/>\n");
198 XML_Backing_Store::init_repo (PortableServer::POA_ptr
)
200 // ignore load return since files don't have to exist
201 this->load_file (this->filename_
);
206 XML_Backing_Store::load_file (const ACE_TString
& filename
, FILE* open_file
)
208 Locator_XMLHandler
xml_handler (*this);
209 return XML_Backing_Store::load_file (filename
, xml_handler
,
210 this->opts_
.debug(), open_file
);
215 XML_Backing_Store::load_file (const ACE_TString
& filename
,
216 ACEXML_DefaultHandler
& xml_handler
,
220 // xml input source will take ownership
221 ACEXML_FileCharStream
* fstm
;
222 ACE_NEW_RETURN (fstm
,
223 ACEXML_FileCharStream
,
227 // use the open_file stream if it is provided
229 err
= fstm
->use_stream(open_file
,
232 err
= fstm
->open (filename
.c_str());
236 ORBSVCS_DEBUG ((LM_INFO
, ACE_TEXT ("(%P|%t) load %s%C\n"), filename
.c_str(),
237 ((err
== 0) ? ACE_TEXT ("")
238 : ACE_TEXT (" (file doesn't exist)"))));
247 ACEXML_Parser parser
;
249 // InputSource takes ownership
250 ACEXML_InputSource
input (fstm
);
252 parser
.setContentHandler (&xml_handler
);
253 parser
.setDTDHandler (&xml_handler
);
254 parser
.setErrorHandler (&xml_handler
);
255 parser
.setEntityResolver (&xml_handler
);
259 parser
.parse (&input
);
261 catch ( const ACEXML_SAXParseException
* sax_ex
)
263 ORBSVCS_ERROR ((LM_ERROR
,
264 ACE_TEXT ("Error during load of ImR persistence xml file (%s)."),
269 catch (const ACEXML_Exception
& ex
)
271 ORBSVCS_ERROR ((LM_ERROR
,
272 ACE_TEXT ("Error during load of ImR persistence xml file (%s)."),
282 XML_Backing_Store::repo_mode() const
284 return this->filename_
.c_str();
288 XML_Backing_Store::load_server (Server_Info
*info
,
292 Server_Info_Ptr
si (info
);
294 this->servers ().rebind (info
->key_name_
, si
);
296 this->create_server (server_started
, si
);
300 XML_Backing_Store::create_server (bool server_started
,
301 const Server_Info_Ptr
& si
)
303 if (!server_started
|| si
->ior
.is_empty ())
308 CORBA::Object_var obj
= this->orb_
->string_to_object (si
->ior
.c_str ());
309 if (!CORBA::is_nil(obj
.in()))
312 ImplementationRepository::ServerObject::_unchecked_narrow (obj
.in ());
313 si
->last_ping
= ACE_Time_Value::zero
;
318 XML_Backing_Store::load_activator (const ACE_CString
& activator_name
,
320 const ACE_CString
& ior
,
323 Activator_Info
*ai
= 0;
325 Activator_Info (activator_name
, token
, ior
));
327 Activator_Info_Ptr
info (ai
);
328 this->activators().rebind(Locator_Repository::lcase (activator_name
), info
);