Knowing the identity of the calling application.
I have a static object which can be shared between two applications, similar to a static object that can be shared between two applets. Now if the object has a method say myMethod() how can i know which application called the method. To be more clear.
Let my object be MyObject.
Now the static instance for the object would be
static MyObject myObject = new MyObject().
I have two applets appletA and appletB;
Suppose appletA or appletB were to call myObject.myMethod(), how will i know that appletA called the method or appletB called the method.
I need to know this for applications not applets if there is a difference in solution for both.
Any help would be really very useful.
Thanks
Rishab
[763 byte] By [
rishab] at [2007-9-26 7:31:55]

One way is to have myMethod() take a parameter and have appletA and appletB pass it different parameters.
It sounds like you're not using RMI (you have only one JVM) so you needn't be too worried about passing object reference. My point is each calling object (or app as you put it) could have a class variable and accessor to identify it. Then, it could call and pass itself as the caller:
myObject.myMethod(this);
and within myMethod:
public ... myMethod(Object o) {
String ID = o.getID();
if (ID.equals("client A")) {...}
}
Variable ID could be a native type as well in order to use if (ID == 3346) {...}.
Do you think you could CALL A 'PRINT STACK STACE' FROM AN EXCEPTION OBJECT AND PARSE THE STRING,a and get the caller's classname? (Smart eh? 8 bucks buddy )
Smart, but dirty, tedious and slow.
Actually there is more to the problem. I have a static object reference in myClass. Okay now when ever the application calls myObject.myMethod() i need to update the reference to the calling application. I cannot change the method signature of myMethod() to pass the reference of the calling application, because this will be called by someone else. Also i forgot to write this , myMethod is a static method. So the only way this can be called are in the following ways.
class myApplication {
myApplication() {
myObject.myMethod();
}
}
myApplication b = new myApplication();
myApplication c = new myApplication();
Now in myMethod i need to know the following
static void myMethod() {
// Caller myApplication b or myApplication C ?
}
I am not sure how to use, this but the Security Packages have some way to do this i think.
I have tried the following, using two applets.
System.getRuntime() in myApplet 1 or myApplet 2 returns the same object, so no use.
The class loader used for both the applets is the system class loader so no use.
One question, taking the case of two applets, they are running on the same browser VM. But wouldnt they be running in two different threads. The currentThread for both still is the same.
Yes RMI is out of question, cause i am not supposed to use the RMI package.
Thanks for the help so far.
regards,
Rishab
Try identifying the thread using something in the thread class. That will tell you the thread that called you and unless your applications share threads, you should have the info you seek.
There is a way, I'm not sure if it is suitable for you, as I don't know of the overhead in terms of performance in a production environment.
Your class extends the java.lang.SecurityManager class.
Within your static method, do the following:
public static void test() {
MyApplication myApp = new MyApplication();
// The class Context contains the hierarchy of calling objects
Class[] classContext = myApp.getClassContext();
// The first element[0] is the current class
// The second element[1] is the calling class
if(classContext.length>1) {
System.out.println("Calling class: "+classContext[1].getName());
}
// Do whatever processing needs to be done.
//Reset object to null for GC
myApp=null;
}
Good luck.
Regards,
Rich
use JAAS (Java Authentication and Authorization Service) which you can download from java.sun.com (it's now included in jdk 1.4) and then you can use the Subject and Principal classes to associate an identity with each calling thread.