Example usage for org.springframework.beans.factory.support GenericBeanDefinition setPropertyValues

List of usage examples for org.springframework.beans.factory.support GenericBeanDefinition setPropertyValues

Introduction

In this page you can find the example usage for org.springframework.beans.factory.support GenericBeanDefinition setPropertyValues.

Prototype

public void setPropertyValues(MutablePropertyValues propertyValues) 

Source Link

Document

Specify property values for this bean, if any.

Usage

From source file:com.laxser.blitz.web.impl.view.ViewDispatcherImpl.java

protected ViewResolver getJspViewResolver() throws IOException {
    if (this.jspViewResolver != null) {
        return this.jspViewResolver;
    }/*from w ww  .ja v a2s.  co m*/
    String beanName = "jspViewResolver";
    this.jspViewResolver = (ViewResolver) SpringUtils.getBean(getApplicationContext(), beanName);
    if (jspViewResolver == null) {
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClass(InternalResourceViewResolver.class);
        MutablePropertyValues propertyValues = new MutablePropertyValues();
        propertyValues.addPropertyValue(new PropertyValue("viewClass", JstlView.class));
        beanDefinition.setPropertyValues(propertyValues);
        ((BeanDefinitionRegistry) getApplicationContext().getBeanFactory()).registerBeanDefinition(beanName,
                beanDefinition);
        logger.info("registered bean definition named " + beanName + ": "
                + InternalResourceViewResolver.class.getName());
        jspViewResolver = (ViewResolver) SpringUtils.getBean(getApplicationContext(), beanName);
    }
    return this.jspViewResolver;
}

From source file:com.sinosoft.one.mvc.web.impl.view.ViewDispatcherImpl.java

private VelocityViewResolver createVelocityViewResolver(Invocation inv, String beanName, String layoutUrl)
        throws MalformedURLException, IOException {
    if (SpringUtils.getBean(getApplicationContext(), VelocityConfig.class) == null) {
        URL propertiesLocation = inv.getServletContext().getResource("/WEB-INF/velocity.properties");
        Properties velocityProperties = new Properties();
        if (propertiesLocation != null) {
            InputStream is = propertiesLocation.openStream();
            velocityProperties.load(is);
            is.close();/*  ww  w  .  ja v  a2s  . com*/
        }
        if (StringUtils.isBlank(velocityProperties.getProperty("input.encoding"))) {
            velocityProperties.setProperty("input.encoding", "UTF-8");
        }
        if (StringUtils.isBlank(velocityProperties.getProperty("output.encoding"))) {
            velocityProperties.setProperty("output.encoding", "UTF-8");
        }
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClass(MvcVelocityConfigurer.class);
        MutablePropertyValues propertyValues = new MutablePropertyValues();
        propertyValues.addPropertyValue(new PropertyValue("velocityProperties", velocityProperties));
        propertyValues.addPropertyValue(new PropertyValue("resourceLoaderPath", "/"));
        beanDefinition.setPropertyValues(propertyValues);
        ((BeanDefinitionRegistry) getApplicationContext().getBeanFactory())
                .registerBeanDefinition("velocityConfigurer", beanDefinition);
        logger.info("registered bean definition named" + " velocityConfigurer: "
                + MvcVelocityConfigurer.class.getName());
    }
    //
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    MutablePropertyValues propertyValues = new MutablePropertyValues();
    if (layoutUrl == null) {
        beanDefinition.setBeanClass(VelocityViewResolver.class);
    } else {
        beanDefinition.setBeanClass(VelocityLayoutViewResolver.class);
        propertyValues.addPropertyValue(new PropertyValue("layoutUrl", layoutUrl));
    }
    propertyValues.addPropertyValue(new PropertyValue("contentType", "text/html;charset=UTF-8"));
    String toolboxConfigLocation = "/WEB-INF/velocity-toolbox.xml";
    propertyValues.addPropertyValue(new PropertyValue("cache", Boolean.TRUE));
    URL toolbox = inv.getServletContext().getResource(toolboxConfigLocation);
    if (toolbox == null) {
        toolboxConfigLocation = "/WEB-INF/toolbox.xml";
        toolbox = inv.getServletContext().getResource(toolboxConfigLocation);
    }
    if (toolbox != null) {
        propertyValues.addPropertyValue(new PropertyValue("toolboxConfigLocation", toolboxConfigLocation));
    }
    beanDefinition.setPropertyValues(propertyValues);
    ((BeanDefinitionRegistry) getApplicationContext().getBeanFactory()).registerBeanDefinition(beanName,
            beanDefinition);
    logger.info("registered bean definition named " + beanName + ": " + VelocityViewResolver.class.getName());
    return (VelocityViewResolver) SpringUtils.getBean(getApplicationContext(), beanName);
}

