org.springframework.boot.configurationmetadata.ValueHint Java Examples

The following examples show how to use org.springframework.boot.configurationmetadata.ValueHint. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProposalsCollectorSupportUtils.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
void addValueHintsProposals(final String dsl, AppRegistration appRegistration, final List<CompletionProposal> collector, final String propertyName, final ValueHintProvider[] valueHintProviders){
	final Resource metadataResource = this.appRegistry.getAppMetadataResource(appRegistration);
	if (metadataResource != null) {
		final URLClassLoader classLoader = metadataResolver.createAppClassLoader(metadataResource);
		this.doWithClassLoader(classLoader, () -> {
			CompletionProposal.Factory proposals = CompletionProposal.expanding(dsl);
			List<ConfigurationMetadataProperty> whiteList = metadataResolver.listProperties(metadataResource);
			for (ConfigurationMetadataProperty property : metadataResolver.listProperties(metadataResource, true)) {
				if (CompletionUtils.isMatchingProperty(propertyName, property, whiteList)) {
					for (ValueHintProvider valueHintProvider : valueHintProviders) {
						for (ValueHint valueHint : valueHintProvider.generateValueHints(property, classLoader)) {
							collector.add(proposals.withSuffix(String.valueOf(valueHint.getValue()),
									valueHint.getShortDescription()));
						}
					}
				}
			}
			return null;
		});
	}
}
 
Example #2
Source File: EnumValueHintProvider.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Override
public List<ValueHint> generateValueHints(ConfigurationMetadataProperty property, ClassLoader classLoader) {
	List<ValueHint> result = new ArrayList<>();
	if (ClassUtils.isPresent(property.getType(), classLoader)) {
		Class<?> clazz = ClassUtils.resolveClassName(property.getType(), classLoader);
		if (clazz.isEnum()) {
			for (Object o : clazz.getEnumConstants()) {
				ValueHint hint = new ValueHint();
				hint.setValue(o);
				result.add(hint);
			}
		}
	}
	return result;
}
 
Example #3
Source File: EnumValueHintProvider.java    From spring-cloud-dashboard with Apache License 2.0 5 votes vote down vote up
@Override
public List<ValueHint> generateValueHints(ConfigurationMetadataProperty property, ClassLoader classLoader) {
	List<ValueHint> result = new ArrayList<>();
	if (ClassUtils.isPresent(property.getType(), classLoader)) {
		Class<?> clazz = ClassUtils.resolveClassName(property.getType(), classLoader);
		if (clazz.isEnum()) {
			for (Object o : clazz.getEnumConstants()) {
				ValueHint hint = new ValueHint();
				hint.setValue(o);
				result.add(hint);
			}
		}
	}
	return result;
}
 
Example #4
Source File: ProposalsCollectorSupportUtils.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
boolean addAlreadyTypedValueHintsProposals(final String text, AppRegistration appRegistration, final List<CompletionProposal> collector, final String propertyName, final ValueHintProvider[] valueHintProviders, final String alreadyTyped){
	final Resource metadataResource = this.appRegistry.getAppMetadataResource(appRegistration);
	boolean result = false;
	if (metadataResource == null) {
		result = false;
	} else {
		final URLClassLoader classLoader = metadataResolver.createAppClassLoader(metadataResource);
		result =  this.doWithClassLoader(classLoader, () -> {
			CompletionProposal.Factory proposals = CompletionProposal.expanding(text);
			List<ConfigurationMetadataProperty> allProps = metadataResolver.listProperties(metadataResource, true);
			List<ConfigurationMetadataProperty> whiteListedProps = metadataResolver.listProperties(metadataResource);
			for (ConfigurationMetadataProperty property : allProps) {
				if (CompletionUtils.isMatchingProperty(propertyName, property, whiteListedProps)) {
					for (ValueHintProvider valueHintProvider : valueHintProviders) {
						List<ValueHint> valueHints = valueHintProvider.generateValueHints(property, classLoader);
						if (!valueHints.isEmpty() && valueHintProvider.isExclusive(property)) {
							collector.clear();
						}
						for (ValueHint valueHint : valueHints) {
							String candidate = String.valueOf(valueHint.getValue());
							if (!candidate.equals(alreadyTyped) && candidate.startsWith(alreadyTyped)) {
								collector.add(proposals.withSuffix(candidate.substring(alreadyTyped.length()),
										valueHint.getShortDescription()));
							}
						}
						if (!valueHints.isEmpty() && valueHintProvider.isExclusive(property)) {
							return true;
						}
					}
				}
			}
			return false;
		});
	}
	return result;
}
 
