00001 /*------------------------------------------------------------------------------ 00002 Name: SessionQos.cpp 00003 Project: xmlBlaster.org 00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file 00005 Comment: Factory for SessionQosData (for ConnectReturnQos and ConnectQos) 00006 ------------------------------------------------------------------------------*/ 00007 00008 #include <util/qos/SessionQos.h> 00009 #include <stdlib.h> 00010 #include <util/lexical_cast.h> 00011 #include <util/StringStripper.h> 00012 #include <util/StringTrim.h> 00013 #include <util/Global.h> 00014 00015 namespace org { namespace xmlBlaster { namespace util { namespace qos { 00016 00017 using namespace org::xmlBlaster::util; 00018 using namespace org::xmlBlaster::util::parser; 00019 using namespace std; 00020 00021 00022 /*---------------------------- SessionQosData --------------------------------*/ 00023 00024 SessionQosData::SessionQosData(Global& global, const string& defaultUserName, long publicSessionId) 00025 : ReferenceCounterBase(), ME("SessionQosData"), sessionName_(0), global_(global) 00026 { 00027 initialize(); 00028 SessionName *p = new SessionName(global, defaultUserName, publicSessionId); 00029 SessionNameRef r(p); 00030 sessionName_ = r; 00031 } 00032 00033 SessionQosData::SessionQosData(Global& global, const string& absoluteName) 00034 : ReferenceCounterBase(), ME("SessionQosData"), sessionName_(0), global_(global) 00035 { 00036 initialize(); 00037 SessionName *p = new SessionName(global, absoluteName); 00038 SessionNameRef r(p); 00039 sessionName_ = r; 00040 } 00041 00042 SessionQosData::~SessionQosData() 00043 { 00044 } 00045 00046 void SessionQosData::copy(const SessionQosData& data) 00047 { 00048 SessionName *p = new SessionName(global_, data.sessionName_->getAbsoluteName()); 00049 SessionNameRef r(p); 00050 sessionName_ = r; 00051 00052 timeout_ = data.timeout_; 00053 maxSessions_ = data.maxSessions_; 00054 clearSessions_ = data.clearSessions_; 00055 reconnectSameClientOnly_ = data.reconnectSameClientOnly_; 00056 sessionId_ = data.sessionId_; 00057 } 00058 00059 00060 void SessionQosData::initialize() 00061 { 00062 timeout_ = global_.getProperty().getLongProperty("session.timeout", 86400000); 00063 maxSessions_ = global_.getProperty().getIntProperty("session.maxSessions", 10); 00064 clearSessions_ = global_.getProperty().getBoolProperty("session.clearSessions", false); 00065 reconnectSameClientOnly_ = global_.getProperty().getBoolProperty("session.reconnectSameClientOnly", false); 00066 sessionId_ = global_.getProperty().getStringProperty("session.secretSessionId", ""); 00067 } 00068 00069 00070 SessionQosData::SessionQosData(const SessionQosData& data) : 00071 ReferenceCounterBase(), ME(data.ME), sessionName_(0), global_(data.global_) 00072 { 00073 copy(data); 00074 } 00075 00076 SessionQosData& SessionQosData::operator =(const SessionQosData& data) 00077 { 00078 copy(data); 00079 return *this; 00080 } 00081 00082 00083 SessionNameRef SessionQosData::getSessionName() 00084 { 00085 return sessionName_; 00086 } 00087 00088 void SessionQosData::setAbsoluteName(const string& name) 00089 { 00090 sessionName_->setAbsoluteName(name); 00091 } 00092 00093 string SessionQosData::getRelativeName() const 00094 { 00095 return sessionName_->getRelativeName(); 00096 } 00097 00098 string SessionQosData::getAbsoluteName() const 00099 { 00100 return sessionName_->getAbsoluteName(); 00101 } 00102 00103 string SessionQosData::getClusterNodeId() const 00104 { 00105 return sessionName_->getClusterNodeId(); 00106 } 00107 00108 void SessionQosData::setClusterNodeId(const string& clusterNodeId) 00109 { 00110 return sessionName_->setClusterNodeId(clusterNodeId); 00111 } 00112 00113 string SessionQosData::getSubjectId() const 00114 { 00115 return sessionName_->getSubjectId(); 00116 } 00117 00118 void SessionQosData::setSubjectId(const string& subjectId) 00119 { 00120 return sessionName_->setSubjectId(subjectId); 00121 } 00122 00123 long SessionQosData::getPubSessionId() const 00124 { 00125 return sessionName_->getPubSessionId(); 00126 } 00127 00128 void SessionQosData::setPubSessionId(const long pubSessionId) 00129 { 00130 return sessionName_->setPubSessionId(pubSessionId); 00131 } 00132 00133 long SessionQosData::getTimeout() const 00134 { 00135 return timeout_; 00136 } 00137 00138 void SessionQosData::setTimeout(long timeout) 00139 { 00140 timeout_ = timeout; 00141 } 00142 00143 int SessionQosData::getMaxSessions() const 00144 { 00145 return maxSessions_; 00146 } 00147 00148 void SessionQosData::setMaxSessions(int maxSessions) 00149 { 00150 maxSessions_ = maxSessions; 00151 } 00152 00153 bool SessionQosData::getClearSessions() const 00154 { 00155 return clearSessions_; 00156 } 00157 00158 void SessionQosData::setClearSessions(bool clearSessions) 00159 { 00160 clearSessions_ = clearSessions; 00161 } 00162 00163 bool SessionQosData::getReconnectSameClientOnly() const 00164 { 00165 return reconnectSameClientOnly_; 00166 } 00167 00168 void SessionQosData::setReconnectSameClientOnly(bool reconnectSameClientOnly) 00169 { 00170 reconnectSameClientOnly_ = reconnectSameClientOnly; 00171 } 00172 00173 string SessionQosData::getSecretSessionId() const 00174 { 00175 return sessionId_; 00176 } 00177 00178 void SessionQosData::setSecretSessionId(const string& sessionId) 00179 { 00180 sessionId_ = sessionId; 00181 } 00182 00183 string SessionQosData::toXml(const string& extraOffset) const 00184 { 00185 string offset = Constants::OFFSET + extraOffset; 00186 string ret; 00187 00188 ret += offset + string("<session"); 00189 ret += string(" name='") + getAbsoluteName() + string("'"); 00190 ret += string(" timeout='") + lexical_cast<std::string>(getTimeout()) + string("'"); 00191 ret += string(" maxSessions='") + lexical_cast<std::string>(getMaxSessions()) + string("'"); 00192 ret += string(" clearSessions='") + Global::getBoolAsString(clearSessions_) + string("'"); 00193 ret += string(" reconnectSameClientOnly='") + Global::getBoolAsString(reconnectSameClientOnly_) + string("'"); 00194 00195 if (!sessionId_.empty()) { 00196 ret += string(" sessionId='") + sessionId_ + string("'"); 00197 } 00198 ret += string("/>\n"); 00199 return ret; 00200 } 00201 00202 00203 /*-------------------------- SessionQosFactory -------------------------------*/ 00204 00205 SessionQosFactory::SessionQosFactory(Global& global) 00206 : XmlQoSBase(global), ME("SessionQosFactory") 00207 { 00208 sessionQos_ = NULL; 00209 log_.call(ME, "constructor"); 00210 } 00211 00212 00213 SessionQosFactory::~SessionQosFactory() 00214 { 00215 delete sessionQos_; 00216 } 00217 00218 void SessionQosFactory::characters(const string &ch) 00219 { 00220 string trimmedCh = StringTrim::trim(ch); 00221 character_ += trimmedCh; 00222 if (log_.trace()) 00223 log_.trace(ME, string("characters, character:'") + ch + string("'")); 00224 } 00225 00226 void SessionQosFactory::startElement(const string &name, const AttributeMap& attrs) { 00227 if (log_.call()) log_.call(ME, "startElement: " + getStartElementAsString(name, attrs)); 00228 00229 if (util::XmlQoSBase::startElementBase(name, attrs)) return; 00230 00231 if (name.compare("session") == 0) { 00232 // get all attributes which are needed ... 00233 AttributeMap::const_iterator iter = attrs.begin(); 00234 while (iter != attrs.end()) { 00235 string tmpName = (*iter).first; 00236 string tmpValue = (*iter).second; 00237 if (tmpName.compare("name") == 0) { 00238 sessionQos_->setAbsoluteName(tmpValue); 00239 } 00240 else if (tmpName.compare("timeout") == 0) { 00241 sessionQos_->timeout_ = XmlHandlerBase::getLongValue(tmpValue); 00242 } 00243 else if (tmpName.compare("maxSessions") == 0) { 00244 sessionQos_->maxSessions_ = XmlHandlerBase::getIntValue(tmpValue); 00245 } 00246 else if (tmpName.compare("clearSessions") == 0) { 00247 sessionQos_->clearSessions_ = StringTrim::isTrueTrim(tmpValue); 00248 } 00249 else if (tmpName.compare("reconnectSameClientOnly") == 0) { 00250 sessionQos_->reconnectSameClientOnly_ = StringTrim::isTrueTrim(tmpValue); 00251 } 00252 else if (tmpName.compare("sessionId") == 0) { 00253 sessionQos_->sessionId_ = tmpValue; 00254 } 00255 iter++; 00256 } 00257 return; 00258 } 00259 } 00260 00261 void SessionQosFactory::endElement(const string &name) { 00262 log_.trace(ME, "endElement"); 00263 if (util::XmlQoSBase::endElementBase(name)) return; 00264 00265 if (name.compare("session") == 0) { 00266 return; 00267 } 00268 } 00269 00270 void SessionQosFactory::reset() 00271 { 00272 if (sessionQos_ != NULL) delete sessionQos_; 00273 sessionQos_ = NULL; 00274 sessionQos_ = new SessionQosData(global_); 00275 } 00276 00277 const SessionQosData& SessionQosFactory::getData() const 00278 { 00279 return *sessionQos_; 00280 } 00281 00282 SessionQosData SessionQosFactory::readObject(const string& qos) 00283 { 00284 // this should be synchronized here .... 00285 reset(); 00286 init(qos); 00287 return *sessionQos_; 00288 } 00289 00293 string SessionQosData::usage() 00294 { 00295 string text; 00296 text += string("Control my login session settings:\n"); 00297 text += string(" -session.name []\n"); 00298 text += string(" The name for login, e.g. 'joe' or with public session ID 'joe/2'.\n"); 00299 text += string(" -session.timeout ["+lexical_cast<std::string>((int)Constants::DAY_IN_MILLIS)+"], defaults to one day.\n"); 00300 text += string(" How long lasts our login session in milliseconds, 0 is forever.\n"); 00301 text += " -session.maxSessions [10]\n"; 00302 text += string(" Maximum number of simultanous logins per client.\n"); 00303 text += string(" -session.clearSessions [false]\n"); 00304 text += string(" Kill other sessions running under my login name.\n"); 00305 text += string(" -session.reconnectSameClientOnly [false]\n"); 00306 text += string(" Only creator client may reconnect to session.\n"); 00307 text += string(" -session.secretSessionId []\n"); 00308 text += string(" The secret sessionId.\n"); 00309 return text; 00310 } 00311 }}}} // namespaces 00312 00313 00314 #ifdef _XMLBLASTER_CLASSTEST 00315 00316 using namespace std; 00317 using namespace org::xmlBlaster::util::qos; 00318 00320 int main(int args, char* argv[]) 00321 { 00322 { 00323 string qos = "<session name='/node/http:/client/ticheta/-3' timeout='86400000' maxSessions='10' \n" + 00324 string(" clearSessions='false' reconnectSameClientOnly='false' sessionId='IIOP:01110728321B0222011028'/>\n"); 00325 00326 Global& glob = Global::getInstance(); 00327 glob.initialize(args, argv); 00328 SessionQosFactory factory(glob); 00329 SessionQosData data = factory.readObject(qos); 00330 00331 string ret = data.toXml(); 00332 cout << ret << endl; 00333 00334 cout << data.getPubSessionId() << endl; 00335 cout << data.getSubjectId() << endl; 00336 cout << data.getClusterNodeId() << endl << endl; 00337 00338 SessionQosData data2(glob); 00339 cout << "second session qos: " << endl; 00340 cout << data2.toXml() << endl; 00341 cout << data2.getPubSessionId() << endl; 00342 cout << data2.getSubjectId() << endl; 00343 cout << data2.getClusterNodeId() << endl << endl; 00344 } 00345 return 0; 00346 } 00347 00348 #endif