dca_interface  6.3.4
customdb_samples/customdbsample/main.cpp
1 /* IBM Source Code */
2 /* (C) Copyright IBM Corp. 2009, 2012 */
3 /* Licensed Materials - Property of IBM */
4 /* US Government Users Restricted Rights - Use duplication or disclosure restricted by GSA Schedule Contract with IBM Corp. */
5 
43 #include <string>
44 #include <set>
45 #include <iostream>
46 #include <fstream>
47 #include <ctime>
48 
49 #include "dca/dca_base.h"
51 #include "dca/dca_callbacks.h"
52 
53 using namespace dca;
54 
55 const std::string S_ToolName = "customdbsample";
56 const std::string S_ToolVersion = "1.2";
57 
58 std::string custom_db_folder;
59 std::string categories_folder;
60 
65 const std::string S_UsageString =
66  "<redist-folder> <ticket> <product> <custom-db-folder> <categories-folder>\n"
67  " redist-folder - the folder where the DCA is installed to\n"
68  " ticket - a valid ticket\n"
69  " product - the product associated with your ticket\n"
70  " custom-db-folder - the folder where the Custom Database should be created in\n"
71  " categories-folder - the folder where your XML files are located in\n"
72  ;
73 
78 #ifdef WIN32
79 # define DCA_BINDIR "bin/Win32"
80 #else
81 # define DCA_BINDIR "bin/linux"
82 #endif
83 
88 #define DCA_INITDIR "init"
89 
93 #define DCA_LOGDIR "./logs"
94 
105 static void SetupInitData( const std::string& redist_folder, InitData& initData )
106 {
107  initData.binDir = redist_folder + DCA_BINDIR;
108  initData.initDir = redist_folder + DCA_INITDIR;
109  initData.logDir = DCA_LOGDIR;
110 }
111 
120 static void SetupLicense( const std::string& ticket, const std::string& product,
121  LicenseData& licenseData )
122 {
123  licenseData.ticket = ticket;
124  licenseData.product = product;
125 }
126 
135 static void SetupConnectionData( const std::string& custom_db_folder,
136  DbConnectionData& cData )
137 {
138  cData.useLocalDatabase = true;
139  cData.dbType = DBT_Custom;
140  cData.customData.configDir = custom_db_folder;
141 }
142 
147 static void PrintToolHeader()
148 {
149  std::cout << "IBM DCA Sample: " << S_ToolName << " (" << S_ToolVersion <<
150  ")" << std::endl;
151 }
152 
158 static void PrintUsage( const char *name )
159 {
160  std::cout << " usage:" << std::endl;
161  std::cout << name << " " << S_UsageString << std::endl;
162 }
163 
170 static void PrintLicenseInfo( const License& aLicense )
171 {
172  const time_t expirationDate = aLicense.getExpirationDate();
173  struct tm *expirationTime = localtime( &expirationDate );
174 
175  std::cout << "License Info:" << std::endl;
176  std::cout << " DCA is " << ( aLicense.isLicensed() ? "licensed." :
177  "not licensed." ) << std::endl;
178  std::cout << " MaxUsers:" << aLicense.getMaxUsers() <<
179  std::endl;
180  std::cout << " MaxSessions:" << aLicense.getMaxSessions() <<
181  std::endl;
182  std::cout << " Ticket:" << aLicense.getTicket() <<
183  std::endl;
184  std::cout << " Session:" << aLicense.getSession() <<
185  std::endl;
186  std::cout << " Last Message:" << aLicense.getLastMessage() <<
187  std::endl;
188  std::cout << " Expiration Date:" << asctime( expirationTime ) <<
189  std::endl;
190 }
191 
198 static void PrintCategoriesInfo( const CategoriesInfo& myCategoriesInfo )
199 {
200  // iterate and print out given locales
201  const Locales myLocales = myCategoriesInfo.getLocales();
202  if( myLocales.size() == 0 ) {
203  std::cout << "There are no locales defined or not found!" <<
204  std::endl;
205  }
206  else {
207  for( DCA_INDEX_TYPE i = 0; i < myLocales.size(); ++i ) {
208  const Locale myLocale = myLocales[i];
209  std::cout << "Locale #" << (i+1) << ", displayname='" <<
210  myLocale.displayName() << "', languageid='" <<
211  myLocale.languageId() << "'." << std::endl;
212  }
213  }
214 
215  // iterate and print out given groups
216  const Groups myGroups = myCategoriesInfo.getGroups();
217  if( myGroups.size() == 0 ) {
218  std::cout << "There are no groups defined or not found!" <<
219  std::endl;
220  }
221  else {
222  for( DCA_INDEX_TYPE i = 0; i < myLocales.size(); ++i ) {
223  const Group myGroup = myGroups[i];
224  std::cout << "Group #" << (i+1) << ", name='" <<
225  myGroup.name( "" ) << "', id=" <<
226  myGroup.id() << "." << std::endl;
227  }
228  }
229 
230  // iterate and print out given categories
231  const Categories myCategories = myCategoriesInfo.getCategories();
232  if( myCategories.size() == 0 ) {
233  std::cout << "There are no categories defined or not found!" <<
234  std::endl;
235  }
236  else {
237  for( DCA_INDEX_TYPE i = 0; i < myCategories.size(); ++i ) {
238  const Category myCategory = myCategories[i];
239  std::cout << "Category #" << (i+1) << ", name='" <<
240  myCategory.name( "" ) << "', id=" <<
241  myCategory.id() << ", groupid=" <<
242  myCategory.groupId() << "." << std::endl;
243  }
244  }
245 }
246 
254 static void PrintUrlResult( const std::string& myUrlString,
255  const std::set< int >& myCatSet )
256 {
257  std::cout << "URL '" << myUrlString <<
258  "' contains the following categories: ";
259  for( std::set< int >::const_iterator I = myCatSet.begin(),
260  IEnd = myCatSet.end(); I != IEnd; ++I ) {
261 
262  std::cout << *I << std::endl;
263  }
264  std::cout << std::endl;
265 }
266 
282 void InitializeCustomDatabase( const DcaInstance& aDca, const License& aLicense,
283  UrlCustomDb& myUrlCustomDb )
284 {
285  // initialize the UrlCustomModule
286  UrlCustomDbModule myUrlCustomDbModule =
287  UrlCustomDbModule::create( aDca, aLicense );
288 
289  // create a CategoriesInfo by using the provided
290  // categories_folder
291  const CategoriesInfo myCategoriesInfo =
292  myUrlCustomDbModule.createCategoriesInfo(
293  categories_folder );
294 
295  // print out the custom CategoriesInfo
296  PrintCategoriesInfo( myCategoriesInfo );
297 
298  // ensure that the Custom Database is already existing or if
299  // not, create a new blank Custom Database
300  FunctionResult fr = myUrlCustomDbModule.createCustomDb(
301  custom_db_folder );
302 
303  if( !fr ) {
305  std::cout << "Database already existing..." <<
306  std::endl;
307  } else {
308  std::cout << "Creation of Custom Database failed..." <<
309  std::endl;
310  }
311  }
312 
313  // set up a signature database connection
314  DbConnectionData myDbConnectionData;
315  SetupConnectionData( custom_db_folder, myDbConnectionData );
316 
317  // if the creation of the DbConnection will fail, there is no
318  // Custom Database available
319  const DbConnection myDbConnection =
320  aDca.createDbConnection( aLicense, myDbConnectionData );
321 
322  // create the CustomDb instance and open the database
323  myUrlCustomDb = myUrlCustomDbModule.openCustomDb( myDbConnection );
324 }
325 
342 int ProcessCustomDatabase( const DcaInstance& aDca, const License& aLicense )
343 {
344  UrlCustomDb myUrlCustomDb;
345 
346  // Initialize and set up myUrlCustomDb
347  InitializeCustomDatabase( aDca, aLicense, myUrlCustomDb );
348 
349  // this is the URL we want to use
350  const std::string myUrlString( "www.an_example_custom_url.com" );
351 
352  // add a URL to the database and add category 0 and 1
353  {
354  std::set< int > myCatSet;
355  myCatSet.insert( 0 );
356  myCatSet.insert( 1 );
357  myUrlCustomDb.setUrl( myUrlString, myCatSet );
358  }
359 
360  // retrieve the categories for the URL
361  {
362  std::set< int > myCatSet;
363  if( !myUrlCustomDb.getUrl( myUrlString, myCatSet ) ) {
364  // this should never happen!
365  std::cout << "We could not retrieve the previously added URL?"
366  << std::endl;
367  return 5;
368  }
369 
370  // print out the categories associated with given URL
371  PrintUrlResult( myUrlString, myCatSet );
372  }
373 
374  // remove the URL from the custom database
375  myUrlCustomDb.removeUrl( myUrlString );
376 
377  // Again retrieve the URL from database - this should fail since we
378  // recently removed it from the custom database
379  {
380  std::set< int > myCatSet;
381  if( myUrlCustomDb.getUrl( myUrlString, myCatSet ) ) {
382  // this should never happen!
383  std::cout << "We could still retrieve the URL we already removed?"
384  << std::endl;
385  return 6;
386  }
387  }
388 
389  std::cout << "URL has been successfully removed from " <<
390  "Custom Database." << std::endl;
391 
392  return 0;
393 }
394 
406 int main( int argc, char *argv[] )
407 {
408  PrintToolHeader();
409 
410  int rc = 5;
411 
412  try {
413  if( argc < 6 ) {
414  PrintUsage( argv[0] );
415  return 1;
416  }
417 
418  std::string myRedistFolder = argv[ 1 ];
419  const std::string myTicket = argv[ 2 ];
420  const std::string myProduct = argv[ 3 ];
421  custom_db_folder = argv[ 4 ];
422  categories_folder = argv[ 5 ];
423 
424  if( myRedistFolder.empty() || myTicket.empty() ||
425  myProduct.empty() || custom_db_folder.empty() ||
426  categories_folder.empty() ) {
427  PrintUsage( argv[0] );
428  return 5;
429  }
430 
431  // check for trailing fileslash on directories - and add if necessary
432  char c = myRedistFolder[ myRedistFolder.length() - 1 ];
433  if( c != '/' && c != '\\' )
434  myRedistFolder += "/";
435 
436  c = custom_db_folder[ custom_db_folder.length() - 1 ];
437  if( c != '/' && c != '\\' )
438  custom_db_folder += "/";
439 
440  c = categories_folder[ categories_folder.length() - 1 ];
441  if( c != '/' && c != '\\' )
442  categories_folder += "/";
443 
444  // init the 3rd party libraries
445  InitCUrl();
447 
448  { // scope for DcaInstance and License
449 
450  // setup DCA directories
451  InitData myInitData;
452  SetupInitData( myRedistFolder, myInitData );
453 
454  // instantiate the DCA API
455  DcaInstance myDca;
456  myDca = DcaInstance::create( myInitData );
457 
458  // setup license data
459  LicenseData myLicenseData;
460  SetupLicense( myTicket, myProduct, myLicenseData );
461  const License myLicense = myDca.createLicense( myLicenseData );
462 
463  // print out all information about the created License object
464  PrintLicenseInfo( myLicense );
465 
466  if( myLicense.isLicensed( UrlCustomDbModule::ID ) ) {
467  // Create custom database, add an entry, remove entry,
468  // retrieve categories etc.
469  rc = ProcessCustomDatabase( myDca, myLicense );
470  }
471  }
472  }
473  catch( const ExDca& ex ) {
474  std::cerr << "DCA Exception occured. Details: " << ex.getDescription()
475  << " (" << ex.getReturnCode() << ")." << std::endl;
476  rc = 10;
477  }
478  catch( const std::exception& s ) {
479  std::cerr << "std::exception occured. Details: " << s.what() << "." <<
480  std::endl;
481  rc = 10;
482  }
483  catch(...) {
484  std::cerr << "Unknown exception caught." << std::endl;
485  rc = 10;
486  }
487 
488  // deinit the 3rd party libraries
490  DeinitCUrl();
491 
492  return rc;
493 }
The Custom Database module, used to create new custom databases or open existing custom databases.
Is used to create a License object. A license first must be created with DcaInstance::createLicense t...
Definition: base_classes.h:547
Exception class used in the DCA.
Definition: base_classes.h:237
UrlCustomDb openCustomDb(const DbConnection &aDbConnection) const
Creates a UrlCustomDb instance that is connected to the database defined by the given DbConnection.
void InitCUrl()
Initializes libcurl. Do not use any DCA function before initializing libcurl.
std::string initDir
the directory in which the DCA init files are stored
Definition: base_classes.h:266
DCA_SIZE_TYPE size() const
Returns the number of categories in the container.
int getMaxSessions() const
Returns the maximum allowed sessions associated with your ticket/license.
static UrlCustomDbModule create(const DcaInstance &aDcaInstance, const License &aLicense)
Loads and initializes the custom database module.
time_t getExpirationDate() const
Returns the expiration date of the license in UTC.
std::string getDescription() const
Returns a description of the error.
std::string configDir
Specifies the complete folder path where the custom database is located, or the folder in which it sh...
Definition: base_classes.h:754
Definition of a container class for Category objects.
Encapsulation of a locale class, which allows access to the language id and the display name of local...
Definition: base_locale.h:28
DCA_GROUP_ID_TYPE id() const
Returns the id of the group as defined in the related categories XML schema.
DbConnectionCustomData customData
Fill out this structure only if you are using a custom database.
Definition: base_classes.h:823
const int ERR_DATABASE_ALREADY_EXISTING
Error code: The specified database already exists.
DCA_RESULT_TYPE getReturnCode() const
Returns the last error code (if any).
static void SetupInitData(const std::string &redist_folder, InitData &initData)
Sets up the given initData by substituting the given redist_folder with DCA subdirectories.
std::string binDir
the directory in which the DCA binary (*.dca) files are stored
Definition: base_classes.h:265
void removeUrl(const std::string &aUrlString)
Removes the given URL string from the database, if present.
This header includes initialization/deinitialization support functions for the 3rd party libraries us...
A container class that allows access to the contained Categories, Groups and Locales.
Encapsulates the Custom Database maintenance interface.
void SetOpenSslCallbacks()
Initializes the required callbacks for OpenSSL when using HTTPS or SSL connections in a multi-threade...
Stores the connection data for a database.
Definition: base_classes.h:815
void UnsetOpenSslCallbacks()
Unsets the openssl callbacks. Do not call any DCA function after you have called this function.
DCA_CATEGORY_ID_TYPE id() const
The category id.
int getMaxUsers() const
Returns the maximum allowed users associated with your ticket/license.
void DeinitCUrl()
Deinitializes libcurl. Do not call any DCA function after you have called this function.
Database connection class for a local or remote database.
Definition: base_classes.h:859
void setUrl(const std::string &aUrlString, const CategorySet &categories)
Updates or creates categories for the given URL string.
DCA_SIZE_TYPE size() const
Returns the number of categories in the container.
#define DCA_LOGDIR
Relative directory for logfile(s).
#define DCA_INITDIR
DCA subdirectory of the DCA initialization data.
Definition of a container class for Group objects.
Definition: base_groups.h:34
DCA_RESULT_TYPE getReturnCode() const
Gets the code of the error.
std::string name(const std::string &localeString=std::string()) const
Returns the localized (display) name of the group.
Locales getLocales() const
Returns the contained Locales.
static void PrintToolHeader()
Prints out the name and the version of this sample.
Use a License to initialize a classification package or a toolbox package.
Definition: base_classes.h:560
FunctionResult createCustomDb(const std::string &aDatabaseFolder) const
Creates a new custom database file in the given folder.
const DbType DBT_Custom
Used for DbConnection classes of custom databases.
bool isLicensed(DCA_MODULE_ID_TYPE id=0, bool force=false) const
Checks whether the given License is valid for the given module id.
static void PrintLicenseInfo(const License &aLicense)
Prints out the information about the provided License.
std::string ticket
The ticket as provided in the license.
Definition: base_classes.h:548
This header includes all header files of the DCA Base Package.
bool useLocalDatabase
Set to true to connect to a local or custom database, set to false to use a remote database.
Definition: base_classes.h:821
Encapsulates the init and deinit of the DCA API.
Definition: base_classes.h:315
DCA_SIZE_TYPE size() const
The number of items in the container.
std::string product
The product code used with the license.
Definition: base_classes.h:549
std::string logDir
the directory in which the DCA log file should be created
Definition: base_classes.h:267
static void PrintUsage(const char *name)
Prints out the syntax of the sample.
std::string getLastMessage() const
Returns the last message received from our license server or if none available the last available mes...
size_t DCA_INDEX_TYPE
Type for index access (used for arrays and collections).
Definition: base_types.h:66
DbType dbType
The type of the database.
Definition: base_classes.h:820
Categories getCategories() const
Returns the contained Categories.
std::string name(const std::string &localeString=std::string()) const
Returns the localized (display) name of the category.
std::string displayName() const
Returns the display name of the locale.
CategoriesInfo createCategoriesInfo(const std::string &categoriesXmlFolder) const
Creates a CategoriesInfo object by using the provided categoriesXmlFolder.
bool getUrl(const std::string &aUrlString, CategorySet &foundCategories) const
Retrieves the categories of URL string (if found).
std::string getTicket() const
Returns the ticket of the license as string.
DCA_GROUP_ID_TYPE groupId() const
If the category is associated with a group, this is the group id.
#define DCA_BINDIR
DCA subdirectory of the DCA binaries.
Standard function result.
Definition: base_classes.h:148
std::string getSession() const
Returns the session of the license as string.
Definition of a container class for Locale objects.
Definition: base_locales.h:34
std::string languageId() const
Returns the language id as defined in the related categories.xml.
const std::string S_UsageString
Usage string, displayed if a parameter is missing.
Encapsulates a group as defined in the related categories XML schema (see Categories XML: Groups).
Definition: base_group.h:26
DbConnection createDbConnection(const License &aLicense, const DbConnectionData &dbcData, const ProxySettings &proxySettings=ProxySettings(), LogLevel aLogLevel=LOG_Initial) const
Creates a DbConnection object using the given DbConnectionData.
This structure is used to initialize the DcaInstance.
Definition: base_classes.h:264
This header includes all header files of the Custom Database Module Toolbox.
Encapsulates a category as defined in the categories XML schema (see Categories XML: Categories).
Definition: base_category.h:26
Groups getGroups() const
Returns the contained Groups.
static DcaInstance create(const InitData &initData)
Creates a DcaInstance, starts up the DCA API and initializes the required main module.
static void SetupLicense(const std::string &ticket, const std::string &product, LicenseData &licenseData)
Sets up the given licenseData by copying the given ticket and product strings.
static DCA_MODULE_ID_TYPE ID
The unique ID of the Custom DB module.
License createLicense(const LicenseData &licData, const ProxySettings &proxySettings=ProxySettings(), LogLevel aLogLevel=LOG_Initial) const
Creates a License object using the given LicenseData.
int main(int argc, char *argv[])
The main routine.