permaculture playing cards*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Allow the user to change data file when running app in client mode? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Allow the user to change data file when running app in client mode?" Watch "Allow the user to change data file when running app in client mode?" New topic
Author

Allow the user to change data file when running app in client mode?

Björn Wessman
Greenhorn

Joined: Jun 01, 2009
Posts: 21
Hi,

I use a static reference to my Data class (the database/file accessor) from my DataFacade class, like in Andrew Monkhouse's book. In network mode, a default constant path is used to locate the data file, whereas the user will choose the location in stand-alone mode. Once an instance of Data is created it can't be re-instantiated until the application is restarted (This would also be true if I implemented the singleton pattern in the Data class).

Example from Data class constructor:

if (database == null) {
database = new Data(filePath);
}
return database;

For server mode, this makes sense, but when running in stand-alone mode, the only reason I see for not allowing to change data file while the application is running is that both stand-alone and network mode should use the same generic code to instantiate the database. I was however thinking about adding another constructor in my Data class with a flag indicating stand-alone mode and then allowing the database reference to refer to a new data file. I think this might be a good thing in case the user chooses the wrong data file (perhaps he/she has several working files). It's not very user friendly to force the user to restart the application in order to change data file...

Does anyone have an opinion on this?

BR
Björn
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5131
    
  12

Hi Björn,

First of all: welcome to the javaranch!

If i understand you correctly when the server starts you use a default constant path locating your database file (so no choosing of database file). when the user starts standalone he can choose the location of database file. Is my understanding correct? If it is, I don't see why in server mode you don't let the user choose his database file he wants to use when running the server...

A second thought: from a design perspective i don't think the data class should be aware in which mode it's running. so i would certainly not add a flag indicating the application is running in standalone mode.

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Björn Wessman
Greenhorn

Joined: Jun 01, 2009
Posts: 21
Hi Roel, and thanks for the welcome!

You are correct in all your assumptions. I could let the user choose file in server mode as well, but I sort of think that's more of an admin setting, not a regular user setting. It wouldn't be a good thing if any user connected to the server could change the data file that all connected users are working towards... and since I think adding authorization is way out of the certification scope, I chose to not let the user specify data file location in server mode. When running in stand-alone mode though, a change of data file wouldn't affect any other user so I thought it would be a good idea to add that possibility.

But I agree that the Data class shouldn't be aware of what mode the app is running in. I guess I could instantiate a new instance of Data if the location of the file differs from the current (which it never would in server mode if I use the contant path.

Then I'd change my code snippet from my first post from:

if (database == null) {
database = new Data(filePath);
}

to:

if (!database.dataFilePath.equals(filePath)) {
database = new Data(filePath);
}

Or I could add a getNewInstance() method in the Data class. Then it could be used in both modes but in my implementation I would only use it when instantiating the Data class from the client, not from the server...
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Howdy, Björn! Welcome to JavaRanch as well!

...I chose to not let the user specify data file location in server mode.


Partner, I'm affraid this goes against one of the "musts" of the assignment. I believe you also have this statement in your assignment:

Your choice of RMI or serialized objects will not affect your grade, but no other approach is acceptable. In either case, the program must allow the user to specify the location of the database, and it must also accept an indication that a local database is to be used, in which case, the networking must be bypassed entirely. No authentication is required for database access.


To give you an idea, here's what I did: when the user runs java -jar runme.jar server, a dialog is displayed, asking the user to enter the database path and the port number in which the server will run. After providing these settings, a message that says "Server running, press Ctrl + C to exit..." is displayed. Pretty simple. But the most important thing is, you have to allow the user to provide the database location every time the server starts.

And, as always, I definitely agree with my good buddy Roel; the Data class doesn't need to know in which mode the application is running.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2242
    
    7

Hi Bjorn and welcome to the Ranch. About switching data file for the client mode, it really not necessary. From the network mode, does the client care what or where the data file is? No. All network clients care about is connecting to the "correct" server where that server already points to the data file. From the local mode, if the data file is "incorrect" (eg wrong cookie value) then the client app won't even start up because if it did, there would be just a blank screen with no table or data. The local database setup is similar for the server setup. When server starts, if the data file is incorrect, how can the server instantiate that data class.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Björn Wessman
Greenhorn

