How to determine in what context the Java Applet works without passing an identifier?

I am part of a team that is developing a fairly large Swing Java applet. Most of our code is legacy, and there are tons of links to singleton. We grouped all of them into one single "Application Context". Now we need to create a way to separate the general context (common to all displayed applets) and not the general context (specific to each applet currently displayed).

However, we do not have an identifier in each of the places that access singleton, and we do not want to distribute the ID in all places. What is the easiest way to determine in which context of the applet we are launching? (I tried to mess with class loaders, thread groups, thread IDs ... so far I have not been able to find anything that would allow me to identify the origin of the call).

+6
java swing applet
source share
3 answers

Singlets are evil, what do you expect ?;)

Perhaps the most complete approach would be to load most of the applet into another classloader (use java.net.URLClassLoader.newInstance). Then use WeakHashMap to associate the class loader with the applet. If you could divide most of the code into a regular classloader (as the parent of each classloader for an applet) and into the normal applet codebase, this would be faster, but more work.

Other hacks:

If you have access to any component, you can reuse Component.getParent or SwingUtilities.getRoot.

If you are in the instance stream for each applet, then you can configure ThreadLocal.

From EDT you can read the current event from the queue (java.awt.EventQueue.getCurrentEvent ()) and possibly find the component from this. Alternatively, click EventQueue with the dispatchEvent method overridden.

+2
source share

If I understand you correctly, the idea is to get another "singleton" object for each calling object or "context". One thing you can do is create a global thread variable where you write the identifier of the current context. (This can be done using AOP.) Then, in a single-user getter, the context identifier is retrieved from the local stream to use as the key to the correct "singleton" instance for the calling context.

With regard to AOP, there should be no problem using it in applets, because, depending on your point abbreviations, the tips are woven at compile time, and the JAR is added to the runtime dependencies. Therefore, no special evidence of AOP should remain at runtime.

0
source share

@Hugo regarding threadlocal:

I thought about this solution. However, from the experiments, I found two problems with this approach:

  • A common problem (connecting to a server, etc.) are problematic. This can be solved by paying special attention to this thread (they are all under my control and are largely isolated from outdated code).
  • The EDT thread is used for all applets. I could not find a way to create a new EDT stream for each applet. This means that threadlocal for EDT will be passed through applets. This I do not know how to solve. Suggestions?
0
source share

All Articles