44
55package oracle .kubernetes .operator .helpers ;
66
7+ import java .lang .reflect .Constructor ;
78import java .lang .reflect .InvocationTargetException ;
89import java .lang .reflect .Method ;
910import java .util .ArrayList ;
1011import java .util .HashMap ;
11- import java .util .Iterator ;
1212import java .util .List ;
13- import java .util .ListIterator ;
1413import java .util .Map ;
15- import java .util .Set ;
1614
1715import io .kubernetes .client .models .V1EnvVar ;
1816import io .kubernetes .client .models .V1Pod ;
1917import oracle .kubernetes .operator .Pair ;
2018import oracle .kubernetes .operator .TuningParameters ;
2119import oracle .kubernetes .operator .logging .LoggingFacade ;
2220import oracle .kubernetes .operator .logging .LoggingFactory ;
23- import oracle .kubernetes .operator .logging .MessageKeys ;
2421import oracle .kubernetes .weblogic .domain .model .Domain ;
2522
2623public abstract class StepContextBase implements StepContextConstants {
@@ -74,49 +71,46 @@ protected void doSubstitution(final Map<String, String> substitutionVariables, L
7471 }
7572 }
7673
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 )) {
9394 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 )));
110103 }
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 );
113110 }
114111 }
115112 }
116- }
117-
118- private boolean isModelOrListClass (Class cls ) {
119- return isModelClass (cls ) || List .class .isAssignableFrom (cls );
113+ return obj ;
120114 }
121115
122116 private static final String MODELS_PACKAGE = V1Pod .class .getPackageName ();
@@ -127,39 +121,20 @@ private boolean isModelClass(Class cls) {
127121 || cls .getPackageName ().startsWith (DOMAIN_MODEL_PACKAGE );
128122 }
129123
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 ) {
154125 List <Pair <Method , Method >> results = new ArrayList <>();
155126 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 ) {
161136 try {
162- Method set = cls .getMethod ("set" + m . getName (). substring ( 3 ), type );
137+ Method set = cls .getMethod ("set" + beanName , m . getReturnType () );
163138 if (set != null ) {
164139 results .add (new Pair <>(m , set ));
165140 }
@@ -182,26 +157,6 @@ private String translate(final Map<String, String> substitutionVariables, String
182157 return result ;
183158 }
184159
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-
205160 protected void addEnvVar (List <V1EnvVar > vars , String name , String value ) {
206161 vars .add (new V1EnvVar ().name (name ).value (value ));
207162 }
0 commit comments