Joined: Jun 01, 2009
Posts: 21
K. Tsang wrote:From the local mode, if the data file is "incorrect" (eg wrong cookie value) then the client app won't even start up because if it did, there would be just a blank screen with no table or data. The local database setup is similar for the server setup. When server starts, if the data file is incorrect, how can the server instantiate that data class.


Yes, but there can be several valid data files. The instructions says:

"Start of file
4 byte numeric, magic cookie value. Identifies this as a data file"

To me, that doesn't sound like a unique file identifier, just a flag saying that it is a data file. So in my validation check, I just compare the first four bytes of the file with that magic cookie value {00, 00, 02, 03} in my case.

When you run in local mode, you'd have the possibility to have perhaps one data file for each new week, or day, or month, or whatever...
Björn Wessman
Greenhorn

Joined: Jun 01, 2009
Posts: 21
Roberto Perillo wrote:
Partner, I'm affraid this goes against one of the "musts" of the assignment. I believe you also have this statement in your assignment:

Your choice of RMI or serialized objects will not affect your grade, but no other approach is acceptable. In either case, the program must allow the user to specify the location of the database, and it must also accept an indication that a local database is to be used, in which case, the networking must be bypassed entirely. No authentication is required for database access.



Yeah, you're right about that. I think I will make a property file that the server reads on start-up.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5131
    
  12

Hi Bjorn,

That property file is also not the right way to go, because it requires manual editing of file by the accessor and that's also not allowed.

Kind regards,
Roel
Björn Wessman
Greenhorn

Joined: Jun 01, 2009
Posts: 21
Roel De Nijs wrote:Hi Bjorn,

That property file is also not the right way to go, because it requires manual editing of file by the accessor and that's also not allowed.

Kind regards,
Roel


Hi,

Yes, I will start some server dialog, but I will save the settings to a property file as the instructions require that the configuration "must be persistent between runs of the program".
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Björn Wessman wrote:Yes, I will start some server dialog, but I will save the settings to a property file as the instructions require that the configuration "must be persistent between runs of the program".


Correct! And that property file must be located in the current working directory (System.getProperty("user.dir")). That's the right approach!
Björn Wessman
Greenhorn

Joined: Jun 01, 2009
Posts: 21
Roberto Perillo wrote:
To give you an idea, here's what I did: when the user runs java -jar runme.jar server, a dialog is displayed, asking the user to enter the database path and the port number in which the server will run. After providing these settings, a message that says "Server running, press Ctrl + C to exit..." is displayed. Pretty simple. But the most important thing is, you have to allow the user to provide the database location every time the server starts.


Hey Roberto, did you use a JFileChooser for selecting the database or just a regular JTextField?
I'm using a JFileChooser when selecting the database file in stand-alone mode, but in this case I feel a text field might be more valid as the remote database doesn't necessarily might be a file later on.

And also, do you think it would be OK to set a default path for the first time the server is started (which would just be "db-2x3.db")? Perhaps it's better to force the user to type a path the first time the server is run...

BR
Björn
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Hey, Björn!

I used JTextFields for these windows... but before anything, I verify if the provided file really exists (new File(path).exists()). The application is not started while the user does not provide a valid path.

Honestly, I think setting a default path is unnecessary... in my case, the first time the application is run, the suncertify.properties file does not exist, so I just let the user provide the required settings (such as database path, port number, etc) and create the properties file with the values provided. The next time the application is started, the properties file is already there, so I just read it and populate the JTextFields with the proper values.
Björn Wessman
Greenhorn

Joined: Jun 01, 2009
Posts: 21
Alright, thanks for the input!
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5131
    
  12

Hi Björn,

