Example usage for java.beans PropertyEditor setAsText

List of usage examples for java.beans PropertyEditor setAsText

Introduction

In this page you can find the example usage for java.beans PropertyEditor setAsText.

Prototype

void setAsText(String text) throws java.lang.IllegalArgumentException;

Source Link

Document

Set the property value by parsing a given String.

Usage

From source file:io.tilt.minka.utils.Defaulter.java

private static PropertyEditor edit(final Properties props, final Object configurable, final Field staticField,
        final Field instanceField) throws IllegalAccessException {

    staticField.setAccessible(true);//from w  w w  .  j a v a 2s  .  c o  m
    final String name = instanceField.getName();
    final String staticValue = staticField.get(configurable).toString();
    final Object propertyOrDefault = props.getProperty(name, System.getProperty(name, staticValue));
    final String objName = configurable.getClass().getSimpleName();
    final PropertyEditor editor = PropertyEditorManager.findEditor(instanceField.getType());
    final String setLog = "Defaulter: set <{}> field [{}] = '{}' from {} ";
    try {
        editor.setAsText(propertyOrDefault.toString());
        logger.info(setLog, objName, name, editor.getValue(),
                propertyOrDefault != staticValue ? " property " : staticField.getName());
    } catch (Exception e) {
        logger.error(
                "Defaulter: object <{}> field: {} does not accept property or static "
                        + "default value: {} (reason: {})",
                objName, name, propertyOrDefault, e.getClass().getSimpleName());
        try { // at this moment only prop. might've been failed
            editor.setAsText(staticValue);
            logger.info(setLog, objName, name, editor.getValue(), staticField.getName());
        } catch (Exception e2) {
            final StringBuilder sb = new StringBuilder().append("Defaulter: object <").append(objName)
                    .append("> field: ").append(name).append(" does not accept static default value: ")
                    .append(propertyOrDefault).append(" (reason: ").append(e.getClass().getSimpleName())
                    .append(")");
            throw new RuntimeException(sb.toString());
        }

    }

    return editor;
}

From source file:org.fusesource.meshkeeper.util.internal.IntrospectionSupport.java

private static Object convert(Object value, Class<?> type) {
    PropertyEditor editor = PropertyEditorManager.findEditor(type);
    if (editor != null) {
        editor.setAsText(value.toString());
        return editor.getValue();
    }/*from  ww  w.  j  a v  a 2s.  c  om*/
    return null;
}

From source file:com.wavemaker.runtime.server.ServerUtils.java

private static Object convert(String value, Class clazz) {
    PropertyEditor editor = PropertyEditorManager.findEditor(clazz);
    editor.setAsText(value);
    return editor.getValue();
}

From source file:org.apache.camel.util.IntrospectionSupport.java

@SuppressWarnings("unchecked")
private static Object convert(TypeConverter typeConverter, Class type, Object value)
        throws URISyntaxException, NoTypeConversionAvailableException {
    if (typeConverter != null) {
        return typeConverter.mandatoryConvertTo(type, value);
    }// w w w .  j  a  va2  s  . co  m
    PropertyEditor editor = PropertyEditorManager.findEditor(type);
    if (editor != null) {
        editor.setAsText(value.toString());
        return editor.getValue();
    }
    if (type == URI.class) {
        return new URI(value.toString());
    }
    return null;
}

From source file:io.fabric8.devops.ProjectConfigs.java

/**
 * Configures the given {@link ProjectConfig} with a map of key value pairs from
 * something like a JBoss Forge command/*from  w w w  .  j  a  va 2 s. c o  m*/
 */
