aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Generated Cookie Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Generated Cookie" Watch "Generated Cookie" New topic
Author

Generated Cookie

Fernando Franzini
Ranch Hand

Joined: Jan 09, 2009
Posts: 486
    
    2

Hi Ranchers

I would like to know wich are the cookie strategies generation ? I've read here about "System.nanoTime()" but....there are another options ?
Regards.


Fernando Franzini - Java Blog
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5406
    
  13

Hi Fernando,

I used System.nanoTime(), but of course you can use the Random class and use the nextLong() method. Or you can use both and multiply both results with each other. But i think System.nanoTime() should do the job. You can always add something to your choices.txt that generation cookie logic can be refined (extended) in a future release...

Kind regards,
Roel


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

Joined: Dec 28, 2007
Posts: 2266
    
    3

Howdy, Fernando!

You may also want to take a look at the SecureRandom class.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Fernando Franzini
Ranch Hand

Joined: Jan 09, 2009
Posts: 486
    
    2

Thanks Roel and Roberto.

Another question about it: What should I do when someone try lock an ID that already looked ?? My inteface only throw RecordNotFoundException ? Should I throw one database runtime exception ? What did you do Roel ??
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

Hey, Fernando!

What should I do when someone try lock an ID that already looked ??


Ah... that's where the locking mechanism comes in! This is what I have in my instructions.html file:

Any attempt to lock a resource that is already locked should cause the current thread to give up the CPU, consuming no CPU cycles until the desired resource becomes available.


So... the client trying to lock a record that is already locked should wait until that particular record is available. After getting its lock, then the update/delete should proceed normaly.
Fernando Franzini
Ranch Hand

Joined: Jan 09, 2009
Posts: 486
    
    2

OHH MY GODDD !!! This is exactly what my assignment said !!! Wowwwwwwwww......sorry
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5406
    
  13

Hi Fernando,

I would certainly not throw any exception, otherwise you will fail automatically, like Laura Pecoraro. Here you can find some information about her mistake and how you can/should deal with it.

Kind regards,
Roel
Fernando Franzini
Ranch Hand

Joined: Jan 09, 2009
Posts: 486
    
    2

Thanks again Roel.... I'm sure that I wont comit the Laura mistake.
About SecurityException in unlock method. Can I throw it if passed a recordNumber that wasn't locked or if passed a wrong cookie value ?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5406
    
  13

Hi Fernando,

You can certainly throw it if a wrong cookie value is passed, because I believe that is exactly why the SecurityException exists. But a record that is not locked, is a subtle difference.
In my assignment I had no SecurityException, so I throw in both situations an IllegalStateException.

In this thread someone asked the same question like you.

Kind regards,
Roel
Fernando Franzini
Ranch Hand

Joined: Jan 09, 2009
Posts: 486
    
    2

Hi folks

I'm writting choice.txt and I need put 'why' I choose System.nanoTime() to generate locke cookie.
I'm actually did cause you guys suggest for me.... And it really work !
Could you help me please ?
Regards.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

You can just say that this approach is secure enough to distinguish one client from the others.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generated Cookie