4
4
5
5
package oracle .kubernetes .operator .helpers ;
6
6
7
+ import java .lang .reflect .Constructor ;
7
8
import java .lang .reflect .InvocationTargetException ;
8
9
import java .lang .reflect .Method ;
9
10
import java .util .ArrayList ;
10
11
import java .util .HashMap ;
11
- import java .util .Iterator ;
12
12
import java .util .List ;
13
- import java .util .ListIterator ;
14
13
import java .util .Map ;
15
- import java .util .Set ;
16
14
17
15
import io .kubernetes .client .models .V1EnvVar ;
18
16
import io .kubernetes .client .models .V1Pod ;
19
17
import oracle .kubernetes .operator .Pair ;
20
18
import oracle .kubernetes .operator .TuningParameters ;
21
19
import oracle .kubernetes .operator .logging .LoggingFacade ;
22
20
import oracle .kubernetes .operator .logging .LoggingFactory ;
23
- import oracle .kubernetes .operator .logging .MessageKeys ;
24
21
import oracle .kubernetes .weblogic .domain .model .Domain ;
25
22
26
23
public abstract class StepContextBase implements StepContextConstants {
@@ -74,49 +71,46 @@ protected void doSubstitution(final Map<String, String> substitutionVariables, L
74
71
}
75
72
}
76
73
77
- protected void doDeepSubstitution (final Map <String , String > substitutionVariables , Object obj ) {
78
- if (obj != null ) {
79
- if (obj instanceof List ) {
80
- ListIterator <Object > it = ((List ) obj ).listIterator ();
81
- while (it .hasNext ()) {
82
- Object member = it .next ();
83
- if (member instanceof String ) {
84
- String trans = translate (substitutionVariables , (String ) member );
85
- if (!member .equals (trans )) {
86
- it .set (trans );
87
- }
88
- } else if (member != null && isModelClass (member .getClass ())) {
89
- doDeepSubstitution (substitutionVariables , member );
90
- }
91
- }
92
- } else {
74
+ protected <T > T doDeepSubstitution (final Map <String , String > substitutionVariables , T obj ) {
75
+ if (obj instanceof String ) {
76
+ return (T ) translate (substitutionVariables , (String ) obj );
77
+ } else if (obj instanceof List ) {
78
+ List <Object > result = new ArrayList <>();
79
+ for (Object o : (List ) obj ) {
80
+ result .add (doDeepSubstitution (substitutionVariables , o ));
81
+ }
82
+ return (T ) result ;
83
+ } else if (obj instanceof Map ) {
84
+ Map <String , Object > result = new HashMap <>();
85
+ for (Map .Entry <String , Object > entry : ((Map <String , Object >) obj ).entrySet ()) {
86
+ result .put (
87
+ translate (substitutionVariables , entry .getKey ()),
88
+ doDeepSubstitution (substitutionVariables , entry .getValue ()));
89
+ }
90
+ return (T ) result ;
91
+ } else if (obj != null ) {
92
+ Class <T > cls = (Class <T >) obj .getClass ();
93
+ if (isModelClass (cls )) {
93
94
try {
94
- Class cls = obj .getClass ();
95
- if (isModelClass (cls )) {
96
- List <Method > modelOrListBeans = modelOrListBeans (cls );
97
- for (Method item : modelOrListBeans ) {
98
- doDeepSubstitution (substitutionVariables , item .invoke (obj ));
99
- }
100
-
101
- List <Pair <Method , Method >> stringBeans = stringBeans (cls );
102
- for (Pair <Method , Method > item : stringBeans ) {
103
- item .getRight ().invoke (obj , translate (substitutionVariables , (String ) item .getLeft ().invoke (obj )));
104
- }
105
-
106
- List <Pair <Method , Method >> mapBeans = mapBeans (cls );
107
- for (Pair <Method , Method > item : mapBeans ) {
108
- item .getRight ().invoke (obj , translate (substitutionVariables , (Map ) item .getLeft ().invoke (obj )));
109
- }
95
+ Constructor <T > constructor = cls .getConstructor ();
96
+ T subObj = constructor .newInstance ();
97
+
98
+ List <Pair <Method , Method >> typeBeans = typeBeans (cls );
99
+ for (Pair <Method , Method > item : typeBeans ) {
100
+ item .getRight ()
101
+ .invoke (
102
+ subObj , doDeepSubstitution (substitutionVariables , item .getLeft ().invoke (obj )));
110
103
}
111
- } catch (IllegalAccessException | InvocationTargetException e ) {
112
- LOGGER .severe (MessageKeys .EXCEPTION , e );
104
+ return subObj ;
105
+ } catch (NoSuchMethodException
106
+ | InstantiationException
107
+ | IllegalAccessException
108
+ | InvocationTargetException e ) {
109
+ throw new RuntimeException (e );
113
110
}
114
111
}
115
112
}
116
- }
117
-
118
- private boolean isModelOrListClass (Class cls ) {
119
- return isModelClass (cls ) || List .class .isAssignableFrom (cls );
113
+ return obj ;
120
114
}
121
115
122
116
private static final String MODELS_PACKAGE = V1Pod .class .getPackageName ();
@@ -127,39 +121,20 @@ private boolean isModelClass(Class cls) {
127
121
|| cls .getPackageName ().startsWith (DOMAIN_MODEL_PACKAGE );
128
122
}
129
123
130
- private List <Method > modelOrListBeans (Class cls ) {
131
- List <Method > results = new ArrayList <>();
132
- Method [] methods = cls .getMethods ();
133
- if (methods != null ) {
134
- for (Method m : methods ) {
135
- if (m .getName ().startsWith ("get" )
136
- && isModelOrListClass (m .getReturnType ())
137
- && m .getParameterCount () == 0 ) {
138
- results .add (m );
139
- }
140
- }
141
- }
142
- return results ;
143
- }
144
-
145
- private List <Pair <Method , Method >> stringBeans (Class cls ) {
146
- return typeBeans (cls , String .class );
147
- }
148
-
149
- private List <Pair <Method , Method >> mapBeans (Class cls ) {
150
- return typeBeans (cls , Map .class );
151
- }
152
-
153
- private List <Pair <Method , Method >> typeBeans (Class cls , Class type ) {
124
+ private List <Pair <Method , Method >> typeBeans (Class cls ) {
154
125
List <Pair <Method , Method >> results = new ArrayList <>();
155
126
Method [] methods = cls .getMethods ();
156
- if (methods != null ) {
157
- for (Method m : methods ) {
158
- if (m .getName ().startsWith ("get" )
159
- && m .getReturnType ().equals (type )
160
- && m .getParameterCount () == 0 ) {
127
+ for (Method m : methods ) {
128
+ if (m .getParameterCount () == 0 ) {
129
+ String beanName = null ;
130
+ if (m .getName ().startsWith ("get" )) {
131
+ beanName = m .getName ().substring (3 );
132
+ } else if (m .getName ().startsWith ("is" )) {
133
+ beanName = m .getName ().substring (2 );
134
+ }
135
+ if (beanName != null ) {
161
136
try {
162
- Method set = cls .getMethod ("set" + m . getName (). substring ( 3 ), type );
137
+ Method set = cls .getMethod ("set" + beanName , m . getReturnType () );
163
138
if (set != null ) {
164
139
results .add (new Pair <>(m , set ));
165
140
}
@@ -182,26 +157,6 @@ private String translate(final Map<String, String> substitutionVariables, String
182
157
return result ;
183
158
}
184
159
185
- private Map <String , Object > translate (final Map <String , String > substitutionVariables , Map <String , Object > rawValue ) {
186
- if (rawValue == null )
187
- return null ;
188
-
189
- Map <String , Object > trans = new HashMap <>();
190
- for (Map .Entry <String , ?> entry : rawValue .entrySet ()) {
191
- Object value = entry .getValue ();
192
- if (value instanceof String ) {
193
- value = translate (substitutionVariables , (String ) value );
194
- } else if (value instanceof Map ) {
195
- value = translate (substitutionVariables , (Map ) value );
196
- } else {
197
- doDeepSubstitution (substitutionVariables , value );
198
- }
199
- trans .put (translate (substitutionVariables , entry .getKey ()), value );
200
- }
201
-
202
- return trans ;
203
- }
204
-
205
160
protected void addEnvVar (List <V1EnvVar > vars , String name , String value ) {
206
161
vars .add (new V1EnvVar ().name (name ).value (value ));
207
162
}
0 commit comments