Example #5
Source File: Utils.java    From nb-springboot with Apache License 2.0 5 votes vote down vote up
public static void completeEnum(ClassPath cp, String dataType, String filter, Consumer<ValueHint> consumer) {
    try {
        Object[] enumvals = cp.getClassLoader(true).loadClass(dataType).getEnumConstants();
        if (enumvals != null) {
            for (Object val : enumvals) {
                final String valName = val.toString().toLowerCase();
                if (filter == null || valName.contains(filter)) {
                    consumer.accept(createEnumHint(valName));
                }
            }
        }
    } catch (ClassNotFoundException ex) {
        // enum not available in project classpath, no completion possible
    }
}
 
Example #6
Source File: Utils.java    From nb-springboot with Apache License 2.0 5 votes vote down vote up
public static void completeMimetype(String filter, Consumer<ValueHint> consumer) {
    HintSupport.MIMETYPES.stream()
            .filter(mime -> mime.toLowerCase().contains(filter.toLowerCase()))
            .forEachOrdered(mime -> {
                consumer.accept(Utils.createHint(mime));
            });
}
 
Example #7
Source File: Utils.java    From nb-springboot with Apache License 2.0 5 votes vote down vote up
public static void completeLocale(String filter, Consumer<ValueHint> consumer) {
    HintSupport.getAllLocales().stream()
            .filter(lclName -> lclName.toLowerCase().contains(filter.toLowerCase()))
            .forEachOrdered(lclName -> {
                consumer.accept(Utils.createHint(lclName));
            });
}
 
Example #8
Source File: Utils.java    From nb-springboot with Apache License 2.0 5 votes vote down vote up
public static void completeCharset(String filter, Consumer<ValueHint> consumer) {
    HintSupport.getAllCharsets().stream()
            .filter(chrsName -> chrsName.toLowerCase().contains(filter.toLowerCase()))
            .forEachOrdered(chrsName -> {
                consumer.accept(Utils.createHint(chrsName));
            });
}
 
Example #9
Source File: Utils.java    From nb-springboot with Apache License 2.0 5 votes vote down vote up
public static void completeBoolean(String filter, Consumer<ValueHint> consumer) {
    if ("true".contains(filter)) {
        consumer.accept(Utils.createHint("true"));
    }
    if ("false".contains(filter)) {
        consumer.accept(Utils.createHint("false"));
    }
}
 
Example #10
Source File: BooleanValueHintProvider.java    From spring-cloud-dataflow with Apache License 2.0 4 votes vote down vote up
@Override
public List<ValueHint> generateValueHints(ConfigurationMetadataProperty property, ClassLoader classLoader) {
	return "java.lang.Boolean".equals(property.getType()) ? BOOLEANS : Collections.<ValueHint>emptyList();
}
 
Example #11
Source File: DefaultValueHintProvider.java    From spring-cloud-dataflow with Apache License 2.0 4 votes vote down vote up
@Override
public List<ValueHint> generateValueHints(ConfigurationMetadataProperty property, ClassLoader classLoader) {
	return property.getHints().getValueHints();
}
 
Example #12
Source File: CfgPropCompletionDocumentation.java    From nb-springboot with Apache License 2.0 4 votes vote down vote up
@Override
public String getText() {
    StringBuilder sb = new StringBuilder();
    // name
    sb.append("<b>").append(configurationMeta.getId()).append("</b>");
    // type (may be null for deprecated props)
    final String type = configurationMeta.getType();
    if (type != null) {
        sb.append("<br/>").append(simpleHtmlEscape(type));
    }
    // deprecation (optional)
    Deprecation deprecation = configurationMeta.getDeprecation();
    if (deprecation != null) {
        sb.append("<br/><br/><b><i>");
        if (Utils.isErrorDeprecated(configurationMeta)) {
            sb.append("REMOVED");
        } else {
            sb.append("Deprecated");
        }
        // deprecation reason if present
        String reason = deprecation.getReason();
        if (reason != null) {
            sb.append(":</i></b> ").append(simpleHtmlEscape(reason));
        } else {
            sb.append("</i></b>");
        }
        String replacement = deprecation.getReplacement();
        if (replacement != null) {
            sb.append("<br/><i>Replaced by:</i> <code>").append(replacement).append("</code>");
        }
    }
    // default value (optional)
    final Object defaultValue = configurationMeta.getDefaultValue();
    if (null != defaultValue) {
        sb.append("<br/><br/><i>Default:</i> ");
        if (defaultValue instanceof Object[]) {
            sb.append(Arrays.toString((Object[]) defaultValue));
        } else {
            sb.append(String.valueOf(defaultValue));
        }
    }
    // description (optional)
    final String description = configurationMeta.getDescription();
    if (description != null) {
        sb.append("<br/><br/>").append(description);
    }
    // list of values (optional)
    Hints hints = configurationMeta.getHints();
    List<ValueHint> valueHints = hints.getValueHints();
    if (valueHints != null && !valueHints.isEmpty()) {
        sb.append("<br/><br/><table><tr><td><i>Value</i></td><td><i>Description</i></td></tr>");
        for (ValueHint vHint : valueHints) {
            sb.append("<tr><td>").append(vHint.getValue()).append("</td><td>");
            final String vDesc = vHint.getDescription();
            if (vDesc != null) {
                sb.append(simpleHtmlEscape(vDesc)).append("</th></tr>");
            }
        }
        sb.append("</table>");
    }
    return sb.toString();
}
 
