0

I have a desktop application that runs every 10 mins. I ask for login credentials at the beginning and then it carries on till the user exits the application.

I have my logic in such a way that if he enters correct credentials then the table is created and his login information is stored. If he enters wrong credentials then the table storing wrong credentials would be deleted and a pop up with invalid login pops up.

But when the user's system goes to a stand by or hibernate or if there is no internet connection, so basically if the application is unable to connect to the mail then it goes into the table deletion part and drops the table.

How can i handle this? If there is an internet interruption i want that if the user had previously successfully logged in then it should not drop the table rather exit.

Here is the code - After the login table is created and record inserted (immaterial it is authentic or not) the control comes over here -

public void run(){
    Properties props = System.getProperties();
    props.setProperty("mail.store.protocol", "imap");
    try{
        Session session = Session.getDefaultInstance(props, null);
        Store store = session.getStore("imap");
        store.connect(getMailboxId(), getEmailId(), getPassword());
        inbox = store.getFolder("Inbox");

                    <<some data processing>>
}

catch (NoSuchProviderException e){
        e.printStackTrace();
        System.exit(1);
    }
    catch (MessagingException e){
        try {
              new DbConnect().deleteRecords(); //Delete table if invalid credentials
        } catch (ClassNotFoundException d) {
            d.printStackTrace();
        }
        e.printStackTrace();
        JOptionPane.showMessageDialog(controllingFrame,
                "Invalid Credentials. Try again.",
                "Error Message",
                JOptionPane.ERROR_MESSAGE);
        System.exit(2);
    }
Dan
  • 801
  • 2
  • 14
  • 29

1 Answers1

0

Could you ping your server (java code to ping an IP address) then act accourdingly?

psuedocode:

pingResult = ping server;
if (pingResult == connectedToServer) {
    result = table save or delete; //Working method
}else {
    result = only test against last login table;
}
continue with rest of program;

EDIT: Note that you are throwing the NoSuchProviderException before/alongside the MessagingException. I'm sure you're aware that the NoSuchProviderException extends the MessagingException (http://docs.oracle.com/javaee/1.4/api/javax/mail/NoSuchProviderException.html) and as such, the error is probably always going to the MessagingException catch.

The tricky part is that the Store throws MessagingException as a catch all. I would set a flag in the try to some value, and ONLY catch the NoSuchProviderException then outside of the try catch test the flag against the default value and act accourdingly. I am of the opinion that the more specific an error you can catch and be accurate the better.

So the code you provided would look something like:

public void run(){
    **boolean flag = false;**

    Properties props = System.getProperties();
    props.setProperty("mail.store.protocol", "imap");
    try{
        Session session = Session.getDefaultInstance(props, null);
        Store store = session.getStore("imap");
        store.connect(getMailboxId(), getEmailId(), getPassword());
        inbox = store.getFolder("Inbox");

        **flag = true;**

                    <<some data processing>>
}
catch (NoSuchProviderException e){
        e.printStackTrace();
        System.exit(1);
    }

**if (!flag) {**
        try {
              new DbConnect().deleteRecords(); //Delete table if invalid credentials
        } catch (ClassNotFoundException d) {
            d.printStackTrace();
        }
        e.printStackTrace();
        JOptionPane.showMessageDialog(controllingFrame,
                "Invalid Credentials. Try again.",
                "Error Message",
                JOptionPane.ERROR_MESSAGE);
        System.exit(2);
    }
Community
  • 1
  • 1
sparks
  • 736
  • 1
  • 9
  • 29