11/*******************************************************************************
2- * Copyright (c) 2016, 2024 Eurotech and/or its affiliates and others
2+ * Copyright (c) 2016, 2026 Eurotech and/or its affiliates and others
33 *
44 * This program and the accompanying materials are made
55 * available under the terms of the Eclipse Public License 2.0
4545import org .eclipse .kura .core .configuration .XmlComponentConfigurations ;
4646import org .eclipse .kura .core .configuration .metatype .Tad ;
4747import org .eclipse .kura .core .configuration .metatype .Tocd ;
48+ import org .eclipse .kura .core .configuration .util .StringUtil ;
4849import org .eclipse .kura .driver .descriptor .DriverDescriptor ;
4950import org .eclipse .kura .marshalling .Marshaller ;
5051import org .eclipse .kura .rest .configuration .api .ComponentConfigurationList ;
@@ -122,35 +123,35 @@ public static Object getObjectValue(GwtConfigParameter param) {
122123 } else if (strValue != null && !strValue .trim ().isEmpty ()) {
123124 final String trimmedValue = strValue .trim ();
124125 switch (gwtType ) {
125- case LONG :
126- objValue = Long .parseLong (trimmedValue );
127- break ;
128- case DOUBLE :
129- objValue = Double .parseDouble (trimmedValue );
130- break ;
131- case FLOAT :
132- objValue = Float .parseFloat (trimmedValue );
133- break ;
134- case INTEGER :
135- objValue = Integer .parseInt (trimmedValue );
136- break ;
137- case SHORT :
138- objValue = Short .parseShort (trimmedValue );
139- break ;
140- case BYTE :
141- objValue = Byte .parseByte (trimmedValue );
142- break ;
143- case BOOLEAN :
144- objValue = Boolean .parseBoolean (trimmedValue );
145- break ;
146- case PASSWORD :
147- objValue = new Password (trimmedValue );
148- break ;
149- case CHAR :
150- objValue = Character .valueOf (trimmedValue .charAt (0 ));
151- break ;
152- default :
153- break ;
126+ case LONG :
127+ objValue = Long .parseLong (trimmedValue );
128+ break ;
129+ case DOUBLE :
130+ objValue = Double .parseDouble (trimmedValue );
131+ break ;
132+ case FLOAT :
133+ objValue = Float .parseFloat (trimmedValue );
134+ break ;
135+ case INTEGER :
136+ objValue = Integer .parseInt (trimmedValue );
137+ break ;
138+ case SHORT :
139+ objValue = Short .parseShort (trimmedValue );
140+ break ;
141+ case BYTE :
142+ objValue = Byte .parseByte (trimmedValue );
143+ break ;
144+ case BOOLEAN :
145+ objValue = Boolean .parseBoolean (trimmedValue );
146+ break ;
147+ case PASSWORD :
148+ objValue = new Password (trimmedValue );
149+ break ;
150+ case CHAR :
151+ objValue = Character .valueOf (trimmedValue .charAt (0 ));
152+ break ;
153+ default :
154+ break ;
154155 }
155156 }
156157 return objValue ;
@@ -163,117 +164,151 @@ public static Object[] getObjectValues(GwtConfigParameter param, String[] defaul
163164 List <String > trimmedValues = Stream .of (defaultValues ).map (String ::trim ).collect (Collectors .toList ());
164165
165166 switch (type ) {
166- case BOOLEAN :
167- for (String value : trimmedValues ) {
168- if (!value .isEmpty ()) {
169- values .add (Boolean .valueOf (value ));
170- }
167+ case BOOLEAN :
168+ for (String value : trimmedValues ) {
169+ if (!value .isEmpty ()) {
170+ values .add (Boolean .valueOf (value ));
171171 }
172- return values .toArray (new Boolean [] {});
172+ }
173+ return values .toArray (new Boolean [] {});
173174
174- case BYTE :
175- for (String value : trimmedValues ) {
176- if (!value .isEmpty ()) {
177- values .add (Byte .valueOf (value ));
178- }
175+ case BYTE :
176+ for (String value : trimmedValues ) {
177+ if (!value .isEmpty ()) {
178+ values .add (Byte .valueOf (value ));
179179 }
180- return values .toArray (new Byte [] {});
180+ }
181+ return values .toArray (new Byte [] {});
181182
182- case CHAR :
183- for (String value : trimmedValues ) {
184- if (!value .isEmpty ()) {
185- values .add (new Character (value .charAt (0 )));
186- }
183+ case CHAR :
184+ for (String value : trimmedValues ) {
185+ if (!value .isEmpty ()) {
186+ values .add (new Character (value .charAt (0 )));
187187 }
188- return values .toArray (new Character [] {});
188+ }
189+ return values .toArray (new Character [] {});
189190
190- case DOUBLE :
191- for (String value : trimmedValues ) {
192- if (!value .isEmpty ()) {
193- values .add (Double .valueOf (value ));
194- }
191+ case DOUBLE :
192+ for (String value : trimmedValues ) {
193+ if (!value .isEmpty ()) {
194+ values .add (Double .valueOf (value ));
195195 }
196- return values .toArray (new Double [] {});
196+ }
197+ return values .toArray (new Double [] {});
197198
198- case FLOAT :
199- for (String value : trimmedValues ) {
200- if (!value .isEmpty ()) {
201- values .add (Float .valueOf (value ));
202- }
199+ case FLOAT :
200+ for (String value : trimmedValues ) {
201+ if (!value .isEmpty ()) {
202+ values .add (Float .valueOf (value ));
203203 }
204- return values .toArray (new Float [] {});
204+ }
205+ return values .toArray (new Float [] {});
205206
206- case INTEGER :
207- for (String value : trimmedValues ) {
208- if (!value .isEmpty ()) {
209- values .add (Integer .valueOf (value ));
210- }
207+ case INTEGER :
208+ for (String value : trimmedValues ) {
209+ if (!value .isEmpty ()) {
210+ values .add (Integer .valueOf (value ));
211211 }
212- return values .toArray (new Integer [] {});
212+ }
213+ return values .toArray (new Integer [] {});
213214
214- case LONG :
215- for (String value : trimmedValues ) {
216- if (!value .isEmpty ()) {
217- values .add (Long .valueOf (value ));
218- }
215+ case LONG :
216+ for (String value : trimmedValues ) {
217+ if (!value .isEmpty ()) {
218+ values .add (Long .valueOf (value ));
219219 }
220- return values .toArray (new Long [] {});
220+ }
221+ return values .toArray (new Long [] {});
221222
222- case SHORT :
223- for (String value : trimmedValues ) {
224- if (!value .isEmpty ()) {
225- values .add (Short .valueOf (value ));
226- }
223+ case SHORT :
224+ for (String value : trimmedValues ) {
225+ if (!value .isEmpty ()) {
226+ values .add (Short .valueOf (value ));
227227 }
228- return values .toArray (new Short [] {});
228+ }
229+ return values .toArray (new Short [] {});
229230
230- case PASSWORD :
231- for (String value : trimmedValues ) {
232- if (!value .isEmpty ()) {
233- values .add (new Password (value ));
234- }
231+ case PASSWORD :
232+ for (String value : trimmedValues ) {
233+ if (!value .isEmpty ()) {
234+ values .add (new Password (value ));
235235 }
236- return values .toArray (new Password [] {});
236+ }
237+ return values .toArray (new Password [] {});
237238
238- case STRING :
239- for (String value : trimmedValues ) {
240- if (!value .isEmpty ()) {
241- values .add (value );
242- }
239+ case STRING :
240+ for (String value : trimmedValues ) {
241+ if (!value .isEmpty ()) {
242+ values .add (value );
243243 }
244- return values .toArray (new String [] {});
245- default :
246- return null ;
244+ }
245+ return values .toArray (new String [] {});
246+ default :
247+ return null ;
247248 }
248249 }
249250
250251 public static Object getUserDefinedObject (GwtConfigParameter param , Object currentObjValue ) {
251- Object objValue ;
252252
253253 final int cardinality = param .getCardinality ();
254254 if (cardinality == 0 || cardinality == 1 || cardinality == -1 ) {
255- String strValue = param .getValue ();
255+ return getUserDefinedObjectScalar (param , currentObjValue );
256+ } else {
257+ return getUserDefinedObjectArray (param , currentObjValue );
258+ }
259+ }
256260
257- if (currentObjValue instanceof Password && PASSWORD_PLACEHOLDER .equals (strValue )) {
258- objValue = currentObjValue ;
259- } else {
260- objValue = getObjectValue (param );
261+ private static Object getUserDefinedObjectScalar (GwtConfigParameter param , Object currentObjValue ) {
262+ String strValue = param .getValue ();
263+
264+ if (param .getType () == GwtConfigParameterType .PASSWORD && PASSWORD_PLACEHOLDER .equals (strValue )) {
265+
266+ if (currentObjValue instanceof Password ) {
267+ return currentObjValue ;
261268 }
262- } else {
263- String [] strValues = param .getValues ();
269+
270+ if (param .isRequired ()) {
271+ final String defaultValue = param .getDefault ();
272+
273+ if (defaultValue != null && !defaultValue .trim ().isEmpty ()) {
274+ final GwtConfigParameter cloned = new GwtConfigParameter (param );
275+ cloned .setValue (defaultValue );
276+ return getObjectValue (cloned );
277+ }
278+ }
279+ }
280+
281+ return getObjectValue (param );
282+ }
283+
284+ private static Object getUserDefinedObjectArray (GwtConfigParameter param , Object currentObjValue ) {
285+ String [] strValues = param .getValues ();
286+
287+ if (param .getType () == GwtConfigParameterType .PASSWORD ) {
288+
289+ Optional <String []> current = Optional .empty ();
264290
265291 if (currentObjValue instanceof Password []) {
266- Password [] currentPasswordValue = (Password []) currentObjValue ;
292+ current = Optional .of (Arrays .stream ((Password []) currentObjValue ).map (p -> new String (p .getPassword ()))
293+ .collect (Collectors .toList ()).toArray (new String [] {}));
294+ } else if (param .isRequired ()) {
295+ final String defaultValue = param .getDefault ();
296+
297+ if (defaultValue != null && !defaultValue .trim ().isEmpty ()) {
298+ current = Optional .of (StringUtil .splitValues (defaultValue ));
299+ }
300+ }
301+
302+ if (current .isPresent ()) {
267303 for (int i = 0 ; i < strValues .length ; i ++) {
268- if (PASSWORD_PLACEHOLDER .equals (strValues [i ])) {
269- strValues [i ] = new String ( currentPasswordValue [i ]. getPassword ()) ;
304+ if (PASSWORD_PLACEHOLDER .equals (strValues [i ]) && i < current . get (). length ) {
305+ strValues [i ] = current . get () [i ];
270306 }
271307 }
272308 }
273-
274- objValue = getObjectValues (param , strValues );
275309 }
276- return objValue ;
310+
311+ return getObjectValues (param , strValues );
277312 }
278313
279314 /**
0 commit comments