org.apache.commons.beanutils.ConvertUtilsBean Java Examples

The following examples show how to use org.apache.commons.beanutils.ConvertUtilsBean. 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: BeanConfigurator.java    From sailfish-core with Apache License 2.0 6 votes vote down vote up
public static void loadBean(HierarchicalConfiguration context, Object beanObject, ConvertUtilsBean converter)   
{
	PropertyUtilsBean beanUtils = new PropertyUtilsBean(); 
	
	PropertyDescriptor[] descriptors = beanUtils.getPropertyDescriptors(beanObject);

	try
	{
		for ( PropertyDescriptor descr : descriptors )
		{
			//check that setter exists
			if ( descr.getWriteMethod() != null )
			{
				String value = context.getString(descr.getName());

                   if(converter.lookup(descr.getPropertyType()) != null) {
                       BeanUtils.setProperty(beanObject, descr.getName(), converter.convert(value, descr.getPropertyType()));
                   }
			}
		}
	}
	catch ( Exception e )
	{
		throw new EPSCommonException(e);
	}
}
 
Example #2
Source File: ServiceStorageHelper.java    From sailfish-core with Apache License 2.0 6 votes vote down vote up
public static void convertServiceSettingsToMap(Map<String, String> params, IServiceSettings serviceSettings) {
    ConvertUtilsBean converter = new ConvertUtilsBean();
    PropertyUtilsBean beanUtils = new PropertyUtilsBean();
    PropertyDescriptor[] descriptors = beanUtils.getPropertyDescriptors(serviceSettings);

    for (PropertyDescriptor descr : descriptors) {
        //check that setter exists
        try {
            if (descr.getWriteMethod() != null) {
                Object value = BeanUtils.getProperty(serviceSettings, descr.getName());
                params.put(descr.getName(), converter.convert(value));
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
}
 
Example #3
Source File: PollingConfigBeanTest.java    From amazon-kinesis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testAllPropertiesTransit() {
    PollingConfigBean pollingConfigBean = new PollingConfigBean();
    pollingConfigBean.setIdleTimeBetweenReadsInMillis(1000);
    pollingConfigBean.setMaxGetRecordsThreadPool(20);
    pollingConfigBean.setMaxRecords(5000);
    pollingConfigBean.setRetryGetRecordsInSeconds(30);

    ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
    BeanUtilsBean utilsBean = new BeanUtilsBean(convertUtilsBean);

    MultiLangDaemonConfiguration multiLangDaemonConfiguration = new MultiLangDaemonConfiguration(utilsBean, convertUtilsBean);
    multiLangDaemonConfiguration.setStreamName("test-stream");

    PollingConfig pollingConfig = pollingConfigBean.build(kinesisAsyncClient, multiLangDaemonConfiguration);

    assertThat(pollingConfig.kinesisClient(), equalTo(kinesisAsyncClient));
    assertThat(pollingConfig.streamName(), equalTo(multiLangDaemonConfiguration.getStreamName()));
    assertThat(pollingConfig.idleTimeBetweenReadsInMillis(), equalTo(pollingConfigBean.getIdleTimeBetweenReadsInMillis()));
    assertThat(pollingConfig.maxGetRecordsThreadPool(), equalTo(Optional.of(pollingConfigBean.getMaxGetRecordsThreadPool())));
    assertThat(pollingConfig.maxRecords(), equalTo(pollingConfigBean.getMaxRecords()));
    assertThat(pollingConfig.retryGetRecordsInSeconds(), equalTo(Optional.of(pollingConfigBean.getRetryGetRecordsInSeconds())));
}
 
Example #4
Source File: DynaBeanBuilderSupport.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
DynaBeanBuilderSupport(Class<?> destinedClass, ConvertUtilsBean convertUtilsBean,
        List<String> classPrefixSearchList) {
    this.destinedClass = destinedClass;
    this.convertUtilsBean = convertUtilsBean;
    this.classPrefixSearchList = classPrefixSearchList;
    this.builderClass = builderClassFrom(destinedClass);

    buildProperties();
}
 
Example #5
Source File: BuilderDynaBean.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
public BuilderDynaBean(Class<?> destinedClass, ConvertUtilsBean convertUtilsBean,
        Function<String, ?> emtpyPropertyHandler, List<String> classPrefixSearchList) {
    this.convertUtilsBean = convertUtilsBean;
    this.classPrefixSearchList = classPrefixSearchList;
    this.emptyPropertyHandler = emtpyPropertyHandler;
    initialize(destinedClass);
}
 
Example #6
Source File: MultiLangDaemonConfigurationTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    originalRegionValue = System.getProperty(AWS_REGION_PROPERTY_NAME);
    System.setProperty(AWS_REGION_PROPERTY_NAME, "us-east-1");
    convertUtilsBean = new ConvertUtilsBean();
    utilsBean = new BeanUtilsBean(convertUtilsBean);
}
 
Example #7
Source File: FanoutConfigBeanTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testAllConfigurationTransits() {
    FanoutConfigBean fanoutConfigBean = new FanoutConfigBean();
    fanoutConfigBean.setConsumerArn("consumer-arn");
    fanoutConfigBean.setConsumerName("consumer-name");
    fanoutConfigBean.setMaxDescribeStreamConsumerRetries(10);
    fanoutConfigBean.setMaxDescribeStreamSummaryRetries(20);
    fanoutConfigBean.setRegisterStreamConsumerRetries(30);
    fanoutConfigBean.setRetryBackoffMillis(1000);

    ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
    BeanUtilsBean utilsBean = new BeanUtilsBean(convertUtilsBean);

    MultiLangDaemonConfiguration configuration = new MultiLangDaemonConfiguration(utilsBean, convertUtilsBean);
    configuration.setStreamName("test-stream");
    configuration.setApplicationName("test-application");
    FanOutConfig fanOutConfig =fanoutConfigBean.build(kinesisAsyncClient, configuration);

    assertThat(fanOutConfig.kinesisClient(), equalTo(kinesisAsyncClient));
    assertThat(fanOutConfig.streamName(), equalTo(configuration.getStreamName()));
    assertThat(fanOutConfig.applicationName(), equalTo(configuration.getApplicationName()));
    assertThat(fanOutConfig.consumerArn(), equalTo(fanoutConfigBean.getConsumerArn()));
    assertThat(fanOutConfig.consumerName(), equalTo(fanoutConfigBean.getConsumerName()));
    assertThat(fanOutConfig.maxDescribeStreamConsumerRetries(), equalTo(fanoutConfigBean.getMaxDescribeStreamConsumerRetries()));
    assertThat(fanOutConfig.maxDescribeStreamSummaryRetries(), equalTo(fanoutConfigBean.getMaxDescribeStreamSummaryRetries()));
    assertThat(fanOutConfig.registerStreamConsumerRetries(), equalTo(fanoutConfigBean.getRegisterStreamConsumerRetries()));
    assertThat(fanOutConfig.retryBackoffMillis(), equalTo(fanoutConfigBean.getRetryBackoffMillis()));

}
 
Example #8
Source File: DynaBeanCreateSupport.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
DynaBeanCreateSupport(Class<?> destinedClass, ConvertUtilsBean convertUtilsBean,
        List<String> classPrefixSearchList) {
    this.destinedClass = destinedClass;
    this.convertUtilsBean = convertUtilsBean;
    this.classPrefixSearchList = classPrefixSearchList;

    readTypes();
}
 
Example #9
Source File: PojoPlugin.java    From rice with Educational Community License v2.0 5 votes vote down vote up
public static void initBeanUtils() {
    // begin Kuali Foundation modification
    ConvertUtilsBean convUtils = new ConvertUtilsBean();
    PropertyUtilsBean propUtils = new PojoPropertyUtilsBean();
    BeanUtilsBean pojoBeanUtils = new BeanUtilsBean(convUtils, propUtils);

    BeanUtilsBean.setInstance(pojoBeanUtils);
    logger.fine("Initialized BeanUtilsBean with " + pojoBeanUtils);
    // end Kuali Foundation modification
}
 
Example #10
Source File: ConversionUtils.java    From metron with Apache License 2.0 5 votes vote down vote up
@Override
protected ConvertUtilsBean initialValue() {
  ConvertUtilsBean ret = BeanUtilsBean2.getInstance().getConvertUtils();
  ret.deregister();
  ret.register(false, true, 1);
  return ret;
}
 
Example #11
Source File: IqLoadFileCreator.java    From obevo with Apache License 2.0 5 votes vote down vote up
private static Function<FieldToColumnMapping, String> convertWithDefault(final ConvertUtilsBean cub) {
    return new Function<FieldToColumnMapping, String>() {
        @Override
        public String valueOf(FieldToColumnMapping field) {
            return field.getColumnName() + " DEFAULT '" + cub.convert(field.getDefaultValue()) + "'";
        }
    };
}
 
Example #12
Source File: CsvStaticDataReader.java    From obevo with Apache License 2.0 5 votes vote down vote up
MyDerivedField(int csvVersion, String field, Class targetClass, ConvertUtilsBean cub, String nullToken) {
    this.csvVersion = csvVersion;
    this.field = field;
    this.cub = cub;
    this.targetClass = targetClass;
    this.nullToken = nullToken;
}
 
Example #13
Source File: CsvStaticDataReader.java    From obevo with Apache License 2.0 5 votes vote down vote up
CsvReaderDataSource getFileDataSource(int csvVersion, DaTable table, String content, char dataDelimiter, String nullToken, Function<String, String> convertDbObjectName) {
    CsvReaderDataSource fileSource = new CsvReaderDataSource(csvVersion, "fileSource", new StringReader(content),
            dataDelimiter, convertDbObjectName, nullToken);
    ConvertUtilsBean cub = new ConvertUtilsBean();
    for (DaColumn col : table.getColumns()) {
        Class targetClassName;

        // This is to handle cases in Sybase ASE where a column comes back in quotes, e.g. "Date"
        // This happens if the column name happens to be a keyword, e.g. for Date
        String columnName = col.getName();
        if (columnName.startsWith("\"") && columnName.endsWith("\"")) {
            columnName = columnName.substring(1, columnName.length() - 1);
        }
        try {
            if (col.getColumnDataType().getTypeClassName().equalsIgnoreCase("byte")) {
                // this is to handle "tinyint"
                targetClassName = Integer.class;
            } else if (col.getColumnDataType().getName().equalsIgnoreCase("uuid")) {
                // handling UUID (first seen in Postgres)
                targetClassName = UUID.class;
            } else {
                targetClassName = Class.forName(col.getColumnDataType().getTypeClassName());
            }
        } catch (ClassNotFoundException e) {
            throw new DeployerRuntimeException(e);
        }
        fileSource.addDerivedField(new MyDerivedField(csvVersion, convertDbObjectName.valueOf(columnName),
                targetClassName, cub, nullToken));
    }

    fileSource.init();  // initialize so that we can discover the fields in the file
    return fileSource;
}
 
Example #14
Source File: ServiceStorageHelper.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
public static void convertMapToServiceSettings(IServiceSettings serviceSettings, Map<String, String> params) {
    ConvertUtilsBean converter = new ConvertUtilsBean();
    PropertyUtilsBean beanUtils = new PropertyUtilsBean();
    PropertyDescriptor[] descriptors = beanUtils.getPropertyDescriptors(serviceSettings);

    converter.register(new SailfishURIConverter(), SailfishURI.class);
    converter.register(true, false, 0);

    for(PropertyDescriptor descriptor : descriptors) {
        if(descriptor.getWriteMethod() == null) {
            continue;
        }

        String name = descriptor.getName();
        String value = params.get(name);

        if(value == null) {
            continue;
        }

        try {
            BeanUtils.setProperty(serviceSettings, name, converter.convert(value, descriptor.getPropertyType()));
        } catch(Exception e) {
            throw new EPSCommonException(String.format("Failed to set setting '%s' to: %s", name, value), e);
        }
    }
}
 
Example #15
Source File: BeanConfigurator.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
public static void saveBean(HierarchicalConfiguration context, Object beanObject)
{
	ConvertUtilsBean converter = new ConvertUtilsBean();
	
	PropertyUtilsBean beanUtils = new PropertyUtilsBean(); 
	
	PropertyDescriptor[] descriptors = beanUtils.getPropertyDescriptors(beanObject);

	try
	{
		for ( PropertyDescriptor descr : descriptors )
		{
			//check that setter exists
			if ( descr.getWriteMethod() != null )
			{
				Object value = BeanUtils.getProperty(beanObject, descr.getName());
				
				context.setProperty(descr.getName(), converter.convert(value));
			}
		}
	}
	catch ( Exception e )
	{
		throw new EPSCommonException(e);
	}
	
}
 
Example #16
Source File: MultiLangDaemonConfigTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
    BeanUtilsBean utilsBean = new BeanUtilsBean(convertUtilsBean);
    MultiLangDaemonConfiguration multiLangDaemonConfiguration = new MultiLangDaemonConfiguration(utilsBean,
            convertUtilsBean);
    multiLangDaemonConfiguration.setApplicationName("cool-app");
    multiLangDaemonConfiguration.setStreamName("cool-stream");
    multiLangDaemonConfiguration.setWorkerIdentifier("cool-worker");
    when(credentialsProvider.resolveCredentials()).thenReturn(creds);
    when(creds.accessKeyId()).thenReturn("cool-user");
    when(configurator.getConfiguration(any(Properties.class))).thenReturn(multiLangDaemonConfiguration);
}
 
Example #17
Source File: BuilderDynaBeanTest.java    From amazon-kinesis-client with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() {
    convertUtilsBean = new ConvertUtilsBean();
    utilsBean = new BeanUtilsBean(convertUtilsBean);
}
 
Example #18
Source File: KinesisClientLibConfigurator.java    From amazon-kinesis-client with Apache License 2.0 4 votes vote down vote up
/**
 * Constructor.
 */
public KinesisClientLibConfigurator() {
    this.convertUtilsBean = new ConvertUtilsBean();
    this.utilsBean = new BeanUtilsBean(convertUtilsBean);
    this.configuration = new MultiLangDaemonConfiguration(utilsBean, convertUtilsBean);
}
 
Example #19
Source File: BuilderDynaBean.java    From amazon-kinesis-client with Apache License 2.0 4 votes vote down vote up
public BuilderDynaBean(Class<?> destinedClass, ConvertUtilsBean convertUtilsBean,
        Function<String, ?> emptyPropertyHandler, String... classPrefixSearchList) {
    this(destinedClass, convertUtilsBean, emptyPropertyHandler, Arrays.asList(classPrefixSearchList));
}
 
Example #20
Source File: BuilderDynaBean.java    From amazon-kinesis-client with Apache License 2.0 4 votes vote down vote up
public BuilderDynaBean(Class<?> destinedClass, ConvertUtilsBean convertUtilsBean, String... classPrefixSearchList) {
    this(destinedClass, convertUtilsBean, null, Arrays.asList(classPrefixSearchList));
}
 
Example #21
Source File: ServiceActions.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
@Override
      protected ConvertUtilsBean initialValue() {
    ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
    convertUtilsBean.register(new SailfishURIConverter(), SailfishURI.class);
    return convertUtilsBean;
}
 
Example #22
Source File: CamsFixture.java    From kfs with GNU Affero General Public License v3.0 4 votes vote down vote up
private CamsFixture() {
    BeanUtilsBean instance = BeanUtilsBean.getInstance();
    ConvertUtilsBean convertUtils = instance.getConvertUtils();
    // Register Kuali Decimal Converter
    convertUtils.register(new KualiDecimalConverter(), KualiDecimal.class);
}
 
Example #23
Source File: BeanConfigurator.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
public static void loadBean(HierarchicalConfiguration context, Object beanObject)   
{
	ConvertUtilsBean converter = new ConvertUtilsBean();
	
	loadBean(context, beanObject, converter);
}
 
Example #24
Source File: BeanHelper.java    From commons-configuration with Apache License 2.0 3 votes vote down vote up
/**
 * Initializes the shared {@code BeanUtilsBean} instance. This method sets
 * up custom bean introspection in a way that fluent parameter interfaces
 * are supported.
 *
 * @return the {@code BeanUtilsBean} instance to be used for all property
 *         set operations
 */
private static BeanUtilsBean initBeanUtilsBean()
{
    final PropertyUtilsBean propUtilsBean = new PropertyUtilsBean();
    propUtilsBean.addBeanIntrospector(new FluentPropertyBeanIntrospector());
    return new BeanUtilsBean(new ConvertUtilsBean(), propUtilsBean);
}