Cache default method types for obtaining peer credentials 76/38076/3
authorJacek Bukarewicz <j.bukarewicz@samsung.com>
Fri, 10 Apr 2015 13:58:57 +0000 (15:58 +0200)
committerJacek Bukarewicz <j.bukarewicz@samsung.com>
Tue, 14 Apr 2015 11:14:37 +0000 (13:14 +0200)
Default method types for obtaining user and client strings are stored in
configuration file. Typically, these values are not going to be changed
dynamically so they can be cached.

Change-Id: Id2d74af29f43eb565f563d65fa7115dd762ede8d

src/helpers/creds-commons/creds-commons.cpp
src/include/cynara-creds-commons.h

index 973f278..30a24e1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
  * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
  * @author      Radoslaw Bartosiak <r.bartosiak@samsung.com>
  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
+ * @author      Jacek Bukarewicz <j.bukarewicz@samsung.com>
  * @version     1.0
  * @brief       Implementation of external libcynara-creds-commons API
  */
 
 CYNARA_API
 int cynara_creds_get_default_client_method(enum cynara_client_creds *method) {
-    return Cynara::tryCatch([&] () {
-        int methodCode, ret;
-        static const Cynara::CredentialsMap clientCredsMap{{"smack", CLIENT_METHOD_SMACK},
-                                                           {"pid", CLIENT_METHOD_PID}};
+    static int cachedMethodCode = -1;
+    static const Cynara::CredentialsMap clientCredsMap{{"smack", CLIENT_METHOD_SMACK},
+                                                       {"pid", CLIENT_METHOD_PID}};
 
-        if ((ret = Cynara::CredsCommonsInnerBackend::
-                           getMethodFromConfigurationFile(clientCredsMap,
-                                                          "client_default",
-                                                          methodCode))
-            != CYNARA_API_SUCCESS)
+    if (cachedMethodCode == -1) {
+        int ret = Cynara::tryCatch([&] () {
+            return Cynara::CredsCommonsInnerBackend::
+                      getMethodFromConfigurationFile(clientCredsMap, "client_default",
+                                                     cachedMethodCode);
+        });
+        if (ret != CYNARA_API_SUCCESS)
             return ret;
+    }
+    *method = static_cast<enum cynara_client_creds>(cachedMethodCode);
+    return CYNARA_API_SUCCESS;
 
-        *method = static_cast<enum cynara_client_creds>(methodCode);
-        return CYNARA_API_SUCCESS;
-    });
 }
 
 CYNARA_API
 int cynara_creds_get_default_user_method(enum cynara_user_creds *method) {
-     return Cynara::tryCatch([&] () {
-        int methodCode, ret;
-        static const Cynara::CredentialsMap userCredsMap{{"uid", USER_METHOD_UID},
-                                                         {"gid", USER_METHOD_GID}};
-        if ((ret = Cynara::CredsCommonsInnerBackend::
-                           getMethodFromConfigurationFile(userCredsMap,
-                                                          "user_default",
-                                                          methodCode))
-            != CYNARA_API_SUCCESS)
+    static int cachedMethodCode = -1;
+    static const Cynara::CredentialsMap userCredsMap{{"uid", USER_METHOD_UID},
+                                                     {"gid", USER_METHOD_GID}};
+
+    if (cachedMethodCode == -1) {
+        int ret = Cynara::tryCatch([&] () {
+            return Cynara::CredsCommonsInnerBackend::
+                                       getMethodFromConfigurationFile(userCredsMap,
+                                                                      "user_default",
+                                                                      cachedMethodCode);
+        });
+        if (ret != CYNARA_API_SUCCESS)
             return ret;
+    }
 
-        *method = static_cast<enum cynara_user_creds>(methodCode);
-        return CYNARA_API_SUCCESS;
-    });
+    *method = static_cast<enum cynara_user_creds>(cachedMethodCode);
+    return CYNARA_API_SUCCESS;
 }
index ee6d2b8..4c5c417 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -58,8 +58,11 @@ extern "C" {
  * value of method parameter.
  *
  * \par Method of function operation:
- * The function will read and return the value of parameter client_default set
- * in /etc/cynara/creds.conf file (the path is determined by CYNARA_CONFIGURATION_DIR).
+ * When this function is called for the first time it reads and returns the value of client_default
+ * parameter from /etc/cynara/creds.conf file (the path is determined by CYNARA_CONFIGURATION_DIR).
+ * Returned value is cached so subsequent calls will not consult file again but use cached value.
+ * This also means that after the initial call any changes in the file will be ignored for the
+ * remaining lifetime of the process.
  *
  * \par Sync (or) Async:
  * This is a synchronous API.
@@ -95,8 +98,11 @@ int cynara_creds_get_default_client_method(enum cynara_client_creds *method);
  * value of method parameter.
  *
  * \par Method of function operation:
- * The function will read and return the value of parameter user_default set
- * in /etc/cynara/creds.conf file (the path is determined by CYNARA_CONFIGURATION_DIR).
+ * When this function is called for the first time it reads and returns the value of user_default
+ * parameter from /etc/cynara/creds.conf file (the path is determined by CYNARA_CONFIGURATION_DIR).
+ * Returned value is cached so subsequent calls will not consult file again but use cached value.
+ * This also means that after the initial call any changes in the file will be ignored for the
+ * remaining lifetime of the process.
  *
  * \par Sync (or) Async:
  * This is a synchronous API.