This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 484
    
    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: 4907
    
  10

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: 2246

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: 484
    
    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: 2246

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: 484
    
    2

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

Joined: Jul 19, 2004
Posts: 4907
    
  10

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: 484
    
    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: 4907
    
  10

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: 484
    
    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: 2246

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
 
Similar Threads
Not able to post
Cookies using Jsp
Famous Quotes
How to set JSESSIONID's maxAge?
WA #1.....word association