util/qos/address/AddressFactory.cpp

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------------
00002 Name:      AddressFactory.cpp
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Factory Object for parsing Address objects.
00006 Version:   $Id: AddressFactory.cpp 15130 2006-05-10 09:53:35Z ruff $
00007 ------------------------------------------------------------------------------*/
00008 
00019 #include <util/qos/address/AddressFactory.h>
00020 #include <util/Global.h>
00021 #include <util/lexical_cast.h>
00022 
00023 namespace org { namespace xmlBlaster { namespace util { namespace qos { namespace address {
00024 
00025 using namespace std;
00026 using namespace org::xmlBlaster::util;
00027 using namespace org::xmlBlaster::util::parser;
00028 
00029 
00030 AddressFactory::AddressFactory(Global& global)
00031    : XmlHandlerBase(global), ME("AddressFactory"),
00032    address_(new Address(global)), // dummy allocation
00033    inAttribute_(false),
00034    attribute_(0)
00035 {
00036 }
00037 
00038 AddressFactory::~AddressFactory()
00039 {
00040    log_.call(ME, "destructor");
00041    if (attribute_ != 0) {
00042       delete(attribute_);
00043    }
00044 }
00045 
00046 void AddressFactory::reset(const AddressBaseRef& address)
00047 {
00048    address_ = address;
00049    inAttribute_ = false;
00050    if (attribute_ != 0) {
00051       delete(attribute_);
00052    }
00053 }
00054 
00055 AddressBaseRef AddressFactory::getAddress()
00056 {
00057    return address_;
00058 }
00059 
00063 // void startElement(const string& uri, const string& localName, const string& name, const string& character, Attributes attrs)
00064 void AddressFactory::startElement(const string &name, const AttributeMap& attrs)
00065 {
00066    //if (log_.call()) log_.call(ME, "startElement: " + getStartElementAsString(name, attrs));
00067 
00068    if (character_.length() > 0) {
00069       StringTrim::trim(character_);
00070       if (!character_.empty()) {
00071          address_->setAddress(character_);
00072       }
00073       character_.erase();
00074    }
00075 
00076    if (name.compare(address_->rootTag_) == 0) { // callback
00077       AttributeMap::const_iterator iter = attrs.begin();
00078       while (iter != attrs.end()) {
00079          string tmpName = (*iter).first;
00080          string tmpValue = (*iter).second;
00081          if (tmpName.compare("type") == 0) {
00082                address_->setType(tmpValue);
00083          }
00084          else if (tmpName.compare("version") == 0) {
00085             address_->setVersion(tmpValue);
00086          }
00087          else if (tmpName.compare("bootstrapHostname") == 0) {
00088             address_->setHostname(tmpValue);
00089          }
00090          else if (tmpName.compare("bootstrapPort") == 0) {
00091             address_->setPort(XmlHandlerBase::getIntValue(tmpValue));
00092          }
00093          else if (tmpName.compare("sessionId") == 0) {
00094             address_->setSecretSessionId(tmpValue);
00095          }
00096          else if (tmpName.compare("pingInterval") == 0) {
00097             address_->setPingInterval(XmlHandlerBase::getLongValue(tmpValue));
00098          }
00099          else if (tmpName.compare("retries") == 0) {
00100             address_->setRetries(XmlHandlerBase::getIntValue(tmpValue));
00101          }
00102          else if (tmpName.compare("delay") == 0) {
00103             address_->setDelay(XmlHandlerBase::getLongValue(tmpValue));
00104          }
00105          else if (tmpName.compare("oneway") == 0) {
00106             address_->setOneway(XmlHandlerBase::getBoolValue(tmpValue));
00107          }
00108          else if (tmpName.compare("dispatcherActive") == 0) {
00109             address_->setDispatcherActive(XmlHandlerBase::getBoolValue(tmpValue));
00110          }
00111          else if (tmpName.compare("useForSubjectQueue") == 0) {
00112             address_->useForSubjectQueue_ = XmlHandlerBase::getBoolValue(tmpValue);
00113          }
00114          else if (tmpName.compare("dispatchPlugin") == 0) {
00115             address_->dispatchPlugin_ = tmpValue;
00116          }
00117          else {
00118             log_.warn(ME, string("Ignoring unknown attribute ") +
00119               tmpName +  string(" in ") + address_->rootTag_ + string(" section."));
00120          }
00121          iter++;
00122       }
00123       if (address_->getType() == "") {
00124          log_.error(ME, string("Missing '") + address_->rootTag_ + string("' attribute 'type' in QoS"));
00125          address_->setType(Global::getDefaultProtocol());
00126       }
00127 
00128       if (address_->getSecretSessionId() == "") {
00129          log_.warn(ME, string("Missing '") + address_->rootTag_ + string("' attribute 'sessionId' QoS"));
00130       }
00131       return;
00132    }
00133 
00134    if (name.compare("burstMode") == 0) {
00135       AttributeMap::const_iterator iter = attrs.begin();
00136       bool found = false;
00137       while (iter != attrs.end()) {
00138          if (((*iter).first).compare("collectTime") == 0) {
00139             address_->setCollectTime(XmlHandlerBase::getLongValue((*iter).second));
00140             found = true;
00141          }
00142          else if (((*iter).first).compare("maxEntries") == 0) {
00143             address_->setBurstModeMaxEntries(XmlHandlerBase::getIntValue((*iter).second));
00144             found = true;
00145          }
00146          else if (((*iter).first).compare("maxBytes") == 0) {
00147             address_->setBurstModeMaxBytes(XmlHandlerBase::getLongValue((*iter).second));
00148             found = true;
00149          }
00150          iter++;
00151       }
00152       if (!found) log_.warn(ME, "Missing attributes in login-qos <burstMode>");
00153       return;
00154    }
00155 
00156    if (name.compare("compress") == 0) {
00157       AttributeMap::const_iterator iter = attrs.begin();
00158       string tmpName = (*iter).first;
00159       string tmpValue = (*iter).second;
00160       bool found = false;
00161       while (iter != attrs.end()) {
00162          if (tmpName.compare("type") == 0) {
00163             address_->setCompressType(tmpValue);
00164             found = true;
00165          }
00166          else if (tmpName.compare("minSize") == 0) {
00167             address_->setMinSize(XmlHandlerBase::getLongValue(tmpValue));
00168          }
00169          iter++;
00170       }
00171       if (!found) log_.error(ME, "Missing 'type' attribute in qos <compress>");
00172       return;
00173    }
00174    if (name.compare("ptp") == 0) {
00175       return;
00176    }
00177 
00178    if (name.compare(ATTRIBUTE_TAG) == 0) { // "attribute"
00179       inAttribute_ = true;
00180       attributeCharacter_.erase();
00181       string nameAttr;
00182       AttributeMap::const_iterator iter = attrs.find("name");
00183       if (iter != attrs.end()) nameAttr = (*iter).second;
00184       string encoding;
00185       iter = attrs.find("encoding");
00186       if (iter != attrs.end()) encoding = (*iter).second;
00187       string type;
00188       iter = attrs.find("type");
00189       if (iter != attrs.end()) type = (*iter).second;
00190       string charset;
00191       iter = attrs.find("charset");
00192       if (iter != attrs.end()) charset = (*iter).second;
00193       attribute_ = new ClientProperty(true, nameAttr, type, encoding, charset);
00194    }
00195 }
00196 
00202 // public final void endElement(String uri, String localName, String name, StringBuffer character) {
00203 void AddressFactory::endElement(const string &name)
00204 {
00205    // if (log_.trace()) log_.trace(ME, string("::endElement: '") + name + string("'"));
00206    if (name.compare(address_->rootTag_) == 0) { // callback
00207       StringTrim::trim(character_);
00208       if (!character_.empty()) address_->setAddress(character_);
00209       else if (address_->getRawAddress() == "")
00210          log_.error(ME, address_->rootTag_ + string(" QoS contains no address data"));
00211 
00212    }
00213    else if (name.compare("burstMode") == 0) {
00214    }
00215    else if (name.compare("compress") == 0) {
00216    }
00217    else if (name.compare("ptp") == 0) {
00218       address_->ptpAllowed_ = StringTrim::isTrueTrim(character_);
00219    }
00220    if (name.compare(ATTRIBUTE_TAG) == 0) { // "attribute"
00221       inAttribute_ = false;
00222       attribute_->setValueRaw(attributeCharacter_);
00223       address_->addAttribute(*attribute_);
00224       delete attribute_;
00225       attribute_ = 0;
00226       attributeCharacter_.erase();
00227    }
00228    character_.erase();
00229 }
00230 
00231 
00232 AddressBaseRef AddressFactory::readAddress(const string& litteral, const AddressBaseRef& address)
00233 {
00234    reset(address);
00235    init(litteral);
00236    return getAddress();
00237 }
00238 
00239 }}}}} // namespaces
00240 
00241 
00242 #ifdef _XMLBLASTER_CLASSTEST
00243 #include <util/qos/address/Address.h>
00244 
00245 using namespace std;
00246 using namespace org::xmlBlaster::util::qos::address;
00247 
00249 int main(int args, char* argv[])
00250 {
00251    try {
00252       Global& glob = Global::getInstance();
00253       glob.initialize(args, argv);
00254       Log& log = glob.getLog("org.xmlBlaster.util.qos");
00255       log.info("main", "This is a simple info");
00256       Address a(glob);
00257       a.setType("SOCKET");
00258       a.setAddress("127.0.0.1:7600");
00259       a.setCollectTime(12345l);
00260       a.setPingInterval(54321l);
00261       a.setRetries(17);
00262       a.setDelay(7890l);
00263       a.setOneway(true);
00264       a.setDispatcherActive(false);
00265       a.setSecretSessionId("0x4546hwi89");
00266       cout << a.toXml() << endl;
00267 
00268       AddressFactory factory(glob);
00269       Address addr(glob);
00270       AddressBaseRef ptr = &factory.readAddress(a.toXml(), addr);
00271       cout << "parsed one: " << endl << ptr->toXml() << endl;
00272 
00273       string nodeId = "heron";
00274       int                nmax = 8;
00275       const char** argc = new const char*[nmax];
00276       argc[0] = "-sessionId";
00277       argc[1] = "ERROR";
00278       string help = string("-sessionId[") + nodeId + string("]");
00279       argc[2] = string(help).c_str();
00280       argc[3] = "OK";
00281       argc[4] = "-pingInterval";
00282       argc[5] = "8888";
00283       help = string("-delay[") + nodeId + string("]");
00284       argc[6] = help.c_str();
00285       argc[7] = "8888";
00286 
00287       Global& glob = Global::getInstance();
00288       glob.initialize(nmax, argc);
00289       Address a(glob, "RMI", nodeId);
00290       cout << a.toXml() << endl;
00291    }
00292    catch(...) {
00293       cout << "unknown uncatched exception" << endl;
00294    }
00295 }
00296 
00297 #endif
00298 
00299 
00300