icvg
icvg

Reputation: 197

AspectJ & controlling calls in other jars

POST 1: theoretical question

We use some software, that is actually a Web Module with its own Tomcat and shell scripts for controlling it. It has also a Plugin System, which allows you to upload a .jar file with a certain structure to add new functionality to the Application.

Question: I would like to control&actually change the responses to different calls in the main system/application (not in my jar). Could I use AspectJ to do that? Why or why not? What would be the other general possibilities, except changing the code of the Main Application.


POST 2: the try

I tried to do it this way (in Eclipse):

Questions:

  1. In the exported aspect-jar, there are only the .class files of the AspectJ project, no .class files for the INPATH-Jar. Should there be other classes, from the imported INPATH-jar?

  2. In the exported aspect-jar there is no jar with the aspectj-runtime (aspectj-rt.jar). Should it be there, or how to configure the virtual machine to have it?

Upvotes: 0

Views: 961

Answers (1)

kriegaex
kriegaex

Reputation: 67297

Yes, why not? If you could extend your question and explain (maybe with an example) which actors and actions there are in the system, we might be able to help you in a more conrete fashion. But basically I see no problem. The JAR modules might be loaded dynamically, but if you know which calls in the Tomcat app you want to intercept, you can easily instrument them either statically by reweaving the existing classes or dynamically via LTW (load-time weaving) during JVM start-up. There is no need to touch your uploaded JAR modules, which is, as I understand you, what you want to avoid.

You probably want to weave your main application's target classes via

  • execution(<methodsToBeChecked>) pointcut in combination with
  • around() advice.

The other details depend on your specific use case, the package, class and method names, parameters etc. The around advice can do one or several of the following things:

  • determine caller,
  • check call paramaters,
  • manipulate call parameters,
  • call original target with original or changed parameters,
  • alternatively not perform the original call at all,
  • pass back the result of the original call to the caller,
  • pass back a manipulated version of the result to the caller,
  • pass any synthetic value with the correct return type to the caller,
  • catch exceptions raised by the original call,
  • throw your own exceptions
  • etc.

Your fantasy (and AspectJ's few limitations) are the limit. :-)

Upvotes: 1

Related Questions