1 /*------------------------------------------------------------------------------
  2 Name:      CbQueueProperty.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Holding callback queue properties
  6 Version:   $Id: CbQueueProperty.cpp 17555 2008-11-24 10:45:20Z ruff $
  7 ------------------------------------------------------------------------------*/
  8 
  9 /**
 10  * Helper class holding callback queue properties.
 11  * <p />
 12  * See ConnectQos for XML sysntax.
 13  * @see org.xmlBlaster.client.qos.ConnectQos
 14  */
 15 
 16 #include <util/qos/storage/CbQueueProperty.h>
 17 #include <util/lexical_cast.h>
 18 #include <ctype.h> // for toUpper
 19 #include <util/Global.h>
 20 
 21 namespace org { namespace xmlBlaster { namespace util { namespace qos { namespace storage {
 22 
 23 using namespace std;
 24 using namespace org::xmlBlaster::util;
 25 using namespace org::xmlBlaster::util::qos::address;
 26 
 27    CbQueueProperty::CbQueueProperty(Global& global,
 28                                     const string& relating,
 29                                     const string& nodeId)
 30                                   : QueuePropertyBase(global, nodeId)
 31    {
 32       ME = "CbQueueProperty";
 33       setRelating(relating);
 34       QueuePropertyBase::initialize(Constants::RELATING_CALLBACK);
 35       getCurrentCallbackAddress(); // Force creation, to read environment settings
 36    }
 37 
 38    CbQueueProperty::CbQueueProperty(const QueuePropertyBase& prop)
 39       : QueuePropertyBase(prop)
 40    {
 41    }
 42 
 43    CbQueueProperty&
 44    CbQueueProperty::operator =(const QueuePropertyBase& prop)
 45    {
 46       copy(prop);
 47       return *this;
 48    }
 49 
 50    CbQueueProperty::~CbQueueProperty()
 51    {
 52    }
 53 
 54    /**
 55     * Show some important settings for logging
 56     */
 57    string CbQueueProperty::getSettings()
 58    {
 59       string ret;
 60       ret += string("type=") + getType() + string(" onOverflow=") +
 61              getOnOverflow() + string(" onFailure=") + getOnFailure() +
 62              string(" maxEntries=") + lexical_cast<std::string>(getMaxEntries());
 63       if (!addressArr_.empty())
 64          ret += string(" ") + getCurrentCallbackAddress()->getSettings();
 65       return ret;
 66    }
 67 
 68 
 69    /**
 70     * This method converts a string to lowercase. Note that the input string is
 71     * modified and a reference to it is returned.
 72     */
 73    string& CbQueueProperty::toLowerCase(string& ref)
 74    {
 75       string::iterator iter = ref.begin();
 76       while (iter != ref.end()) {
 77          *iter = (char)::tolower(*iter);
 78          iter++;
 79       }
 80       return ref;
 81    }
 82 
 83    /**
 84     * @param relating    To what is this queue related: Constants.RELATING_CALLBACK | Constants.RELATING_SUBJECT
 85     */
 86    void CbQueueProperty::setRelating(const string& relating)
 87    {
 88       if (relating == "") {
 89          relating_ = Constants::RELATING_CALLBACK;
 90          return;
 91       }
 92       string help = relating;
 93       help = toLowerCase(help);
 94 
 95       if (Constants::RELATING_CALLBACK == help)
 96          relating_ = Constants::RELATING_CALLBACK;
 97       else if (Constants::RELATING_SUBJECT == help)
 98          relating_ = Constants::RELATING_SUBJECT;
 99       else {
100          log_.warn(ME, string("The queue relating attribute is invalid '") + relating + string("', setting to session scope"));
101          relating_ = Constants::RELATING_CALLBACK;
102       }
103    }
104 
105    bool CbQueueProperty::isSubjectRelated()
106    {
107       return Constants::RELATING_SUBJECT == getRelating();
108    }
109 
110    bool CbQueueProperty::isSessionRelated()
111    {
112       return Constants::RELATING_CALLBACK == getRelating();
113    }
114 
115    bool CbQueueProperty::onOverflowDeadMessage()
116    {
117       if (Constants::ONOVERFLOW_DEADMESSAGE == getOnOverflow())
118          return true;
119       return false;
120    }
121 
122 
123    /**
124     * Currently only one address is allowed, failover addresses will be implemented in a future version
125     */
126    void CbQueueProperty::setCallbackAddress(const AddressBaseRef& address)
127    {
128 //      addressArr_.insert(addressArr_.begin(), address);
129       if (!addressArr_.empty()) addressArr_.erase(addressArr_.begin());
130       addressArr_.insert(addressArr_.begin(), address);
131    }
132 
133    /**
134     */
135    void CbQueueProperty::setCallbackAddresses(const AddressVector& addresses)
136    {
137       addressArr_ = AddressVector(addresses);
138    }
139 
140    /**
141     * @return array with size 0 if none available
142     */
143    AddressVector CbQueueProperty::getCallbackAddresses()
144    {
145       return addressArr_;
146    }
147 
148    /**
149     * @return a default if none available
150     */
151    AddressBaseRef CbQueueProperty::getCurrentCallbackAddress()
152    {
153       if (addressArr_.empty()) {
154          setCallbackAddress(new CallbackAddress(global_));
155       }
156       return *addressArr_.begin();
157    }
158 
159    /**
160     * Get a usage string for the connection parameters
161     */
162    string CbQueueProperty::usage()
163    {
164       string text;
165       text += string("Control the callback queue properties (on server side):\n");
166       text += string("   -queue/callback/maxEntries [") + lexical_cast<std::string>(DEFAULT_maxEntriesDefault) + string("]\n");
167       text += string("                       The maximum allowed number of messages in this queue.\n");
168       text += string("   -queue/callback/maxEntriesCache [") + lexical_cast<std::string>(DEFAULT_maxEntriesDefault) + string("]\n");
169       text += string("                       The maximum allowed number of messages in the cache of this queue.\n");
170       text += string("   -queue/callback/maxBytes [") + lexical_cast<std::string>(DEFAULT_bytesDefault) + string("]\n");
171       text += string("                       The maximum size in kBytes of this queue.\n");
172       text += string("   -queue/callback/maxBytesCache [") + lexical_cast<std::string>(DEFAULT_bytesDefault) + string("]\n");
173       text += string("                       The maximum size in kBytes in the cache of this queue.\n");
174     //text += "   -queue/callback/expires  If not otherwise noted a queue dies after these milliseconds [" + DEFAULT_expiresDefault + "].\n";
175     //text += "   -queue/callback/onOverflow What happens if queue is full. " + Constants.ONOVERFLOW_BLOCK + " | " + Constants.ONOVERFLOW_DEADMESSAGE + " [" + DEFAULT_onOverflow + "]\n";
176       //text += string("   -queue/callback/onOverflow What happens if queue is full [") + DEFAULT_onOverflow + string("]\n");
177       //text += string("   -queue/callback/onFailure  Error handling when callback failed [") + DEFAULT_onFailure + string("]\n");
178       text += string("   -queue/callback/type [") + DEFAULT_type + string("]\n");
179       text += string("                       The callback queue plugin type on server side\n");
180       //text += string("   -queue/callback/version    The plugin version [") + DEFAULT_version + string("]\n");
181       return text;
182    }
183 
184 }}}}} // namespace
185 
186 
187 #ifdef _XMLBLASTER_CLASSTEST
188 
189 using namespace std;
190 using namespace org::xmlBlaster::util::qos::storage;
191 
192 /** For testing */
193 int main(int args, char* argv[])
194 {
195    Global& glob = Global::getInstance();
196    glob.initialize(args, argv);
197 
198    CbQueueProperty prop(glob, "", "");
199 
200    cout << prop.toXml() << endl;
201    CallbackAddress *adr = new CallbackAddress(glob, "EMAIL");
202    adr.setAddress("et@mars.sun");
203    prop.setCallbackAddress(adr);
204    cout << prop.toXml() << endl;
205 }
206 
207 #endif


syntax highlighted by Code2HTML, v. 0.9.1