util/qos/ConnectQosFactory.cpp

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------------
00002 Name:      ConnectQosFactory.cpp
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Factory for ConnectQosData (for ConnectReturnQos and ConnectQos)
00006 ------------------------------------------------------------------------------*/
00007 
00008 #include <util/qos/ConnectQosFactory.h>
00009 #include <util/Global.h>
00010 // #include <util/XmlBlasterException>
00011 #include <util/lexical_cast.h>
00012 
00013 namespace org { namespace xmlBlaster { namespace util { namespace qos {
00014 
00015 using namespace std;
00016 using namespace org::xmlBlaster::authentication;
00017 using namespace org::xmlBlaster::util;
00018 using namespace org::xmlBlaster::util::parser;
00019 using namespace org::xmlBlaster::util::qos::storage;
00020 
00021 ConnectQosFactory::ConnectQosFactory(Global& global)
00022    : XmlHandlerBase(global),
00023      ME("ConnectQosFactory"),
00024      sessionQosFactory_(global),
00025      securityQosFactory_(global),
00026      queuePropertyFactory_(global),
00027      addressFactory_(global),
00028 //     securityQos_(global),
00029 //     serverRef_(Global::getDefaultProtocol()), //"SOCKET"),
00030      clientProperty_(0),
00031      connectQos_((ConnectQos*)0)
00032 {
00033    log_.call(ME, "constructor");
00034    prep();
00035 }
00036 
00037 void ConnectQosFactory::prep()
00038 {
00039    inSecurityService_ = false;
00040    inServerRef_       = false;
00041    inSession_         = false;
00042    inRefreshSession_  = false;
00043    inInstanceId_      = false;
00044    inReconnected_     = false;
00045    inIsPersistent_    = false;
00046    serverRefType_     = "";
00047    subFactory_        = NULL;
00048    inClientProperty_  = false;
00049    inQos_             = false;
00050 }
00051 
00052 ConnectQosFactory::~ConnectQosFactory()
00053 {
00054    log_.call(ME, "destructor");
00055    if (clientProperty_ != 0) {
00056       delete(clientProperty_);
00057    }
00058    //if (securityQos_ != NULL) delete securityQos_;
00059    //if (serverRef_ != NULL) delete serverRef_;
00060 }
00061 
00062 void ConnectQosFactory::characters(const string &ch)
00063 {
00064    if (subFactory_) {
00065       subFactory_->characters(ch);
00066       return;
00067    }
00068 
00069    if (inSession_) {
00070       sessionQosFactory_.characters(ch);
00071       return;
00072    }
00073 
00074    character_ += StringTrim::trim(ch);
00075    if (log_.trace()) log_.trace(ME, string("characters, character:'") + character_ + string("'"));
00076 }
00077 
00078 void ConnectQosFactory::startElement(const string& name, const AttributeMap& attrs) {
00079    //if (log_.call()) log_.call(ME, "startElement: " + getStartElementAsString(name, attrs));
00080 
00081    if (name.compare("qos") == 0) {
00082      inQos_ = true;
00083      connectQos_ = new ConnectQos(global_); // kind of reset
00084      return;
00085    }
00086 
00087    if (name.compare("queue") == 0) {
00088       if (!inQos_) return;
00089       subFactory_ = &queuePropertyFactory_;
00090    }
00091 
00092    if (subFactory_) {
00093       subFactory_->startElement(name, attrs);
00094       return;
00095    }
00096 
00097    if (name.compare("securityService") == 0) {
00098       if (!inQos_) return;
00099       inSecurityService_ = true;
00100       character_ = getStartElementAsString(name, attrs);
00101       return;
00102    }
00103 
00104    if (name.compare("session") == 0) {
00105       if (!inQos_) return;
00106       inSession_ = true;
00107       sessionQosFactory_.reset();
00108    }
00109    if (inSession_) {
00110       sessionQosFactory_.startElement(name, attrs);
00111       return;
00112    }
00113 
00114    if (name.compare("ptp") == 0) {
00115       if (!inQos_) return;
00116       connectQos_->setPtp(true);
00117       character_.erase();
00118       return;
00119    }
00120 
00121    if (name.compare("clusterNode") == 0) {
00122       if (!inQos_) return;
00123       connectQos_->setClusterNode(true);
00124       character_.erase();
00125       return;
00126    }
00127 
00128    if (name.compare("refreshSession") == 0) {
00129       if (!inQos_) return;
00130       inRefreshSession_ = true;
00131       connectQos_->setRefreshSession(true);
00132       character_.erase();
00133       return;
00134    }
00135 
00136    if (name.compare("duplicateUpdates") == 0) {
00137       if (!inQos_) return;
00138       connectQos_->setDuplicateUpdates(true);
00139       character_.erase();
00140       return;
00141    }
00142 
00143    if (name.compare("reconnected") == 0) {
00144       if (!inQos_) return;
00145       inReconnected_ = true;
00146       connectQos_->setReconnected(true);
00147       character_.erase();
00148       return;
00149    }
00150 
00151    if (name.compare("instanceId") == 0) {
00152       if (!inQos_) return;
00153       inInstanceId_ = true;
00154       character_.erase();
00155       return;
00156    }
00157 
00158    if (name.compare("persistent") == 0) {
00159       if (!inQos_) return;
00160       inIsPersistent_ = true;
00161       connectQos_->setPersistent(true);
00162       character_.erase();
00163       return;
00164    }
00165 
00166    if (name.compare("serverRef") == 0) {
00167       if (!inQos_) return;
00168       character_.erase();
00169       inServerRef_ = true;
00170       AttributeMap::const_iterator iter = attrs.begin();
00171       while (iter != attrs.end()) {
00172          if (  ((*iter).first).compare("type") == 0) {
00173             serverRefType_ = (*iter).second;
00174          }
00175          iter++;
00176       }
00177    }
00178    
00179    if (name.compare("clientProperty") == 0) {
00180       if (!inQos_) return;
00181       inClientProperty_ = true;
00182       character_.erase();
00183       string nameAttr;
00184       AttributeMap::const_iterator iter = attrs.find("name");
00185       if (iter != attrs.end()) nameAttr = (*iter).second;
00186       string encoding;
00187       iter = attrs.find("encoding");
00188       if (iter != attrs.end()) encoding = (*iter).second;
00189       string type;
00190       iter = attrs.find("type");
00191       if (iter != attrs.end()) type = (*iter).second;
00192       string charset;
00193       iter = attrs.find("charset");
00194       if (iter != attrs.end()) charset = (*iter).second;
00195       clientProperty_ = new ClientProperty(true, nameAttr, type, encoding, charset);
00196    }
00197 }
00198 
00199 void ConnectQosFactory::endElement(const string &name) {
00200    //if (log_.call()) log_.call(ME, "endElement");
00201    //if (log_.trace()) log_.trace(ME, string("endElement. name:'") + name + string("' character: '") + character_ + string("'"));   
00202 
00203    if (inQos_ && subFactory_) {
00204       subFactory_->endElement(name);
00205       if (name.compare("queue") == 0) {
00206          // determine wether it is a callback or a client queue ...
00207          QueuePropertyBase help = queuePropertyFactory_.getQueueProperty();
00208          if (help.getRelating() == Constants::RELATING_CLIENT) {
00209             ClientQueueProperty prop = help;
00210             connectQos_->addClientQueueProperty(prop);
00211          }
00212          else {
00213              CbQueueProperty prop = help;
00214              connectQos_->setSessionCbQueueProperty(prop);
00215          }
00216          subFactory_ = NULL;
00217       }
00218       return;
00219    }
00220 
00221    if (name.compare("qos") == 0) {
00222      inQos_ = false;
00223      character_.erase();
00224      return;
00225    }
00226 
00227    if (!inQos_) {
00228       return;
00229    }
00230 
00231    if (name.compare("securityService") == 0) {
00232       inSecurityService_ = false;
00233       character_ += string("\n</securityService>\n");
00234 //      securityQos_ = securityQosFactory_.parse(character_);
00235       connectQos_->setSecurityQos(securityQosFactory_.parse(character_));
00236       character_.erase();
00237       return;
00238    }
00239 
00240    if (name.compare("session") == 0) {
00241       sessionQosFactory_.endElement(name);
00242       inSession_ = false;
00243       return;
00244    }
00245 
00246    if (name.compare("ptp") == 0) {
00247       connectQos_->setPtp(StringTrim::isTrueTrim(character_));
00248       character_.erase();
00249       return;
00250    }
00251 
00252    if (name.compare("clusterNode") == 0) {
00253       connectQos_->setClusterNode(StringTrim::isTrueTrim(character_));
00254       character_.erase();
00255       return;
00256    }
00257 
00258    if(name.compare("refreshSession") == 0) {
00259       inRefreshSession_ = false;
00260       connectQos_->setRefreshSession(StringTrim::isTrueTrim(character_));
00261       character_.erase();
00262       return;
00263    }
00264 
00265    if (name.compare("duplicateUpdates") == 0) {
00266       connectQos_->setDuplicateUpdates(StringTrim::isTrueTrim(character_));
00267       character_.erase();
00268       return;
00269    }
00270 
00271    if(name.compare("instanceId") == 0) {
00272       inInstanceId_ = false;
00273       StringTrim::trim(character_);
00274       connectQos_->setInstanceId(character_);
00275       character_.erase();
00276       return;
00277    }
00278 
00279    if(name.compare("reconnected") == 0) {
00280       inReconnected_ = false;
00281       connectQos_->setReconnected(StringTrim::isTrueTrim(character_));
00282       character_.erase();
00283       return;
00284    }
00285 
00286    if(name.compare("persistent") == 0) {
00287       inIsPersistent_ = false;
00288       connectQos_->setPersistent(StringTrim::isTrueTrim(character_));
00289       character_.erase();
00290       return;
00291    }
00292 
00293    if (inSession_) {
00294       sessionQosFactory_.endElement(name);
00295       return;
00296    }
00297 
00298    if (name.compare("serverRef") == 0) {
00299       inServerRef_ = false;
00300       string address = character_;
00301 //      serverRef_ = ServerRef(serverRefType_, address);
00302       connectQos_->addServerRef(ServerRef(serverRefType_, address));
00303       character_.erase();
00304    }
00305 
00306    if (name.compare("clientProperty") == 0) {
00307       inClientProperty_ = false;
00308       clientProperty_->setValueRaw(character_);
00309       connectQos_->addClientProperty(*clientProperty_);
00310       delete clientProperty_;
00311       clientProperty_ = 0;
00312       character_.erase();
00313    }
00314 
00315    return;
00316 }
00317 
00318 
00319 ConnectQosDataRef ConnectQosFactory::readObject(const string& qos)
00320 {
00321    // this should be synchronized here ....
00322 //   userId_ = "";
00323    prep();
00324    delete clientProperty_;
00325    clientProperty_ = 0;
00326    init(qos);
00327 //   ConnectQosData data(global_);
00328 //   connectQos_->setSecurityQos(securityQos_);
00329 //   connectQos_->addServerRef(serverRef_);
00330    connectQos_->setSessionQos(sessionQosFactory_.getData());
00331 //   connectQos_->setPtp(isPtp_);
00332 
00333    return connectQos_;
00334 }
00335 
00336 }}}} // namespaces
00337 
00338 
00339 #ifdef _XMLBLASTER_CLASSTEST
00340 
00341 using namespace std;
00342 using namespace org::xmlBlaster::util::qos;
00343 
00345 int main(int args, char* argv[])
00346 {
00347    string qos =
00348    string("<qos>\n") +
00349    string("   <securityService type='htpasswd' version='1.0'>\n") +
00350    string("     <![CDATA[\n") +
00351    string("     <user>joe</user>\n") +
00352    string("     <passwd>secret</passwd>\n") +
00353    string("     ]]>\n") +
00354    string("   </securityService>\n") +
00355    string("   <session name='/node/heron/client/joe/-9' timeout='3600000' maxSessions='10' clearSessions='false' sessionId='4e56890ghdFzj0'/>\n") +
00356    string("   <ptp>true</ptp>\n") +
00357    string("   <!-- The client side queue: -->\n") +
00358    string("   <queue relating='client' type='CACHE' version='1.0' maxEntries='1000' maxBytes='4000' onOverflow='exception'>\n") +
00359    string("      <address type='IOR' sessionId='4e56890ghdFzj0'>\n") +
00360    string("         IOR:10000010033200000099000010....\n") +
00361    string("      </address>\n") +
00362    string("   </queue>\n") +
00363    string("   <!-- The server side callback queue: -->\n") +
00364    string("   <queue relating='callback' type='CACHE' version='1.0' maxEntries='1000' maxBytes='4000' onOverflow='deadMessage'>\n") +
00365    string("      <callback type='IOR' sessionId='4e56890ghdFzj0'>\n") +
00366    string("         IOR:10000010033200000099000010....\n") +
00367    string("         <burstMode collectTime='400' />\n") +
00368    string("         <attribute name='aKey'>aValue</attribute>\n") +
00369    string("      </callback>\n") +
00370    string("   </queue>\n") +
00371    string("   <serverRef type='IOR'>IOR:100000100332...</serverRef>\n") +
00372    string("</qos>\n");
00373 
00374    Global& glob = Global::getInstance();
00375    glob.initialize(args, argv);
00376    ConnectQosFactory factory(glob);
00377    ConnectQosDataRef data = factory.readObject(qos);
00378    cout << "sessionId    : " << data->getSecretSessionId() << endl;
00379    cout << "userId       : " << data->getUserId() << endl;
00380    cout << " type: " << data->getCallbackType() << endl;
00381    cout << "is ptp       : " << data->getBoolAsString(data->getPtp()) << endl;
00382    cout << "securityQos  : " << data->getSecurityQos().toXml() << endl;
00383    cout << "sessionQos   : " << data->getSessionQos().toXml() << endl;
00384 
00385    ServerRef ref = data->getServerRef();
00386    cout << "server reference:  " << ref.toXml() << endl;
00387 
00388    return 0;
00389 }
00390 
00391 #endif