gcontini
2019-10-12 dda16735b94661b798d6c0fd3e41af944de0a1fe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
 * EnvironmentVarLocation.cpp
 *
 *  Created on: Oct 12, 2019
 *      Author: Gabriele Contini
 */
 
#include <build_properties.h>
 
#include "../base/FileUtils.hpp"
#include "../base/StringUtils.h"
#include "EnvironmentVarLocation.hpp"
 
namespace license {
namespace locate {
using namespace std;
 
EnvironmentVarLocation::EnvironmentVarLocation() :
        LocatorStrategy("EnvironmentVarLocation") {
}
 
EnvironmentVarLocation::~EnvironmentVarLocation() {
}
 
const vector<string> EnvironmentVarLocation::licenseLocations(
        EventRegistry &eventRegistry) const {
    vector<string> licenseFileFoundWithEnvVariable;
 
    const string varName(LICENSE_LOCATION_ENV_VAR);
    if (varName.length() > 0) {
        //var name is defined in header files.
        char *env_var_value = getenv(LICENSE_LOCATION_ENV_VAR);
        if (env_var_value != nullptr && env_var_value[0] != '\0') {
            const vector<string> declared_positions = license::split_string(
                    string(env_var_value), ';');
            vector<string> existing_pos = license::filter_existing_files(
                    declared_positions);
            if (existing_pos.size() > 0) {
                for (auto it = existing_pos.begin(); it != existing_pos.end();
                        ++it) {
                    licenseFileFoundWithEnvVariable.push_back(*it);
                    eventRegistry.addEvent(LICENSE_FILE_FOUND, SVRT_INFO, *it);
                }
            } else {
                eventRegistry.addEvent(LICENSE_FILE_NOT_FOUND, SVRT_WARN,
                        env_var_value);
            }
        } else {
            eventRegistry.addEvent(ENVIRONMENT_VARIABLE_NOT_DEFINED, SVRT_WARN);
        }
    }
    return licenseFileFoundWithEnvVariable;
}
 
}
}