At this talk we will start from the basics and come shortly to DynamicProxies, generated type-save DynamicObjectAdapterBuilder and more. We will have a deep dive to this pattern group , and I am sure you will like it ;-)
This talk is based on the german book "Dynamic Proxies" written from Dr. Heinz Kabutz and me.
4. @SvenRuppert
has been coding java since 1996
Projects in the field of:
•Automobile-industry
•Energy
•Finance / Leasing
•Space- Satellit-
•Government / UN / World-bank
Where?
•Europe
•Asia - from India up to Malaysia
3
5. Why a Proxy ?
4
@SvenRuppert
Proxies you can use them for :
generic parts inside your application
hiding technical stuff
decouple parts of your applications
6. Why a Proxy ?
5
@SvenRuppert
but it must be easy to use like…
7. Why a Proxy ?
5
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
but it must be easy to use like…
8. Why a Proxy ?
5
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
final InnerDemoInterface demoLogic =
but it must be easy to use like…
9. Why a Proxy ?
5
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
final InnerDemoInterface demoLogic =
ProxyBuilder.createBuilder(InnerDemoInterface.class, original)
but it must be easy to use like…
10. Why a Proxy ?
5
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
final InnerDemoInterface demoLogic =
ProxyBuilder.createBuilder(InnerDemoInterface.class, original)
.addLogging()
but it must be easy to use like…
11. Why a Proxy ?
5
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
final InnerDemoInterface demoLogic =
ProxyBuilder.createBuilder(InnerDemoInterface.class, original)
.addLogging()
.addMetrics()
but it must be easy to use like…
12. Why a Proxy ?
5
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
final InnerDemoInterface demoLogic =
ProxyBuilder.createBuilder(InnerDemoInterface.class, original)
.addLogging()
.addMetrics()
.addSecurityRule(() -> true)
but it must be easy to use like…
13. Why a Proxy ?
5
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
final InnerDemoInterface demoLogic =
ProxyBuilder.createBuilder(InnerDemoInterface.class, original)
.addLogging()
.addMetrics()
.addSecurityRule(() -> true)
.addSecurityRule(() -> true)
but it must be easy to use like…
14. Why a Proxy ?
5
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
final InnerDemoInterface demoLogic =
ProxyBuilder.createBuilder(InnerDemoInterface.class, original)
.addLogging()
.addMetrics()
.addSecurityRule(() -> true)
.addSecurityRule(() -> true)
.build();
but it must be easy to use like…
16. Why a Proxy ?
6
@SvenRuppert
@Inject
@Proxy(virtual = true, metrics = true)
Service service;
17. Adapter v Proxy - Pattern Hello World
Adapter - called Wrapper
maybe : Use an Adapter if you have to use an incompatible
interface between two systems.
In detail I am using/talking about an Adapter with Delegator.
7
@SvenRuppert
19. Adapter v Proxy - Pattern Hello World
public class LegacyWorker {
public void writeTheStrings(String[] strArray){
Arrays.stream(strArray).forEach(System.out::println);
}
}
8
@SvenRuppert
20. Adapter v Proxy - Pattern Hello World
public class LegacyWorker {
public void writeTheStrings(String[] strArray){
Arrays.stream(strArray).forEach(System.out::println);
}
}
8
public interface WorkerAdapter {
public void workOnSomething(List<String> stringListe);
}
@SvenRuppert
21. Adapter v Proxy - Pattern Hello World
public class LegacyWorker {
public void writeTheStrings(String[] strArray){
Arrays.stream(strArray).forEach(System.out::println);
}
}
8
public interface WorkerAdapter {
public void workOnSomething(List<String> stringListe);
}
public class Adapter implements WorkerAdapter {
private LegacyWorker worker = new LegacyWorker();
@Override
public void workOnSomething(List<String> stringListe) {
final String[] strings = stringListe.toArray(new String[0]);
worker.writeTheStrings(strings);
}
}
@SvenRuppert
22. Adapter v Proxy - Pattern Hello World
public class LegacyWorker {
public void writeTheStrings(String[] strArray){
Arrays.stream(strArray).forEach(System.out::println);
}
}
8
public interface WorkerAdapter {
public void workOnSomething(List<String> stringListe);
}
public class Adapter implements WorkerAdapter {
private LegacyWorker worker = new LegacyWorker();
@Override
public void workOnSomething(List<String> stringListe) {
final String[] strings = stringListe.toArray(new String[0]);
worker.writeTheStrings(strings);
}
}
@SvenRuppert
23. Adapter v Proxy - Pattern Hello World
public class LegacyWorker {
public void writeTheStrings(String[] strArray){
Arrays.stream(strArray).forEach(System.out::println);
}
}
8
public interface WorkerAdapter {
public void workOnSomething(List<String> stringListe);
}
public class Adapter implements WorkerAdapter {
private LegacyWorker worker = new LegacyWorker();
@Override
public void workOnSomething(List<String> stringListe) {
final String[] strings = stringListe.toArray(new String[0]);
worker.writeTheStrings(strings);
}
}
@SvenRuppert
24. Adapter v Proxy - Pattern Hello World
public class LegacyWorker {
public void writeTheStrings(String[] strArray){
Arrays.stream(strArray).forEach(System.out::println);
}
}
8
public interface WorkerAdapter {
public void workOnSomething(List<String> stringListe);
}
public class Adapter implements WorkerAdapter {
private LegacyWorker worker = new LegacyWorker();
@Override
public void workOnSomething(List<String> stringListe) {
final String[] strings = stringListe.toArray(new String[0]);
worker.writeTheStrings(strings);
}
}
@SvenRuppert
25. Adapter v Proxy - Pattern Hello World
public class LegacyWorker {
public void writeTheStrings(String[] strArray){
Arrays.stream(strArray).forEach(System.out::println);
}
}
8
public interface WorkerAdapter {
public void workOnSomething(List<String> stringListe);
}
public class Adapter implements WorkerAdapter {
private LegacyWorker worker = new LegacyWorker();
@Override
public void workOnSomething(List<String> stringListe) {
final String[] strings = stringListe.toArray(new String[0]);
worker.writeTheStrings(strings);
}
}
@SvenRuppert
26. Adapter v Proxy - Pattern Hello World
public class Main {
public static void main(String[] args) {
final ArrayList<String> strings = new ArrayList<>();
Collections.addAll(strings, "A","B","C","D");
new Adapter().workOnSomething(strings);
}
}
9
@SvenRuppert
27. Adapter v Proxy - Pattern Hello World
public class Main {
public static void main(String[] args) {
final ArrayList<String> strings = new ArrayList<>();
Collections.addAll(strings, "A","B","C","D");
new Adapter().workOnSomething(strings);
}
}
9
• No need to know the LegacyWorker
@SvenRuppert
28. Adapter v Proxy - Pattern Hello World
public class Main {
public static void main(String[] args) {
final ArrayList<String> strings = new ArrayList<>();
Collections.addAll(strings, "A","B","C","D");
new Adapter().workOnSomething(strings);
}
}
9
• No need to know the LegacyWorker
• No inheritance between LegacyWorker and Adapter
@SvenRuppert
29. Adapter v Proxy - Pattern Hello World
public class Main {
public static void main(String[] args) {
final ArrayList<String> strings = new ArrayList<>();
Collections.addAll(strings, "A","B","C","D");
new Adapter().workOnSomething(strings);
}
}
9
• No need to know the LegacyWorker
• No inheritance between LegacyWorker and Adapter
• only a transformation of an interface
@SvenRuppert
30. Adapter v Proxy - Pattern Hello World
public class Main {
public static void main(String[] args) {
final ArrayList<String> strings = new ArrayList<>();
Collections.addAll(strings, "A","B","C","D");
new Adapter().workOnSomething(strings);
}
}
9
• No need to know the LegacyWorker
• No inheritance between LegacyWorker and Adapter
• only a transformation of an interface
• same creation time for Delegator and Adapter
@SvenRuppert
31. Proxy - the easiest version
10
@SvenRuppert
We need a few steps to come from an Adapter with
Delegator to a Proxy
32. Proxy - the easiest version
10
• We need to know the LegacyWorker
@SvenRuppert
We need a few steps to come from an Adapter with
Delegator to a Proxy
33. Proxy - the easiest version
10
• We need to know the LegacyWorker
• Inheritance between LegacyWorker and Adapter
@SvenRuppert
We need a few steps to come from an Adapter with
Delegator to a Proxy
34. Proxy - the easiest version
10
• We need to know the LegacyWorker
• Inheritance between LegacyWorker and Adapter
• do not use it like an Adapter !
@SvenRuppert
We need a few steps to come from an Adapter with
Delegator to a Proxy
35. Proxy - the easiest version
10
• We need to know the LegacyWorker
• Inheritance between LegacyWorker and Adapter
• do not use it like an Adapter !
@SvenRuppert
We need a few steps to come from an Adapter with
Delegator to a Proxy
please show the code
36. Proxy - the easiest version - (Delegator)
11
@SvenRuppert
37. Proxy - the easiest version - (Delegator)
public interface Service {
String work(String txt);
}
11
@SvenRuppert
38. Proxy - the easiest version - (Delegator)
public interface Service {
String work(String txt);
}
11
@SvenRuppert
public class ServiceImpl implements Service {
public String work(String txt) {
return "ServiceImpl - " + txt;
}
}
39. Proxy - the easiest version - (Delegator)
public interface Service {
String work(String txt);
}
11
@SvenRuppert
public class ServiceImpl implements Service {
public String work(String txt) {
return "ServiceImpl - " + txt;
}
}
public class ServiceProxy implements Service {
private Service service = new ServiceImpl();
public String work(String txt) { return service.work(txt); }
}
41. Security Proxy
12
@SvenRuppert
public class ServiceProxy implements Service {
private Service service = new ServiceImpl();
public String work(String txt) { return service.work(txt); }
}
42. Security Proxy
12
@SvenRuppert
public class ServiceProxy implements Service {
private Service service = new ServiceImpl();
public String work(String txt) { return service.work(txt); }
}
What could we
change now ?
43. Security Proxy
12
@SvenRuppert
public class ServiceProxy implements Service {
private Service service = new ServiceImpl();
public String work(String txt) { return service.work(txt); }
}
What could we
change now ?
SecurityProxy:
execute only if it is allowed
44. Security Proxy
12
@SvenRuppert
public class ServiceProxy implements Service {
private Service service = new ServiceImpl();
public String work(String txt) { return service.work(txt); }
}
What could we
change now ?
SecurityProxy:
execute only if it is allowed
public class ServiceSecurityProxy implements Service {
private Service service = new ServiceImpl();
private String code; //who will set this ?
public String work(String txt) {
if(code.equals(„hoppel")) { return service.work(txt); }
else { return "nooooop"; }
}
}
45. Security Proxy
12
@SvenRuppert
public class ServiceProxy implements Service {
private Service service = new ServiceImpl();
public String work(String txt) { return service.work(txt); }
}
What could we
change now ?
SecurityProxy:
execute only if it is allowed
public class ServiceSecurityProxy implements Service {
private Service service = new ServiceImpl();
private String code; //who will set this ?
public String work(String txt) {
if(code.equals(„hoppel")) { return service.work(txt); }
else { return "nooooop"; }
}
}
47. Remote Proxy - JDK only
13
@SvenRuppert
public class ServiceProxy implements Service {
private Service service = new ServiceImpl();
public String work(String txt) { return service.work(txt); }
}
48. Remote Proxy - JDK only
13
@SvenRuppert
public class ServiceProxy implements Service {
private Service service = new ServiceImpl();
public String work(String txt) { return service.work(txt); }
}
What could we
change now ?
49. Remote Proxy - JDK only
13
@SvenRuppert
public class ServiceProxy implements Service {
private Service service = new ServiceImpl();
public String work(String txt) { return service.work(txt); }
}
What could we
change now ?
RemoteProxy:
execute outside of my process
50. Remote Proxy - JDK only
13
@SvenRuppert
public class ServiceProxy implements Service {
private Service service = new ServiceImpl();
public String work(String txt) { return service.work(txt); }
}
What could we
change now ?
RemoteProxy:
execute outside of my process
Service proxy = new ServiceRemoteProxy();
String hello = proxy.work(„Hello");
51. Remote Proxy - JDK only
13
@SvenRuppert
public class ServiceProxy implements Service {
private Service service = new ServiceImpl();
public String work(String txt) { return service.work(txt); }
}
What could we
change now ?
RemoteProxy:
execute outside of my process
Service proxy = new ServiceRemoteProxy();
String hello = proxy.work(„Hello");
52. Remote Proxy - JDK only
13
@SvenRuppert
public class ServiceProxy implements Service {
private Service service = new ServiceImpl();
public String work(String txt) { return service.work(txt); }
}
What could we
change now ?
RemoteProxy:
execute outside of my process
Service proxy = new ServiceRemoteProxy();
String hello = proxy.work(„Hello");
some work needed
77. Remote Proxy - JDK only
20
@SvenRuppert
Proxy
Remote
ServiceInterface
Interface
remote communication
client server
78. Remote Proxy - JDK only
20
@SvenRuppert
Proxy
Remote
ServiceInterface
Interface
remote communication
client server
79. Remote Proxy - JDK only
21
@SvenRuppert
public class ServiceRemoteProxy implements Service {
private URL url;
private Service realSubject;
final String namespaceURI = "http://rapidpm.org/";
final String localPart = "ServiceImplService";
public ServiceRemoteProxy() {
try {
url = new URL("http://localhost:9999/ws/service?wsdl");
QName qname = new QName(namespaceURI, localPart);
javax.xml.ws.Service service = javax.xml.ws.Service.create(url, qname);
realSubject = service.getPort(Service.class);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public String work(String txt){
return realSubject.work(txt);
}
}
80. Remote Proxy - JDK only
21
@SvenRuppert
public class ServiceRemoteProxy implements Service {
private URL url;
private Service realSubject;
final String namespaceURI = "http://rapidpm.org/";
final String localPart = "ServiceImplService";
public ServiceRemoteProxy() {
try {
url = new URL("http://localhost:9999/ws/service?wsdl");
QName qname = new QName(namespaceURI, localPart);
javax.xml.ws.Service service = javax.xml.ws.Service.create(url, qname);
realSubject = service.getPort(Service.class);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public String work(String txt){
return realSubject.work(txt);
}
}
81. Remote Proxy - JDK only
21
@SvenRuppert
public class ServiceRemoteProxy implements Service {
private URL url;
private Service realSubject;
final String namespaceURI = "http://rapidpm.org/";
final String localPart = "ServiceImplService";
public ServiceRemoteProxy() {
try {
url = new URL("http://localhost:9999/ws/service?wsdl");
QName qname = new QName(namespaceURI, localPart);
javax.xml.ws.Service service = javax.xml.ws.Service.create(url, qname);
realSubject = service.getPort(Service.class);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public String work(String txt){
return realSubject.work(txt);
}
}
82. Remote Proxy - JDK only
22
@SvenRuppert
Proxy
Remote
ServiceInterface
Interface
remote communication
client server
83. Remote Proxy - JDK only
22
@SvenRuppert
Proxy
Remote
ServiceInterface
Interface
remote communication
client server
• remote communication
84. Remote Proxy - JDK only
22
@SvenRuppert
Proxy
Remote
ServiceInterface
Interface
remote communication
client server
• remote communication
• most of this is technology based work
85. Remote Proxy - JDK only
22
@SvenRuppert
Proxy
Remote
ServiceInterface
Interface
remote communication
client server
• remote communication
• most of this is technology based work
• goal: developer will see no remote
communication
87. Virtual Proxy
23
@SvenRuppert
public class ServiceProxy implements Service {
private Service service = new ServiceImpl();
public String work(String txt) { return service.work(txt); }
}
88. Virtual Proxy
23
@SvenRuppert
public class ServiceProxy implements Service {
private Service service = new ServiceImpl();
public String work(String txt) { return service.work(txt); }
}
What could we
change now ?
89. Virtual Proxy
23
@SvenRuppert
public class ServiceProxy implements Service {
private Service service = new ServiceImpl();
public String work(String txt) { return service.work(txt); }
}
What could we
change now ?
Virtual Proxy:
create the Delegator later
90. Virtual Proxy
23
@SvenRuppert
public class ServiceProxy implements Service {
private Service service = new ServiceImpl();
public String work(String txt) { return service.work(txt); }
}
What could we
change now ?
Virtual Proxy:
create the Delegator later
public class VirtualService implements Service {
private Service service = null;
public String work(String txt) {
if(service == null) { service = new ServiceImpl(); }
return service.work(txt);
}
}
91. Virtual Proxy
23
@SvenRuppert
public class ServiceProxy implements Service {
private Service service = new ServiceImpl();
public String work(String txt) { return service.work(txt); }
}
What could we
change now ?
Virtual Proxy:
create the Delegator later
public class VirtualService implements Service {
private Service service = null;
public String work(String txt) {
if(service == null) { service = new ServiceImpl(); }
return service.work(txt);
}
}
92. Virtual Proxy
23
@SvenRuppert
public class ServiceProxy implements Service {
private Service service = new ServiceImpl();
public String work(String txt) { return service.work(txt); }
}
What could we
change now ?
Virtual Proxy:
create the Delegator later
public class VirtualService implements Service {
private Service service = null;
public String work(String txt) {
if(service == null) { service = new ServiceImpl(); }
return service.work(txt);
}
}
93. Virtual Proxy
24
@SvenRuppert
public class VirtualService implements Service {
private Service service = null;
public String work(String txt) {
if(service == null) { service = new ServiceImpl(); }
return service.work(txt);
}
}
94. Virtual Proxy
24
@SvenRuppert
public class VirtualService implements Service {
private Service service = null;
public String work(String txt) {
if(service == null) { service = new ServiceImpl(); }
return service.work(txt);
}
}
95. Virtual Proxy
24
@SvenRuppert
public class VirtualService implements Service {
private Service service = null;
public String work(String txt) {
if(service == null) { service = new ServiceImpl(); }
return service.work(txt);
}
}
This is NOT ThreadSave
96. Virtual Proxy
24
@SvenRuppert
public class VirtualService implements Service {
private Service service = null;
public String work(String txt) {
if(service == null) { service = new ServiceImpl(); }
return service.work(txt);
}
}
This is NOT ThreadSave
97. Virtual Proxy
24
@SvenRuppert
public class VirtualService implements Service {
private Service service = null;
public String work(String txt) {
if(service == null) { service = new ServiceImpl(); }
return service.work(txt);
}
}
This is NOT ThreadSave
fixed decision for
an implementation
98. Virtual Proxy
24
@SvenRuppert
public class VirtualService implements Service {
private Service service = null;
public String work(String txt) {
if(service == null) { service = new ServiceImpl(); }
return service.work(txt);
}
}
This is NOT ThreadSave
fixed decision for
an implementation
99. Virtual Proxy
24
@SvenRuppert
public class VirtualService implements Service {
private Service service = null;
public String work(String txt) {
if(service == null) { service = new ServiceImpl(); }
return service.work(txt);
}
}
This is NOT ThreadSave
fixed decision for
an implementation
how to combine it with
a FactoryPattern ?
104. Virtual Proxy
25
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }
public interface ServiceFactory {
Service createInstance();
}
public interface ServiceStrategyFactory {
Service realSubject(ServiceFactory factory);
}
105. Virtual Proxy - Not - ThreadSave
26
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }
106. Virtual Proxy - Not - ThreadSave
26
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }
107. Virtual Proxy - Not - ThreadSave
26
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }
private ServiceFactory serviceFactory = ServiceImpl::new;
108. Virtual Proxy - Not - ThreadSave
26
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }
private ServiceFactory serviceFactory = ServiceImpl::new;
109. Virtual Proxy - Not - ThreadSave
26
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }
public class ServiceStrategyFactoryImpl implements ServiceStrategyFactory {
Service realSubject;
public Service realSubject(final ServiceFactory factory) {
if (realSubject == null) {
realSubject = factory.createInstance();
}
return realSubject;
}
}
private ServiceFactory serviceFactory = ServiceImpl::new;
110. Virtual Proxy - Not - ThreadSave
27
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }
public static class ServiceProxy implements Service {
private ServiceFactory serviceFactory = ServiceImpl::new;
private ServiceStrategyFactory strategyFactory = new ServiceStrategyFactoryImpl();
@Override
public String work(String txt) {
return strategyFactory.realSubject(serviceFactory).work(txt);
}
}
111. Virtual Proxy - Synchronized
28
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }
112. Virtual Proxy - Synchronized
28
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }
115. Virtual Proxy - Synchronized
28
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }
public class ServiceStrategyFactorySynchronized implements ServiceStrategyFactory {
Service realSubject;
public synchronized Service realSubject(final ServiceFactory factory) {
if (realSubject == null) {
realSubject = factory.createInstance();
}
return realSubject;
}
}
private ServiceFactory serviceFactory = ServiceImpl::new;
116. Virtual Proxy - Synchronized
28
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }
public class ServiceStrategyFactorySynchronized implements ServiceStrategyFactory {
Service realSubject;
public synchronized Service realSubject(final ServiceFactory factory) {
if (realSubject == null) {
realSubject = factory.createInstance();
}
return realSubject;
}
}
private ServiceFactory serviceFactory = ServiceImpl::new;
This is ThreadSave
117. Combining Proxies
29
@SvenRuppert
Security Proxy Virtual ProxyRemote Proxy
Security ProxyVirtual ProxyRemote Proxy
Security ProxyVirtual Proxy Remote Proxy
VirtualProxies are mostly the last one
combining Proxies is easy
SecurityProxies are mostly the first one ?
118. Combining Proxies
29
@SvenRuppert
Security Proxy Virtual ProxyRemote Proxy
Security ProxyVirtual ProxyRemote Proxy
Security ProxyVirtual Proxy Remote Proxy
VirtualProxies are mostly the last one
combining Proxies is easy
SecurityProxies are mostly the first one ?
119. Combining Proxies
29
@SvenRuppert
Security Proxy Virtual ProxyRemote Proxy
Security ProxyVirtual ProxyRemote Proxy
Security ProxyVirtual Proxy Remote Proxy
VirtualProxies are mostly the last one
combining Proxies is easy
SecurityProxies are mostly the first one ?
120. Combining Proxies
29
@SvenRuppert
Security Proxy Virtual ProxyRemote Proxy
Security ProxyVirtual ProxyRemote Proxy
Security ProxyVirtual Proxy Remote Proxy
VirtualProxies are mostly the last one
combining Proxies is easy
SecurityProxies are mostly the first one ?
121. Combining Proxies
29
@SvenRuppert
Security Proxy Virtual ProxyRemote Proxy
Security ProxyVirtual ProxyRemote Proxy
Security ProxyVirtual Proxy Remote Proxy
VirtualProxies are mostly the last one
combining Proxies is easy
SecurityProxies are mostly the first one ?
122. Combining Proxies - SecureVirtualProxy
30
@SvenRuppert
Security Proxy Virtual Proxy
public class VirtualProxy implements Service {
public VirtualProxy() { out.println("VirtualProxy " + now()); }
private Service service = null;
public String work(String txt) {
if(service == null) { service = new ServiceImpl(); }
return service.work(txt);
}
}
public class SecureVirtualProxy implements Service {
public SecureProxy() { out.println("SecureProxy " + now()); }
private Service service = new VirtualProxy();
//SNIPP….
public String work(String txt) {
if(code.equals("hoppel")) { return service.work(txt); }
return { return "nooooop"; }
}
}
123. Combining Proxies - SecureVirtualProxy
30
@SvenRuppert
Security Proxy Virtual Proxy
public class VirtualProxy implements Service {
public VirtualProxy() { out.println("VirtualProxy " + now()); }
private Service service = null;
public String work(String txt) {
if(service == null) { service = new ServiceImpl(); }
return service.work(txt);
}
}
public class SecureVirtualProxy implements Service {
public SecureProxy() { out.println("SecureProxy " + now()); }
private Service service = new VirtualProxy();
//SNIPP….
public String work(String txt) {
if(code.equals("hoppel")) { return service.work(txt); }
return { return "nooooop"; }
}
}
124. Combining Proxies - SecureVirtualProxy
30
@SvenRuppert
Security Proxy Virtual Proxy
public class VirtualProxy implements Service {
public VirtualProxy() { out.println("VirtualProxy " + now()); }
private Service service = null;
public String work(String txt) {
if(service == null) { service = new ServiceImpl(); }
return service.work(txt);
}
}
public class SecureVirtualProxy implements Service {
public SecureProxy() { out.println("SecureProxy " + now()); }
private Service service = new VirtualProxy();
//SNIPP….
public String work(String txt) {
if(code.equals("hoppel")) { return service.work(txt); }
return { return "nooooop"; }
}
}
125. Combining Proxies - SecureVirtualProxy
30
@SvenRuppert
Security Proxy Virtual Proxy
public class VirtualProxy implements Service {
public VirtualProxy() { out.println("VirtualProxy " + now()); }
private Service service = null;
public String work(String txt) {
if(service == null) { service = new ServiceImpl(); }
return service.work(txt);
}
}
public class SecureVirtualProxy implements Service {
public SecureProxy() { out.println("SecureProxy " + now()); }
private Service service = new VirtualProxy();
//SNIPP….
public String work(String txt) {
if(code.equals("hoppel")) { return service.work(txt); }
return { return "nooooop"; }
}
}
126. Combining Proxies - SecureVirtualProxy
30
@SvenRuppert
Security Proxy Virtual Proxy
public class VirtualProxy implements Service {
public VirtualProxy() { out.println("VirtualProxy " + now()); }
private Service service = null;
public String work(String txt) {
if(service == null) { service = new ServiceImpl(); }
return service.work(txt);
}
}
public class SecureVirtualProxy implements Service {
public SecureProxy() { out.println("SecureProxy " + now()); }
private Service service = new VirtualProxy();
//SNIPP….
public String work(String txt) {
if(code.equals("hoppel")) { return service.work(txt); }
return { return "nooooop"; }
}
}
127. Combining Proxies - SecureVirtualProxy
30
@SvenRuppert
Security Proxy Virtual Proxy
public class VirtualProxy implements Service {
public VirtualProxy() { out.println("VirtualProxy " + now()); }
private Service service = null;
public String work(String txt) {
if(service == null) { service = new ServiceImpl(); }
return service.work(txt);
}
}
public class SecureVirtualProxy implements Service {
public SecureProxy() { out.println("SecureProxy " + now()); }
private Service service = new VirtualProxy();
//SNIPP….
public String work(String txt) {
if(code.equals("hoppel")) { return service.work(txt); }
return { return "nooooop"; }
}
}
hard wired proxies
128. Combining Proxies - SecureVirtualProxy
30
@SvenRuppert
Security Proxy Virtual Proxy
public class VirtualProxy implements Service {
public VirtualProxy() { out.println("VirtualProxy " + now()); }
private Service service = null;
public String work(String txt) {
if(service == null) { service = new ServiceImpl(); }
return service.work(txt);
}
}
public class SecureVirtualProxy implements Service {
public SecureProxy() { out.println("SecureProxy " + now()); }
private Service service = new VirtualProxy();
//SNIPP….
public String work(String txt) {
if(code.equals("hoppel")) { return service.work(txt); }
return { return "nooooop"; }
}
}
hard wired proxies
What could we
change now ?
129. Combining Proxies - SecureVirtualProxy
31
@SvenRuppert
Security Proxy Virtual Proxy
hard wired proxies
What could we
change now ?
we need a Generic Proxy
130. Combining Proxies - SecureVirtualProxy
31
@SvenRuppert
Security Proxy Virtual Proxy
hard wired proxies
What could we
change now ?
we need a Generic Proxy
since JDK1.3 we have it
131. Combining Proxies - SecureVirtualProxy
31
@SvenRuppert
Security Proxy Virtual Proxy
hard wired proxies
What could we
change now ?
we need a Generic Proxy
since JDK1.3 we have it
the DynamicProxy
132. Dynamic Proxy - Hello World
32
@SvenRuppert
How to use it ?
133. Dynamic Proxy - Hello World
32
@SvenRuppert
How to use it ?
java.lang.reflect.Proxy
134. Dynamic Proxy - Hello World
32
@SvenRuppert
How to use it ?
java.lang.reflect.Proxy
we need an interface : here Service
135. Dynamic Proxy - Hello World
32
@SvenRuppert
How to use it ?
java.lang.reflect.Proxy
we need an interface : here Service
we need an implementation : here ServiceImpl
136. Dynamic Proxy - Hello World
32
@SvenRuppert
How to use it ?
java.lang.reflect.Proxy
we need an interface : here Service
we need an implementation : here ServiceImpl
Service proxyInstance = Service.class.cast( Proxy.newProxyInstance(
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
new InvocationHandler() {
private final ServiceImpl service = new ServiceImpl();
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(service, args);
}
}
)
);
137. Dynamic Proxy - Hello World
32
@SvenRuppert
How to use it ?
java.lang.reflect.Proxy
we need an interface : here Service
we need an implementation : here ServiceImpl
Service proxyInstance = Service.class.cast( Proxy.newProxyInstance(
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
new InvocationHandler() {
private final ServiceImpl service = new ServiceImpl();
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(service, args);
}
}
)
);
OK, step by step , please
139. Dynamic Proxy - Hello World
33
@SvenRuppert
Service proxyInstance = Service.class.cast (
140. Dynamic Proxy - Hello World
33
@SvenRuppert
Proxy.newProxyInstance (
Service proxyInstance = Service.class.cast (
141. Dynamic Proxy - Hello World
33
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
Service proxyInstance = Service.class.cast (
142. Dynamic Proxy - Hello World
33
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
143. Dynamic Proxy - Hello World
33
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {
private final ServiceImpl service = new ServiceImpl();
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
return m.invoke(service, args);
}
}
144. Dynamic Proxy - Hello World
33
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {
private final ServiceImpl service = new ServiceImpl();
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
return m.invoke(service, args);
}
}
)
);
145. Dynamic Proxy - Hello World
33
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {
private final ServiceImpl service = new ServiceImpl();
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
return m.invoke(service, args);
}
}
)
);
how to make a VirtualProxy ?
147. Dynamic Virtual Proxy
34
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {
private final ServiceImpl service;
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
if(service == null) { service = new ServiceImpl(); }
return m.invoke(service, args);
}
}
)
);
148. Dynamic Virtual Proxy
34
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {
private final ServiceImpl service;
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
if(service == null) { service = new ServiceImpl(); }
return m.invoke(service, args);
}
}
)
);
149. Dynamic Virtual Proxy
34
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {
private final ServiceImpl service;
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
if(service == null) { service = new ServiceImpl(); }
return m.invoke(service, args);
}
}
)
);
150. Dynamic Proxy - make it generic
35
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {
private final ServiceImpl service = new ServiceImpl();
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
return m.invoke(service, args);
}
}
)
);
151. Dynamic Proxy - make it generic
35
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {
private final ServiceImpl service = new ServiceImpl();
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
return m.invoke(service, args);
}
}
)
);
152. Dynamic Proxy - make it generic
35
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {
private final ServiceImpl service = new ServiceImpl();
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
return m.invoke(service, args);
}
}
)
);
153. Dynamic Proxy - make it generic
35
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {
private final ServiceImpl service = new ServiceImpl();
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
return m.invoke(service, args);
}
}
)
);
154. Dynamic Proxy - make it generic
35
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {
private final ServiceImpl service = new ServiceImpl();
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
return m.invoke(service, args);
}
}
)
);
get it from outside
155. Dynamic Proxy - make it generic
35
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {
private final ServiceImpl service = new ServiceImpl();
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
return m.invoke(service, args);
}
}
)
);
get it from outside
156. Dynamic Proxy - make it generic
35
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {
private final ServiceImpl service = new ServiceImpl();
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
return m.invoke(service, args);
}
}
)
);
get it from outside
we will remove it
157. Dynamic Proxy - make it generic
35
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {
private final ServiceImpl service = new ServiceImpl();
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
return m.invoke(service, args);
}
}
)
);
get it from outside
we will remove it
158. Dynamic Proxy - make it generic
35
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {
private final ServiceImpl service = new ServiceImpl();
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
return m.invoke(service, args);
}
}
)
);
get it from outside
we will remove it
we will get it from outside
159. Dynamic Proxy - make it generic
35
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {
private final ServiceImpl service = new ServiceImpl();
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
return m.invoke(service, args);
}
}
)
);
get it from outside
we will remove it
we will get it from outside
and finally we will call it ProxyGenerator
160. Dynamic Proxy - make it generic
36
@SvenRuppert
public class ProxyGenerator {
public static <P> P makeProxy(Class<P> subject, P realSubject) {
Object proxyInstance = Proxy.newProxyInstance(
subject.getClassLoader(),
new Class<?>[]{subject},
new InvocationHandler() {
@Override
public Object invoke(final Object proxy, final Method m, final Object[] args) throws Throwable {
return m.invoke(realSubject, args);
}
}
);
return subject.cast(proxyInstance);
}
}
161. Dynamic Proxy - make it generic
37
@SvenRuppert
public class ProxyGenerator {
public static <P> P makeProxy(Class<P> subject, P realSubject) {
Object proxyInstance = Proxy.newProxyInstance(
subject.getClassLoader(),
new Class<?>[]{subject},
(proxy, m, args) -> m.invoke(realSubject, args)
);
return subject.cast(proxyInstance);
}
}
162. Dynamic Proxy - make it generic
38
@SvenRuppert
with DynamicProxies
- we are writing less (more generic) code
- we can create Virtual-/Remote-/Security Proxies
163. Dynamic Proxy - make it generic
38
@SvenRuppert
with DynamicProxies
- we are writing less (more generic) code
- we can create Virtual-/Remote-/Security Proxies
but how to combine Proxies more generic ?
164. Dynamic Proxy - make it generic
39
@SvenRuppert
but how to combine Proxies more generic ?
for this we have to switch from
165. Dynamic Proxy - make it generic
39
@SvenRuppert
but how to combine Proxies more generic ?
for this we have to switch from
Factory-Method-Pattern
166. Dynamic Proxy - make it generic
39
@SvenRuppert
but how to combine Proxies more generic ?
for this we have to switch from
Factory-Method-Pattern
167. Dynamic Proxy - make it generic
39
@SvenRuppert
but how to combine Proxies more generic ?
for this we have to switch from
Factory-Method-Pattern
Builder-Pattern
169. DynamicProxyBuilder
40
@SvenRuppert
but how to combine Proxies more generic ?
Security Proxy Virtual ProxySecurity Proxy
let´s think about Two possible ways:
1 Security Proxy with 2 Rules and 1 VirtualProxy
170. DynamicProxyBuilder
40
@SvenRuppert
but how to combine Proxies more generic ?
Security Proxy Virtual ProxySecurity Proxy
let´s think about Two possible ways:
1 Security Proxy with 2 Rules and 1 VirtualProxy
2 Security Proxies with 1 Rule and 1 VirtualProxy
171. DynamicProxyBuilder
40
@SvenRuppert
but how to combine Proxies more generic ?
Security Proxy Virtual ProxySecurity Proxy
let´s think about Two possible ways:
1 Security Proxy with 2 Rules and 1 VirtualProxy
2 Security Proxies with 1 Rule and 1 VirtualProxy
OK, we need a generic CascadedProxy
172. DynamicProxyBuilder
41
@SvenRuppert
OK, we need a generic CascadedProxy
Proxy n
Proxy n-1
Proxy n-2
Proxy n-3
a child must know his parent
first we only add different DynamicProxies
always the same target interface
173. DynamicProxyBuilder
41
@SvenRuppert
OK, we need a generic CascadedProxy
Proxy n
Proxy n-1
Proxy n-2
Proxy n-3
a child must know his parent
first we only add different DynamicProxies
always the same target interface
174. DynamicProxyBuilder
41
@SvenRuppert
OK, we need a generic CascadedProxy
Proxy n
Proxy n-1
Proxy n-2
Proxy n-3
a child must know his parent
first we only add different DynamicProxies
always the same target interface
175. DynamicProxyBuilder
41
@SvenRuppert
OK, we need a generic CascadedProxy
Proxy n
Proxy n-1
Proxy n-2
Proxy n-3
a child must know his parent
first we only add different DynamicProxies
always the same target interface
176. DynamicProxyBuilder
41
@SvenRuppert
OK, we need a generic CascadedProxy
Proxy n
Proxy n-1
Proxy n-2
Proxy n-3
a child must know his parent
first we only add different DynamicProxies
always the same target interface
177. DynamicProxyBuilder
41
@SvenRuppert
OK, we need a generic CascadedProxy
Proxy n
Proxy n-1
Proxy n-2
Proxy n-3
a child must know his parent
first we only add different DynamicProxies
always the same target interface
178. DynamicProxyBuilder
41
@SvenRuppert
OK, we need a generic CascadedProxy
Proxy n
Proxy n-1
Proxy n-2
Proxy n-3
a child must know his parent
first we only add different DynamicProxies
always the same target interface
211. DynamicObjectAdapter - orig Version
46
@SvenRuppert
Proxy
orig /
adapter
invoke orig
invoke adapter
Goal: do not need to write an
Adapter with Delegator
213. DynamicObjectAdapter - orig Version
47
@SvenRuppert
core concept:
switching between method implementations
how to identify a method?
214. DynamicObjectAdapter - orig Version
48
@SvenRuppert
how to identify a method?
public class MethodIdentifier {
private final String name;
private final Class[] parameters;
public MethodIdentifier(Method m) {
name = m.getName();
parameters = m.getParameterTypes();
}
// we can save time by assuming that we only compare against
// other MethodIdentifier objects
public boolean equals(Object o) {
MethodIdentifier mid = (MethodIdentifier) o;
return name.equals(mid.name) &&
Arrays.equals(parameters, mid.parameters);
}
public int hashCode() { return name.hashCode(); }
}
215. DynamicObjectAdapter - orig Version
48
@SvenRuppert
how to identify a method?
public class MethodIdentifier {
private final String name;
private final Class[] parameters;
public MethodIdentifier(Method m) {
name = m.getName();
parameters = m.getParameterTypes();
}
// we can save time by assuming that we only compare against
// other MethodIdentifier objects
public boolean equals(Object o) {
MethodIdentifier mid = (MethodIdentifier) o;
return name.equals(mid.name) &&
Arrays.equals(parameters, mid.parameters);
}
public int hashCode() { return name.hashCode(); }
}
216. DynamicObjectAdapter - orig Version
48
@SvenRuppert
how to identify a method?
public class MethodIdentifier {
private final String name;
private final Class[] parameters;
public MethodIdentifier(Method m) {
name = m.getName();
parameters = m.getParameterTypes();
}
// we can save time by assuming that we only compare against
// other MethodIdentifier objects
public boolean equals(Object o) {
MethodIdentifier mid = (MethodIdentifier) o;
return name.equals(mid.name) &&
Arrays.equals(parameters, mid.parameters);
}
public int hashCode() { return name.hashCode(); }
}
217. DynamicObjectAdapter - orig Version
49
@SvenRuppert
public interface Service {
String doWork_A(String txt);
String doWork_B(String txt);
}
public static class ServiceImpl implements Service {
public String doWork_A(String txt) {
return "doWorkd_A_Original";
}
public String doWork_B(String txt) {
return "doWorkd_B_Original";
}
}
218. DynamicObjectAdapter - orig Version
50
@SvenRuppert
public interface Service {
String doWork_A(String txt);
String doWork_B(String txt);
}
public interface ServiceAdapter_A {
String doWork_A(String txt);
}
public interface ServiceAdapter_B {
String doWork_B(String txt);
}
219. DynamicObjectAdapter - orig Version
50
@SvenRuppert
public interface Service {
String doWork_A(String txt);
String doWork_B(String txt);
}
public interface ServiceAdapter_A {
String doWork_A(String txt);
}
public interface ServiceAdapter_B {
String doWork_B(String txt);
}
220. DynamicObjectAdapter - orig Version
50
@SvenRuppert
public interface Service {
String doWork_A(String txt);
String doWork_B(String txt);
}
public interface ServiceAdapter_A {
String doWork_A(String txt);
}
public interface ServiceAdapter_B {
String doWork_B(String txt);
}
functional interface
221. DynamicObjectAdapter - orig Version
50
@SvenRuppert
public interface Service {
String doWork_A(String txt);
String doWork_B(String txt);
}
public interface ServiceAdapter_A {
String doWork_A(String txt);
}
public interface ServiceAdapter_B {
String doWork_B(String txt);
}
functional interface
222. DynamicObjectAdapter - orig Version
50
@SvenRuppert
public interface Service {
String doWork_A(String txt);
String doWork_B(String txt);
}
public interface ServiceAdapter_A {
String doWork_A(String txt);
}
public interface ServiceAdapter_B {
String doWork_B(String txt);
}
functional interface
functional interface
223. DynamicObjectAdapter - orig Version
51
@SvenRuppert
private Map<MethodIdentifier, Method> adaptedMethods = new HashMap<>();
private Map<MethodIdentifier, Object> adapters = new HashMap<>();
private T original;
private Class<T> target;
224. DynamicObjectAdapter - orig Version
51
@SvenRuppert
private Map<MethodIdentifier, Method> adaptedMethods = new HashMap<>();
private Map<MethodIdentifier, Object> adapters = new HashMap<>();
private T original;
private Class<T> target;
private void addAdapter(Object adapter) {
225. DynamicObjectAdapter - orig Version
51
@SvenRuppert
private Map<MethodIdentifier, Method> adaptedMethods = new HashMap<>();
private Map<MethodIdentifier, Object> adapters = new HashMap<>();
private T original;
private Class<T> target;
private void addAdapter(Object adapter) {
final Class<?> adapterClass = adapter.getClass();
227. DynamicObjectAdapter - orig Version
51
@SvenRuppert
private Map<MethodIdentifier, Method> adaptedMethods = new HashMap<>();
private Map<MethodIdentifier, Object> adapters = new HashMap<>();
private T original;
private Class<T> target;
private void addAdapter(Object adapter) {
final Class<?> adapterClass = adapter.getClass();
Method[] methods = adapterClass.getDeclaredMethods();
for (Method m : methods) {
228. DynamicObjectAdapter - orig Version
51
@SvenRuppert
private Map<MethodIdentifier, Method> adaptedMethods = new HashMap<>();
private Map<MethodIdentifier, Object> adapters = new HashMap<>();
private T original;
private Class<T> target;
private void addAdapter(Object adapter) {
final Class<?> adapterClass = adapter.getClass();
Method[] methods = adapterClass.getDeclaredMethods();
for (Method m : methods) {
final MethodIdentifier key = new MethodIdentifier(m);
229. DynamicObjectAdapter - orig Version
51
@SvenRuppert
private Map<MethodIdentifier, Method> adaptedMethods = new HashMap<>();
private Map<MethodIdentifier, Object> adapters = new HashMap<>();
private T original;
private Class<T> target;
private void addAdapter(Object adapter) {
final Class<?> adapterClass = adapter.getClass();
Method[] methods = adapterClass.getDeclaredMethods();
for (Method m : methods) {
final MethodIdentifier key = new MethodIdentifier(m);
adaptedMethods.put(key, m);
230. DynamicObjectAdapter - orig Version
51
@SvenRuppert
private Map<MethodIdentifier, Method> adaptedMethods = new HashMap<>();
private Map<MethodIdentifier, Object> adapters = new HashMap<>();
private T original;
private Class<T> target;
private void addAdapter(Object adapter) {
final Class<?> adapterClass = adapter.getClass();
Method[] methods = adapterClass.getDeclaredMethods();
for (Method m : methods) {
final MethodIdentifier key = new MethodIdentifier(m);
adaptedMethods.put(key, m);
adapters.put(key, adapter);
231. DynamicObjectAdapter - orig Version
51
@SvenRuppert
private Map<MethodIdentifier, Method> adaptedMethods = new HashMap<>();
private Map<MethodIdentifier, Object> adapters = new HashMap<>();
private T original;
private Class<T> target;
}
}
private void addAdapter(Object adapter) {
final Class<?> adapterClass = adapter.getClass();
Method[] methods = adapterClass.getDeclaredMethods();
for (Method m : methods) {
final MethodIdentifier key = new MethodIdentifier(m);
adaptedMethods.put(key, m);
adapters.put(key, adapter);
232. DynamicObjectAdapter - orig Version
52
@SvenRuppert
private Map<MethodIdentifier, Method> adaptedMethods = new HashMap<>();
private Map<MethodIdentifier, Object> adapters = new HashMap<>();
private T original;
private Class<T> target;
233. DynamicObjectAdapter - orig Version
52
@SvenRuppert
private Map<MethodIdentifier, Method> adaptedMethods = new HashMap<>();
private Map<MethodIdentifier, Object> adapters = new HashMap<>();
private T original;
private Class<T> target;
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
234. DynamicObjectAdapter - orig Version
52
@SvenRuppert
private Map<MethodIdentifier, Method> adaptedMethods = new HashMap<>();
private Map<MethodIdentifier, Object> adapters = new HashMap<>();
private T original;
private Class<T> target;
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
final MethodIdentifier key = new MethodIdentifier(method);
Method other = adaptedMethods.get(key);
235. DynamicObjectAdapter - orig Version
52
@SvenRuppert
private Map<MethodIdentifier, Method> adaptedMethods = new HashMap<>();
private Map<MethodIdentifier, Object> adapters = new HashMap<>();
private T original;
private Class<T> target;
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
final MethodIdentifier key = new MethodIdentifier(method);
Method other = adaptedMethods.get(key);
if (other != null) {
return other.invoke(adapters.get(key), args);
} else {
return method.invoke(original, args);
}
236. DynamicObjectAdapter - orig Version
52
@SvenRuppert
private Map<MethodIdentifier, Method> adaptedMethods = new HashMap<>();
private Map<MethodIdentifier, Object> adapters = new HashMap<>();
private T original;
private Class<T> target;
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
final MethodIdentifier key = new MethodIdentifier(method);
Method other = adaptedMethods.get(key);
if (other != null) {
return other.invoke(adapters.get(key), args);
} else {
return method.invoke(original, args);
}
237. DynamicObjectAdapter - orig Version @SvenRuppert
private Map<MethodIdentifier, Method> adaptedMethods = new HashMap<>();
private Map<MethodIdentifier, Object> adapters = new HashMap<>();
private T original;
private Class<T> target;
if you will use this without a Builder
you can add useless Adapter !!
238. DynamicObjectAdapter - orig Version @SvenRuppert
private Map<MethodIdentifier, Method> adaptedMethods = new HashMap<>();
private Map<MethodIdentifier, Object> adapters = new HashMap<>();
private T original;
private Class<T> target;
if you will use this without a Builder
you can add useless Adapter !!
239. DynamicObjectAdapter - orig Version @SvenRuppert
private Map<MethodIdentifier, Method> adaptedMethods = new HashMap<>();
private Map<MethodIdentifier, Object> adapters = new HashMap<>();
private T original;
private Class<T> target;
if you will use this without a Builder
you can add useless Adapter !!
Add some typed with-Methods
240. DynamicObjectAdapter - orig Version @SvenRuppert
Add some typed with-Methods
public AdapterBuilder<T> withDoWork_A(ServiceAdapter_A adapter) {
addAdapter(adapter);
return this;
}
241. DynamicObjectAdapter - orig Version @SvenRuppert
Add some typed with-Methods
public AdapterBuilder<T> withDoWork_A(ServiceAdapter_A adapter) {
addAdapter(adapter);
return this;
}
remember
242. DynamicObjectAdapter - orig Version @SvenRuppert
Add some typed with-Methods
public AdapterBuilder<T> withDoWork_A(ServiceAdapter_A adapter) {
addAdapter(adapter);
return this;
}
remember public interface Service {
String doWork_A(String txt);
String doWork_B(String txt);
}
243. DynamicObjectAdapter - orig Version @SvenRuppert
Add some typed with-Methods
public AdapterBuilder<T> withDoWork_A(ServiceAdapter_A adapter) {
addAdapter(adapter);
return this;
}
remember public interface Service {
String doWork_A(String txt);
String doWork_B(String txt);
}
public interface ServiceAdapter_A {
String doWork_A(String txt);
}
245. DynamicObjectAdapter - orig Version @SvenRuppert
How to use this?
Service service = AdapterBuilder.<Service>newBuilder()
246. DynamicObjectAdapter - orig Version @SvenRuppert
How to use this?
Service service = AdapterBuilder.<Service>newBuilder()
.withTarget(Service.class)
247. DynamicObjectAdapter - orig Version @SvenRuppert
How to use this?
Service service = AdapterBuilder.<Service>newBuilder()
.withTarget(Service.class)
.withOriginal(new ServiceImpl())
248. DynamicObjectAdapter - orig Version @SvenRuppert
How to use this?
Service service = AdapterBuilder.<Service>newBuilder()
.withTarget(Service.class)
.withOriginal(new ServiceImpl())
.withDoWork_A((txt) -> txt + "_part")
249. DynamicObjectAdapter - orig Version @SvenRuppert
.build();
How to use this?
Service service = AdapterBuilder.<Service>newBuilder()
.withTarget(Service.class)
.withOriginal(new ServiceImpl())
.withDoWork_A((txt) -> txt + "_part")
259. generated DynamicObjectAdapterBuilder @SvenRuppert
use Annotation Processing
create a marker like
@DynamicObjectAdapterBuilder
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface DynamicObjectAdapterBuilder {
}
260. generated DynamicObjectAdapterBuilder @SvenRuppert
use Annotation Processing
create a marker like
@DynamicObjectAdapterBuilder
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface DynamicObjectAdapterBuilder {
}
public class AnnotationProcessor extends AbstractProcessor
264. Summary
60
@SvenRuppert
we got type-save ProxyBuilder
..type-save generated DynamicObjectAdapter
..at runtime we could change the chain of Proxies
..could be used for
(Dynamic)
Dependency Injection Implementations
265. Summary
61
@SvenRuppert
If you are interested…
have a look at RapidPM on github
rapidpm-proxybuilder
rapidpm-dynamic-cdi
rapidpm-microservice
or contact me ;-) @SvenRuppert
266. Summary
61
@SvenRuppert
If you are interested…
have a look at RapidPM on github
rapidpm-proxybuilder
rapidpm-dynamic-cdi
rapidpm-microservice
or contact me ;-) @SvenRuppert
Thank You !!!