Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Is my singleton implementation a true singleton?

 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Getting in on the fun... Could this implementation provide a rock solid singleton class?

 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that produces a threadsafe Singleton implementation, and I've seen implementations like that in my company.

It is a little awkward if your Singleton constructor can throw an Exception. Java gives a confusing ClassNotFound error if there is an exception thrown from a static initializer in a class.
 
Ranch Hand
Posts: 262
4
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No idea what you mean by a rock solid singleton, but reflection attack is possible with your implementation.



The output of the above mentioned code is --

false
1741555911
128087567

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed that is true. Is there a way to avoid a reflection attack without using a security manager? I think I can think of a way to update Rico's implementation to stop your attack working, but I don't have a compiler to hand to test it.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike. J. Thompson wrote:Indeed that is true. Is there a way to avoid a reflection attack without using a security manager? I think I can think of a way to update Rico's implementation to stop your attack working, but I don't have a compiler to hand to test it.



Yes there is with ReflectPermisson and a proper policy file, but it's extremely rare to have to worry about such security considerations.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Informative
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rico Felix wrote:Getting in on the fun... Could this implementation provide a rock solid singleton class?


Sure. However, the need for such an idiom (I believe it's called "lazy initialization holder") is extremely rare.
It's also quite possible that it will be made redundant by future releases (several others already have been), in which case what you'll be left with is a piece of arcane code.

That's assuming you even need a singleton to begin with.

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic