00001 #include "stdafx.h"
00002
00003 #include "logger.h"
00004 #include "Broker.h"
00005 #include "AspectRep.h"
00006 #include "map"
00007 #include "Exceptions.h"
00008
00009
00010 int Broker::m_constraintId = 1;
00011 const int Broker::INVALID_METAREF = -1;
00012 const int Broker::INITIAL_METAREF = 1001;
00013 const std::string Broker::ROOTFOLDER_METAREF_STR = "1000";
00014 int Broker::m_metaRefId = INITIAL_METAREF;
00015 int Broker::m_firstFree = INITIAL_METAREF;
00016 std::list<Broker::MetaRefNode> Broker::m_metaRefDB;
00017
00018
00019
00020 void Broker::reset()
00021 {
00022 m_metaRefDB.clear();
00023 m_firstFree = INITIAL_METAREF;
00024
00025 m_constraintId = 0;
00026 m_metaRefId = INITIAL_METAREF;
00027 }
00028
00029
00030 void Broker::init()
00031 {
00032
00033
00034 m_constraintId = 1;
00035 m_metaRefId = (m_firstFree > INITIAL_METAREF)?m_firstFree : INITIAL_METAREF;
00036
00037
00038
00039
00040 m_metaRefDB.clear();
00041 }
00042
00043
00044 int Broker::getNextConstraintId()
00045 {
00046 return m_constraintId++;
00047 }
00048
00049
00050 int Broker::getNextMetaRefId()
00051 {
00052 return m_metaRefId++;
00053 }
00054
00055
00056 std::string Broker::getRegistryTokenName( const BON::Object& obj)
00057 {
00058 std::string kind = obj->getObjectMeta().name();
00059 std::string name = obj->getPath();
00060
00061 BON::ObjectPtr p = obj->getObjectI();
00062 long relid = 0;
00063 BONCOMTHROW( p->get_RelID(&relid));
00064 char t[16];
00065 sprintf( t, "%x", relid);
00066
00067 ASSERT( name.length() >= 1);
00068 std::string tmp = name.substr(1) + '-' + t + '-' + kind;
00069
00070 return tmp;
00071 }
00072
00073
00074 void Broker::initFromObj( BON::Object& obj, BON::Folder& folder, const std::string& kind)
00075 {
00076
00077
00078
00079 std::string token = Broker::getRegistryTokenName( obj) + "/MetaRef";
00080 initFromRegistry( obj, folder, folder->getRegistry()->getDescendantByPath( token));
00081 }
00082
00083
00084 void Broker::initFromAspectObj( BON::Object& obj, const std::string& name, BON::Folder& folder)
00085 {
00086
00087
00088
00089 std::string token = AspectRep::m_aspectRegistryRoot + '/' + AspectRep::m_aspectMetaRefsRoot + '/' + name + "/MetaRef";
00090 initFromRegistry( obj, folder, folder->getRegistry()->getDescendantByPath( token));
00091 }
00092
00093 void Broker::initFromRegistry( BON::Object& obj, BON::Folder& folder, const BON::RegistryNode& reg_node)
00094 {
00095 if ( reg_node->getStatus() == BON::RNS_Here)
00096 {
00097 int k = reg_node->getIntegerValue();
00098 MetaRefNode node( obj, folder, reg_node->getPath(), reg_node->getIntegerValue());
00099 m_metaRefDB.push_back( node);
00100 m_firstFree = max( m_firstFree, node.metaref + 1);
00101 }
00102
00103 std::set<BON::RegistryNode> children = reg_node->getChildren();
00104 std::set<BON::RegistryNode>::iterator c_it = children.begin();
00105 for(; c_it != children.end(); ++c_it)
00106 initFromRegistry( obj, folder, *c_it);
00107 }
00108
00109
00110 void Broker::checkDuplicates()
00111 {
00112
00113
00114 std::map< int, MetaRefDB_Iterator > check_map;
00115
00116 MetaRefDB_Iterator it = m_metaRefDB.begin();
00117 for( ; it != m_metaRefDB.end(); ++it)
00118 {
00119 MetaRefNode node = *it;
00120
00121 std::map< int, MetaRefDB_Iterator >::iterator map_it = check_map.end();
00122 map_it = check_map.find( node.metaref);
00123
00124 if ( map_it != check_map.end())
00125 {
00126 MetaRefDB_Iterator jt = map_it->second;
00127 MetaRefNode otherNode = *jt;
00128 int t_1 = otherNode.metaref;
00129 bool flag = false;
00130 std::string m1 = node.obj->getID();
00131 std::string m2 = otherNode.obj->getID();
00132
00133 std::string p = node.obj->getName();
00134 if ( m1 < m2)
00135 {
00136 m_metaRefDB.erase( jt);
00137 check_map[ node.metaref] = it;
00138 flag = true;
00139 }
00140 else
00141 {
00142 it = m_metaRefDB.erase(it);
00143 --it;
00144 flag = false;
00145 }
00146 std::string token = (flag ? otherNode : node).path.substr(1);
00147 try {
00148
00149 if ( flag)
00150 otherNode.folder->getRegistry()->getDescendantByPath( token)->clear();
00151 else
00152 node.folder->getRegistry()->getDescendantByPath( token)->clear();
00153 #ifdef _DEBUG
00154 std::string fmtstr = std::string("Duplicate metaref found. Class ") + (flag ? otherNode : node).obj->getName() +
00155 " subnode: " + std::string((flag ? otherNode : node).path.empty() ? "n/a" : (flag ? otherNode : node).path + " has been reset to Undefined value");
00156 std::string n1str = "Name" + node.obj->getName() + " path: " + node.path;
00157 std::string n2str = "Name" + otherNode.obj->getName() + " path: " + otherNode.path;
00158
00159 #endif
00160 }
00161 catch(...) {
00162
00163 }
00164 }
00165 else
00166 {
00167 check_map[node.metaref] = it;
00168 }
00169 }
00170 }
00171