Example usage for org.springframework.beans PropertyAccessorFactory forBeanPropertyAccess

List of usage examples for org.springframework.beans PropertyAccessorFactory forBeanPropertyAccess

Introduction

In this page you can find the example usage for org.springframework.beans PropertyAccessorFactory forBeanPropertyAccess.

Prototype

public static BeanWrapper forBeanPropertyAccess(Object target) 

Source Link

Document

Obtain a BeanWrapper for the given target object, accessing properties in JavaBeans style.

Usage

From source file:net.solarnetwork.central.dras.dao.ibatis.DrasIbatisGenericDaoSupport.java

@Override
protected void preprocessInsert(T datum) {
    if (datum.getCreated() == null) {
        // get creation date from current DB transaction
        Object o = getSqlMapClientTemplate().queryForObject("NOW");
        if (o != null) {
            BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(datum);
            wrapper.setPropertyValue("created", o);
        }// w w  w . j  ava 2  s.  co  m
    }
}

From source file:com.saysth.commons.quartz.QuartzJobBean.java

/**
 * This implementation applies the passed-in job data map as bean property
 * values, and delegates to <code>executeInternal</code> afterwards.
 * /*  www.  java2s . c  om*/
 * @see #executeInternal
 */
public final void execute(JobExecutionContext context) throws JobExecutionException {
    try {
        BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
        MutablePropertyValues pvs = new MutablePropertyValues();
        pvs.addPropertyValues(context.getScheduler().getContext());
        pvs.addPropertyValues(context.getMergedJobDataMap());
        bw.setPropertyValues(pvs, true);
    } catch (SchedulerException ex) {
        throw new JobExecutionException(ex);
    }
    executeInternal(context);
}

From source file:net.solarnetwork.util.ClassUtils.java

/**
 * Set bean property values on an object from a Map.
 * /*from   ww w  .ja v  a2 s . co m*/
 * @param o the bean to set JavaBean properties on
 * @param values a Map of JavaBean property names and their corresponding values to set
 */
public static void setBeanProperties(Object o, Map<String, ?> values) {
    BeanWrapper bean = PropertyAccessorFactory.forBeanPropertyAccess(o);
    bean.setPropertyValues(values);
}

From source file:net.solarnetwork.node.util.BeanConfigurationFactoryBean.java

private T createObject() throws InstantiationException, IllegalAccessException {
    T obj = beanClass.newInstance();/* w  ww .  ja v a2  s  . c  o  m*/
    BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(obj);
    if (this.staticProperties != null) {
        wrapper.setPropertyValues(this.staticProperties);
    }
    wrapper.setPropertyValues(config.getConfiguration());
    return obj;
}

From source file:com.xyxy.platform.modules.core.web.taglib.BSAbstractMultiCheckedElementTag.java

/**
 * Copy & Paste, ./*ww  w  .  j ava2 s  .  c  om*/
 */
private void writeObjectEntry(TagWriter tagWriter, String valueProperty, String labelProperty, Object item,
        int itemIndex) throws JspException {

    BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(item);
    Object renderValue;
    if (valueProperty != null) {
        renderValue = wrapper.getPropertyValue(valueProperty);
    } else if (item instanceof Enum) {
        renderValue = ((Enum<?>) item).name();
    } else {
        renderValue = item;
    }
    Object renderLabel = (labelProperty != null ? wrapper.getPropertyValue(labelProperty) : item);
    writeElementTag(tagWriter, item, renderValue, renderLabel, itemIndex);
}

From source file:org.syncope.core.scheduling.SpringBeanJobFactory.java

/**
 * An implementation of SpringBeanJobFactory that retrieves the bean from
 * the Spring context so that autowiring and transactions work.
 *
 * {@inheritDoc}/*from  w  w w . j a v a 2s  .  co m*/
 */
@Override
protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {

    final ApplicationContext ctx = ((ConfigurableApplicationContext) schedulerContext
            .get("applicationContext"));

    // Try to re-create job bean from underlying task (useful for managing
    // failover scenarios)
    if (!ctx.containsBean(bundle.getJobDetail().getName())) {
        Long taskId = JobInstanceLoader.getTaskIdFromJobName(bundle.getJobDetail().getName());
        if (taskId != null) {
            TaskDAO taskDAO = ctx.getBean(TaskDAO.class);
            SchedTask task = taskDAO.find(taskId);

            JobInstanceLoader jobInstanceLoader = ctx.getBean(JobInstanceLoader.class);
            jobInstanceLoader.registerJob(task, task.getJobClassName(), task.getCronExpression());
        }

        Long reportId = JobInstanceLoader.getReportIdFromJobName(bundle.getJobDetail().getName());
        if (reportId != null) {
            ReportDAO reportDAO = ctx.getBean(ReportDAO.class);
            Report report = reportDAO.find(reportId);

            JobInstanceLoader jobInstanceLoader = ctx.getBean(JobInstanceLoader.class);
            jobInstanceLoader.registerJob(report);
        }
    }

    final Object job = ctx.getBean(bundle.getJobDetail().getName());
    final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(job);
    if (isEligibleForPropertyPopulation(wrapper.getWrappedInstance())) {
        final MutablePropertyValues pvs = new MutablePropertyValues();
        if (this.schedulerContext != null) {
            pvs.addPropertyValues(this.schedulerContext);
        }
        pvs.addPropertyValues(bundle.getJobDetail().getJobDataMap());
        pvs.addPropertyValues(bundle.getTrigger().getJobDataMap());
        if (this.ignoredUnknownProperties == null) {
            wrapper.setPropertyValues(pvs, true);
        } else {
            for (String propName : this.ignoredUnknownProperties) {
                if (pvs.contains(propName) && !wrapper.isWritableProperty(propName)) {

                    pvs.removePropertyValue(propName);
                }
            }
            wrapper.setPropertyValues(pvs);
        }
    }
    return job;
}