I did the same as Roberto, buth i had a JTextField and a JFileChooser. So user can enter the path to the database file manually or choose one. And the entered or chosen path is also validated: is extension db, is readible, is writable, is not empty file,... As long as path is not valid, user can not continue, that is: button to start server or to start standalone client is disabled.
the magic cookie validation is executed when the server (or standalone application) is started (so after user clicks on start-button).

Kind regards,
Roel
Fernando Franzini
Ranch Hand

Joined: Jan 09, 2009
Posts: 486
    
    2

Hi ranchers....very amazing post and replays....But I wanna rise some point....
If user start server and chose a data-base-file...in host A..let suppose that a client in host B acess server in host A...very good !!!
The situations is: if the one standalone local client in host A (the same host that server are running) wanna conect in the same file that server are using ?
The singleton patterns wont work because it will be another static stack application inside JVM !!!
So...how deal with that ? The assessors could try that !!!
Regards...


Fernando Franzini - Java Blog
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Hey, Fernando!

The situations is: if the one standalone local client in host A (the same host that server are running) wanna conect in the same file that server are using ?


That's not supposed to happen, partner. I think you also have this in your assignment:

Your server must be capable of handling multiple concurrent requests, and as part of this capability, must provide locking functionality as specified in the interface provided above. You may assume that at any moment, at most one program is accessing the database file
Fernando Franzini
Ranch Hand

Joined: Jan 09, 2009
Posts: 486
    
    2

Hi Roberto....you are right.....I forget this part of assignment !!! and...it's is good !! because we have to deal with this situations......complicated...anyway.....less one think to fry ours headdddddd
Thankssss and regardsss
Björn Wessman
Greenhorn

Joined: Jun 01, 2009
Posts: 21
Roel De Nijs wrote:Hi Björn,

I did the same as Roberto, buth i had a JTextField and a JFileChooser. So user can enter the path to the database file manually or choose one. And the entered or chosen path is also validated: is extension db, is readible, is writable, is not empty file,... As long as path is not valid, user can not continue, that is: button to start server or to start standalone client is disabled.
the magic cookie validation is executed when the server (or standalone application) is started (so after user clicks on start-button).

Kind regards,
Roel


Hey Roel, I actually ended up doing the same as you - both text field and file chooser. I only prompt for the db file when starting the server though. The sclient application can always be started, but I have all functionality disabled in it until a valid data file is chosen. This is because I decided to give the user the possibility to change data file while the application is already running in stand-alone mode, just as the user could choose to reconnect to a different server when running in network-client mode.

I'm not sure that I will do the thorough validation of the file that you do before starting the server. Right now, I only check that it exists. If it's invalid in some way, an IOException will be thrown either when trying to access it (and then the server won't start, because I only read it once and cache it) or when trying to write to it (the exception is caught and an error message is presented to the user). I don't think I will put too much effort into restrictions that isn't explicitly required in the specification.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5131
    
  12

Hi Björn,

This is because I decided to give the user the possibility to change data file while the application is already running in stand-alone mode

I don't think I will put too much effort into restrictions that isn't explicitly required in the specification.


That's a bit contradictory: both are certainly not required by the instructions, but the 1st you do, 2nd you don't But you are right: when a file is empty for example an IOException will be thrown in the Data class, so these checks are not really needed.

Kind regards,
Roel
Björn Wessman
Greenhorn

Joined: Jun 01, 2009
Posts: 21
Roel De Nijs wrote:Hi Björn,

This is because I decided to give the user the possibility to change data file while the application is already running in stand-alone mode

I don't think I will put too much effort into restrictions that isn't explicitly required in the specification.


That's a bit contradictory: both are certainly not required by the instructions, but the 1st you do, 2nd you don't But you are right: when a file is empty for example an IOException will be thrown in the Data class, so these checks are not really needed.

Kind regards,
Roel


Ha ha, yes I guess that's a bit contradictory. But I'm a front-end developer so I can't stop myself from caring about usability issues
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Allow the user to change data file when running app in client mode?
 
Similar Threads
Indication that a local database is to be used ?
Please help me to understand these requirements
Layers in SCJD assignment
Database location
server/alone command line