From source file:net.paoding.rose.web.impl.view.ViewDispatcherImpl.java

private VelocityViewResolver createVelocityViewResolver(Invocation inv, String beanName, String layoutUrl)
        throws MalformedURLException, IOException {
    if (SpringUtils.getBean(getApplicationContext(), VelocityConfig.class) == null) {
        URL propertiesLocation = inv.getServletContext().getResource("/WEB-INF/velocity.properties");
        Properties velocityProperties = new Properties();
        if (propertiesLocation != null) {
            InputStream is = propertiesLocation.openStream();
            velocityProperties.load(is);
            is.close();//from  w  w  w. j a v  a 2 s.  c  o m
        }
        if (StringUtils.isBlank(velocityProperties.getProperty("input.encoding"))) {
            velocityProperties.setProperty("input.encoding", "UTF-8");
        }
        if (StringUtils.isBlank(velocityProperties.getProperty("output.encoding"))) {
            velocityProperties.setProperty("output.encoding", "UTF-8");
        }
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClass(RoseVelocityConfigurer.class);
        MutablePropertyValues propertyValues = new MutablePropertyValues();
        propertyValues.addPropertyValue(new PropertyValue("velocityProperties", velocityProperties));
        propertyValues.addPropertyValue(new PropertyValue("resourceLoaderPath", "/"));
        beanDefinition.setPropertyValues(propertyValues);
        ((BeanDefinitionRegistry) getApplicationContext().getBeanFactory())
                .registerBeanDefinition("velocityConfigurer", beanDefinition);
        logger.info("registered bean definition named" + " velocityConfigurer: "
                + RoseVelocityConfigurer.class.getName());
    }
    //
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    MutablePropertyValues propertyValues = new MutablePropertyValues();
    if (layoutUrl == null) {
        beanDefinition.setBeanClass(VelocityViewResolver.class);
    } else {
        beanDefinition.setBeanClass(VelocityLayoutViewResolver.class);
        propertyValues.addPropertyValue(new PropertyValue("layoutUrl", layoutUrl));
    }
    propertyValues.addPropertyValue(new PropertyValue("contentType", "text/html;charset=UTF-8"));
    String toolboxConfigLocation = "/WEB-INF/velocity-toolbox.xml";
    propertyValues.addPropertyValue(new PropertyValue("cache", Boolean.TRUE));
    URL toolbox = inv.getServletContext().getResource(toolboxConfigLocation);
    if (toolbox == null) {
        toolboxConfigLocation = "/WEB-INF/toolbox.xml";
        toolbox = inv.getServletContext().getResource(toolboxConfigLocation);
    }
    if (toolbox != null) {
        propertyValues.addPropertyValue(new PropertyValue("toolboxConfigLocation", toolboxConfigLocation));
    }
    beanDefinition.setPropertyValues(propertyValues);
    ((BeanDefinitionRegistry) getApplicationContext().getBeanFactory()).registerBeanDefinition(beanName,
            beanDefinition);
    logger.info("registered bean definition named " + beanName + ": " + VelocityViewResolver.class.getName());
    return (VelocityViewResolver) SpringUtils.getBean(getApplicationContext(), beanName);
}

