问题
I want to write an application that when deployed on a server discovers some classes on that server and their methods and exposé a list of it to a client (via web service, servlet or RMI).
the client can then choose a method and activate it (with reflection).
Problem is that my application is not in the same context of the other applications on the server so I can't activate their methods (I don't have access to their classloader).
I have a few ideas but not sure if they are feasible:
Create a jar and make all applications on the server know it (via Manifes file or by putting it in the server lib) - Problem is ,that way I can't exposé an interface to the client (maybe I can through RMI?)
Create a WAR and link all applications to this WAR, so when they startup they load it (like linking to a jar) - as far as I know its not possible.
Is there a classloader that knows all classes? is there a way to get it?
How does profilers do it?
Any idea will be welcomed.
回答1:
3) AFAIK application servers generally have a class loader hierarchy and thus there's no single class loader that has access to all classes. In fact, you often need to load multiple instances of the same class, e.g. when deploying independent applications that use static class variables or different versions of a class or a library. Here's a short description of how JBoss does it.
4) Most likely profilers use the JVMTI.
However, you should keep in mind that it is not advisable to expose every class and every object due to security, stability and performance reasons. Should the user be able to invoke methods on internal classes or basic classes like String
? Should the user be able to call System.exit()
? I'd say no, and thus you need to define some method of determining what to expose. Most likely a much simpler solution (dedicated services) would meet your use case requirements (you didn't state them so that's just a guess).
来源:https://stackoverflow.com/questions/8326653/sharing-java-objects-between-applications