Example usage for org.springframework.beans BeanWrapper getWrappedInstance

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

Introduction

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

Prototype

Object getWrappedInstance();

Source Link

Document

Return the bean instance wrapped by this object.

Usage

From source file:net.tirasa.blog.springquartz.SpringBeanJobFactory.java

/**
 * An implementation of SpringBeanJobFactory that retrieves the bean from
 * the Spring context so that autowiring and transactions work
 *
 * This method is overriden.//from  w  w  w.  j a v a  2  s. com
 * @see org.springframework.scheduling.quartz.SpringBeanJobFactory#createJobInstance(org.quartz.spi.TriggerFiredBundle)
 */
@Override
protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {

    final ConfigurableApplicationContext ctx = ((ConfigurableApplicationContext) schedulerContext
            .get("applicationContext"));
    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:com.saysth.commons.quartz.SpringBeanJobFactory.java

/**
 * Create the job instance, populating it with property values taken from
 * the scheduler context, job data map and trigger data map.
 *//*from   www  .  j  av  a  2 s . c  o m*/
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
    Object job = bundle.getJobDetail().getJobClass().newInstance();
    BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(job);
    if (isEligibleForPropertyPopulation(bw.getWrappedInstance())) {
        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) {
            for (String propName : this.ignoredUnknownProperties) {
                if (pvs.contains(propName) && !bw.isWritableProperty(propName)) {
                    pvs.removePropertyValue(propName);
                }
            }
            bw.setPropertyValues(pvs);
        } else {
            bw.setPropertyValues(pvs, true);
        }
    }
    return job;
}

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 ww  .  jav a  2s.c  o 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:com.all.app.BeanCreationMonitor.java

@Override
protected void populateBean(String beanName, AbstractBeanDefinition mbd, BeanWrapper bw) {
    statistics.get(beanName, bw.getWrappedInstance().getClass()).beginAutowire();
    doSuperPopulateBean(beanName, mbd, bw);
    statistics.get(beanName, bw.getWrappedInstance().getClass()).endAutowire();
}

From source file:com.all.app.BeanCreationMonitor.java

@Override
public BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, Object[] args) {
    try {//ww  w  .  j  ava  2  s  .co  m
        BeanStatisticFactory stat = BeanStatistic.create();
        BeanWrapper bean = doSuperCreateBeanInstance(beanName, mbd, args);
        statistics.add(stat.instantiate(beanName, bean.getWrappedInstance().getClass()));
        return bean;
    } catch (BeansException e) {
        log.error(e, e);
        throw e;
    }
}

From source file:com.foilen.smalltools.reflection.BeanPropertiesCopierTools.java

/**
 * Provide the bean wrappers.// w  w w  .java  2 s .  c  o m
 *
 * @param sourceWrapper
 *            the source
 * @param destinationWrapper
 *            the destination
 */
public BeanPropertiesCopierTools(BeanWrapper sourceWrapper, BeanWrapper destinationWrapper) {
    AssertTools.assertNotNull(sourceWrapper, "The sourceWrapper cannot be null");
    AssertTools.assertNotNull(sourceWrapper.getWrappedInstance(), "The source cannot be null");
    AssertTools.assertNotNull(destinationWrapper, "The destinationWrapper cannot be null");
    AssertTools.assertNotNull(destinationWrapper.getWrappedInstance(), "The destination cannot be null");

    this.sourceWrapper = sourceWrapper;
    this.destinationWrapper = destinationWrapper;
}

From source file:net.solarnetwork.support.XmlSupport.java

/**
 * Turn an object into a simple XML Element, supporting custom property
 * editors./*from   w w w. ja v  a 2 s. c  om*/
 * 
 * <p>
 * The returned XML will be a single element with all JavaBean properties
 * turned into attributes and the element named after the bean object's
 * class name. For example:
 * <p>
 * 
 * <pre>
 * &lt;PowerDatum
 *   id="123"
 *   pvVolts="123.123"
 *   ... /&gt;
 * </pre>
 * 
 * <p>
 * {@link PropertyEditor} instances can be registered with the supplied
 * {@link BeanWrapper} for custom handling of properties, e.g. dates.
 * </p>
 * 
 * @param bean
 *        the object to turn into XML
 * @return the element, as an XML DOM Document
 */
public Element getElement(BeanWrapper bean, Document dom) {
    String elementName = bean.getWrappedInstance().getClass().getSimpleName();
    return getElement(bean, elementName, dom);
}

From source file:org.brushingbits.jnap.common.bean.cloning.BeanCloner.java

private Object cloneBean(Object bean, Class<?> type) {
    BeanWrapper source = PropertyAccessorFactory.forBeanPropertyAccess(bean);
    BeanWrapper copy = PropertyAccessorFactory.forBeanPropertyAccess(BeanUtils.instantiate(type));

    // keep instance for circular and multiple references
    context.setAsVisited(bean);/*w  w w . ja v a2  s . c om*/
    alreadyCloned.put(bean, copy.getWrappedInstance());

    PropertyDescriptor[] beanProperties = copy.getPropertyDescriptors();
    for (PropertyDescriptor propertyDescriptor : beanProperties) {
        String name = propertyDescriptor.getName();
        context.pushPath(name);
        if (copy.isReadableProperty(name) && copy.isWritableProperty(name)) {
            Object value = source.getPropertyValue(name);
            copy.setPropertyValue(name, clone(value));
        }
        context.popPath();
    }
    Object beanCopy = copy.getWrappedInstance();
    source = null;
    copy = null;
    return beanCopy;
}

From source file:com.htmlhifive.sync.resource.AbstractCrudSyncResource.java

/**
 * ???ID???????./*from w  ww  . j  a v a 2  s .c o  m*/
 *
 * @param common ?
 * @return 
 */
private T createDeletedItem(ResourceItemCommonData common) {
    Class<T> itemType = getItemType();

    BeanWrapper deletedWrapper = PropertyAccessorFactory
            .forBeanPropertyAccess(BeanUtils.instantiateClass(itemType));
    deletedWrapper.setPropertyValue(getIdFieldName(), common.getTargetItemId());

    return itemType.cast(deletedWrapper.getWrappedInstance());
}

From source file:org.springmodules.cache.provider.ReflectionCacheModelEditor.java

/**
 * @throws IllegalStateException//  w  w  w .  j av  a2  s . co m
 *           if the class of the cache model to create has not been set.
 * @see SemicolonSeparatedPropertiesParser#parseProperties(String)
 * @see PropertyEditor#setAsText(String)
 * @see org.springframework.beans.PropertyAccessor#setPropertyValue(String,
 *      Object)
 */
public final void setAsText(String text) {
    if (cacheModelClass == null) {
        throw new IllegalStateException("cacheModelClass should not be null");
    }

    Properties properties = SemicolonSeparatedPropertiesParser.parseProperties(text);

    BeanWrapper beanWrapper = new BeanWrapperImpl(cacheModelClass);

    if (properties != null) {
        for (Iterator i = properties.keySet().iterator(); i.hasNext();) {
            String propertyName = (String) i.next();
            String textProperty = properties.getProperty(propertyName);

            Object propertyValue = null;

            PropertyEditor propertyEditor = getPropertyEditor(propertyName);
            if (propertyEditor != null) {
                propertyEditor.setAsText(textProperty);
                propertyValue = propertyEditor.getValue();
            } else {
                propertyValue = textProperty;
            }
            beanWrapper.setPropertyValue(propertyName, propertyValue);
        }
    }

    setValue(beanWrapper.getWrappedInstance());
}