aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/vespa/config/file/filesourcefactory.cpp
blob: 09f1d23ed1732fbaa8c4644033d620c26cedfe31 (plain) (blame)
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
57
58
59
60
61
62
63
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "filesourcefactory.h"
#include "filesource.h"
#include <vespa/config/subscription/sourcespec.h>
#include <vespa/vespalib/io/fileutil.h>
#include <vespa/vespalib/stllike/asciistream.h>
#include <vespa/log/log.h>
LOG_SETUP(".config.filesourcefactory");

namespace config {

DirSourceFactory::DirSourceFactory(const DirSpec & dirSpec)
    : _dirName(dirSpec.getDirName()),
      _fileNames()
{
    vespalib::DirectoryList files(vespalib::listDirectory(_dirName));
    for (size_t i = 0; i < files.size(); i++) {
        const vespalib::DirectoryList::value_type & fname(files[i]);
        if (fname.length() > 4 && fname.substr(fname.length() - 4) == ".cfg") {
            _fileNames.push_back(fname);
        }
    }
}

std::unique_ptr<Source>
DirSourceFactory::createSource(std::shared_ptr<IConfigHolder> holder, const ConfigKey & key) const
{
    vespalib::string fileId(key.getDefName());
    if (!key.getConfigId().empty()) {
        fileId += "." + key.getConfigId();
    }
    fileId += ".cfg";

    bool found(false);
    for (const auto & fileName : _fileNames) {
        if (fileName.compare(fileId) == 0) {
            found = true;
            break;
        } 
    }
    if ( !found ) {
        LOG(warning, "Filename '%s' was expected in the spec, but does not exist.", fileId.c_str());
    }
    vespalib::string fName = _dirName;
    if (!fName.empty()) fName += "/";
    fName += fileId;
    return std::make_unique<FileSource>(std::move(holder), fName);
}

FileSourceFactory::FileSourceFactory(const FileSpec & fileSpec)
    : _fileName(fileSpec.getFileName())
{
}

std::unique_ptr<Source>
FileSourceFactory::createSource(std::shared_ptr<IConfigHolder> holder, const ConfigKey & key) const
{
    (void) key;
    return std::make_unique<FileSource>(std::move(holder), _fileName);
}

} // namespace config