Example #13
Source File: CfgPropValueCompletionDocumentation.java    From nb-springboot with Apache License 2.0 4 votes vote down vote up
public CfgPropValueCompletionDocumentation(ValueHint valueHint) {
    this.valueHint = valueHint;
}
 
Example #14
Source File: KeyCompletionItem.java    From nb-springboot with Apache License 2.0 4 votes vote down vote up
public ValueHint getHint() {
    return hint;
}
 
Example #15
Source File: KeyCompletionItem.java    From nb-springboot with Apache License 2.0 4 votes vote down vote up
public KeyCompletionItem(ValueHint hint, int dotOffset, int caretOffset) {
    this.hint = hint;
    this.dotOffset = dotOffset;
    this.caretOffset = caretOffset;
}
 
Example #16
Source File: ValueCompletionItem.java    From nb-springboot with Apache License 2.0 4 votes vote down vote up
public ValueHint getHint() {
    return hint;
}
 
Example #17
Source File: ValueCompletionItem.java    From nb-springboot with Apache License 2.0 4 votes vote down vote up
public ValueCompletionItem(ValueHint hint, int dotOffset, int caretOffset, boolean continueCompletion) {
    this.hint = hint;
    this.dotOffset = dotOffset;
    this.caretOffset = caretOffset;
    this.continueCompletion = continueCompletion;
}
 
Example #18
Source File: ValueCompletionItem.java    From nb-springboot with Apache License 2.0 4 votes vote down vote up
public ValueCompletionItem(ValueHint hint, int dotOffset, int caretOffset) {
    this(hint, dotOffset, caretOffset, false);
}
 
Example #19
Source File: CfgPropsCompletionQuery.java    From nb-springboot with Apache License 2.0 4 votes vote down vote up
private void completePropName(CompletionResultSet completionResultSet, String filter, int startOffset, int caretOffset) {
    final Preferences prefs = NbPreferences.forModule(PrefConstants.class);
    final boolean bDeprLast = prefs.getBoolean(PREF_DEPR_SORT_LAST, true);
    final boolean bErrorShow = prefs.getBoolean(PREF_DEPR_ERROR_SHOW, true);
    long mark = System.currentTimeMillis();
    // check if completing a property map key
    if (filter != null) {
        ClassPath cpExec = Utils.execClasspathForProj(proj);
        for (String mapProp : sbs.getMapPropertyNames()) {
            if (filter.length() > mapProp.length() && filter.contains(mapProp)) {
                String key = filter.substring(mapProp.length() + 1);
                logger.log(FINER, "Completing key for map property {0} from: ''{1}''", new Object[]{mapProp, key});
                final ConfigurationMetadataProperty propMetadata = sbs.getPropertyMetadata(mapProp);
                // if key data type is an enum complete with enum values
                final String keyDataType = extractMapKeyType(propMetadata);
                if (!keyDataType.contains("<")) {
                    Utils.completeEnum(cpExec, keyDataType, key, hint -> {
                        completionResultSet.addItem(new KeyCompletionItem(hint, startOffset + mapProp.length() + 1,
                                caretOffset));
                    });
                }
                // check if key data type is boolean
                if (keyDataType.equals("java.lang.Boolean")) {
                    Utils.completeBoolean(key, hint -> {
                        completionResultSet.addItem(new KeyCompletionItem(hint, startOffset + mapProp.length() + 1,
                                caretOffset));
                    });
                }
                // check if key data type is Charset
                if (keyDataType.equals("java.nio.charset.Charset")) {
                    Utils.completeCharset(key, hint -> {
                        completionResultSet.addItem(new KeyCompletionItem(hint, startOffset + mapProp.length() + 1,
                                caretOffset));
                    });
                }
                // add metadata defined key hints to completion list
                final Hints hints = propMetadata.getHints();
                if (!hints.getKeyHints().isEmpty()) {
                    String keyLowcase = key.toLowerCase();
                    for (ValueHint keyHint : hints.getKeyHints()) {
                        if (keyHint.getValue().toString().toLowerCase().contains(keyLowcase)) {
                            completionResultSet.addItem(new KeyCompletionItem(keyHint, startOffset + mapProp.length() + 1,
                                    caretOffset));
                        }
                    }
                }
                // invoke key providers
                if (!hints.getKeyProviders().isEmpty()) {
                    logger.log(FINER, "Key providers for {0}:", mapProp);
                    for (ValueProvider vp : hints.getKeyProviders()) {
                        logger.log(FINER, "  {0} - params: {1}", new Object[]{vp.getName(), vp.getParameters()});
                        sbs.getHintProvider(vp.getName()).provide(vp.getParameters(), propMetadata, key, true,
                                completionResultSet, startOffset + mapProp.length() + 1, caretOffset);
                    }
                }
            }
        }
    }
    for (ConfigurationMetadataProperty propMeta : sbs.queryPropertyMetadata(filter)) {
        if (Utils.isErrorDeprecated(propMeta)) {
            // show error level deprecated props based on pref
            if (bErrorShow) {
                completionResultSet.addItem(new CfgPropCompletionItem(propMeta, startOffset, caretOffset, bDeprLast));
            }
        } else {
            completionResultSet.addItem(new CfgPropCompletionItem(propMeta, startOffset, caretOffset, bDeprLast));
        }
    }
    final long elapsed = System.currentTimeMillis() - mark;
    logger.log(FINE, "Name completion of ''{0}'' took: {1} msecs", new Object[]{filter, elapsed});
}
 
