| 1 | /********************************************************************** |
|---|
| 2 | * Copyright (c) 2005-2008 David Carter <dcarter@arm4.org> and others. |
|---|
| 3 | * All rights reserved. This program and the accompanying materials |
|---|
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
|---|
| 5 | * which accompanies this distribution, and is available at |
|---|
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
|---|
| 7 | * |
|---|
| 8 | * Contributors: |
|---|
| 9 | * David Carter - Initial API and implementation |
|---|
| 10 | **********************************************************************/ |
|---|
| 11 | |
|---|
| 12 | #ifndef LIBARM4DB_CONF_H |
|---|
| 13 | #define LIBARM4DB_CONF_H 1 |
|---|
| 14 | |
|---|
| 15 | #include "config.h" |
|---|
| 16 | |
|---|
| 17 | #include <sys/types.h> |
|---|
| 18 | #include <sys/stat.h> |
|---|
| 19 | #include <unistd.h> |
|---|
| 20 | #include <db.h> |
|---|
| 21 | |
|---|
| 22 | #define DEFAULT_LOG_FILE_SIZE (128 * 1024) |
|---|
| 23 | #define DEFAULT_LOG_BUFFER_SIZE (2 * 1024 * 1024) |
|---|
| 24 | |
|---|
| 25 | #if defined (__cplusplus) |
|---|
| 26 | |
|---|
| 27 | #include <string> |
|---|
| 28 | |
|---|
| 29 | typedef enum { |
|---|
| 30 | /* All supported database types */ |
|---|
| 31 | Arm4dbBerkeleyDB, |
|---|
| 32 | Arm4dbSqlite, |
|---|
| 33 | Arm4dbMySql |
|---|
| 34 | } Arm4dbDatabaseType; |
|---|
| 35 | |
|---|
| 36 | #if defined(HAVE_BDB) |
|---|
| 37 | #define DEFAULT_ARM4DB_TYPE Arm4dbBerkeleyDB |
|---|
| 38 | #elif defined(HAVE_SQLITE) |
|---|
| 39 | #define DEFAULT_ARM4DB_TYPE Arm4dbSqlite |
|---|
| 40 | #elif defined(HAVE_MYSQL) |
|---|
| 41 | #define DEFAULT_ARM4DB_TYPE Arm4dbMySql |
|---|
| 42 | #endif |
|---|
| 43 | |
|---|
| 44 | #ifdef DOCROOT |
|---|
| 45 | #define DEFAULT_SCHEMA_LOCATION DOCROOT |
|---|
| 46 | #else |
|---|
| 47 | #define DEFAULT_SCHEMA_LOCATION "/usr/local/share/" PACKAGE_NAME "-" PACKAGE_VERSION |
|---|
| 48 | #endif |
|---|
| 49 | |
|---|
| 50 | class Arm4dbConfig |
|---|
| 51 | { |
|---|
| 52 | public: |
|---|
| 53 | /* Constructors */ |
|---|
| 54 | |
|---|
| 55 | /* Destructors */ |
|---|
| 56 | virtual ~Arm4dbConfig (); |
|---|
| 57 | |
|---|
| 58 | /* Accessors */ |
|---|
| 59 | static const Arm4dbConfig &getConfig (const std::string &filename); /* Singleton factory method */ |
|---|
| 60 | static const Arm4dbConfig &getConfig (void); |
|---|
| 61 | |
|---|
| 62 | const std::string &getConfigFilename (void) const { return m_filename; } |
|---|
| 63 | const std::string &getHostname (void) const { return m_hostname; } |
|---|
| 64 | const std::string &getLogFilename (void) const { return m_logFilename; } |
|---|
| 65 | Arm4dbDatabaseType getDbType (void) const { return m_dbType; } |
|---|
| 66 | int getInstance (void) const { return m_instance; } |
|---|
| 67 | uid_t getUid (void) const { return m_uid; } |
|---|
| 68 | gid_t getGid (void) const { return m_gid; } |
|---|
| 69 | mode_t getUmask (void) const { return m_umask; } |
|---|
| 70 | const std::string &getSchemaLocation (void) const { return m_schemaLocation; } |
|---|
| 71 | const std::string &getDbHome (void) const { return m_dbHome; } |
|---|
| 72 | const char *getDbHomeC (void) const |
|---|
| 73 | { return const_cast<Arm4dbConfig *> (this)->toC (m_dbHome, &(const_cast<Arm4dbConfig *> (this)->m_dbHomeC)); } |
|---|
| 74 | const std::string &getDbBackup (void) const { return m_dbBackup; } |
|---|
| 75 | const char *getDbBackupC (void) const |
|---|
| 76 | { return const_cast<Arm4dbConfig *> (this)->toC (m_dbBackup, &(const_cast<Arm4dbConfig *> (this)->m_dbBackupC)); } |
|---|
| 77 | long getDbKey (void) const { return m_dbKey; } |
|---|
| 78 | u_int32_t getDbLogFileSize (void) const { return m_dbLogFileSize; } |
|---|
| 79 | u_int32_t getDbLogBufferSize (void) const { return m_dbLogBufferSize; } |
|---|
| 80 | int getCheckpointInterval (void) const { return m_checkpointInterval; } |
|---|
| 81 | |
|---|
| 82 | const std::string &getMySqlHost (void) const { return m_mysqlHost; } |
|---|
| 83 | const char *getMySqlHostC (void) const |
|---|
| 84 | { return const_cast<Arm4dbConfig *> (this)->toC (m_mysqlHost, &(const_cast<Arm4dbConfig *> (this)->m_mysqlHostC)); } |
|---|
| 85 | const std::string &getMySqlUser (void) const { return m_mysqlUser; } |
|---|
| 86 | const char *getMySqlUserC (void) const |
|---|
| 87 | { return const_cast<Arm4dbConfig *> (this)->toC (m_mysqlUser, &(const_cast<Arm4dbConfig *> (this)->m_mysqlUserC)); } |
|---|
| 88 | const std::string &getMySqlPassword (void) const { return m_mysqlPassword; } |
|---|
| 89 | const char *getMySqlPasswordC (void) const |
|---|
| 90 | { return const_cast<Arm4dbConfig *> (this)->toC (m_mysqlPassword, &(const_cast<Arm4dbConfig *> (this)->m_mysqlPasswordC)); } |
|---|
| 91 | const std::string &getMySqlDb (void) const { return m_mysqlDb; } |
|---|
| 92 | const char *getMySqlDbC (void) const |
|---|
| 93 | { return const_cast<Arm4dbConfig *> (this)->toC (m_mysqlDb, &(const_cast<Arm4dbConfig *> (this)->m_mysqlDbC)); } |
|---|
| 94 | const std::string &getMySqlSocket (void) const { return m_mysqlSocket; } |
|---|
| 95 | const char *getMySqlSocketC (void) const |
|---|
| 96 | { return ((m_mysqlSocket.length() > 0) ? |
|---|
| 97 | const_cast<Arm4dbConfig *> (this)->toC (m_mysqlSocket, &(const_cast<Arm4dbConfig *> (this)->m_mysqlSocketC)) |
|---|
| 98 | : NULL); } |
|---|
| 99 | int getMySqlPort (void) const { return m_mysqlPort; } |
|---|
| 100 | |
|---|
| 101 | /* Mutators */ |
|---|
| 102 | static void setConfig (const std::string &filename); /* Singleton factory method */ |
|---|
| 103 | static void setDBHome (const std::string &home); |
|---|
| 104 | |
|---|
| 105 | /* Operations */ |
|---|
| 106 | void read (const char *config_filename_ptr); |
|---|
| 107 | |
|---|
| 108 | /* Operators */ |
|---|
| 109 | |
|---|
| 110 | private: |
|---|
| 111 | // These are private so that we may implement a singleton |
|---|
| 112 | Arm4dbConfig (); |
|---|
| 113 | Arm4dbConfig (const Arm4dbConfig &config); |
|---|
| 114 | Arm4dbConfig (const std::string &filename); |
|---|
| 115 | Arm4dbConfig &operator= (const Arm4dbConfig &config); |
|---|
| 116 | |
|---|
| 117 | void setDefaultHostname (void); |
|---|
| 118 | void setFilename (const std::string &filename); |
|---|
| 119 | void setHostname (const std::string &hostname); |
|---|
| 120 | void setLogFilename (const std::string &logFilename); |
|---|
| 121 | void setSchemaLocation (const std::string &schemaLocation); |
|---|
| 122 | void setDbHome (const std::string &dbHome); |
|---|
| 123 | void setDbBackup (const std::string &dbBackup); |
|---|
| 124 | void setMysqlHost (const std::string &host); |
|---|
| 125 | void setMysqlUser (const std::string &user); |
|---|
| 126 | void setMysqlPassword (const std::string &password); |
|---|
| 127 | void setMysqlDb (const std::string &db); |
|---|
| 128 | void setMysqlSocket (const std::string &socket); |
|---|
| 129 | |
|---|
| 130 | const char *toC (const std::string &cppString, char **cString); |
|---|
| 131 | void clearC (char **cString); |
|---|
| 132 | |
|---|
| 133 | int parse_integer (const char *value_ptr); |
|---|
| 134 | long parse_long (const char *value_ptr); |
|---|
| 135 | void read_line (FILE *file, char *buffer_ptr, int size); |
|---|
| 136 | char *uncomment (char *buffer_ptr); |
|---|
| 137 | void trim_tail (char *buffer_ptr); |
|---|
| 138 | void get_parameters (char *line_ptr, char **value_pptr); |
|---|
| 139 | void parse_hostname (const char *value_ptr); |
|---|
| 140 | void parse_log_filename (const char *value_ptr); |
|---|
| 141 | void parse_uid (const char *value_ptr); |
|---|
| 142 | void parse_gid (const char *value_ptr); |
|---|
| 143 | void parse_umask (const char *value_ptr); |
|---|
| 144 | void parse_port (const char *value_ptr); |
|---|
| 145 | void parse_schema_location (const char *value_ptr); |
|---|
| 146 | void parse_db (const char *value_ptr); |
|---|
| 147 | void parse_db_home (const char *value_ptr); |
|---|
| 148 | void parse_db_backup (const char *value_ptr); |
|---|
| 149 | void parse_db_key (const char *value_ptr); |
|---|
| 150 | void parse_db_log_buffer_size (const char *value_ptr); |
|---|
| 151 | void parse_db_log_file_size (const char *value_ptr); |
|---|
| 152 | void parse_instance (const char *value_ptr); |
|---|
| 153 | void parse_checkpoint_interval (const char *value_ptr); |
|---|
| 154 | |
|---|
| 155 | void parse_mysql_host (const char *value_ptr); |
|---|
| 156 | void parse_mysql_user (const char *value_ptr); |
|---|
| 157 | void parse_mysql_password (const char *value_ptr); |
|---|
| 158 | void parse_mysql_db (const char *value_ptr); |
|---|
| 159 | void parse_mysql_socket (const char *value_ptr); |
|---|
| 160 | void parse_mysql_port (const char *value_ptr); |
|---|
| 161 | |
|---|
| 162 | void adjustDbHome (void); |
|---|
| 163 | |
|---|
| 164 | std::string m_filename; // The name of the config file used to generate this config |
|---|
| 165 | std::string m_hostname; |
|---|
| 166 | std::string m_logFilename; |
|---|
| 167 | Arm4dbDatabaseType m_dbType; |
|---|
| 168 | int m_instance; |
|---|
| 169 | uid_t m_uid; |
|---|
| 170 | gid_t m_gid; |
|---|
| 171 | mode_t m_umask; |
|---|
| 172 | std::string m_schemaLocation; |
|---|
| 173 | std::string m_dbHome; |
|---|
| 174 | std::string m_dbBackup; |
|---|
| 175 | long m_dbKey; |
|---|
| 176 | u_int32_t m_dbLogFileSize; |
|---|
| 177 | u_int32_t m_dbLogBufferSize; |
|---|
| 178 | int m_checkpointInterval; |
|---|
| 179 | |
|---|
| 180 | // MySql parameters |
|---|
| 181 | std::string m_mysqlHost; |
|---|
| 182 | std::string m_mysqlUser; |
|---|
| 183 | std::string m_mysqlPassword; |
|---|
| 184 | std::string m_mysqlDb; |
|---|
| 185 | std::string m_mysqlSocket; |
|---|
| 186 | int m_mysqlPort; |
|---|
| 187 | |
|---|
| 188 | // Pointers to the C style string equivalents |
|---|
| 189 | char *m_dbHomeC; |
|---|
| 190 | char *m_dbBackupC; |
|---|
| 191 | char *m_mysqlHostC; |
|---|
| 192 | char *m_mysqlUserC; |
|---|
| 193 | char *m_mysqlPasswordC; |
|---|
| 194 | char *m_mysqlDbC; |
|---|
| 195 | char *m_mysqlSocketC; |
|---|
| 196 | |
|---|
| 197 | static Arm4dbConfig *m_singletonPtr; // The agent wide configuration |
|---|
| 198 | }; |
|---|
| 199 | |
|---|
| 200 | #endif /* __cplusplus */ |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | #endif /* LIBARM4DB_CONF_H */ |
|---|
| 204 | |
|---|