From source file:com.laxser.blitz.web.impl.view.ViewDispatcherImpl.java

private VelocityViewResolver createVelocityViewResolver(Invocation inv, String beanName, String layoutUrl)
        throws MalformedURLException, IOException {
    if (SpringUtils.getBean(getApplicationContext(), VelocityConfig.class) == null) {
        URL propertiesLocation = inv.getServletContext().getResource("/WEB-INF/velocity.properties");
        Properties velocityProperties = new Properties();
        if (propertiesLocation != null) {
            InputStream is = propertiesLocation.openStream();
            velocityProperties.load(is);
            is.close();/*  w w w .j a va  2  s .  c o m*/
        }
        if (StringUtils.isBlank(velocityProperties.getProperty("input.encoding"))) {
            velocityProperties.setProperty("input.encoding", "UTF-8");
        }
        if (StringUtils.isBlank(velocityProperties.getProperty("output.encoding"))) {
            velocityProperties.setProperty("output.encoding", "UTF-8");
        }
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClass(BlitzVelocityConfigurer.class);
        MutablePropertyValues propertyValues = new MutablePropertyValues();
        propertyValues.addPropertyValue(new PropertyValue("velocityProperties", velocityProperties));
        propertyValues.addPropertyValue(new PropertyValue("resourceLoaderPath", "/"));
        beanDefinition.setPropertyValues(propertyValues);
        ((BeanDefinitionRegistry) getApplicationContext().getBeanFactory())
                .registerBeanDefinition("velocityConfigurer", beanDefinition);
        logger.info("registered bean definition named" + " velocityConfigurer: "
                + BlitzVelocityConfigurer.class.getName());
    }
    //
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    MutablePropertyValues propertyValues = new MutablePropertyValues();
    if (layoutUrl == null) {
        beanDefinition.setBeanClass(VelocityViewResolver.class);
    } else {
        beanDefinition.setBeanClass(VelocityLayoutViewResolver.class);
        propertyValues.addPropertyValue(new PropertyValue("layoutUrl", layoutUrl));
    }
    propertyValues.addPropertyValue(new PropertyValue("contentType", "text/html;charset=UTF-8"));
    String toolboxConfigLocation = "/WEB-INF/velocity-toolbox.xml";
    propertyValues.addPropertyValue(new PropertyValue("cache", Boolean.TRUE));
    URL toolbox = inv.getServletContext().getResource(toolboxConfigLocation);
    if (toolbox == null) {
        toolboxConfigLocation = "/WEB-INF/toolbox.xml";
        toolbox = inv.getServletContext().getResource(toolboxConfigLocation);
    }
    if (toolbox != null) {
        propertyValues.addPropertyValue(new PropertyValue("toolboxConfigLocation", toolboxConfigLocation));
    }
    beanDefinition.setPropertyValues(propertyValues);
    ((BeanDefinitionRegistry) getApplicationContext().getBeanFactory()).registerBeanDefinition(beanName,
            beanDefinition);
    logger.info("registered bean definition named " + beanName + ": " + VelocityViewResolver.class.getName());
    return (VelocityViewResolver) SpringUtils.getBean(getApplicationContext(), beanName);
}

From source file:net.frontlinesms.FrontlineSMS.java

/**
 * Create the configLocations bean for the Hibernate config.
 * This method should only be called from within {@link FrontlineSMS#initApplicationContext()}
 * @return {@link BeanDefinition} containing details of the hibernate config for the app and its plugins.
 *///ww  w  . ja  va 2  s.c  om
