Hi to all, I\'m tring to understand how create and use dynamic proxies in JAVA.
ID: 3621461 • Letter: H
Question
Hi to all,I'm tring to understand how create and use dynamic proxies in JAVA. I'm using Eclipse for write the code and as school exercice I must create proxies with the JAVA library as this link: http://commons.apache.org/proxy/ .
I read that proxies can make classes that can be an interface for the communication between other classes. Unfortunately I do not undersood how start create the first proxy class. I've made a really banal program with the classes main, client and server. The main initializes tho objects, one declared as client and one as server. Calls on the client object a method that generate a integer random number, catchs it and pass it to the server object, invoking a method that return the banal operation number*number, where number is the numbr passed to the server.
At this point, I would create communication between these classes by a proxy object.
I've seen that in commons proxy library it can have 3 kind of proxies: delegator, invoker and interceptor. For starting I've taken the definition of the method (one of the two) that permits to declare a delegator proxy.
createDelegatorProxy(ClassLoader classLoader, ObjectProvider delegateProvider, Class[] proxyClasses)
So I've created a ProxyFactory object and I'm tring to call on it createDelegatorProxy method, but I haven't idea of what ClassLoader, ObjectProvider and Class[] (array) arguments pass to it.
So I start think that there are some things that I cannot see under the proxy logic and search examples online for understand the usage of this library, but I do not found anything.
I really need an example that shows me all steps I must do to create a working proxy in this library. After that I think to be ready for do exercices and tests to study in deep this argument.
Eventually for others questions I wll open new topics.
Thanks very much.
Explanation / Answer
Dear... Am giving you the sample code for Create proxies with apache commons proxy library: package org.apache.commons.proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public interface ProxyFactory public class ProxyFactory { public boolean canProxy( Class[] proxyClasses ) { for( int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.