1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
package org.openpermis.editor.policy.adapter; |
9 | |
|
10 | |
import static org.picocontainer.Characteristics.NO_CACHE; |
11 | |
|
12 | |
import org.picocontainer.MutablePicoContainer; |
13 | |
import org.picocontainer.PicoBuilder; |
14 | |
import org.picocontainer.PicoContainer; |
15 | |
|
16 | |
import org.openpermis.editor.policy.adapter.overview.Overview; |
17 | |
import org.openpermis.policy.bean.ActionBean; |
18 | |
import org.openpermis.policy.bean.DomainBean; |
19 | |
import org.openpermis.policy.bean.ObligationBean; |
20 | |
import org.openpermis.policy.bean.PartBean; |
21 | |
import org.openpermis.policy.bean.TargetAccessRuleBean; |
22 | |
import org.openpermis.policy.bean.TargetBean; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
public class BasicAdapterTrader implements AdapterTrader { |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
private final MutablePicoContainer pico; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public BasicAdapterTrader () { |
44 | 0 | this.pico = createContainer(new PicoBuilder().build()); |
45 | 0 | } |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
private static final MutablePicoContainer createContainer (PicoContainer parent) { |
54 | 0 | final MutablePicoContainer container = new PicoBuilder(parent).withCaching().build(); |
55 | 0 | container.change(NO_CACHE); |
56 | |
|
57 | 0 | final Class<?>[] partBeanTypes = { |
58 | |
ActionBean.class, DomainBean.class, TargetBean.class, TargetAccessRuleBean.class, |
59 | |
ObligationBean.class |
60 | |
}; |
61 | 0 | final Class<?>[] adapteeTypes = {Overview.class}; |
62 | |
|
63 | 0 | for (Class<?> adapteeType : adapteeTypes) { |
64 | 0 | for (Class<?> partBeanType : partBeanTypes) { |
65 | |
|
66 | |
Class<?> adaptee; |
67 | |
try { |
68 | 0 | adaptee = Class.forName( |
69 | |
adapteeType.getPackage().getName() + |
70 | |
"." + |
71 | |
partBeanType.getSimpleName() + |
72 | |
adapteeType.getSimpleName() |
73 | |
); |
74 | 0 | } catch (ClassNotFoundException e) { |
75 | 0 | throw new IllegalStateException( |
76 | |
"No adaptee class found for part bean <" + partBeanType.getSimpleName() + |
77 | |
"> and adaptee type <" + adapteeType.getSimpleName() + ">." |
78 | |
); |
79 | 0 | } |
80 | 0 | final Object key = createKey(adapteeType, partBeanType); |
81 | 0 | container.addComponent(key, adaptee); |
82 | |
} |
83 | |
} |
84 | |
|
85 | 0 | return container; |
86 | |
} |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
private static String createKey (Class<?> adapteeType, Class<?> partBeanType) { |
97 | 0 | return adapteeType.getCanonicalName() + ":" + partBeanType.getCanonicalName(); |
98 | |
} |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
@SuppressWarnings("unchecked") |
106 | |
public <T extends Adaptee<?>> T adaptTo (PartBean part, Class<T> adapteeType) { |
107 | 0 | final Class<?> partClass = part.getClass(); |
108 | 0 | this.pico.addComponent(partClass, part); |
109 | |
|
110 | |
try { |
111 | 0 | return (T) this.pico.getComponent(createKey(adapteeType, part.getPartBeanType())); |
112 | 0 | } catch (Exception e) { |
113 | 0 | throw new IllegalStateException( |
114 | |
"Adapter trader doesn't provide an adapter for part bean type <" + |
115 | |
part.getPartBeanType().getSimpleName() + |
116 | |
"> and adaptee type <" + adapteeType.getSimpleName() + ">." |
117 | |
); |
118 | |
} finally { |
119 | 0 | this.pico.removeComponent(partClass); |
120 | |
} |
121 | |
} |
122 | |
|
123 | |
} |