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.Role; |
18 | |
import org.openpermis.policy.bean.ActionBean; |
19 | |
import org.openpermis.policy.bean.AuthorityBean; |
20 | |
import org.openpermis.policy.bean.DomainBean; |
21 | |
import org.openpermis.policy.bean.ObligationBean; |
22 | |
import org.openpermis.policy.bean.PartBean; |
23 | |
import org.openpermis.policy.bean.RoleAssignmentRuleBean; |
24 | |
import org.openpermis.policy.bean.RoleHierarchyBean; |
25 | |
import org.openpermis.policy.bean.TargetAccessRuleBean; |
26 | |
import org.openpermis.policy.bean.TargetBean; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class BasicAdapterTrader implements AdapterTrader { |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
private final MutablePicoContainer pico; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | 0 | public BasicAdapterTrader () { |
48 | 0 | this.pico = createContainer(new PicoBuilder().build()); |
49 | 0 | } |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
private static final MutablePicoContainer createContainer (PicoContainer parent) { |
58 | 0 | final MutablePicoContainer container = new PicoBuilder(parent).withCaching().build(); |
59 | 0 | container.change(NO_CACHE); |
60 | |
|
61 | 0 | final Class<?>[] objectTypes = { |
62 | |
ActionBean.class, |
63 | |
DomainBean.class, |
64 | |
TargetBean.class, |
65 | |
TargetAccessRuleBean.class, |
66 | |
ObligationBean.class, |
67 | |
AuthorityBean.class, |
68 | |
Role.class, |
69 | |
RoleHierarchyBean.class, |
70 | |
RoleAssignmentRuleBean.class |
71 | |
}; |
72 | 0 | final Class<?>[] adapteeTypes = {Overview.class}; |
73 | |
|
74 | 0 | for (Class<?> adapteeType : adapteeTypes) { |
75 | 0 | for (Class<?> objectType : objectTypes) { |
76 | |
|
77 | |
Class<?> adaptee; |
78 | |
try { |
79 | 0 | adaptee = Class.forName( |
80 | |
adapteeType.getPackage().getName() + |
81 | |
"." + |
82 | |
objectType.getSimpleName() + |
83 | |
adapteeType.getSimpleName() |
84 | |
); |
85 | 0 | } catch (ClassNotFoundException e) { |
86 | 0 | throw new IllegalStateException( |
87 | |
"No adaptee class found for object class <" + objectType.getSimpleName() + |
88 | |
"> and adaptee type <" + adapteeType.getSimpleName() + ">." |
89 | |
); |
90 | 0 | } |
91 | 0 | final Object key = createKey(adapteeType, objectType); |
92 | 0 | container.addComponent(key, adaptee); |
93 | |
} |
94 | |
} |
95 | 0 | return container; |
96 | |
} |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
private static String createKey (Class<?> adapteeType, Class<?> objectType) { |
106 | 0 | return adapteeType.getCanonicalName() + ":" + objectType.getCanonicalName(); |
107 | |
} |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
@SuppressWarnings("unchecked") |
115 | |
public <T extends Adaptee<?>> T adaptTo (Object object, Class<T> adapteeType) { |
116 | 0 | final Class<?> exactObjectClass = object.getClass(); |
117 | 0 | this.pico.addComponent(exactObjectClass, object); |
118 | |
|
119 | |
final Class<?> objectType; |
120 | 0 | if (object instanceof PartBean) { |
121 | 0 | objectType = ((PartBean) object).getPartBeanType(); |
122 | |
} else { |
123 | 0 | objectType = object.getClass(); |
124 | |
} |
125 | |
|
126 | |
try { |
127 | 0 | return (T) this.pico.getComponent(createKey(adapteeType, objectType)); |
128 | 0 | } catch (Exception e) { |
129 | 0 | throw new IllegalStateException( |
130 | |
"Adapter trader doesn't provide an adapter for type <" + |
131 | |
objectType.getSimpleName() + |
132 | |
"> and adaptee type <" + adapteeType.getSimpleName() + ">." |
133 | |
); |
134 | |
} finally { |
135 | 0 | this.pico.removeComponent(exactObjectClass); |
136 | |
} |
137 | |
} |
138 | |
|
139 | |
} |