1 /*------------------------------------------------------------------------------
2 Name: NodeId.cpp
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Holds the unique name of a cluster node
6 ------------------------------------------------------------------------------*/
7
8
9 /**
10 * Holds the unique name of an xmlBlaster server instance (= cluster node)
11 * @author xmlBlaster@marcelruff.info
12 * @author michele@laghi.eu
13 * @since 0.79e
14 * @url http://www.xmlBlaster.org/xmlBlaster/doc/requirements/cluster.html
15 */
16
17 #include <util/cluster/NodeId.h>
18 #include <util/Global.h>
19 #include <util/lexical_cast.h>
20
21 namespace org { namespace xmlBlaster { namespace util { namespace cluster {
22
23 using namespace std;
24 using namespace org::xmlBlaster::util;
25
26 NodeId::NodeId(Global& global, const string& id)
27 : ME("NodeId"), global_(global), log_(global.getLog("org.xmlBlaster.cluster"))
28 {
29 setId(id);
30 }
31
32 NodeId::NodeId(const NodeId& nodeId) : ME("NodeId"), global_(nodeId.global_), log_(nodeId.log_)
33 {
34 setId(nodeId.id_);
35 }
36
37 NodeId& NodeId::operator =(const NodeId& nodeId)
38 {
39 setId(nodeId.id_);
40 return *this;
41 }
42
43 string NodeId::getId() const
44 {
45 return id_;
46 }
47
48 /**
49 * @param id The cluster node id, e.g. "heron".<br />
50 * If you pass "/node/heron/client/joe" everything ins stripped to get "heron"
51 */
52 void NodeId::setId(const string& id)
53 {
54 if (id.empty()) {
55 // log_.error(ME, "Cluster node has no name");
56 id_ = "NoNameNode";
57 }
58 id_ = id;
59
60 if (id_.find_first_of("/node/") == 0)
61 id_ = id_.substr(string("/node/").length()); // strip leading "/node/"
62 string::size_type index = id_.find_first_of("/"); // strip tailing tokens, e.g. from "heron/client/joe" make a "heron"
63 if (index == 0) {
64 throw XmlBlasterException(INTERNAL_ILLEGALARGUMENT, ME, "setId: The given cluster node ID '" + lexical_cast<std::string>(id_) + "' may not start with a '/'");
65 }
66 if (index > 0) {
67 id_ = id_.substr(0, index);
68 }
69 }
70
71 string NodeId::toString() const
72 {
73 return getId();
74 }
75
76 /**
77 * Needed for use in TreeSet and TreeMap, enforced by java.lang.Comparable
78 */
79 bool NodeId::operator <(const NodeId& nodeId) const
80 {
81 return toString() < nodeId.toString();
82 }
83
84 bool NodeId::operator ==(const NodeId& nodeId) const
85 {
86 return toString() == nodeId.toString();
87 }
88
89 }}}} // namespace
syntax highlighted by Code2HTML, v. 0.9.1