aspose file tools*
The moose likes Servlets and the fly likes Lock a directory... need ideas Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Lock a directory... need ideas" Watch "Lock a directory... need ideas" New topic
Author

Lock a directory... need ideas

Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
Hi friends,
I am trying to do the following from within a servlet, so I am asking it here.

I have a directory on the server and I let multiple users to upload files "in the same time"...

My app gives these files random names while writing them to the server disk, so when a user tries to upload a file and the directory is not locked yet, I have to:

- lock the directory to other users trying to write to it their files.
- decide on a random file name that does not exist yet in the directory.
- write the file to disk.
- release the directory to other users that might be already waiting to write their own files.

My questions are how do I lock the directory to everyone but the user that is now trying to write into, and how do I release the directory.

Could you please give me some direction/terms that I should look to... I would appreciate your ideas.

Thank you....
Joseph
[ May 05, 2005: Message edited by: Joseph Sweet ]

We must know, we will know. -- David Hilbert
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Why do you need to lock the directory?
You could give each user their own sub-directory.
You could prepend each filename with the user's login name.

[Here comes a mid-air collision with Adeel Ansari...]
[ May 05, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Why locking the directory? You need not locking it. Just give the unique names to the file.

And If its something user specific then try to upload on user particular directory. Nonetheless, DB is better to store files, those belong to a particular user.

[edited]
4 minutes late.
[ May 05, 2005: Message edited by: Adeel Ansari ]
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
Because two different threads of the servlet might try to allocate the same random name to two different files that two different users try to upload at the same time.
In some scripting languages the solution to such a problem is to lock the whole application until writing the file is finished.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If each user gets their own directory, this won't happen.
It also won't happen if you prepend the username to the filename.

You could always synchronize the name generator. This would certainly be a smaller bottle neck than locking everyone out for the entire time that a file is being up loaded.

A file upload could take minutes on a slow connection.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
You can go for timestamp till millis.
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
Suppose two users try to upload at the same time: 12pm. their threads see the filename "12pm" does not exist yet, so both of them are allowed to write their files to the disk with the name 12pm. that must not happen.

now why not allocate each user a private subdir. I will give them when they confirm their email. but those uploaded files are uploaded in the registration process. they did confirm their email yet. so until they confirm their email, their uploaded files will be kept in a single temp directory. this is the directory that i want to lock.

i hope it is cleared now. would love to hear your suggestions.
will be back later today.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Create the directory when they upload the file.

Again, you can always synchronize the method that reads the directory and assigns a unique filename.
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
Thank you... I think the best thing is to add the user name to the file.

Please let me know how can a section in a servlet's method be synchornized. Is there something to lock and release critical sections?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

private synchronized String getUniqueName(){

// create name

// read destination directory to verify that it's there isn't already one with that name

// check list of recently created names to make sure we didn't just create this one

// add this filename to the list of recently created filenames.

// return name
}


Once the upload is complete, you could remove the filename from the list of recently created names.


I think it would be a lot less work to just prepend the userId to the filename.
[ May 05, 2005: Message edited by: Ben Souther ]
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
wow that's great.
I did not know there is a word synchronized in java.
Thank you for the info.
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
wouldnt the most unique filename be the session id of the user? add to that the timestamp upto millis, like adeel suggested and tht should be pretty unique.

Joseph, how will you find out which file belongs to which user? or is there anything in the file that does this file to user mapping?
inthis case, Ben's idea of user-name sounds good!!!
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
What about multiple files from the same user? then you have to make the filename, something more then the username.

May be its something you are not concern.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Adeel Ansari:
What about multiple files from the same user? then you have to make the filename, something more then the username.

May be its something you are not concern.


Depending on his rules, prepending with only the userID might actually be beneficial. This would allow users to update certain files by overwriting them with new files of the same name. If you don't want users to be able to do this, then a combination of userID and timestamp would be better.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Ben Souther:
Depending on his rules, prepending with only the userID might actually be beneficial. This would allow users to update certain files by overwriting them with new files of the same name. If you don't want users to be able to do this, then a combination of userID and timestamp would be better.


Yes can say. But I think if you wanna give update facility to the user then better work for it some other way. Otherwise he cant just upload more then one file. It would be just overwritten all the time.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If the filename is a combination of his userName and a filename of his choosing 'aansari-agingReport.xls' then the user can update any file he chooses. He/she can also keep as many different files as he likes on the system.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Ben Souther:
If the filename is a combination of his userName and a filename of his choosing 'aansari-agingReport.xls' then the user can update any file he chooses. He/she can also keep as many different files as he likes on the system.


Actually there are different ways of doing it. In your case he doesn't even need to synchronize the thing. But may be the same user has two different files with the same file name but different location. Then it would be a problem.

- There should be a list of his files
- If he wants to update any, he should go for update
- If he wants to add new, he should go for add new (a new file is going to be added no matter if the file with the same name exist)
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Adeel Ansari:
In your case he doesn't even need to synchronize the thing.


Which was my original point. Synchronization and locking are a drain on performance. They should be avoided if the same results can be achieved in other ways.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Correct. I will go for DB to store the images, as i suggested earlier. Its a best practice and these kinda issues doesnt really bother. But it might be something advance for OP.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lock a directory... need ideas