This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Thoughts on handling RemoteException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Thoughts on handling RemoteException" Watch "Thoughts on handling RemoteException" New topic
Author

Thoughts on handling RemoteException

Anne Crace
Ranch Hand

Joined: Aug 29, 2005
Posts: 223
I am in the process of trying all kinds of "stupid user" input on the setup dialog part of my application. I have a custom Dialog class to display error messages to the user if validation fails on the port, server address and database location. The server and the local client work fine (the main GUI will not start until valid input is received). I ran into a situation with the Network client that had me perplexed for a bit. If I input http://www.yahoo.com as the Ip Address, it passed validation, as it should, it's a valid IP addres, just not for my application. I then tried to input a good address and apparently the application had gotten too far and I got null pointer exception from Event Dispatcher (Swing). My solution, documented in choices.txt was to have my dialog show that a problem had occurred while connecting to the remote system and that the system was going to shut down. I also instructed them to start back up again with a different IP address. I know others have had issues validating the IP address, and it is my feeling that System.exit(0) is generally bad. I found no other good way to recover from this situation and documented that it is warranted for this particular situation. Any thoughts?


SCJP, SCJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5216
    
  12

Hi Anne,

I validated the configuration settings: port number and ip address or hostname (more info on this one, can be found here).

If settings are valid, the connect button is enabled and when user clicks this button, I try to connect to the server and get my service. If that fails, I show an explaining message to the user and then exit the application with an error code. This behavior is also described in my user manual (but not in my choices.txt).

You could also keep showing the dialog until user hits cancel or connecting to server succeeds.

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2379
    
    7

Hi Anne, in my app, i store the server's IP address in the properties file when starting the RMI server. Then in my remote interface, I have a isValidConnection() method checking if the client entered IP and port match with the server IP and port stored in the properties file. If match, open the JFrame. If not match pop up error dialog and the setup window remain open. It is like web form validation. The problem may be once you get an exception, you may exit that method hence closing the setup window.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

Howdy, Anne!

Just to give my insight, when the application starts in client mode, I display a small dialog that asks for the server's IP address and port number. In the IP address field, the user can only enter numbers and dots. Before anything, I verify if the given IP address is a valid one (and for that I use regular expression) and also if the given port number is valid. If so, then these values are stored in the local suncertify.properties file and the application starts normally; otherwise, I display an error message with a JOptionPane, saying that either the IP address or the port number is not valid.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5216
    
  12

Hi (great and wise) Roberto,

I think Anne is pointing to a valid address (hostname or ip doesn't matter) and a valid port number, but at the combination of serverAddress + port no server is running. And how that situation should be handled.

So in your program I enter a valid ip address (10.1.10.1 for example) and a valid port number (1099), but the server is running on 10.1.10.2:1099 and your application can't start normally because there is no server. Or am I missing something?

Kind regards,
Roel
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

Howdy, my good buddy Roel!

Right... now I took the time to read carefully her post, it's just that things at work are currently awful, and I couldn't read it the way I should
Ok, if I understood correctly, the question is, given a valid IP address which does not have the server running, how the RemoteException should be handled, right? In my case, when I catch the RemoteException after trying to connect without success, I simply display a JOptionPane to the user, saying that the server may not be running in the given machine. But the application does not exit; it simply displays the message and waits for the user to provide other data.
Anne Crace
Ranch Hand

Joined: Aug 29, 2005
Posts: 223
Hi all. Quick update. I think K Tsang is right about what is actually occurring here. I have already exited the method. The problem only happens on subsequent clicks of the button. I am trying to take Roberto's approach of only accepting numeric IP addresses as I think it will eliminate most chances for bad input, and simplify things. I already have a PositiveIntTextField class, so I added an overloaded constructor and another Document model that takes numbers and dots. I have been having issues on getting it to accept the period or dot. char period = '.'; doesn't seem to work. The core of the problem is doing like K Tsang said and validating based on the value of what is in the properties file, even though right now it is the same one. I will test it at home on two computers to be sure it works as I don't quite trust localhost testing. It has worked in the past on two computers so it will work again, I'm sure.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2379
    
    7

Anne Crace wrote:Hi all. Quick update. I think K Tsang is right about what is actually occurring here. I have already exited the method. The problem only happens on subsequent clicks of the button. I am trying to take Roberto's approach of only accepting numeric IP addresses as I think it will eliminate most chances for bad input, and simplify things. I already have a PositiveIntTextField class, so I added an overloaded constructor and another Document model that takes numbers and dots. I have been having issues on getting it to accept the period or dot. char period = '.'; doesn't seem to work. The core of the problem is doing like K Tsang said and validating based on the value of what is in the properties file, even though right now it is the same one. I will test it at home on two computers to be sure it works as I don't quite trust localhost testing. It has worked in the past on two computers so it will work again, I'm sure.


Hello Anne,

For remote connection, I recall that RemoteException would only occur if the connection is attempted and failed. This means as long as you have not createRegistry for RMI say the RemoteException would not happen. As for other exceptions like FileNotFoundException ... don't put the logic in the your core method. Use a separate method to do say the database file validation so that if you say enter an incorrect file (like wrong magic cookie value) you can use a error JDialog prompting the error from that separate method rather than having the core method going into catch clause making you exiting your app or window.

Now for the text field validation, you may want to consider a DocumentListener for each field. Eg the port field, you can allow only up to 5 digits and above 1024. Now you use the DocumentListener to check such things when user input each key.

Also I suggest Roel's approach too by disabling the connect or ok button until all inputs are valid. My client setup class is huge.

IP regex thread here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thoughts on handling RemoteException