How can I control public URLs using the java.net.URL class in a Java program?

I am working on a large (many classes) java project, and I have its source code, but most classes are dynamically created or loaded via ClassLoaders. In any case, I would like to be able to "override" the java.net.URL class so that I can track calls to open the url. I can’t use sniffer because the content is SSL encrypted. I tried to extend java.net.URL, but this did not work because it is the last class. I also opened the source for java.net.URL and changed it and was able to create it successfully, now how can I force the java loader to load a modified copy of java.net.URL by default instead of the original one? Any suggestions are welcome! Thanks in advance.

+3
source share
4 answers

The parameter would be to use AspectJ and embed your extension code in the URL class instead of its extension. This way, you don’t need to modify any of the source sources, and you can still “extend” the final class. Of course, the disadvantage is that you add another dependency to your project.

If you haven't worked with AOP yet, you can find a brief introduction to AOP on my blog: http://whatiscomingtomyhead.wordpress.com/2010/02/06/aspect-oriented-programming-an-introduction/

+3
source

URL, . JVM , IMHO .

, URLStreamHandlerFactory URL.setURLStreamHandlerFactory(). , URL- , .

, ; URLStreamHandlerFactories - 1/. (, Tomcat), , , SOL.

+2

AspectJ? pointcut URL , , URL.

+1

API, ( ), VM , . Sun VM -Xbootclasspath/p: , .

, URL-, ProxySelector. URLConnection , URL- ProxySelector.select(URI uri) - . , . ProxySelector ProxySelector.getDefault(), URI, select.

0

Source: https://habr.com/ru/post/1731581/


All Articles