util/SessionName.cpp

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------------
00002 Name:      SessionName.cpp
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   
00006 ------------------------------------------------------------------------------*/
00007 #include <util/SessionName.h>
00008 #include <stdlib.h>
00009 #include <util/lexical_cast.h>
00010 #include <util/StringStripper.h>
00011 #include <util/StringTrim.h>
00012 #include <util/Global.h>
00013 
00014 namespace org { namespace xmlBlaster { namespace util {
00015 
00016 using namespace org::xmlBlaster::util;
00017 using namespace org::xmlBlaster::util::parser;
00018 using namespace std;
00019 
00020 
00021 /*---------------------------- SessionName --------------------------------*/
00022 
00023 SessionName::SessionName(Global& global, const string& defaultUserName, long publicSessionId)
00024     : ReferenceCounterBase(), ME("SessionName"), global_(global)
00025 {
00026    initialize("", defaultUserName, publicSessionId);   
00027 }
00028 
00029 SessionName::SessionName(Global& global, const string& absoluteName)
00030     : ReferenceCounterBase(), ME("SessionName"), global_(global)
00031 {
00032    initialize(absoluteName, "", 0);
00033 }
00034 
00035 SessionName::~SessionName()
00036 {
00037 }
00038 
00039 void SessionName::copy(const SessionName& data)
00040 {
00041    clusterNodeId_ = data.clusterNodeId_;
00042    subjectId_     = data.subjectId_;
00043    pubSessionId_  = data.pubSessionId_;
00044    useSessionMarker_  = data.useSessionMarker_;
00045 }
00046 
00047 
00048 void SessionName::initialize(const string& absoluteName, const string& defaultUserName, long publicSessionId)
00049 {
00050    pubSessionId_ = publicSessionId;
00051 
00052    // Change default to true on version 2.0!
00053    useSessionMarker_ = global_.getProperty().getBoolProperty("xmlBlaster/useSessionMarker", false);
00054 
00055    if (!absoluteName.empty()) {
00056       setAbsoluteName(absoluteName);
00057       return;
00058    }
00059 
00060    string name = global_.getProperty().getStringProperty("session.name", "");
00061    if (!name.empty()) {
00062       setAbsoluteName(name);
00063       return;
00064    }
00065 
00066    clusterNodeId_ = global_.getProperty().getStringProperty("session.clusterNodeId", "");
00067 
00068    if (!defaultUserName.empty()) {
00069       subjectId_ = defaultUserName;
00070       return;
00071    }
00072 
00073    subjectId_ = global_.getProperty().getStringProperty("user.name", "guest");
00074 }
00075 
00076 
00077 SessionName::SessionName(const SessionName& data) : ReferenceCounterBase(), ME(data.ME), global_(data.global_)
00078 {
00079    copy(data);
00080 }
00081 
00082 SessionName& SessionName::operator =(const SessionName& data)
00083 {
00084   copy(data);
00085   return *this;
00086 }
00087 
00088 
00089 void SessionName::setAbsoluteName(const string& name)
00090 {
00091    pubSessionId_ = 0; // resets the value if previously set
00092    string relative = "";
00093    if (name.empty())
00094       throw XmlBlasterException(USER_ILLEGALARGUMENT, ME + "::setAbsoluteName", "the absolute name is empty");
00095 
00096    if (name[0] == '/') { // then it is an absolute name
00097       StringStripper stripper("/");
00098       vector<std::string> help = stripper.strip(name);
00099       help.erase(help.begin()); // since it is empty for sure.
00100       if (help.size() < 4) 
00101          throw XmlBlasterException(USER_ILLEGALARGUMENT, ME + "::setAbsoluteName", "the absolute name '" + name + "' is not allowed");
00102       if (help[0] == "node") clusterNodeId_ = help[1];
00103       else throw XmlBlasterException(USER_ILLEGALARGUMENT, ME + "::setAbsoluteName", "the absolute name '" + name + "' is not allowed. It should start with '/node'");
00104       if (help[2] != "client") 
00105          throw XmlBlasterException(USER_ILLEGALARGUMENT, ME + "::setAbsoluteName", "the absolute name '" + name + "' is not allowed. '/client' is missing");
00106    
00107       for (size_t i=3; i < help.size(); i++) {
00108          relative += help[i];
00109         if ( i < help.size()-1) relative += "/";
00110       }
00111    }
00112    else relative = name;
00113 
00114    StringStripper relStripper("/");
00115    vector<std::string> relHelp = relStripper.strip(relative);
00116    if (relHelp.empty()) {
00117       throw XmlBlasterException(USER_ILLEGALARGUMENT, ME + "::setAbsoluteName", "there is no relative name information: '" + name + "' is not allowed");
00118    }
00119 
00120    size_t ii = 0;
00121    if ( relHelp.size() > ii ) {
00122       string tmp = relHelp[ii++];
00123       if ( tmp == "client" ) {
00124          if ( relHelp.size() > ii ) {
00125             subjectId_ = relHelp[ii++];
00126          }
00127          else {
00128             throw XmlBlasterException(USER_ILLEGALARGUMENT, ME + "::setAbsoluteName", "there is no relative name information: '" + name + "' is not allowed");
00129          }
00130       }
00131       else subjectId_ = tmp;
00132    }
00133    if ( relHelp.size() > ii ) {
00134       string tmp = relHelp[ii++];
00135       if ( tmp == "session" ) {
00136          if ( relHelp.size() > ii ) {
00137             pubSessionId_ = lexical_cast<long>(relHelp[ii++]);
00138          }
00139          else {
00140             throw XmlBlasterException(USER_ILLEGALARGUMENT, ME + "::setAbsoluteName", "there is no relative session number given: '" + name + "' is not allowed");
00141          }
00142       }
00143       else pubSessionId_ = lexical_cast<long>(tmp);
00144    }
00145 }
00146 
00147 string SessionName::getRelativeName() const
00148 {
00149     return getRelativeName(false);
00150 }
00151 
00152 string SessionName::getRelativeName(bool forceSessionMarker) const
00153 {
00154    string ret = string("client/") + subjectId_;
00155    if (pubSessionId_ != 0) {
00156       if (useSessionMarker_ || forceSessionMarker)
00157          ret += string("/session/") + lexical_cast<std::string>(pubSessionId_);
00158       else
00159          ret += string("/") + lexical_cast<std::string>(pubSessionId_);
00160    }
00161    return ret;
00162 }
00163 
00164 string SessionName::getAbsoluteName() const
00165 {
00166    if (clusterNodeId_.empty()) return getRelativeName();
00167    return string("/node/") + clusterNodeId_ + string("/") + getRelativeName();
00168 }
00169 
00170 string SessionName::getClusterNodeId() const
00171 {
00172    return clusterNodeId_;
00173 }
00174 
00175 void SessionName::setClusterNodeId(const string& clusterNodeId)
00176 {
00177    clusterNodeId_ = clusterNodeId;
00178 }
00179 
00180 string SessionName::getSubjectId() const
00181 {
00182    return subjectId_;
00183 }
00184 
00185 void SessionName::setSubjectId(const string& subjectId)
00186 {
00187     subjectId_ = subjectId;
00188 }
00189 
00190 long SessionName::getPubSessionId() const
00191 {
00192    return pubSessionId_;
00193 }
00194 
00195 void SessionName::setPubSessionId(const long pubSessionId)
00196 {
00197    pubSessionId_ = pubSessionId;
00198 }
00199 
00200 string SessionName::toXml(const string& extraOffset) const
00201 {
00202    string offset = Constants::OFFSET + extraOffset;
00203    string ret;
00204    
00205    ret += offset + string("<session");
00206    ret += string(" name='")  + getAbsoluteName() + string("'");
00207    ret += string("/>\n");
00208    return ret;
00209 }
00210 
00211 }}}