private BeanDefinition createHibernateConfigLocationsBeanDefinition() {
    // Initialise list of hibernate config files
    List<String> hibernateConfigList = new ArrayList<String>();
    // Add main hibernate config location
    hibernateConfigList.add("classpath:frontlinesms.hibernate.cfg.xml");
    // Add hibernate config locations for plugins
    for (Class<PluginController> pluginClass : PluginProperties.getInstance().getPluginClasses()) {
        LOG.info("Processing plugin class: " + pluginClass.getName());
        if (pluginClass.isAnnotationPresent(PluginControllerProperties.class)) {
            PluginControllerProperties properties = pluginClass.getAnnotation(PluginControllerProperties.class);
            String pluginHibernateLocation = properties.hibernateConfigPath();
            if (!PluginControllerProperties.NO_VALUE.equals(pluginHibernateLocation)) {
                hibernateConfigList.add(pluginHibernateLocation);
            }
        }
    }

    GenericBeanDefinition myBeanDefinition = new GenericBeanDefinition();
    myBeanDefinition.setBeanClass(ListFactoryBean.class);
    MutablePropertyValues values = new MutablePropertyValues();
    values.addPropertyValue("sourceList", hibernateConfigList);
    myBeanDefinition.setPropertyValues(values);

    return myBeanDefinition;
}

From source file:org.anodyneos.jse.cron.CronDaemon.java

