com.navient.portalupdater.Settings.java Source code

Java tutorial

Introduction

Here is the source code for com.navient.portalupdater.Settings.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.navient.portalupdater;

import com.navient.portalupdater.UI.MessageDialogs;
import com.navient.portalupdater.UI.MessageDialogsImpl;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;

/**
 *
 * @author e65791
 */
public class Settings extends PasswordSupport {

    private final Properties userprop;
    private String user_prop_file;
    public final String portal_location = GetExecutionPath();
    //public static final String portal_location = "/home/e65791/Desktop/Portal/";

    public String hostname;
    public String username;
    public String id;
    public String ad_password;
    public String tldap_password;
    public String opsware_server;

    public Image updating_splash;
    public Image downloading_splash;

    public static final String LATEST_VERSION_FILE = "/home/common/Portal/latest_version";
    public static final String RELEASE_NOTES_FILE = "/home/common/Portal/release_notes";
    public static final String USER_UPDATES_FILE = "/tmp/PortalUpdate/UserPropertiesUpdates.txt";
    public static final String PUSH_PORTAL_SCRIPT = "/home/common/Portal/PushPortal.sh ";
    public final String PORTAL_UPDATE_SCRIPT = portal_location + "PortalUpdateScript.sh";
    public final String START_STOP_SCRIPT = portal_location + "StartStopPortal.sh";

    public Settings() {
        //user_prop_file = "/tmp/user.properties";
        user_prop_file = portal_location + "/files/user.properties";

        try {
            String host = java.net.InetAddress.getLocalHost().getHostName();
            if (host.contains(".")) {
                hostname = host.substring(0, host.indexOf("."));
            } else {
                hostname = host;
            }
        } catch (java.net.UnknownHostException ex) {
            Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, null, ex);
        }

        userprop = new Properties();
        InputStream input = null;
        try {
            // Load the user properties file
            input = new FileInputStream(user_prop_file);
            userprop.load(input);

            username = userprop.getProperty("username");
            if ("t".equals(username.substring(username.length() - 1))) {
                id = username.substring(0, 6);
            } else {
                id = username;
            }

            ad_password = getADPass();
            tldap_password = getTLDAPPass();
            opsware_server = userprop.getProperty("opsware_server");

        } catch (IOException e) {
            MessageDialogs dialogs = new MessageDialogsImpl();
            dialogs.showError("Can't load user.properties file.\n\nNo such file or directory.");
            System.out.println("Error: " + e.getMessage());
            System.exit(1);
        } finally {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        InputStream in;
        try {
            in = ClassLoader.getSystemResourceAsStream("updating.gif");
            updating_splash = Toolkit.getDefaultToolkit().createImage(IOUtils.toByteArray(in));
            in = ClassLoader.getSystemResourceAsStream("downloading.gif");
            downloading_splash = Toolkit.getDefaultToolkit().createImage(IOUtils.toByteArray(in));
        } catch (IOException ex) {
            Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getSetting(String setting) {
        return userprop.getProperty(setting);
    }

    public void saveSetting(String key, String value) {
        System.out.println("Saving key: " + key + " with value: " + value);
        if (value.isEmpty()) {
            System.out.println("value for " + key + " is empty");
            if (getSetting(key) == null) {
                System.out.println(key + " doesn't already exist");
                userprop.setProperty(key, "");
            }
        } else {
            userprop.setProperty(key, value);
        }
        saveUserPropertiesFile();
    }

    private String GetExecutionPath() {
        String absolutePath = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
        absolutePath = absolutePath.substring(0, absolutePath.lastIndexOf("/"));
        absolutePath = absolutePath.replaceAll("%20", " "); // Surely need to do this here
        absolutePath = absolutePath + "/";
        return absolutePath;
    }

    public void deleteUserPropertiesKey(String key) {
        userprop.remove(key);
        saveUserPropertiesFile();
    }

    private void saveUserPropertiesFile() {
        OutputStream out = null;
        try {
            File f = new File(user_prop_file);
            out = new FileOutputStream(f);
            userprop.store(out, null);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                out.close();
            } catch (IOException ex) {
                Logger.getLogger(Settings.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    public boolean isPasswordExpired() {
        return checkIsPasswordExpired("AD", userprop.getProperty("ad_password_timestamp"));
    }

    public String getADPass() {
        PasswordManagement pm = new PasswordManagement();
        String pass = pm.getPassword(userprop.getProperty("ad_password"));
        System.out.println("decoded ADPass: " + pass);
        return pass;
    }

    /**
     *
     * @return
     */
    public String getTLDAPPass() {
        PasswordManagement pm = new PasswordManagement();
        String pass = pm.getPassword(userprop.getProperty("tldap_password"));
        //System.out.println("decoded TLDAPass: " + pass);
        return pass;
    }

}