1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
package com.jcabi.log; |
31 | |
|
32 | |
import com.jcabi.aspects.Immutable; |
33 | |
import java.lang.reflect.Constructor; |
34 | |
import java.lang.reflect.InvocationTargetException; |
35 | |
import java.util.Formattable; |
36 | |
import java.util.concurrent.ConcurrentHashMap; |
37 | |
import java.util.concurrent.ConcurrentMap; |
38 | |
import lombok.EqualsAndHashCode; |
39 | |
import lombok.ToString; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
@Immutable |
50 | 0 | @ToString |
51 | 0 | @EqualsAndHashCode |
52 | |
final class DecorsManager { |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | 0 | private static final ConcurrentMap<String, Class<? extends Formattable>> DECORS = |
59 | |
new ConcurrentHashMap<String, Class<? extends Formattable>>(); |
60 | |
|
61 | |
static { |
62 | 0 | DecorsManager.DECORS.put("dom", DomDecor.class); |
63 | 0 | DecorsManager.DECORS.put("exception", ExceptionDecor.class); |
64 | 0 | DecorsManager.DECORS.put("list", ListDecor.class); |
65 | 0 | DecorsManager.DECORS.put("ms", MsDecor.class); |
66 | 0 | DecorsManager.DECORS.put("nano", NanoDecor.class); |
67 | 0 | DecorsManager.DECORS.put("object", ObjectDecor.class); |
68 | 0 | DecorsManager.DECORS.put("size", SizeDecor.class); |
69 | 0 | DecorsManager.DECORS.put("secret", SecretDecor.class); |
70 | 0 | DecorsManager.DECORS.put("text", TextDecor.class); |
71 | 0 | DecorsManager.DECORS.put("type", TypeDecor.class); |
72 | 0 | } |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | 0 | private DecorsManager() { |
78 | |
|
79 | 0 | } |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public static Formattable decor(final String key, final Object arg) |
89 | |
throws DecorException { |
90 | 0 | final Class<? extends Formattable> type = DecorsManager.find(key); |
91 | |
final Formattable decor; |
92 | |
try { |
93 | 0 | decor = (Formattable) DecorsManager.ctor(type).newInstance(arg); |
94 | 0 | } catch (final InstantiationException ex) { |
95 | 0 | throw new DecorException( |
96 | |
ex, |
97 | |
"Can't instantiate %s(%s)", |
98 | |
type.getName(), |
99 | |
arg.getClass().getName() |
100 | |
); |
101 | 0 | } catch (final IllegalAccessException ex) { |
102 | 0 | throw new DecorException( |
103 | |
ex, |
104 | |
"Can't access %s(%s)", |
105 | |
type.getName(), |
106 | |
arg.getClass().getName() |
107 | |
); |
108 | 0 | } catch (final InvocationTargetException ex) { |
109 | 0 | throw new DecorException( |
110 | |
ex, |
111 | |
"Can't invoke %s(%s)", |
112 | |
type.getName(), |
113 | |
arg.getClass().getName() |
114 | |
); |
115 | 0 | } |
116 | 0 | return decor; |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
@SuppressWarnings("unchecked") |
126 | |
private static Class<? extends Formattable> find(final String key) |
127 | |
throws DecorException { |
128 | |
final Class<? extends Formattable> type; |
129 | 0 | if (DecorsManager.DECORS.containsKey(key)) { |
130 | 0 | type = DecorsManager.DECORS.get(key); |
131 | |
} else { |
132 | |
try { |
133 | 0 | type = (Class<Formattable>) Class.forName(key); |
134 | 0 | } catch (final ClassNotFoundException ex) { |
135 | 0 | throw new DecorException( |
136 | |
ex, |
137 | |
"Decor '%s' not found and class can't be instantiated", |
138 | |
key |
139 | |
); |
140 | 0 | } |
141 | |
} |
142 | 0 | return type; |
143 | |
} |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
private static Constructor<?> ctor(final Class<? extends Formattable> type) |
152 | |
throws DecorException { |
153 | 0 | final Constructor<?>[] ctors = type.getDeclaredConstructors(); |
154 | 0 | if (ctors.length != 1) { |
155 | 0 | throw new DecorException( |
156 | |
"%s should have just one one-arg ctor, but there are %d", |
157 | |
type.getName(), |
158 | |
ctors.length |
159 | |
); |
160 | |
} |
161 | 0 | final Constructor<?> ctor = ctors[0]; |
162 | 0 | if (ctor.getParameterTypes().length != 1) { |
163 | 0 | throw new DecorException( |
164 | |
"%s public ctor should have just once parameter", |
165 | |
type.getName() |
166 | |
); |
167 | |
} |
168 | 0 | return ctor; |
169 | |
} |
170 | |
|
171 | |
} |