From source file:net.solarnetwork.util.ClassUtils.java

/**
 * Get a Map of non-null bean properties for an object.
 * /*from w  w  w  .  ja  v  a  2s. c om*/
 * @param o the object to inspect
 * @param ignore a set of property names to ignore (optional)
 * @return Map (never null)
 */
public static Map<String, Object> getBeanProperties(Object o, Set<String> ignore) {
    if (ignore == null) {
        ignore = DEFAULT_BEAN_PROP_NAME_IGNORE;
    }
    Map<String, Object> result = new LinkedHashMap<String, Object>();
    BeanWrapper bean = PropertyAccessorFactory.forBeanPropertyAccess(o);
    PropertyDescriptor[] props = bean.getPropertyDescriptors();
    for (PropertyDescriptor prop : props) {
        if (prop.getReadMethod() == null) {
            continue;
        }
        String propName = prop.getName();
        if (ignore != null && ignore.contains(propName)) {
            continue;
        }
        Object propValue = bean.getPropertyValue(propName);
        if (propValue == null) {
            continue;
        }
        result.put(propName, propValue);
    }
    return result;
}

From source file:org.grails.datastore.mapping.query.order.ManualEntityOrdering.java

public List applyOrder(List results, Query.Order order) {
    final String name = order.getProperty();

    @SuppressWarnings("hiding")
    final PersistentEntity entity = getEntity();
    PersistentProperty property = entity.getPropertyByName(name);
    if (property == null) {
        final PersistentProperty identity = entity.getIdentity();
        if (name.equals(identity.getName())) {
            property = identity;//  www . j  a v  a 2  s  .  c  o  m
        }
    }

    if (property != null) {
        final PersistentProperty finalProperty = property;
        Collections.sort(results, new Comparator() {

            public int compare(Object o1, Object o2) {

                if (entity.isInstance(o1) && entity.isInstance(o2)) {
                    final String propertyName = finalProperty.getName();
                    Method readMethod = cachedReadMethods.get(propertyName);
                    if (readMethod == null) {
                        BeanWrapper b = PropertyAccessorFactory.forBeanPropertyAccess(o1);
                        final PropertyDescriptor pd = b.getPropertyDescriptor(propertyName);
                        if (pd != null) {
                            readMethod = pd.getReadMethod();
                            if (readMethod != null) {
                                ReflectionUtils.makeAccessible(readMethod);
                                cachedReadMethods.put(propertyName, readMethod);
                            }
                        }
                    }

                    if (readMethod != null) {
                        final Class<?> declaringClass = readMethod.getDeclaringClass();
                        if (declaringClass.isInstance(o1) && declaringClass.isInstance(o2)) {
                            Object left = ReflectionUtils.invokeMethod(readMethod, o1);
                            Object right = ReflectionUtils.invokeMethod(readMethod, o2);

                            if (left == null && right == null)
                                return 0;
                            if (left != null && right == null)
                                return 1;
                            if (left == null)
                                return -1;
                            if ((left instanceof Comparable) && (right instanceof Comparable)) {
                                return ((Comparable) left).compareTo(right);
                            }
                        }
                    }
                }
                return 0;
            }
        });
    }

    if (order.getDirection() == Query.Order.Direction.DESC) {
        results = reverse(results);
    }

    return results;
}

From source file:com.sshdemo.common.schedule.generate.quartz.EwcmsMethodInvokingJobDetailFactoryBean.java

@Override
public void afterPropertiesSet() throws ClassNotFoundException, NoSuchMethodException {
    prepare();/*from www. j ava 2  s  .  c  om*/
    String name = this.name == null ? beanName : this.name;
    Class<? extends MethodInvokingJob> jobClass = concurrent
            ? EwcmsMethodInvokingJobDetailFactoryBean.MethodInvokingJob.class
            : EwcmsMethodInvokingJobDetailFactoryBean.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);
        bw.setPropertyValue("durability", Boolean.valueOf(true));
        ((JobDataMap) bw.getPropertyValue("jobDataMap")).put("methodInvoker", this);
    } else {
        jobDetail = newJob(jobClass).withIdentity(name, group).build();

        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:org.solmix.wmix.web.servlet.AbstractWmixFilter.java

@Override
public void init(FilterConfig config) throws ServletException {
    filterConfig = config;//from  w w  w .ja  v  a2s.com
    logInBothServletAndLoggingSystem("Initializing filter: " + getFilterName());

    try {
        PropertyValues pvs = new FilterConfigPropertyValues(getFilterConfig(), requiredProperties);
        BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
        ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
        bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader));
        initBeanWrapper(bw);
        bw.setPropertyValues(pvs, true);
    } catch (Exception e) {
        throw new ServletException("Failed to set bean properties on filter: " + getFilterName(), e);
    }

    try {
        init();
    } catch (Exception e) {
        throw new ServletException("Failed to init filter: " + getFilterName(), e);
    }

    logInBothServletAndLoggingSystem(
            getClass().getSimpleName() + " - " + getFilterName() + ": initialization completed");
}