1 /*------------------------------------------------------------------------------
  2 Name:      Global.h
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   The global object (a stack for all pseudo static stuff).
  6 Version:   $Id: Global.h 18007 2011-10-12 21:37:44Z ruff $
  7 ------------------------------------------------------------------------------*/
  8 
  9 #ifndef ORG_XMLBLASTER_UTIL_GLOBAL_H
 10 #define ORG_XMLBLASTER_UTIL_GLOBAL_H
 11 
 12 #include <util/xmlBlasterDef.h>
 13 #include <util/I_Log.h>
 14 #include <util/LogManager.h>
 15 #include <util/XmlBlasterException.h>
 16 #include <util/Property.h>
 17 #include <util/SessionName.h>
 18 #include <util/ReferenceCounterBase.h>
 19 #include <util/ReferenceHolder.h>
 20 
 21 #include <client/protocol/CbServerPluginManager.h>
 22 #include <util/dispatch/DispatchManager.h>
 23 #include <util/Timeout.hpp>
 24 
 25 #include <string>
 26 #include <map>
 27 
 28 // for managed objects
 29 #include <util/objman.h>
 30 
 31 namespace org { namespace xmlBlaster { namespace util {
 32 
 33 /**
 34  * Blocks until <enter> on the keyboard is hit. 
 35  * Consumes multiple hits (for Windows DOS box)
 36  * @param str If not null it will be printed on console with cout
 37  * @return The last entered line which contains something, previous lines and following empty lines are ignored
 38  */
 39 Dll_Export std::string waitOnKeyboardHit(const std::string &str);
 40 
 41 /**
 42  * Data holder of Properties dumped to argc/argv command line arguments
 43  * as passed to a main(). 
 44  * Example:
 45  * <pre>
 46  * argv[0] = myProg (the executable name) 
 47  * argv[1] = -name
 48  * argv[2] = joe
 49  * </pre>
 50  */
 51 typedef struct ArgsStruct {
 52    size_t argc;
 53    char **argv;
 54 } ArgsStruct_T;
 55 
 56 //class org::xmlBlaster::util::Global;
 57 typedef org::xmlBlaster::util::ReferenceHolder<org::xmlBlaster::util::Global> GlobalRef;
 58 
 59 /**
 60  * @author <a href="mailto:laghi@swissinfo.org">Michele Laghi</a>
 61  */
 62 class Dll_Export Global : public ReferenceCounterBase {
 63 
 64 friend Global& getInstance(const std::string &instanceName);
 65 
 66 // required for managed objects
 67 template <class TYPE> friend class ManagedObject;
 68 friend class Object_Lifetime_Manager;
 69 
 70 typedef std::map<std::string, GlobalRef> GlobalRefMap;
 71 typedef std::map<std::string, Global*> GlobalMap;
 72 
 73 private:
 74    const std::string      ME;
 75    Property*              property_;
 76    int                    args_;
 77    const char * const*    argv_;
 78    bool                   isInitialized_;
 79    org::xmlBlaster::util::LogManager logManager_;
 80    org::xmlBlaster::client::protocol::CbServerPluginManager* cbServerPluginManager_;
 81    org::xmlBlaster::util::dispatch::DispatchManager* dispatchManager_;
 82    Timeout*               pingTimer_;
 83    std::string            id_;
 84    std::string            immutableId_;
 85    std::string            instanceName_; /**< The name of the global as registered with createInstance(name) */
 86    mutable std::string    instanceId_;   /**< The unique id changes on each startup of same client */
 87    thread::Mutex          pingerMutex_;  /**< mutex to protect the ping thread */
 88    static thread::Mutex   globalMutex_;  /**< mutex for the global class */
 89    org::xmlBlaster::util::SessionNameRef sessionName_;  /**< added for managed objects. */
 90    static GlobalRefMap    globalRefMap_; /**< Map containing all further Global instances, see createInstance(), the Ref prevents the automatic destruction */
 91    static GlobalMap       globalMap_;    /**< Map containing all further Global pointer instances, see createInstance() */
 92    static Global*         global_;       /**< The first singleton instance */
 93 
 94    void copy();
 95 
 96 public:
 97    /**
 98     * The default constructor is made private to implement the singleton
 99     * pattern.
100     */
101    Global();
102    // Global(const Global &global);
103    Global& operator =(const Global &);
104    ~Global();
105 
106    /**
107     * Returns the length of getArgs()
108     */
109    int getArgs();
110 
111    /**
112     * Returns the original argv from main(). 
113     * NOTE: This contains NOT all other properties,
114     * you should in such a case use fillArgs(ArgsStruct_T)
115     */
116    const char * const* getArgc();
117 
118    /**
119     * Fill all properties into a argc/argv representation similar
120     * to those delivered by main(argc, argv). 
121     * Example:
122     * <pre>
123     *  ArgsStruct_T args;
124     *  glob.fillArgs(args);
125     *  // Do something ...
126     *  glob.freeArgs(args);
127     * </pre>
128     * <pre>
129     *  argv[0] = myProg (the executable name) 
130     *  argv[1] = -name
131     *  argv[2] = joe
132     * </pre>
133     * NOTE: You have to take care on the memory allocated into args and free
134     * it with a call to freeArgs()
135     * @param args A struct ArgsStruct instance which is filled with all properties
136     */
137    void fillArgs(ArgsStruct_T &args);
138 
139    /**
140     * Free the allocated memory
141     * @see #fillArgs(ArgsStruct_T &args)
142     */
143    void freeArgs(ArgsStruct_T &args);
144 
145    /**
146     * This method delivers the singleton instance or the named instance. 
147     * @param name If specified the named global instance, "default" is the default
148     *              setting and will return the first singleton instance
149     * @return As default the same first Global instance
150     */
151    static Global& getInstance(std::string name="default");
152 
153    /**
154     * Get or create another instance. 
155     * @param name The unique name of the Global instance, "default" can't be used,
156     *             it is reserved for the first singleton instance
157     * @param propertyMapP The optional configuration
158     * @param holdReferenceCount If true we increment the reference counter on this instance, only
159     *                           an explicit call to destroyInstance() will free the memory.
160     *                           If false (which is default) only the caller code increments the reference counter,
161     *                           on last instance the Global is deleted (the memory is freed) automatically,
162     *                           the Global destructor automatically calls destroyInstance() in this case.
163     * @return The named Global instance, if it does not exist it is created
164     * @throws XmlBlasterException On error
165     */
166    GlobalRef createInstance(const std::string& name, const Property::MapType *propertyMapP=NULL, bool holdReferenceCount=false);
167 
168    /**
169     * Check if the Global instance is known. 
170     * @param name The unique name of the Global instance, "default" can't be used.
171     * @return true If the Global instance is known
172     */
173    bool containsInstance(const std::string& name);
174 
175    /**
176     * Destroying a named instance. 
177     * @param name The unique name of the Global instance, the "default" instance can't be destroyed
178     *             with this method
179     * @return true if one instance was removed
180     * @throws XmlBlasterException On wrong arguments
181     */
182    bool destroyInstance(const std::string &instanceName);
183 
184    /**
185     * Returns the name as registered with createInstance(). 
186     * @return "default" for the first singleton instance
187     */
188    const std::string& getInstanceName();
189 
190    /**
191     * Allows you to query if user wants help.
192     * @return true If '-help' or '-?' was passed to us
193     */
194    bool wantsHelp();
195 
196    /**
197     * The version field is automatically set by ant on compilation (see filter token in build.xml)
198     * @return The version std::string e.g. "0.842"
199     *         or "@version@" if not set
200     */
201    static std::string& getVersion();
202 
203    /**
204     * The subversion revision number field is automatically set by ant on compilation (see filter token in build.xml)
205     * @return The revision number std::string e.g. "12708M"
206     *         or getVersion() if not set
207     */
208    static std::string& getRevisionNumber();
209 
210    /**
211     * String containing getVersion() and getRevisionNumber()
212     * @return For example "0.91 #12107M"
213     */
214    static std::string& getReleaseId();
215 
216    /**
217     * The timestamp field is automatically set by ant on compilation (see filter token in build.xml)
218     * @return The compilation timestamp of format "MM/dd/yyyy hh:mm aa"
219     *         or "@build.timestamp@" if not set
220     */
221    static std::string& getBuildTimestamp();
222 
223    /**
224     * The C++ compiler used. 
225     * @return For example something like "Intel icc" for Intels C++ compiler
226     */
227    static std::string& getCompiler();
228 
229    /**
230     * @return The protocol to use if not otherwise configured, currently "IOR" for CORBA
231     *         and "SOCKET" for our native socket protocol are supported
232     */
233    static std::string& getDefaultProtocol();
234  
235     /**
236     * Command line usage.
237     */
238    static std::string usage();
239 
240    /**
241     * Intitialize with the given environment settings. 
242     * @param argv The command line arguments, for example "-protocol SOCKET"
243     */
244    Global& initialize(int args=0, const char * const argv[]=0);
245 
246    /**
247     * Intitialize with the given environment settings. 
248     * @param propertyMap A std::map which contains key and values pairs,
249     *                    for example key="protocol" and value="SOCKET"
250     */
251    Global& initialize(const org::xmlBlaster::util::Property::MapType &propertyMap);
252 
253    /**
254     * Access the used LogManager. 
255     */
256    org::xmlBlaster::util::LogManager& getLogManager();
257 
258    /**
259     * If no log is found with that name, one is created. 
260     * You should have initialized Global with your properties to
261     * before your first call to getLog(), for example:
262     * <pre>
263     * std::map<std::string, std::string, std::less<std::string> > propertyMap;
264     * ... // insert configuration key / values
265     * Global& myGlobal = Global::getInstance().initialize(propertyMap);
266     * I_Log& log = myGlobal.getLog("BlasterClient"));
267     * ...
268     * </pre>
269     */
270    org::xmlBlaster::util::I_Log& getLog(const std::string &logName="org.xmlBlaster");
271 
272    /**
273     * Returns the property object associated to this global
274     */
275    Property& getProperty() const;
276 
277    std::string getLocalIP() const;
278 
279    /**
280     * Returns the bootstrap host name
281     */
282    std::string getBootstrapHostname() const;
283 
284    std::string getCbHostname() const;
285 
286    org::xmlBlaster::client::protocol::CbServerPluginManager& getCbServerPluginManager();
287 
288    org::xmlBlaster::util::dispatch::DispatchManager& getDispatchManager();
289 
290    Timeout& getPingTimer();
291 
292    /**
293     * Returns the specified value as a std::string. 
294     * @deprecated Please use 'lexical_cast<string>(bool)' instead
295     */
296    static const std::string& getBoolAsString(bool val);
297 
298    /**
299     * Set the clients session name, changes after login
300     */
301    void setSessionName(org::xmlBlaster::util::SessionNameRef sessionName);
302 
303    /**
304     * Get the clients session name, changes after login
305     */
306    org::xmlBlaster::util::SessionNameRef getSessionName() const;
307 
308    /**
309     * Access the unique local id (as a String), the absolute session name. 
310     * Before connection it is temporay the relative session name
311     * and changed to the absolute session name after connection (when we know the cluster id)
312     * @return For example "/node/heron/client/joe/2"
313     */
314    std::string getId() const;
315 
316    /**
317     * Same as getId() but all 'special characters' are stripped
318     * so you can use it for file names.
319     * @return ""
320     */
321    std::string getStrippedId() const;
322 
323    /**
324     * The relative session name. 
325     * We can't use the absolute session name (with cluster node informations)
326     * as this is known after connect(), but we need to name queues
327     * before connect() already -> The Id must be immutable!
328     * @return For example "client/joe/2"
329     */
330    std::string getImmutableId() const;
331 
332    /**
333     * Same as getImmutableId() but all 'special characters' are stripped
334     * so you can use it for queue names.
335     * @return ""
336     */
337    std::string getStrippedImmutableId() const;
338 
339    /**
340     * Utility method to strip any std::string, all characters which prevent
341     * to be used for e.g. file names are replaced. 
342     * @param text e.g. "http://www.xmlBlaster.org:/home\\x"
343     * @return e.g. "http_www_xmlBlaster_org_homex"
344     */
345    std::string getStrippedString(const std::string& text) const;
346 
347    /**
348     * Set after successful login. 
349     * Set by XmlBlasterAccess/ConnectionHandler to absolute session name
350     * after successful connection (before it is temporary the relative name)
351     * @param a unique id, for example "/node/heron/client/joe/2"
352     */
353    void setId(const std::string& id);
354    
355    /**
356     * Set by XmlBlasterAccess/ConnectionHandler to relative session name
357     * @param a unique id, for example "client/joe/2"
358     */
359    void setImmutableId(const std::string& id);
360 
361    /**
362     * Reset the cached instance id
363     */
364    void resetInstanceId();
365 
366    /**
367     * Unique id of the client, changes on each restart. 
368     * If 'client/joe' is restarted, the instanceId changes.
369     * @return id + timestamp, '/client/joe/instanceId/33470080380'
370     */
371    std::string getInstanceId() const;
372 };
373 
374 }}} // namespace
375 
376 # endif // ORG_XMLBLASTER_UTIL_GLOBAL_H


syntax highlighted by Code2HTML, v. 0.9.1