public static void configureProperties(ProjectConfig config, Map map) {
    Class<? extends ProjectConfig> clazz = config.getClass();
    BeanInfo beanInfo = null;
    try {
        beanInfo = Introspector.getBeanInfo(clazz);
    } catch (IntrospectionException e) {
        LOG.warn("Could not introspect " + clazz.getName() + ". " + e, e);
    }
    if (beanInfo != null) {
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor descriptor : propertyDescriptors) {
            Method writeMethod = descriptor.getWriteMethod();
            if (writeMethod != null) {
                String name = descriptor.getName();
                Object value = map.get(name);
                if (value != null) {
                    Object safeValue = null;
                    Class<?> propertyType = descriptor.getPropertyType();
                    if (propertyType.isInstance(value)) {
                        safeValue = value;
                    } else {
                        PropertyEditor editor = descriptor.createPropertyEditor(config);
                        if (editor == null) {
                            editor = PropertyEditorManager.findEditor(propertyType);
                        }
                        if (editor != null) {
                            String text = value.toString();
                            editor.setAsText(text);
                            safeValue = editor.getValue();
                        } else {
                            LOG.warn("Cannot update property " + name + " with value " + value + " of type "
                                    + propertyType.getName() + " on " + clazz.getName());
                        }
                    }
                    if (safeValue != null) {
                        try {
                            writeMethod.invoke(config, safeValue);
                        } catch (Exception e) {
                            LOG.warn("Failed to set property " + name + " with value " + value + " on "
                                    + clazz.getName() + " " + config + ". " + e, e);
                        }

                    }
                }
            }
        }
    }
    String flow = null;
    Object flowValue = map.get("pipeline");
    if (flowValue == null) {
        flowValue = map.get("flow");
    }
    if (flowValue != null) {
        flow = flowValue.toString();
    }
    config.setPipeline(flow);
}

From source file:ar.com.zauber.commons.web.proxy.URLRequestMapperEditorTest.java

/** foo */
public final void testEmptyLines() {
    final PropertyEditor pe = new URLRequestMapperEditor();
    pe.setAsText("    \n" + "a = b          \n");
}

From source file:ar.com.zauber.commons.web.proxy.URLRequestMapperEditorTest.java

/** foo */
public final void testNull() {
    final PropertyEditor pe = new URLRequestMapperEditor();
    pe.setAsText("          ");
    assertNull(pe.getValue());/*  w  w w .j  a  v  a  2 s  .co  m*/
}

From source file:ar.com.zauber.commons.web.proxy.URLRequestMapperEditorTest.java

/** @throws MalformedURLException on error */
public final void testFactory() throws MalformedURLException {
    final PropertyEditor pe = new URLRequestMapperEditor();
    pe.setAsText("^/public/(.*)$=http://localhost:9095/nexus/content/" + "repositories/public/$1\n"
            + "^/nexus/(.*)$=http://localhost:9095/nexus/$1\n");

    final URLRequestMapper c = (URLRequestMapper) pe.getValue();
    assertEquals(new URL("http://localhost:9095/nexus/foo/bar"),
            c.getProxiedURLFromRequest(new MockHttpServletRequest("GET", "/nexus/foo/bar")).getURL());
}

From source file:ar.com.zauber.commons.web.proxy.URLRequestMapperEditorTest.java

/** foo */
public final void testInvalidLine() {
    final PropertyEditor pe = new URLRequestMapperEditor();
    try {//from  w  ww . j  a  va 2  s.  c  o  m
        pe.setAsText("a = b          \n" + "c\n");
        fail();
    } catch (IllegalArgumentException e) {
        // ok
    }
}

From source file:org.tinygroup.springmvc.support.ConventionRestfulWebArgumentResolver.java

@SuppressWarnings("unchecked")
public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception {

    if (!isConventionalRestful(webRequest)) {
        return UNRESOLVED;
    }//from  w w  w . j  a v a  2 s. co  m
    // and the paramName is id
    if (!StringUtils.equals("id", methodParameter.getParameterName())) {
        return UNRESOLVED;
    }

    Map<String, String> uriTemplateVariables = null;
    if (BeanUtils.isSimpleProperty(methodParameter.getParameterType())) {
        uriTemplateVariables = (Map<String, String>) webRequest
                .getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
    }
    if (uriTemplateVariables != null) {
        String val = uriTemplateVariables.get(methodParameter.getParameterName());
        if (val != null && String.class.equals(methodParameter.getParameterType())) {
            return val;
        } else if (val != null) {
            PropertyEditor editor = getDefaultEditor(methodParameter.getParameterType());
            if (editor != null) {
                editor.setAsText(val);
                return editor.getValue();
            }
        }
    }
    return UNRESOLVED;
}