1 /*------------------------------------------------------------------------------
  2 Name:      AddressFactory.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Factory Object for parsing Address objects.
  6 Version:   $Id: AddressFactory.cpp 15130 2006-05-10 09:53:35Z ruff $
  7 ------------------------------------------------------------------------------*/
  8 
  9 /**
 10  * Factory for the creation (SAX parsing from string) of AddressBase objects.
 11  * The created AddressBase objects can easely be converted to Address and
 12  * CallbackAddress objects.
 13  * See classes of the object it creates.
 14  * @see AddressBase
 15  * @see Address
 16  * @see CallbackAddress
 17  */
 18 
 19 #include <util/qos/address/AddressFactory.h>
 20 #include <util/Global.h>
 21 #include <util/lexical_cast.h>
 22 
 23 namespace org { namespace xmlBlaster { namespace util { namespace qos { namespace address {
 24 
 25 using namespace std;
 26 using namespace org::xmlBlaster::util;
 27 using namespace org::xmlBlaster::util::parser;
 28 
 29 
 30 AddressFactory::AddressFactory(Global& global)
 31    : XmlHandlerBase(global), ME("AddressFactory"),
 32    address_(new Address(global)), // dummy allocation
 33    inAttribute_(false),
 34    attribute_(0)
 35 {
 36 }
 37 
 38 AddressFactory::~AddressFactory()
 39 {
 40    log_.call(ME, "destructor");
 41    if (attribute_ != 0) {
 42       delete(attribute_);
 43    }
 44 }
 45 
 46 void AddressFactory::reset(const AddressBaseRef& address)
 47 {
 48    address_ = address;
 49    inAttribute_ = false;
 50    if (attribute_ != 0) {
 51       delete(attribute_);
 52    }
 53 }
 54 
 55 AddressBaseRef AddressFactory::getAddress()
 56 {
 57    return address_;
 58 }
 59 
 60 /**
 61  * Called for SAX callback start tag
 62  */
 63 // void startElement(const string& uri, const string& localName, const string& name, const string& character, Attributes attrs)
 64 void AddressFactory::startElement(const string &name, const AttributeMap& attrs)
 65 {
 66    //if (log_.call()) log_.call(ME, "startElement: " + getStartElementAsString(name, attrs));
 67 
 68    if (character_.length() > 0) {
 69       StringTrim::trim(character_);
 70       if (!character_.empty()) {
 71          address_->setAddress(character_);
 72       }
 73       character_.erase();
 74    }
 75 
 76    if (name.compare(address_->rootTag_) == 0) { // callback
 77       AttributeMap::const_iterator iter = attrs.begin();
 78       while (iter != attrs.end()) {
 79          string tmpName = (*iter).first;
 80          string tmpValue = (*iter).second;
 81          if (tmpName.compare("type") == 0) {
 82                address_->setType(tmpValue);
 83          }
 84          else if (tmpName.compare("version") == 0) {
 85             address_->setVersion(tmpValue);
 86          }
 87          else if (tmpName.compare("bootstrapHostname") == 0) {
 88             address_->setHostname(tmpValue);
 89          }
 90          else if (tmpName.compare("bootstrapPort") == 0) {
 91             address_->setPort(XmlHandlerBase::getIntValue(tmpValue));
 92          }
 93          else if (tmpName.compare("sessionId") == 0) {
 94             address_->setSecretSessionId(tmpValue);
 95          }
 96          else if (tmpName.compare("pingInterval") == 0) {
 97             address_->setPingInterval(XmlHandlerBase::getLongValue(tmpValue));
 98          }
 99          else if (tmpName.compare("retries") == 0) {
100             address_->setRetries(XmlHandlerBase::getIntValue(tmpValue));
101          }
102          else if (tmpName.compare("delay") == 0) {
103             address_->setDelay(XmlHandlerBase::getLongValue(tmpValue));
104          }
105          else if (tmpName.compare("oneway") == 0) {
106             address_->setOneway(XmlHandlerBase::getBoolValue(tmpValue));
107          }
108          else if (tmpName.compare("dispatcherActive") == 0) {
109             address_->setDispatcherActive(XmlHandlerBase::getBoolValue(tmpValue));
110          }
111          else if (tmpName.compare("useForSubjectQueue") == 0) {
112             address_->useForSubjectQueue_ = XmlHandlerBase::getBoolValue(tmpValue);
113          }
114          else if (tmpName.compare("dispatchPlugin") == 0) {
115             address_->dispatchPlugin_ = tmpValue;
116          }
117          else {
118             log_.warn(ME, string("Ignoring unknown attribute ") +
119               tmpName +  string(" in ") + address_->rootTag_ + string(" section."));
120          }
121          iter++;
122       }
123       if (address_->getType() == "") {
124          log_.error(ME, string("Missing '") + address_->rootTag_ + string("' attribute 'type' in QoS"));
125          address_->setType(Global::getDefaultProtocol());
126       }
127 
128       if (address_->getSecretSessionId() == "") {
129          log_.warn(ME, string("Missing '") + address_->rootTag_ + string("' attribute 'sessionId' QoS"));
130       }
131       return;
132    }
133 
134    if (name.compare("burstMode") == 0) {
135       AttributeMap::const_iterator iter = attrs.begin();
136       bool found = false;
137       while (iter != attrs.end()) {
138          if (((*iter).first).compare("collectTime") == 0) {
139             address_->setCollectTime(XmlHandlerBase::getLongValue((*iter).second));
140             found = true;
141          }
142          else if (((*iter).first).compare("maxEntries") == 0) {
143             address_->setBurstModeMaxEntries(XmlHandlerBase::getIntValue((*iter).second));
144             found = true;
145          }
146          else if (((*iter).first).compare("maxBytes") == 0) {
147             address_->setBurstModeMaxBytes(XmlHandlerBase::getLongValue((*iter).second));
148             found = true;
149          }
150          iter++;
151       }
152       if (!found) log_.warn(ME, "Missing attributes in login-qos <burstMode>");
153       return;
154    }
155 
156    if (name.compare("compress") == 0) {
157       AttributeMap::const_iterator iter = attrs.begin();
158       string tmpName = (*iter).first;
159       string tmpValue = (*iter).second;
160       bool found = false;
161       while (iter != attrs.end()) {
162          if (tmpName.compare("type") == 0) {
163             address_->setCompressType(tmpValue);
164             found = true;
165          }
166          else if (tmpName.compare("minSize") == 0) {
167             address_->setMinSize(XmlHandlerBase::getLongValue(tmpValue));
168          }
169          iter++;
170       }
171       if (!found) log_.error(ME, "Missing 'type' attribute in qos <compress>");
172       return;
173    }
174    if (name.compare("ptp") == 0) {
175       return;
176    }
177 
178    if (name.compare(ATTRIBUTE_TAG) == 0) { // "attribute"
179       inAttribute_ = true;
180       attributeCharacter_.erase();
181       string nameAttr;
182       AttributeMap::const_iterator iter = attrs.find("name");
183       if (iter != attrs.end()) nameAttr = (*iter).second;
184       string encoding;
185       iter = attrs.find("encoding");
186       if (iter != attrs.end()) encoding = (*iter).second;
187       string type;
188       iter = attrs.find("type");
189       if (iter != attrs.end()) type = (*iter).second;
190       string charset;
191       iter = attrs.find("charset");
192       if (iter != attrs.end()) charset = (*iter).second;
193       attribute_ = new ClientProperty(true, nameAttr, type, encoding, charset);
194    }
195 }
196 
197 /**
198  * Handle SAX parsed end element
199  */
200 
201 /** End element. */
202 // public final void endElement(String uri, String localName, String name, StringBuffer character) {
203 void AddressFactory::endElement(const string &name)
204 {
205    // if (log_.trace()) log_.trace(ME, string("::endElement: '") + name + string("'"));
206    if (name.compare(address_->rootTag_) == 0) { // callback
207       StringTrim::trim(character_);
208       if (!character_.empty()) address_->setAddress(character_);
209       else if (address_->getRawAddress() == "")
210          log_.error(ME, address_->rootTag_ + string(" QoS contains no address data"));
211 
212    }
213    else if (name.compare("burstMode") == 0) {
214    }
215    else if (name.compare("compress") == 0) {
216    }
217    else if (name.compare("ptp") == 0) {
218       address_->ptpAllowed_ = StringTrim::isTrueTrim(character_);
219    }
220    if (name.compare(ATTRIBUTE_TAG) == 0) { // "attribute"
221       inAttribute_ = false;
222       attribute_->setValueRaw(attributeCharacter_);
223       address_->addAttribute(*attribute_);
224       delete attribute_;
225       attribute_ = 0;
226       attributeCharacter_.erase();
227    }
228    character_.erase();
229 }
230 
231 
232 AddressBaseRef AddressFactory::readAddress(const string& litteral, const AddressBaseRef& address)
233 {
234    reset(address);
235    init(litteral);
236    return getAddress();
237 }
238 
239 }}}}} // namespaces
240 
241 
242 #ifdef _XMLBLASTER_CLASSTEST
243 #include <util/qos/address/Address.h>
244 
245 using namespace std;
246 using namespace org::xmlBlaster::util::qos::address;
247 
248 /** For testing: java org.xmlBlaster.authentication.plugins.simple.SecurityQos */
249 int main(int args, char* argv[])
250 {
251    try {
252       Global& glob = Global::getInstance();
253       glob.initialize(args, argv);
254       Log& log = glob.getLog("org.xmlBlaster.util.qos");
255       log.info("main", "This is a simple info");
256       Address a(glob);
257       a.setType("SOCKET");
258       a.setAddress("127.0.0.1:7600");
259       a.setCollectTime(12345l);
260       a.setPingInterval(54321l);
261       a.setRetries(17);
262       a.setDelay(7890l);
263       a.setOneway(true);
264       a.setDispatcherActive(false);
265       a.setSecretSessionId("0x4546hwi89");
266       cout << a.toXml() << endl;
267 
268       AddressFactory factory(glob);
269       Address addr(glob);
270       AddressBaseRef ptr = &factory.readAddress(a.toXml(), addr);
271       cout << "parsed one: " << endl << ptr->toXml() << endl;
272 
273       string nodeId = "heron";
274       int                nmax = 8;
275       const char** argc = new const char*[nmax];
276       argc[0] = "-sessionId";
277       argc[1] = "ERROR";
278       string help = string("-sessionId[") + nodeId + string("]");
279       argc[2] = string(help).c_str();
280       argc[3] = "OK";
281       argc[4] = "-pingInterval";
282       argc[5] = "8888";
283       help = string("-delay[") + nodeId + string("]");
284       argc[6] = help.c_str();
285       argc[7] = "8888";
286 
287       Global& glob = Global::getInstance();
288       glob.initialize(nmax, argc);
289       Address a(glob, "RMI", nodeId);
290       cout << a.toXml() << endl;
291    }
292    catch(...) {
293       cout << "unknown uncatched exception" << endl;
294    }
295 }
296 
297 #endif
298 
299 
300                             


syntax highlighted by Code2HTML, v. 0.9.1