Example #20
Source File: BooleanValueHintProvider.java    From spring-cloud-dashboard with Apache License 2.0 4 votes vote down vote up
@Override
public List<ValueHint> generateValueHints(ConfigurationMetadataProperty property, ClassLoader classLoader) {
	return "java.lang.Boolean".equals(property.getType())
			? BOOLEANS
			: Collections.<ValueHint>emptyList();
}
 
Example #21
Source File: DefaultValueHintProvider.java    From spring-cloud-dashboard with Apache License 2.0 4 votes vote down vote up
@Override
public List<ValueHint> generateValueHints(ConfigurationMetadataProperty property, ClassLoader classLoader) {
	return property.getValueHints();
}
 
Example #22
Source File: Utils.java    From nb-springboot with Apache License 2.0 3 votes vote down vote up
/**
 * Create a {@code ValueHint} object from the given value and description.
 *
 * @param value the value to use
 * @param description the description to use
 * @return a ValueHint object
 */
public static ValueHint createHint(String value, String description) {
    ValueHint vh = new ValueHint();
    vh.setValue(value);
    vh.setDescription(description);
    return vh;
}
 
Example #23
Source File: Utils.java    From nb-springboot with Apache License 2.0 2 votes vote down vote up
/**
 * Create a {@code ValueHint} object from the given value.
 * <p>
 * Created hint has no description.
 *
 * @param value the value to use
 * @return a ValueHint object
 */
public static ValueHint createHint(String value) {
    ValueHint vh = new ValueHint();
    vh.setValue(value);
    return vh;
}
 
Example #24
Source File: Utils.java    From nb-springboot with Apache License 2.0 2 votes vote down vote up
/**
 * Create a {@code ValueHint} object from the given java enumeration value.
 * <p>
 * Created hint has no description and has a Spring Boot property name canonical format.
 *
 * @param value the value to use
 * @return a ValueHint object
 */
public static ValueHint createEnumHint(String value) {
    ValueHint vh = new ValueHint();
    vh.setValue(value.replaceAll("_", "-"));
    return vh;
}
 
Example #25
Source File: ValueHintProvider.java    From spring-cloud-dataflow with Apache License 2.0 2 votes vote down vote up
/**
 * For a given property, return a list of {@link ValueHint} that may apply.
 *
 * @param property property for which to generate value hints
 * @param classLoader class loader for the artifact/module that this property applies
 * to; this may be used to load other classes/resources for generating value hints
 * @return list of value hints for the provided property
 */
List<ValueHint> generateValueHints(ConfigurationMetadataProperty property, ClassLoader classLoader);
 
Example #26
Source File: ValueHintProvider.java    From spring-cloud-dashboard with Apache License 2.0 2 votes vote down vote up
/**
 * For a given property, return a list of {@link ValueHint} that may apply.
 *
 * @param property     property for which to generate value hints
 * @param classLoader  class loader for the artifact/module that this
 * property applies to; this may be used to load other classes/resources
 * for generating value hints
 * @return list of value hints for the provided property
 */
List<ValueHint> generateValueHints(ConfigurationMetadataProperty property, ClassLoader classLoader);