1 /*------------------------------------------------------------------------------
  2 Name:      QueuePropertyFactory.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Factory which creates objects holding queue properties
  6 Version:   $Id: QueuePropertyFactory.cpp 16373 2007-07-19 19:10:21Z ruff $
  7 ------------------------------------------------------------------------------*/
  8 
  9 #include <util/qos/storage/QueuePropertyFactory.h>
 10 #include <util/lexical_cast.h>
 11 #include <util/Global.h>
 12 
 13 using namespace std;
 14 using namespace org::xmlBlaster::util;
 15 using namespace org::xmlBlaster::util::parser;
 16 using namespace org::xmlBlaster::util::qos::address;
 17 
 18 
 19 namespace org { namespace xmlBlaster { namespace util { namespace qos { namespace storage {
 20 
 21 
 22 QueuePropertyFactory::QueuePropertyFactory(Global& global)
 23    : XmlHandlerBase(global), ME("QueuePropertyFactory"), prop_(global, ""), addressFactory_(global)
 24 {
 25    RELATING   = "relating";
 26    inAddress_ = false;
 27 }
 28 
 29 QueuePropertyFactory::~QueuePropertyFactory()
 30 {
 31 }
 32 
 33 /*
 34 void QueuePropertyFactory::reset(QueuePropertyBase& prop)
 35 {
 36    prop_ = &prop;
 37 }
 38 */
 39 
 40 QueuePropertyBase QueuePropertyFactory::getQueueProperty()
 41 {
 42    return prop_;
 43 }
 44 
 45 
 46 /**
 47  * Called for SAX callback start tag
 48  */
 49 void QueuePropertyFactory::startElement(const string &name, const AttributeMap& attrs)
 50 {
 51    //if (log_.call()) log_.call(ME, "startElement: " + getStartElementAsString(name, attrs));
 52 
 53    // in case it is inside or entering an 'address' or 'callbackAddress'
 54    if (name.compare("address") == 0) {
 55       inAddress_ = true;
 56       addressFactory_.reset(new Address(global_));
 57       addressFactory_.startElement(name, attrs);
 58       return;
 59    }
 60    if (name.compare("callback") == 0) {
 61       inAddress_ = true;
 62       addressFactory_.reset(new CallbackAddress(global_));
 63       addressFactory_.startElement(name, attrs);
 64       return;
 65    }
 66    if (inAddress_) {
 67       addressFactory_.startElement(name, attrs);
 68       return;
 69    }
 70 
 71    // not inside any of the sub-elements (the root element)
 72    prop_ = QueuePropertyBase(global_, "");
 73    //if (log_.trace()) log_.trace(ME, "queue properties are created");
 74 
 75 
 76    string relating;
 77    if (getStringAttr(attrs, RELATING, relating)) {
 78       //if (log_.trace()) log_.trace(ME, "attribute 'relating' found. it is '" + relating + "'");
 79       if (relating == "callback") prop_.initialize("callback");
 80       else prop_.initialize("");
 81       prop_.setRelating(relating);
 82       if (log_.trace()) log_.trace(ME, string("the queue is relating to ") + relating);
 83    }
 84 
 85    AttributeMap::const_iterator iter = attrs.begin();
 86    bool found = false;
 87    string tmpName = (*iter).first;
 88    string tmpValue = (*iter).second;
 89 
 90    while (iter != attrs.end()) {
 91 
 92       if (tmpName.compare("relating") == 0) {
 93           // do nothing since it is already done as the first thing
 94           // but leave it to avoid warnings
 95           found = true;
 96       }
 97       else if (tmpName.compare("maxEntries") == 0) {
 98             prop_.setMaxEntries(XmlHandlerBase::getLongValue(tmpValue));
 99       }
100       else if (tmpName.compare("type") == 0) {
101             prop_.setType(tmpValue);
102       }
103       else if (tmpName.compare("version") == 0) {
104             prop_.setVersion(tmpValue);
105       }
106       else if (tmpName.compare("maxEntriesCache") == 0) {
107             prop_.setMaxEntriesCache(XmlHandlerBase::getLongValue(tmpValue));
108       }
109       else if (tmpName.compare("maxBytes") == 0) {
110             prop_.setMaxBytes(XmlHandlerBase::getLongValue(tmpValue));
111       }
112       else if (tmpName.compare("maxBytesCache") == 0) {
113             prop_.setMaxBytesCache(XmlHandlerBase::getLongValue(tmpValue));
114       }
115       else if (tmpName.compare("storeSwapLevel") == 0) {
116             prop_.setStoreSwapLevel(XmlHandlerBase::getLongValue(tmpValue));
117       }
118       else if (tmpName.compare("storeSwapBytes") == 0) {
119             prop_.setStoreSwapBytes(XmlHandlerBase::getLongValue(tmpValue));
120       }
121       else if (tmpName.compare("reloadSwapLevel") == 0) {
122             prop_.setReloadSwapLevel(XmlHandlerBase::getLongValue(tmpValue));
123       }
124       else if (tmpName.compare("reloadSwapBytes") == 0) {
125             prop_.setReloadSwapBytes(XmlHandlerBase::getLongValue(tmpValue));
126       }
127       else if (tmpName.compare("expires") == 0) {
128             prop_.setExpires(XmlHandlerBase::getTimestampValue(tmpValue));
129       }
130       else if (tmpName.compare("onOverflow") == 0) {
131             prop_.setOnOverflow(tmpValue);
132       }
133       else if (tmpName.compare("onFailure") == 0) {
134             prop_.setOnFailure(tmpValue);
135       }
136 
137       else if (tmpName.compare("size") == 0) {
138          // not doing anything (this is done outside: only in MsgQos Factory
139       }
140       else if (tmpName.compare("index") == 0) {
141          // not doing anything (this is done outside: only in MsgQos Factory
142       }
143       else {
144          log_.warn(ME, string("Ignoring unknown attribute '") + tmpName +
145              string("' in connect QoS <queue>"));
146       }
147       iter++;
148    }
149    if (!found) {
150       if (log_.trace()) log_.trace(ME, "Missing 'relating' attribute in connect QoS <queue>");
151    }
152 }
153 
154 
155 void QueuePropertyFactory::characters(const string &ch)
156 {
157    if (inAddress_) addressFactory_.characters(ch);
158 }
159 
160 /** End element. */
161 void QueuePropertyFactory::endElement(const string &name)
162 {
163    // in case it is inside or entering an 'address' or 'callbackAddress'
164    if (name.compare("address") == 0) {
165       addressFactory_.endElement(name);
166       prop_.addressArr_.insert((prop_.addressArr_).begin(), addressFactory_.getAddress());
167       inAddress_ = false;
168       return;
169    }
170    if (name.compare("callback") == 0) {
171       addressFactory_.endElement(name);
172       prop_.addressArr_.insert((prop_.addressArr_).begin(), addressFactory_.getAddress());
173       inAddress_ = false;
174       return;
175    }
176    if (inAddress_) {
177       addressFactory_.endElement(name);
178       return;
179    }
180 }
181 
182 /*
183 QueuePropertyBase&
184 QueuePropertyFactory::readQueueProperty(const string& literal, QueuePropertyBase& prop)
185 {
186    reset(prop);
187    init(literal);
188    return getQueueProperty();
189 }
190 */
191 
192 QueuePropertyBase QueuePropertyFactory::readObject(const string& literal)
193 {
194    init(literal);
195    return prop_;
196 }
197 
198 }}}}} // namespaces
199 
200 
201 #ifdef _XMLBLASTER_CLASSTEST
202 
203 #include <util/qos/storage/ClientQueueProperty.h>
204 
205 using namespace std;
206 using namespace org::xmlBlaster::util::qos::storage;
207 
208 /** For testing: java org.xmlBlaster.authentication.plugins.simple.SecurityQos */
209 int main(int args, char* argv[])
210 {
211    try {
212       Global& glob = Global::getInstance();
213       glob.initialize(args, argv);
214       ClientQueueProperty prop(glob, "");
215       cout << prop.toXml() << endl;
216       Address adr(glob, "EMAIL");
217       adr.setAddress("et@mars.sun");
218       prop.setAddress(adr);
219       string literal = prop.toXml();
220       cout << literal << endl;
221 
222       QueuePropertyFactory factory(glob);
223 //      ClientQueueProperty prop1(glob, "");
224 
225       QueuePropertyBase base= factory.readObject(literal);
226       cout << "after reparsing the same object : " << endl;
227       cout << base.toXml() << endl;
228    }
229    catch (...) {
230       cout << "an exception occured in the main thread" << endl;
231    }
232   return 0;
233 }
234 
235 #endif


syntax highlighted by Code2HTML, v. 0.9.1