Clover icon

sunshower-core

  1. Project Clover database Fri Apr 6 2018 03:27:42 UTC
  2. Package io.sunshower.service.io

File DefaultConfigurableFileResolutionStrategy.java

 

Coverage histogram

../../../../img/srcFileCovDistChart5.png
36% of files have more coverage

Code metrics

16
27
2
1
77
68
13
0.48
13.5
2
6.5

Classes

Class Line # Actions
DefaultConfigurableFileResolutionStrategy 13 27 13
0.488888948.9%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    package io.sunshower.service.io;
2   
3    import io.sunshower.model.core.auth.Credential;
4    import io.sunshower.model.core.auth.Tenant;
5    import io.sunshower.model.core.auth.User;
6    import io.sunshower.service.model.io.FileResolutionStrategy;
7    import java.io.File;
8    import org.slf4j.Logger;
9    import org.slf4j.LoggerFactory;
10    import org.springframework.beans.factory.annotation.Value;
11   
12    /** Created by haswell on 5/25/17. */
 
13    public class DefaultConfigurableFileResolutionStrategy implements FileResolutionStrategy {
14   
15    static final Logger logger =
16    LoggerFactory.getLogger(DefaultConfigurableFileResolutionStrategy.class);
17   
18    @Value("${sunshower.storage.root:default}")
19    private String storageRoot;
20   
 
21  1 toggle @Override
22    public File resolve(Tenant tenant, User user, Credential credential) {
23  1 if (storageRoot == null || "default".equals(storageRoot)) {
24  1 return configureDefault(tenant, user, credential);
25    }
26  0 logger.info("Storage root set to '{}'", storageRoot);
27  0 final File file = new File(storageRoot, String.format("storage/%s/workspace", user.getId()));
28  0 if (!file.exists()) {
29  0 logger.info("File {} does not exist. Attempting to create it", file.getAbsolutePath());
30  0 if (!file.mkdirs()) {
31  0 throw new IllegalStateException(
32    String.format(
33    "Attempt to " + "create '%s' failed. " + "I cannot continue",
34    file.getAbsolutePath()));
35    }
36    }
37  0 return file;
38    }
39   
 
40  1 toggle private File configureDefault(Tenant tenant, User user, Credential credential) {
41  1 logger.info(
42    "Configuration parameter 'sunshower.storage.root' not set. checking HASLI_HOME...");
43  1 String root = System.getenv("HASLI_HOME");
44  1 logger.info("HASLI_HOME: {}", root == null || root.trim().equals("") ? "<unset>" : root);
45  1 if (root != null) {
46  0 File f = new File(root);
47  0 if (f.exists() && f.isDirectory()) {
48  0 logger.info("Configuring storage from HASLI_HOME at: {}", f);
49  0 return f;
50    }
51    }
52   
53  1 String property = System.getProperty("user.dir");
54  1 logger.info(
55    "Failed to configure from HASLI_HOME. "
56    + "Either the value did not exist or was not a directory. "
57    + "Trying in user's directory: {}",
58    property);
59   
60  1 final File file =
61    new File(property, String.format("sunshower/storage/%s/workspace", user.getId()));
62  1 logger.info("Attempting to use: {}", file.getAbsolutePath());
63  1 if (!file.exists()) {
64  1 logger.info("File does not exist. Attempting to create...");
65  1 if (file.mkdirs()) {
66  1 logger.info("Successfully created {}", file.getAbsolutePath());
67    } else {
68  0 throw new IllegalStateException(
69    "Attempts to resolve default "
70    + "storage directory failed. Cannot create user "
71    + "storage and so cannot continue"
72    + "");
73    }
74    }
75  1 return file;
76    }
77    }