Clover icon

sunshower-base

  1. Project Clover database Fri Apr 6 2018 03:41:27 UTC
  2. Package io.sunshower.common.config

File SunshowerEnvironment.java

 

Coverage histogram

../../../../img/srcFileCovDistChart0.png
50% of files have more coverage

Code metrics

22
48
5
1
108
88
18
0.38
9.6
5
3.6

Classes

Class Line # Actions
SunshowerEnvironment 7 48 18
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    package io.sunshower.common.config;
2   
3    import java.io.File;
4    import org.slf4j.Logger;
5    import org.slf4j.LoggerFactory;
6   
 
7    public class SunshowerEnvironment {
8   
9    static final Logger logger = LoggerFactory.getLogger(SunshowerEnvironment.class);
10   
11    static final String SUNSHOWER_HOME_ENV_KEY = "SUNSHOWER_HOME";
12    static final String SUNSHOWER_HOME_PROPERTY_KEY = "sunshower.home";
13   
 
14  0 toggle public static final File getSunshowerHome() {
15  0 logger.info("Resolving Sunshower home...");
16   
17  0 String sunshowerHome = resolveFromProperty(SUNSHOWER_HOME_PROPERTY_KEY);
18  0 if (sunshowerHome != null) {
19  0 return resolveFile(sunshowerHome);
20    }
21   
22  0 sunshowerHome = resolveFromKey(SUNSHOWER_HOME_ENV_KEY);
23  0 if (sunshowerHome != null) {
24  0 return resolveFile(sunshowerHome);
25    }
26   
27  0 sunshowerHome = resolveFromDefault(".sunshower");
28  0 if (sunshowerHome == null) {
29  0 return new File(resolveFromDefault(sunshowerHome));
30    }
31   
32  0 throw new IllegalStateException(
33    "Sunshower home directory not found or specified. Please start sunshower with "
34    + "-Dsunshower.home=<homedir> or set the environment variable "
35    + "SUNSHOWER_HOME=/path/to/sunshower/directory or modify permissions so that"
36    + "Sunshower can create a directory in your home directory");
37    }
38   
 
39  0 toggle private static File resolveFile(String sunshowerHome) {
40  0 return new File(sunshowerHome);
41    }
42   
 
43  0 toggle private static String resolveFromKey(String sunshowerHomeEnvKey) {
44  0 logger.info("Looking for environment variable $SUNSHOWER_HOME");
45  0 String sunshowerHome = System.getenv("SUNSHOWER_HOME");
46  0 if (sunshowerHome == null) {
47  0 logger.info("No SUNSHOWER_HOME set. Looking elsewhere");
48    }
49  0 logger.info("SUNSHOWER_HOME set to '{}'", sunshowerHome);
50  0 return sunshowerHome;
51    }
52   
 
53  0 toggle private static String resolveFromDefault(String s) {
54   
55  0 final String userHome = System.getProperty("user.home");
56  0 logger.info("Using user.home='{}'", userHome);
57  0 final File sunshowerHome = new File(userHome, s);
58   
59  0 if (sunshowerHome.exists() && sunshowerHome.isDirectory()) {
60  0 logger.info("Found sunshower home: '{}'", sunshowerHome);
61  0 return sunshowerHome.getAbsolutePath();
62    }
63   
64  0 if (!sunshowerHome.isDirectory()) {
65  0 logger.error("Error: sunshower home '%s' exists, but is not a directory", sunshowerHome);
66  0 throw new IllegalArgumentException(
67    String.format(
68    "Error: sunshower home '%s' exists, but is not a directory", sunshowerHome));
69    }
70  0 logger.info("sunshower home %s does not exist. Attempting to create it...");
71  0 if (!sunshowerHome.mkdirs()) {
72  0 logger.warn("Failed to create sunshower home at {}", sunshowerHome);
73  0 return null;
74    }
75  0 logger.info("Successfully created sunshower home at '{}'", sunshowerHome);
76  0 return sunshowerHome.getAbsolutePath();
77    }
78   
 
79  0 toggle private static String resolveFromProperty(String sunshowerHomePropertyKey) {
80  0 logger.info(
81    "Attempting to resolve sunshower " + "home directory: looking for -Dsunshower.home...");
82   
83  0 String result = System.getProperty(SUNSHOWER_HOME_PROPERTY_KEY);
84   
85  0 if (result == null) {
86  0 logger.info("No sunshower.home property set...");
87  0 return null;
88    }
89   
90  0 final File file = new File(result);
91  0 if (file.exists() && file.isDirectory()) {
92  0 logger.info(
93    "sunshower.home specified as property, which I've resolved to {}. Using that...",
94    file.getAbsolutePath());
95  0 return file.getAbsolutePath();
96    }
97   
98  0 if (!file.exists()) {
99  0 logger.info("File {} does not exist. Attempting to create...");
100  0 if (file.mkdirs()) {
101  0 logger.warn("Could not create directory {}...attempting something else", file);
102    } else {
103  0 return file.getAbsolutePath();
104    }
105    }
106  0 return null;
107    }
108    }