• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IQSS / dataverse / #22002

01 Apr 2024 07:56PM CUT coverage: 20.716% (+0.5%) from 20.173%
#22002

push

github

web-flow
Merge pull request #10453 from IQSS/develop

Merge 6.2 into master

704 of 2679 new or added lines in 152 files covered. (26.28%)

81 existing lines in 49 files now uncovered.

17160 of 82836 relevant lines covered (20.72%)

0.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/edu/harvard/iq/dataverse/settings/ConfigCheckService.java
1
package edu.harvard.iq.dataverse.settings;
2

3
import edu.harvard.iq.dataverse.MailServiceBean;
4
import edu.harvard.iq.dataverse.pidproviders.PidUtil;
5
import edu.harvard.iq.dataverse.settings.SettingsServiceBean.Key;
6
import edu.harvard.iq.dataverse.util.FileUtil;
7
import edu.harvard.iq.dataverse.util.MailSessionProducer;
8
import jakarta.annotation.PostConstruct;
9
import jakarta.ejb.DependsOn;
10
import jakarta.ejb.Singleton;
11
import jakarta.ejb.Startup;
12
import jakarta.inject.Inject;
13
import jakarta.mail.internet.InternetAddress;
14

15
import java.io.IOException;
16
import java.nio.file.FileSystemException;
17
import java.nio.file.Files;
18
import java.nio.file.Path;
19
import java.util.Map;
20
import java.util.Optional;
21
import java.util.logging.Level;
22
import java.util.logging.Logger;
23

24
@Startup
25
@Singleton
26
@DependsOn({"StartupFlywayMigrator", "PidProviderFactoryBean"})
27
public class ConfigCheckService {
×
28
    
29
    private static final Logger logger = Logger.getLogger(ConfigCheckService.class.getCanonicalName());
×
30
    
31
    @Inject
32
    MailSessionProducer mailSessionProducer;
33
    @Inject
34
    MailServiceBean mailService;
35

36
    public static class ConfigurationError extends RuntimeException {
37
        public ConfigurationError(String message) {
38
            super(message);
×
39
        }
×
40
    }
41
    
42
    @PostConstruct
43
    public void startup() {
NEW
44
        if (!checkSystemDirectories() || !checkPidProviders()) {
×
45
            throw new ConfigurationError("Not all configuration checks passed successfully. See logs above.");
×
46
        }
47
        
48
        // Only checks resulting in warnings, nothing critical that needs to stop deployment
NEW
49
        checkSystemMailSetup();
×
UNCOV
50
    }
×
51

52
    /**
53
     * In this method, we check the existence and write-ability of all important directories we use during
54
     * normal operations. It does not include checks for the storage system. If directories are not available,
55
     * try to create them (and fail when not allowed to).
56
     *
57
     * @return True if all checks successful, false otherwise.
58
     */
59
    public boolean checkSystemDirectories() {
60
        Map<Path, String> paths = Map.of(
×
61
                Path.of(JvmSettings.UPLOADS_DIRECTORY.lookup()), "temporary JSF upload space (see " + JvmSettings.UPLOADS_DIRECTORY.getScopedKey() + ")",
×
62
                Path.of(FileUtil.getFilesTempDirectory()), "temporary processing space (see " + JvmSettings.FILES_DIRECTORY.getScopedKey() + ")",
×
63
                Path.of(JvmSettings.DOCROOT_DIRECTORY.lookup()), "docroot space (see " + JvmSettings.DOCROOT_DIRECTORY.getScopedKey() + ")");
×
64
        
65
        boolean success = true;
×
66
        for (Path path : paths.keySet()) {
×
67
            // Check if the configured path is absolute - avoid potential problems with relative paths this way
68
            if (! path.isAbsolute()) {
×
69
                logger.log(Level.SEVERE, () -> "Configured directory " + path + " for " + paths.get(path) + " is not absolute");
×
70
                success = false;
×
71
                continue;
×
72
            }
73
            
74
            if (! Files.exists(path)) {
×
75
                try {
76
                    Files.createDirectories(path);
×
77
                } catch (IOException e) {
×
78
                    String details;
79
                    if (e instanceof FileSystemException) {
×
80
                        details = ": " + e.getClass();
×
81
                    } else {
82
                        details = "";
×
83
                    }
84
                    
85
                    logger.log(Level.SEVERE, () -> "Could not create directory " + path + " for " + paths.get(path) + details);
×
86
                    success = false;
×
87
                }
×
88
            } else if (!Files.isWritable(path)) {
×
89
                logger.log(Level.SEVERE, () -> "Directory " + path + " for " + paths.get(path) + " exists, but is not writeable");
×
90
                success = false;
×
91
            }
92
        }
×
93
        return success;
×
94
    }
95
    
96
    /**
97
     * This method is not expected to make a deployment fail, but send out clear warning messages about missing or
98
     * wrong configuration settings.
99
     */
100
    public void checkSystemMailSetup() {
101
        // Check if a system mail setting has been provided or issue warning about disabled mail notifications
NEW
102
        Optional<InternetAddress> mailAddress = mailService.getSystemAddress();
×
103
        
104
        // Not present -> warning
NEW
105
        if (mailAddress.isEmpty()) {
×
NEW
106
            logger.warning("Could not find a system mail setting in database (key :" + Key.SystemEmail + ", deprecated) or JVM option '" + JvmSettings.SYSTEM_EMAIL.getScopedKey() + "'");
×
NEW
107
            logger.warning("Mail notifications and system messages are deactivated until you provide a configuration");
×
108
        }
109
        
110
        // If there is an app server provided mail config, let's determine if the setup is matching
111
        // TODO: when support for appserver provided mail session goes away, this code can be deleted
NEW
112
        if (mailSessionProducer.hasSessionFromAppServer()) {
×
NEW
113
            if (mailAddress.isEmpty()) {
×
NEW
114
                logger.warning("Found a mail session provided by app server, but no system mail address (see logs above)");
×
115
            // Check if the "from" in the session is the same as the system mail address (see issue 4210)
116
            } else {
NEW
117
                String sessionFrom = mailSessionProducer.getSession().getProperty("mail.from");
×
NEW
118
                if (! mailAddress.get().toString().equals(sessionFrom)) {
×
NEW
119
                    logger.warning(() -> String.format(
×
120
                        "Found app server mail session provided 'from' (%s) does not match system mail setting (%s)",
NEW
121
                        sessionFrom, mailAddress.get()));
×
122
                }
123
            }
124
        }
NEW
125
    }
×
126

127
    /**
128
     * Verifies that at least one PidProvider capable of editing/minting PIDs is
129
     * configured. Requires the @DependsOn("PidProviderFactoryBean") annotation above
130
     * since it is the @PostCOnstruct init() method of that class that loads the PidProviders
131
     *
132
     * @return True if all checks successful, false otherwise.
133
     */
134
    private boolean checkPidProviders() {
NEW
135
        return PidUtil.getManagedProviderIds().size() > 0;
×
136
    }
137
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc