• Post Reply Bookmark Topic Watch Topic
  • New Topic

get the calling object from within method code  RSS feed

 
Dan Ludwig
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i'm implementing a sequence block behind a session facade to serve as a primary key generator for other entities (imitating a database auto-increment column). i want to name each sequence according to the entity ejb it serves, without passing a string if possible.

i have the ejbCreate method of each entity bean obtain an instance of the sequence's session facade bean, which issues it a new integral primary key. the session ejb looks up the correct number based on the name of the sequence, so the sequence name needs to be passed from the client entity that is requesting the new primary key doesn't it?

i thought i remembered reading somewhere that a method's client (the class calling the method) can be looked up and referenced without passing the client itself as an argument. if this is possible, i'd like to do that instead, i.e.:

1.) EntityEJB calls the getPrimaryKey() method on a Stateless Session EJB.

2.) getPrimaryKey() *somehow* retrieves a reference to the EntityEJB, even though an instance of it was not passed as an argument.

3.) getPrimaryKey uses this *magic* reference to call getClass().getName() and get the EntityEJB's class name as a String.

4.) getPrimaryKey uses this String to either create a new sequence or findByPrimaryKey and return the next number in the sequence.

i'd rather not hard-code the String name of each sequence into its respective EntityEJB if this can be retrieved automatically. i'd also like to avoid doing a getPrimaryKey(this), since the facade bean will work with different entities and i don't want my entities to polymorph at all.

if anyone knows how i can get the name of the class that called a method, from within the method itself, without passing an instance of the class as an argument, please let me know. Thanks!
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not aware that this can be done. As you know, EJB keeps track of the caller's identity, but that doesn't extend itself to the initiating component. Keep in mind that there may not actually be an initiating component since EJB may be called from non-object-oriented languages bound through Corba/IDL. Sorry for the bad news.
 
Dan Ludwig
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the tips,

i actually did find a way to do this, although i admit it's a bit crude:

try {
throw new Throwable();
} catch (Throwable t) {
StachTraceElement[] s = t.getStackTrace();
locate: for (int i=0; i<s.length; i++) {
if (!s.getClassName().equals(this.getClass().getName())) {
// this will execute when the calling class
// is found
break locate;
}
}
}

as long as i define a rule in my application that this method is always called from the client entity with the class name that i need, and no other classes, i think it'll work.

i'd like to hear some opinions on this, i'm sure there are at least a couple...

thanks!
 
Dan Ludwig
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... it's such a crude way to do it that i've abandoned this approach altogether. if you're reading this post, don't do what i did!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!