Example usage for org.springframework.beans BeanWrapper getPropertyValue

List of usage examples for org.springframework.beans BeanWrapper getPropertyValue

Introduction

In this page you can find the example usage for org.springframework.beans BeanWrapper getPropertyValue.

Prototype

@Nullable
Object getPropertyValue(String propertyName) throws BeansException;

Source Link

Document

Get the current value of the specified property.

Usage

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testNumberObjects() {
    NumberTestBean tb = new NumberTestBean();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(tb);

    try {/* w  ww. j  a  v a2s . c  o  m*/
        bw.setPropertyValue("short2", "2");
        bw.setPropertyValue("int2", "8");
        bw.setPropertyValue("long2", "6");
        bw.setPropertyValue("bigInteger", "3");
        bw.setPropertyValue("float2", "8.1");
        bw.setPropertyValue("double2", "6.1");
        bw.setPropertyValue("bigDecimal", "4.0");
    } catch (BeansException ex) {
        fail("Should not throw BeansException: " + ex.getMessage());
    }

    assertTrue("Correct short2 value", new Short("2").equals(bw.getPropertyValue("short2")));
    assertTrue("Correct short2 value", new Short("2").equals(tb.getShort2()));
    assertTrue("Correct int2 value", new Integer("8").equals(bw.getPropertyValue("int2")));
    assertTrue("Correct int2 value", new Integer("8").equals(tb.getInt2()));
    assertTrue("Correct long2 value", new Long("6").equals(bw.getPropertyValue("long2")));
    assertTrue("Correct long2 value", new Long("6").equals(tb.getLong2()));
    assertTrue("Correct bigInteger value", new BigInteger("3").equals(bw.getPropertyValue("bigInteger")));
    assertTrue("Correct bigInteger value", new BigInteger("3").equals(tb.getBigInteger()));
    assertTrue("Correct float2 value", new Float("8.1").equals(bw.getPropertyValue("float2")));
    assertTrue("Correct float2 value", new Float("8.1").equals(tb.getFloat2()));
    assertTrue("Correct double2 value", new Double("6.1").equals(bw.getPropertyValue("double2")));
    assertTrue("Correct double2 value", new Double("6.1").equals(tb.getDouble2()));
    assertTrue("Correct bigDecimal value", new BigDecimal("4.0").equals(bw.getPropertyValue("bigDecimal")));
    assertTrue("Correct bigDecimal value", new BigDecimal("4.0").equals(tb.getBigDecimal()));
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testNumberCoercion() {
    NumberTestBean tb = new NumberTestBean();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(tb);

    try {//from  w  ww  .  j a  v  a  2  s .  com
        bw.setPropertyValue("short2", new Integer(2));
        bw.setPropertyValue("int2", new Long(8));
        bw.setPropertyValue("long2", new BigInteger("6"));
        bw.setPropertyValue("bigInteger", new Integer(3));
        bw.setPropertyValue("float2", new Double(8.1));
        bw.setPropertyValue("double2", new BigDecimal(6.1));
        bw.setPropertyValue("bigDecimal", new Float(4.0));
    } catch (BeansException ex) {
        fail("Should not throw BeansException: " + ex.getMessage());
    }

    assertTrue("Correct short2 value", new Short("2").equals(bw.getPropertyValue("short2")));
    assertTrue("Correct short2 value", new Short("2").equals(tb.getShort2()));
    assertTrue("Correct int2 value", new Integer("8").equals(bw.getPropertyValue("int2")));
    assertTrue("Correct int2 value", new Integer("8").equals(tb.getInt2()));
    assertTrue("Correct long2 value", new Long("6").equals(bw.getPropertyValue("long2")));
    assertTrue("Correct long2 value", new Long("6").equals(tb.getLong2()));
    assertTrue("Correct bigInteger value", new BigInteger("3").equals(bw.getPropertyValue("bigInteger")));
    assertTrue("Correct bigInteger value", new BigInteger("3").equals(tb.getBigInteger()));
    assertTrue("Correct float2 value", new Float("8.1").equals(bw.getPropertyValue("float2")));
    assertTrue("Correct float2 value", new Float("8.1").equals(tb.getFloat2()));
    assertTrue("Correct double2 value", new Double("6.1").equals(bw.getPropertyValue("double2")));
    assertTrue("Correct double2 value", new Double("6.1").equals(tb.getDouble2()));
    assertTrue("Correct bigDecimal value", new BigDecimal("4.0").equals(bw.getPropertyValue("bigDecimal")));
    assertTrue("Correct bigDecimal value", new BigDecimal("4.0").equals(tb.getBigDecimal()));
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testIndexedProperties() {
    IndexedTestBean bean = new IndexedTestBean();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(bean);
    TestBean tb0 = bean.getArray()[0];/*from   w w  w  .  j  av a2 s.  co m*/
    TestBean tb1 = bean.getArray()[1];
    TestBean tb2 = ((TestBean) bean.getList().get(0));
    TestBean tb3 = ((TestBean) bean.getList().get(1));
    TestBean tb6 = ((TestBean) bean.getSet().toArray()[0]);
    TestBean tb7 = ((TestBean) bean.getSet().toArray()[1]);
    TestBean tb4 = ((TestBean) bean.getMap().get("key1"));
    TestBean tb5 = ((TestBean) bean.getMap().get("key.3"));
    assertEquals("name0", tb0.getName());
    assertEquals("name1", tb1.getName());
    assertEquals("name2", tb2.getName());
    assertEquals("name3", tb3.getName());
    assertEquals("name6", tb6.getName());
    assertEquals("name7", tb7.getName());
    assertEquals("name4", tb4.getName());
    assertEquals("name5", tb5.getName());
    assertEquals("name0", bw.getPropertyValue("array[0].name"));
    assertEquals("name1", bw.getPropertyValue("array[1].name"));
    assertEquals("name2", bw.getPropertyValue("list[0].name"));
    assertEquals("name3", bw.getPropertyValue("list[1].name"));
    assertEquals("name6", bw.getPropertyValue("set[0].name"));
    assertEquals("name7", bw.getPropertyValue("set[1].name"));
    assertEquals("name4", bw.getPropertyValue("map[key1].name"));
    assertEquals("name5", bw.getPropertyValue("map[key.3].name"));
    assertEquals("name4", bw.getPropertyValue("map['key1'].name"));
    assertEquals("name5", bw.getPropertyValue("map[\"key.3\"].name"));
    assertEquals("nameX", bw.getPropertyValue("map[key4][0].name"));
    assertEquals("nameY", bw.getPropertyValue("map[key4][1].name"));

    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("array[0].name", "name5");
    pvs.add("array[1].name", "name4");
    pvs.add("list[0].name", "name3");
    pvs.add("list[1].name", "name2");
    pvs.add("set[0].name", "name8");
    pvs.add("set[1].name", "name9");
    pvs.add("map[key1].name", "name1");
    pvs.add("map['key.3'].name", "name0");
    pvs.add("map[key4][0].name", "nameA");
    pvs.add("map[key4][1].name", "nameB");
    bw.setPropertyValues(pvs);
    assertEquals("name5", tb0.getName());
    assertEquals("name4", tb1.getName());
    assertEquals("name3", tb2.getName());
    assertEquals("name2", tb3.getName());
    assertEquals("name1", tb4.getName());
    assertEquals("name0", tb5.getName());
    assertEquals("name5", bw.getPropertyValue("array[0].name"));
    assertEquals("name4", bw.getPropertyValue("array[1].name"));
    assertEquals("name3", bw.getPropertyValue("list[0].name"));
    assertEquals("name2", bw.getPropertyValue("list[1].name"));
    assertEquals("name8", bw.getPropertyValue("set[0].name"));
    assertEquals("name9", bw.getPropertyValue("set[1].name"));
    assertEquals("name1", bw.getPropertyValue("map[\"key1\"].name"));
    assertEquals("name0", bw.getPropertyValue("map['key.3'].name"));
    assertEquals("nameA", bw.getPropertyValue("map[key4][0].name"));
    assertEquals("nameB", bw.getPropertyValue("map[key4][1].name"));
}

From source file:com.jaspersoft.jasperserver.api.engine.scheduling.quartz.JSMethodInvokingJobDetailFactoryBean.java

public void afterPropertiesSet() throws ClassNotFoundException, NoSuchMethodException {
    prepare();/*w w  w . j a  va 2  s  . c o  m*/
    String name = this.name == null ? beanName : this.name;
    Class jobClass = concurrent
            ? com.jaspersoft.jasperserver.api.engine.scheduling.quartz.JSMethodInvokingJobDetailFactoryBean.MethodInvokingJob.class
            : com.jaspersoft.jasperserver.api.engine.scheduling.quartz.JSMethodInvokingJobDetailFactoryBean.StatefulMethodInvokingJob.class;
    if (jobDetailImplClass != null) {
        jobDetail = (JobDetail) BeanUtils.instantiate(jobDetailImplClass);
        BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(jobDetail);
        bw.setPropertyValue("name", name);
        bw.setPropertyValue("group", group);
        bw.setPropertyValue("jobClass", jobClass);
        // 2012-01-25   thorick:  no more durability in Quartz 2.1.2 ?
        bw.setPropertyValue("durability", Boolean.valueOf(true));
        ((JobDataMap) bw.getPropertyValue("jobDataMap")).put("methodInvoker", this);
    } else {
        jobDetail = new JobDetailImpl(name, group, jobClass);
        // 2012-01-25  thorick:  there is no more volatility in Quartz 2.1.2
        //jobDetail.setVolatility(true);

        if (!(jobDetail instanceof JobDetailImpl))
            throw new RuntimeException("Expected JobDetail to be an instance of '" + JobDetailImpl.class
                    + "' but instead we got '" + jobDetail.getClass().getName() + "'");
        ((JobDetailImpl) jobDetail).setDurability(true);
        jobDetail.getJobDataMap().put("methodInvoker", this);
    }
    if (jobListenerNames != null) {
        String as[];
        int j = (as = jobListenerNames).length;
        for (int i = 0; i < j; i++) {
            String jobListenerName = as[i];
            if (jobDetailImplClass != null)
                throw new IllegalStateException(
                        "Non-global JobListeners not supported on Quartz 2 - manually register a Matcher against the Quartz ListenerManager instead");

            JobKey jk = jobDetail.getKey();
            Matcher<JobKey> matcher = KeyMatcher.keyEquals(jk);
            try {
                getScheduler().getListenerManager().addJobListenerMatcher(jobListenerName, matcher);
            } catch (org.quartz.SchedulerException e) {
                throw new RuntimeException("Error adding Quartz Trigger Listener: " + e.getMessage());
            }

            //jobDetail.addJobListener(jobListenerName);
        }

    }
    postProcessJobDetail(jobDetail);
}

From source file:com.myspring.integration.schedule.quartz.MethodInvokingJobDetailFactoryBean.java

public void afterPropertiesSet() throws ClassNotFoundException, NoSuchMethodException {
    prepare();/*ww w .  j ava 2s. co  m*/

    // Use specific name if given, else fall back to bean name.
    String name = (this.name != null ? this.name : this.beanName);

    // Consider the concurrent flag to choose between stateful and stateless job.
    Class<?> jobClass = (this.concurrent ? MethodInvokingJob.class : StatefulMethodInvokingJob.class);

    // Build JobDetail instance.
    if (jobDetailImplClass != null) {
        // Using Quartz 2.0 JobDetailImpl class...
        Object jobDetail = BeanUtils.instantiate(jobDetailImplClass);
        BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(jobDetail);
        bw.setPropertyValue("name", name);
        bw.setPropertyValue("group", this.group);
        bw.setPropertyValue("jobClass", jobClass);
        bw.setPropertyValue("durability", true);
        ((JobDataMap) bw.getPropertyValue("jobDataMap")).put("methodInvoker", this);
        this.jobDetail = (JobDetail) jobDetail;
    } else {
        throw new IllegalStateException("This instance no longer supports Quartz 1.x as a runtime");
    }

    // Register job listener names.
    if (this.jobListenerNames != null) {
        if (jobDetailImplClass != null) {
            throw new IllegalStateException("Non-global JobListeners not supported on Quartz 2 - "
                    + "manually register a Matcher against the Quartz ListenerManager instead");
        }
    }

    postProcessJobDetail(this.jobDetail);
}

From source file:com.newtranx.util.mysql.fabric.SpringMybatisSetShardKeyAspect.java

@Around("@annotation(com.newtranx.util.mysql.fabric.WithShardKey) || @within(com.newtranx.util.mysql.fabric.WithShardKey)")
public Object setShardKey(ProceedingJoinPoint pjp) throws Throwable {
    Method method = AspectJUtils.getMethod(pjp);
    String key = null;/*from w  w w. jav  a 2s. co  m*/
    boolean force = method.getAnnotation(WithShardKey.class).force();
    int i = 0;
    for (Parameter p : method.getParameters()) {
        ShardKey a = p.getAnnotation(ShardKey.class);
        if (a != null) {
            if (key != null)
                throw new RuntimeException("found multiple shardkey");
            Object obj = pjp.getArgs()[i];
            if (StringUtils.isEmpty(a.property()))
                key = obj.toString();
            else {
                BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(obj);
                key = bw.getPropertyValue(a.property()).toString();
            }
        }
        i++;
    }
    if (key == null)
        throw new RuntimeException("can not find shardkey");
    fabricShardKey.set(key, force);
    return pjp.proceed();
}

From source file:de.tudarmstadt.ukp.clarin.webanno.api.dao.RepositoryServiceDbData.java

@Override
public <T> void saveUserSettings(String aUsername, Project aProject, Mode aSubject, T aConfigurationObject)
        throws IOException {
    BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(aConfigurationObject);
    Properties property = new Properties();
    for (PropertyDescriptor value : wrapper.getPropertyDescriptors()) {
        if (wrapper.getPropertyValue(value.getName()) == null) {
            continue;
        }// w w w .  j  a  v a  2s.  c o  m
        property.setProperty(aSubject + "." + value.getName(),
                wrapper.getPropertyValue(value.getName()).toString());
    }
    String propertiesPath = dir.getAbsolutePath() + PROJECT + aProject.getId() + SETTINGS + aUsername;
    // append existing preferences for the other mode
    if (new File(propertiesPath, annotationPreferencePropertiesFileName).exists()) {
        // aSubject = aSubject.equals(Mode.ANNOTATION) ? Mode.CURATION :
        // Mode.ANNOTATION;
        for (Entry<Object, Object> entry : loadUserSettings(aUsername, aProject).entrySet()) {
            String key = entry.getKey().toString();
            // Maintain other Modes of annotations confs than this one
            if (!key.substring(0, key.indexOf(".")).equals(aSubject.toString())) {
                property.put(entry.getKey(), entry.getValue());
            }
        }
    }
    FileUtils.forceDeleteOnExit(new File(propertiesPath, annotationPreferencePropertiesFileName));
    FileUtils.forceMkdir(new File(propertiesPath));
    property.store(new FileOutputStream(new File(propertiesPath, annotationPreferencePropertiesFileName)),
            null);

    createLog(aProject).info(" Saved preferences file [" + annotationPreferencePropertiesFileName
            + "] for project [" + aProject.getName() + "] with ID [" + aProject.getId() + "] to location: ["
            + propertiesPath + "]");
    createLog(aProject).removeAllAppenders();

}