001package dk.netarkivet.common.tools;
002
003import java.io.File;
004
005import dk.netarkivet.common.CommonSettings;
006import dk.netarkivet.common.utils.EMailNotifications;
007import dk.netarkivet.common.utils.NotificationType;
008import dk.netarkivet.common.utils.NotificationsFactory;
009import dk.netarkivet.common.utils.Settings;
010import dk.netarkivet.common.utils.SystemUtils;
011
012/**
013 * Reads the mail-settings and tries to send a mail-notification. 
014 */
015public class MailValidator {
016
017    public static final String SETTINGSFILEPATH = "dk.netarkivet.settings.file";
018    public static final String EmailNotificationsClass = EMailNotifications.class.getName();
019
020    /**
021     * @param args The path to a settings.xml (optional). If no argument, uses the existing settings.(e.g. by explicit setting it
022     *  -Ddk.netarkivet.settings.file=/fullOrrelative/path/to/settings.xml )
023     */
024    public static void main(String[] args) {
025        if (args.length == 1) {
026            System.out.println("Using settingsfile given as argument: " + args[0]); 
027            System.setProperty(SETTINGSFILEPATH, args[0]);
028            File settingsfile = new File(args[0]);
029            if (!settingsfile.exists()) {
030                System.err.println("Aborting program. Settingsfile '" + settingsfile.getAbsolutePath() + "' does not exist or is not a file");
031                System.exit(1);
032            }
033        } else {
034            String settingsfilename = System.getProperty(SETTINGSFILEPATH);
035            if (settingsfilename == null) {
036                System.out.println("Using default settings");
037            } else {
038                System.out.println("Using settingsfile '" + settingsfilename + "' defined by setting '" + SETTINGSFILEPATH + "'");
039            }
040        }
041        String notificationsClass = Settings.get(CommonSettings.NOTIFICATIONS_CLASS);
042        if (!notificationsClass.equals(EmailNotificationsClass)) {
043            System.err.println("Aborting program. Wrong notificationClass defined in the settings. Should be '" + EmailNotificationsClass + "' but was '" + notificationsClass + "'");
044            System.exit(1);
045        }
046        NotificationsFactory.getInstance().notify("Test-message sent from " + MailValidator.class.getName() + " from host '" + SystemUtils.getLocalHostName(), NotificationType.INFO);
047        System.out.println("Test-Mail now sent successfully to address '" + Settings.get(EMailNotifications.MAIL_RECEIVER_SETTING) + "' using '" 
048                + Settings.get(CommonSettings.MAIL_SERVER) + "' as mailserver, and '" + Settings.get(EMailNotifications.MAIL_SENDER_SETTING) + "' as sender.");
049    }
050}