util/cluster/NodeId.cpp

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------------
00002 Name:      NodeId.cpp
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Holds the unique name of a cluster node
00006 ------------------------------------------------------------------------------*/
00007 
00008 
00017 #include <util/cluster/NodeId.h>
00018 #include <util/Global.h>
00019 #include <util/lexical_cast.h>
00020 
00021 namespace org { namespace xmlBlaster { namespace util { namespace cluster {
00022 
00023 using namespace std;
00024 using namespace org::xmlBlaster::util;
00025 
00026 NodeId::NodeId(Global& global, const string& id)
00027 : ME("NodeId"), global_(global), log_(global.getLog("org.xmlBlaster.cluster"))
00028 {
00029    setId(id);
00030 }
00031 
00032 NodeId::NodeId(const NodeId& nodeId) : ME("NodeId"), global_(nodeId.global_), log_(nodeId.log_)
00033 {
00034    setId(nodeId.id_);
00035 }
00036 
00037 NodeId& NodeId::operator =(const NodeId& nodeId)
00038 {
00039    setId(nodeId.id_);
00040    return *this;
00041 }
00042 
00043 string NodeId::getId() const
00044 {
00045    return id_;
00046 }
00047 
00052 void NodeId::setId(const string& id)
00053 {
00054    if (id.empty()) {
00055 //      log_.error(ME, "Cluster node has no name");
00056       id_ = "NoNameNode";
00057    }
00058    id_ = id;
00059 
00060    if (id_.find_first_of("/node/") == 0)
00061       id_ = id_.substr(string("/node/").length()); // strip leading "/node/"
00062    string::size_type index = id_.find_first_of("/");   // strip tailing tokens, e.g. from "heron/client/joe" make a "heron"
00063    if (index == 0) {
00064       throw XmlBlasterException(INTERNAL_ILLEGALARGUMENT, ME, "setId: The given cluster node ID '" + lexical_cast<std::string>(id_) + "' may not start with a '/'");
00065    }
00066    if (index > 0) {
00067       id_ = id_.substr(0, index);
00068    }
00069 }
00070 
00071 string NodeId::toString() const
00072 {
00073    return getId();
00074 }
00075  
00079 bool NodeId::operator <(const NodeId& nodeId) const
00080 {
00081    return toString() < nodeId.toString();
00082 }
00083 
00084 bool NodeId::operator ==(const NodeId& nodeId) const
00085 {
00086    return toString() == nodeId.toString();
00087 }
00088 
00089 }}}} // namespace