9de311a34f0a88011f35c7fc2c207b229eeca6d8
[framework/web/wrt-commons.git] / modules / ace / include / dpl / ace / ConfigurationManager.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 #ifndef _CONFIGURATIONMANAGER_H_
17 #define _CONFIGURATIONMANAGER_H_
18
19 #include <list>
20 #include <string.h>
21 #include <string>
22 #include <libxml/xmlreader.h>
23 #include "Constants.h"
24 #include <fstream>
25 #include <iostream>
26 #include <dpl/log/log.h>
27
28 #define ATTR_ACTIVE_POLICY BAD_CAST("active")
29
30 #define PARSER_ERROR   1
31 #define PARSER_SUCCESS  0
32
33 class ConfigurationManager
34 {
35   public:
36     enum ConfigurationManagerResult
37     {
38         CM_OPERATION_SUCCESS = 0,
39         CM_GENERAL_ERROR = -1,
40         CM_FILE_EXISTS = -2,
41         CM_REMOVE_ERROR = -3,
42         CM_REMOVE_CURRENT = -4,
43         CM_REMOVE_NOT_EXISTING = -5
44     };
45
46     /**
47      * Current policy file getter
48      * @return Name of the current ACE policy file
49      */
50     std::string getCurrentPolicyFile(void) const;
51
52     /**
53      * ACE policy file path getter
54      * @return Full path to ACE current policy file
55      */
56     std::string getFullPathToCurrentPolicyFile(void) const;
57
58     /**
59      * ACE policy dtd file path getter
60      * @return Full path to ACE current policy file
61      */
62     std::string getFullPathToCurrentPolicyXMLSchema(void) const;
63
64     /**
65      * ACE policy storage path getter
66      * @return Full path to ACE policy file storage
67      */
68     std::string getStoragePath(void) const;
69
70     /**
71      * Adds file to ACE policy storage
72      * @param filePath full path to policy to be added
73      * @return CM_OPERATION_SUCCESS on success,
74      *         CM_FILE_EXISTS if file with the same name already exists in the storage (the new file is not added),
75      *         other error code on other error
76      *
77      */
78     int addPolicyFile(const std::string & filePath);
79
80     /**
81      * Removes file with a given filename from ACE policy storage
82      * @param fileName name of the policy to be removed
83      * @return CM_OPERATION_SUCCESS on success,
84      *         CM_REMOVE_CURRENT if file to be removed is a current file (it cannot be removed)
85      *         CM_REMOVE_NOT_EXISTING if file already doesn't exists
86      *         other error code on other error
87      *
88      */
89     int removePolicyFile(const std::string& fileName);
90
91     //change current PolicyFile
92     /**
93      * @param filePath Name of the policy file in the policy storage that should be made
94      * a current policy file
95      * @param CM_OPERATION_SUCCESS on success or error code on error
96      */
97     int changeCurrentPolicyFile(const std::string& filePath);
98
99     //TODO this getInstance() method is a little bit to complicated
100     /**
101      * Method to obtain instance of configuration manager
102      * @return retuns pointer to configuration manager or NULL in case of error
103      */
104     static ConfigurationManager * getInstance()
105     {
106         if (!instance) {
107             instance = new ConfigurationManager();
108             if (instance->parse(std::string(ACE_CONFIGURATION_PATH)) !=
109                 PARSER_SUCCESS) {
110                 delete instance;
111                 LogError(
112                     "Couldn't parse configuration file " ACE_CONFIGURATION_PATH);
113                 return NULL;
114             }
115         }
116         return instance;
117     }
118
119     /**
120      * Extracts filename from full path
121      * @param path Full path from which filename should be extracted
122      * @return returns extracted filename
123      */
124     std::string extractFilename(const std::string& path) const;
125
126   protected:
127
128     /**
129      * Parse given ACE configuration file
130      * @param configFileName full path to configuration file to be parsed
131      * @return PARSER_SUCCESS on succes, PARSER_ERROR on error
132      */
133     int parse(const std::string &configFileName);
134     /**
135      * @param path full path to the file which size should be obtained
136      * @return size of a given file in bytes
137      */
138     int getFileSize(const std::string & path) const;
139     bool copyFile(FILE * source,
140             FILE * destination,
141             int lenght = 1024) const;
142     bool checkIfFileExistst(const std::string & newFilePath) const;
143
144     const std::list<std::string> & getPolicyFiles() const
145     {
146         return policyFiles;
147     }
148
149     const std::string & getConfigFile() const
150     {
151         return configFile;
152     }
153
154     ConfigurationManager() : xmlActive(false)
155     {
156     }
157     virtual ~ConfigurationManager()
158     {
159     }
160
161   private:
162
163     /**
164      * Internal parser methods
165      */
166     void extractFileAttributes(void);
167     void startNodeHandler(void);
168     void endNodeHandler(void);
169     void textNodeHandler(void);
170     void processNode(void);
171     //Save configuration file
172     int saveConfig();
173
174     //Private fields for parser state representation
175     xmlTextReaderPtr reader;
176     std::string currentText;
177     bool xmlActive;
178
179     static ConfigurationManager * instance;
180
181     /**
182      * Full path to ACE configuration file
183      */
184     std::string configFile;
185     /**
186      * ACE policy file storage path
187      */
188     std::string storagePath;
189     /**
190      * Name of the current ACE policy
191      */
192     std::string currentPolicyFile;
193     /**
194      * List of available ACE policy files
195      */
196     std::list<std::string> policyFiles;
197
198     //////////NEW
199 };
200
201 #endif
202