Roberto Perillo wrote:
I have 2 getInstance methods: one with no arguments, and another one that takes a String. So, since it is a singleton, it is necessary to primarily call the one that takes a String, indicating the db path. This sets a local static variable called databaseLocation (has to be static because the getInstance(String) method is also static). Then, after this method sets the db path, then we can normally call the getInstance method. When we call getInstance and the databaseLocation variable is null, then I throw an IllegalStateException.
There's no need to make the constructor of the server class private. I at least didn't. There I just have one no-arg public constructor. I have a method called startServer, which also takes a String, and there I call Data's getInstance(String) method.
About your databaseLocation variable, do you set it from within your startServer method? If so, how do you know what string to pass? I doubt I can get it from the properties file.
Assume I got the string settle which I haven't yet, and go on to create the remote object ... I now get a java.rmi.ConnectException: Connection refused: connect. Further checking it ultimately calls Socket class's Socket(String host, int port) constructor.
Also is there any difference between registry.rebind() and registry.bind()?
While I'm at it, for the client side, can I pass in a string that looks like "//host:port/filename" for remote connection when creating my Data class? Yes I changed my Data contructor to string instead of using File earlier. Since my local connection is simply "filename".
in my remote connection I have:
Registry registry = LocateRegistry.getRegistry(address, port); // address and port got from properties
DBRemote dbr = (DBRemote) registry.lookup(name);
But then I still need to return an instance of Data through Data.getInstance(String). Or do I return DBRemote for remote connections?
OK so your approach is when user runs the network client, they choose the db location then save it to properties file then you start the server?
But I thought, at least mine is, start server is independent from running the network client. Of course I can do it like your way which is even more simpler.
Also for remote connection, did you use a JFileChooser to locate your db file? Cos I'm thinking the file chooser can't possibly know which IP address you are going to use even if file chooser can indeed browse remote file using URL or some other means.