summaryrefslogtreecommitdiffstats
path: root/configgen/src/main/java/com/yahoo/config/codegen/MakeConfigProperties.java
blob: e23b33ae63c3846554c56455c1cea2dc4c79f020 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.codegen;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Encapsulates data extracted from system properties.
 *
 * @author gjoranv
 */
class MakeConfigProperties {

    private static final List<String> legalLanguages = Arrays.asList("java", "cpp" );

    final File destDir;
    final File[] specFiles;
    final String language;
    final String dirInRoot; // Where within fileroot to store generated class files
    final String javaPackagePrefix;
    final boolean dumpTree;
    final boolean generateFrameworkCode;

    MakeConfigProperties() throws PropertyException {
        this(System.getProperty("config.dest"),
             System.getProperty("config.spec"),
             System.getProperty("config.lang"),
             System.getProperty("config.subdir"),
             System.getProperty("config.dumpTree"),
             System.getProperty("config.useFramework"),
             System.getProperty("config.packagePrefix"));
    }

    @SuppressWarnings("WeakerAccess") // Used by ConfigGenMojo
    public MakeConfigProperties(String destDir,
                                String specFiles,
                                String language,
                                String dirInRoot,
                                String dumpTree,
                                String generateFrameworkCode,
                                String javaPackagePrefix) throws PropertyException {
        this.destDir = checkDestinationDir(destDir);
        this.specFiles = checkSpecificationFiles(specFiles);
        this.language = checkLanguage(language);
        this.dirInRoot = checkDirInRoot(this.destDir, dirInRoot);
        this.dumpTree = Boolean.parseBoolean(dumpTree);
        this.generateFrameworkCode = Boolean.parseBoolean(generateFrameworkCode);
        this.javaPackagePrefix = javaPackagePrefix;
    }

    private static File checkDestinationDir(String destination) throws PropertyException {
        if (destination == null)
            throw new PropertyException("Missing property: config.dest.");

        File dir = new File(destination);
        if (!dir.isDirectory()) {
            throw new PropertyException("Could not find directory: " + dir.getPath());
        }
        return dir;
    }

    private static String checkDirInRoot(File destDir,  String dirInRoot) throws PropertyException {
            // Optional parameter
        if (dirInRoot == null) { return null; }
        File f = new File(destDir, dirInRoot);
        if (!f.isDirectory()) {
            throw new PropertyException("Could not find directory: " + f.getPath());
        }
        return dirInRoot;
    }

    private static String checkLanguage(String lang) throws PropertyException {
        String inputLang = lang != null ? lang.toLowerCase() : "java";
        if (! legalLanguages.contains(inputLang)) {
            throw new PropertyException
                    ("Unsupported code language: '" + inputLang + "'. Supported languages are: " + legalLanguages);
        }
        return inputLang;
    }

    private static File[] checkSpecificationFiles(String spec) throws PropertyException {
        if (spec == null)
            throw new PropertyException("Missing property: config.spec.");

        StringTokenizer st = new StringTokenizer(spec, ",");
        if (st.countTokens() == 0)
            throw new PropertyException("Missing property: config.spec.");

        File[] files = new File[st.countTokens()];
        for (int i = 0; st.hasMoreElements(); i++) {
            files[i] = new File((String) st.nextElement());
            if (!files[i].isFile())
                throw new PropertyException("Could not read file " + files[i].getPath());
        }
        return files;
    }

}