Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 345955a5

Přidáno uživatelem stepanekp před asi 3 roky(ů)

#14 Loading configuration from json edit

Zobrazit rozdíly:

src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/repository/ConfigurationRepository.java
8 8
import org.springframework.web.context.ServletContextAware;
9 9

  
10 10
import javax.servlet.ServletContext;
11
import java.io.File;
11
import java.io.*;
12 12
import java.net.URL;
13
import java.nio.file.Files;
14 13
import java.util.HashMap;
15 14
import java.util.Map;
16 15

  
......
34 33
    private void loadConfigurations(){
35 34
        LOGGER.info("-------START READING CONFIGURATIONS FROM FILES-------");
36 35
        Map<String, Map<String, Map<String, String>>> configurations = new HashMap<>();
36

  
37
        File folder = null;
38

  
37 39
        try {
38 40
            URL url = servletContext.getResource(CONFIGURATION_DIR);
39
            File folder = new File(url.getFile());
41
            folder = new File(url.getFile());
42
        } catch (Exception e) {
43
            e.printStackTrace();
44
            LOGGER.error("Cannot access folder with configurations " + CONFIGURATION_DIR);
45
        }
40 46

  
47
        for (final File fileEntry : folder.listFiles()) {
48
            LOGGER.info("Reading configuration from file " + fileEntry.getName());
41 49

  
50
            String json = "";   // json configuration file
51
            try {
52
                BufferedReader read = new BufferedReader(new InputStreamReader(fileEntry.toURI().toURL().openStream()));
42 53

  
43
            for (final File fileEntry : folder.listFiles()) {
44
                LOGGER.info("Reading configuration from file " + fileEntry.getName());
45
                String json = "";   // json configuration file
46
                JsonNode node = null;
47
                try {
48
                    json = new String(Files.readAllBytes(fileEntry.toPath()));
49
                    node = JsonParser.parse(json);
50
                } catch (Exception e) {
51
                    e.printStackTrace();
54
                String line;
55
                while ((line = read.readLine()) != null) {
56
                    json += line;
52 57
                }
58
            }
59
            catch(Exception e){
60
                e.printStackTrace();
61
                LOGGER.error("Cannot read configuration from file " + fileEntry.getName());
62
                continue;
63
            }
53 64

  
65
            JsonNode node = null;
66
            try {
67
                node = JsonParser.parse(json);
68
            } catch (IOException e) {
69
                e.printStackTrace();
70
                LOGGER.error("Cannot parse configuration from file " + fileEntry.getName());
71
                continue;
72
            }
54 73

  
55
                String configurationName = fileEntry.getName().split("\\.")[0];
74
            String configurationName = fileEntry.getName().split("\\.")[0];
56 75

  
57
                JsonNode arrayOfConfigurations = node.get("configuration");
76
            JsonNode arrayOfConfigurations = node.get("configuration");
58 77

  
59
                Map<String, Map<String, String>> newAntiPatternMap = new HashMap<>();
78
            if(arrayOfConfigurations == null)
79
                continue;
60 80

  
61
                for (int i = 0; i < arrayOfConfigurations.size(); i++) {
81
            Map<String, Map<String, String>> newAntiPatternMap = new HashMap<>();
62 82

  
63
                    JsonNode antiPatternNode = arrayOfConfigurations.get(i);
83
            for (int i = 0; i < arrayOfConfigurations.size(); i++) {
64 84

  
65
                    String antiPatternName = antiPatternNode.get("antiPattern").asText();
85
                JsonNode antiPatternNode = arrayOfConfigurations.get(i);
66 86

  
67
                    JsonNode arrayOfThresholds = antiPatternNode.get("thresholds");
87
                String antiPatternName = antiPatternNode.get("antiPattern").asText();
68 88

  
69
                    Map<String, String> newThresholds = new HashMap<String, String>();
89
                JsonNode arrayOfThresholds = antiPatternNode.get("thresholds");
70 90

  
71
                    for (int j = 0; j < arrayOfThresholds.size(); j++) {
72
                        JsonNode thresholdNode = arrayOfThresholds.get(j);
91
                Map<String, String> newThresholds = new HashMap<String, String>();
73 92

  
74
                        String thresholdName = thresholdNode.get("thresholdName").asText();
75
                        String value = thresholdNode.get("value").asText();
93
                for (int j = 0; j < arrayOfThresholds.size(); j++) {
94
                    JsonNode thresholdNode = arrayOfThresholds.get(j);
76 95

  
96
                    String thresholdName = thresholdNode.get("thresholdName").asText();
97
                    String value = thresholdNode.get("value").asText();
77 98

  
78
                        newThresholds.put(thresholdName, value);
79
                    }
99
                    newThresholds.put(thresholdName, value);
100
                }
80 101

  
81
                    newAntiPatternMap.put(antiPatternName, newThresholds);
102
                newAntiPatternMap.put(antiPatternName, newThresholds);
82 103

  
83
                }
84
                configurations.put(configurationName, newAntiPatternMap);
85 104
            }
86

  
87
        } catch (Exception e) {
88
            e.printStackTrace();
105
            configurations.put(configurationName, newAntiPatternMap);
89 106
        }
107

  
90 108
        this.allConfigurations = configurations;
91 109

  
92 110
        LOGGER.info("-------FINISHED READING CONFIGURATIONS FROM FILES-------");

Také k dispozici: Unified diff