public CronDaemon(InputSource source) throws JseException {

    Schedule schedule;/*from ww w.j  a v  a  2s  .  co  m*/

    // parse source
    try {

        JAXBContext jc = JAXBContext.newInstance("org.anodyneos.jse.cron.config");
        Unmarshaller u = jc.createUnmarshaller();
        //Schedule
        Source schemaSource = new StreamSource(Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("org/anodyneos/jse/cron/cron.xsd"));

        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(schemaSource);
        u.setSchema(schema);
        ValidationEventCollector vec = new ValidationEventCollector();
        u.setEventHandler(vec);

        JAXBElement<?> rootElement;
        try {
            rootElement = ((JAXBElement<?>) u.unmarshal(source));
        } catch (UnmarshalException ex) {
            if (!vec.hasEvents()) {
                throw ex;
            } else {
                for (ValidationEvent ve : vec.getEvents()) {
                    ValidationEventLocator vel = ve.getLocator();
                    log.error("Line:Col[" + vel.getLineNumber() + ":" + vel.getColumnNumber() + "]:"
                            + ve.getMessage());
                }
                throw new JseException("Validation failed for source publicId='" + source.getPublicId()
                        + "'; systemId='" + source.getSystemId() + "';");
            }
        }

        schedule = (Schedule) rootElement.getValue();

        if (vec.hasEvents()) {
            for (ValidationEvent ve : vec.getEvents()) {
                ValidationEventLocator vel = ve.getLocator();
                log.warn("Line:Col[" + vel.getLineNumber() + ":" + vel.getColumnNumber() + "]:"
                        + ve.getMessage());
            }
        }

    } catch (JseException e) {
        throw e;
    } catch (Exception e) {
        throw new JseException("Cannot parse " + source + ".", e);
    }

    SpringHelper springHelper = new SpringHelper();

    ////////////////
    //
    // Configure Spring and Create Beans
    //
    ////////////////

    TimeZone defaultTimeZone;

    if (schedule.isSetTimeZone()) {
        defaultTimeZone = getTimeZone(schedule.getTimeZone());
    } else {
        defaultTimeZone = TimeZone.getDefault();
    }

    if (schedule.isSetSpringContext() && schedule.getSpringContext().isSetConfig()) {
        for (Config config : schedule.getSpringContext().getConfig()) {
            springHelper.addXmlClassPathConfigLocation(config.getClassPathResource());
        }
    }

    for (org.anodyneos.jse.cron.config.JobGroup jobGroup : schedule.getJobGroup()) {
        for (Job job : jobGroup.getJob()) {
            if (job.isSetBeanRef()) {
                if (job.isSetBean() || job.isSetClassName()) {
                    throw new JseException("Cannot set bean or class attribute for job when beanRef is set.");
                } // else config ok
            } else {
                if (!job.isSetClassName()) {
                    throw new JseException("must set either class or beanRef for job.");
                }
                GenericBeanDefinition beanDef = new GenericBeanDefinition();
                MutablePropertyValues propertyValues = new MutablePropertyValues();

                if (!job.isSetBean()) {
                    job.setBean(UUID.randomUUID().toString());
                }

                if (springHelper.containsBean(job.getBean())) {
                    throw new JseException(
                            "Bean name already used; overriding not allowed here: " + job.getBean());
                }

                beanDef.setBeanClassName(job.getClassName());

                for (Property prop : job.getProperty()) {
                    String value = null;
                    if (prop.isSetSystemProperty()) {
                        value = System.getProperty(prop.getSystemProperty());
                    }
                    if (null == value) {
                        value = prop.getValue();
                    }

                    propertyValues.addPropertyValue(prop.getName(), value);
                }

                beanDef.setPropertyValues(propertyValues);
                springHelper.registerBean(job.getBean(), beanDef);
                job.setBeanRef(job.getBean());
            }
        }
    }

    springHelper.init();

    ////////////////
    //
    // Configure Timer Services
    //
    ////////////////

    for (org.anodyneos.jse.cron.config.JobGroup jobGroup : schedule.getJobGroup()) {

        String jobGroupName;
        JseTimerService service = new JseTimerService();

        timerServices.add(service);

        if (jobGroup.isSetName()) {
            jobGroupName = jobGroup.getName();
        } else {
            jobGroupName = UUID.randomUUID().toString();
        }

        if (jobGroup.isSetMaxConcurrent()) {
            service.setMaxConcurrent(jobGroup.getMaxConcurrent());
        }

        for (Job job : jobGroup.getJob()) {

            TimeZone jobTimeZone = defaultTimeZone;

            if (job.isSetTimeZone()) {
                jobTimeZone = getTimeZone(job.getTimeZone());
            } else {
                jobTimeZone = defaultTimeZone;
            }

            Object obj;

            Date notBefore = null;
            Date notAfter = null;

            if (job.isSetNotBefore()) {
                notBefore = job.getNotBefore().toGregorianCalendar(jobTimeZone, null, null).getTime();
            }
            if (job.isSetNotAfter()) {
                notAfter = job.getNotAfter().toGregorianCalendar(jobTimeZone, null, null).getTime();
            }

            CronSchedule cs = new CronSchedule(job.getSchedule(), jobTimeZone, job.getMaxIterations(),
                    job.getMaxQueue(), notBefore, notAfter);

            obj = springHelper.getBean(job.getBeanRef());
            log.info("Adding job " + jobGroup.getName() + "/" + job.getName() + " using bean "
                    + job.getBeanRef());
            if (obj instanceof CronJob) {
                ((CronJob) obj).setCronContext(new CronContext(jobGroupName, job.getName(), cs));
            }
            if (obj instanceof JseDateAwareJob) {
                service.createTimer((JseDateAwareJob) obj, cs);
            } else if (obj instanceof Runnable) {
                service.createTimer((Runnable) obj, cs);
            } else {
                throw new JseException("Job must implement Runnable or JseDateAwareJob");
            }
        }
    }
}

From source file:org.hx.rainbow.server.oc.monitor.service.MonitorService.java

private GenericBeanDefinition getDefinition(String serverName, String url, String userName, String password) {
    GenericBeanDefinition sessionFactoryDef = new GenericBeanDefinition();
    Map<String, Object> paramData = new HashMap<String, Object>();
    paramData.put("driverClassName", "com.mysql.jdbc.Driver");
    paramData.put("url", url);
    paramData.put("username", userName);
    paramData.put("password", password);
    paramData.put("initialSize", 5);
    paramData.put("maxActive", 20);
    paramData.put("maxIdle", 10);
    sessionFactoryDef.setBeanClass(BasicDataSource.class);
    sessionFactoryDef.setPropertyValues(new MutablePropertyValues(paramData));
    return sessionFactoryDef;
}