Example usage for org.springframework.beans.factory.support BeanDefinitionBuilder genericBeanDefinition

List of usage examples for org.springframework.beans.factory.support BeanDefinitionBuilder genericBeanDefinition

Introduction

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

Prototype

public static BeanDefinitionBuilder genericBeanDefinition(Class<?> beanClass) 

Source Link

Document

Create a new BeanDefinitionBuilder used to construct a GenericBeanDefinition .

Usage

From source file:com.quancheng.saluki.boot.runner.GrpcServiceRunner.java

@Override
public void run(String... arg0) throws Exception {
    System.out.println("Starting GRPC Server ...");
    RpcServiceConfig rpcSerivceConfig = new RpcServiceConfig();
    this.addRegistyAddress(rpcSerivceConfig);
    rpcSerivceConfig.setApplication(applicationName);
    this.addHostAndPort(rpcSerivceConfig);
    rpcSerivceConfig.setMonitorinterval(thrallProperties.getMonitorinterval());
    Collection<Object> instances = getTypedBeansWithAnnotation(SalukiService.class);
    if (instances.size() > 0) {
        try {//  w w  w.ja  v a 2  s  .  co  m
            for (Object instance : instances) {
                SalukiService serviceAnnotation = instance.getClass().getAnnotation(SalukiService.class);
                String serviceName = serviceAnnotation.service();
                Set<String> serviceNames = Sets.newHashSet();
                Object target = instance;
                if (StringUtils.isBlank(serviceName)) {
                    if (this.isGrpcServer(instance)) {
                        throw new java.lang.IllegalArgumentException(
                                "you use grpc stub service,must set service name,service instance is"
                                        + instance);
                    } else {
                        target = GrpcAopUtils.getTarget(target);
                        Class<?>[] interfaces = ClassUtils.getAllInterfacesForClass(target.getClass());
                        for (Class<?> interfaceClass : interfaces) {
                            String interfaceName = interfaceClass.getName();
                            if (StringUtils.startsWith(interfaceName, "com.quancheng")) {
                                serviceNames.add(interfaceName);
                            }
                        }
                    }
                } else {
                    serviceNames.add(serviceName);
                }
                for (String realServiceName : serviceNames) {
                    rpcSerivceConfig.addServiceDefinition(realServiceName, getGroup(serviceAnnotation),
                            getVersion(serviceAnnotation), instance);
                }
            }
        } finally {
            Object healthInstance = new HealthImpl(applicationContext);
            BeanDefinitionRegistry beanDefinitonRegistry = (BeanDefinitionRegistry) applicationContext
                    .getBeanFactory();
            BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder
                    .genericBeanDefinition(Health.class);
            beanDefinitonRegistry.registerBeanDefinition(Health.class.getName(),
                    beanDefinitionBuilder.getRawBeanDefinition());
            applicationContext.getBeanFactory().registerSingleton(Health.class.getName(), healthInstance);
            String group = thrallProperties.getGroup() != null ? thrallProperties.getGroup() : "default";
            String version = thrallProperties.getVersion() != null ? thrallProperties.getVersion() : "1.0.0";
            rpcSerivceConfig.addServiceDefinition(Health.class.getName(), group, version, healthInstance);
        }
    }
    this.rpcService = rpcSerivceConfig;
    rpcSerivceConfig.export();
    System.out.println(String.format("GRPC server has started!You can do test by %s \n %s",
            "http://localhost:" + httpPort + "/doc", "http://saluki.dev.quancheng-ec.com"));
}

From source file:fr.acxio.tools.agia.alfresco.configuration.NodeDefinitionParser.java

protected BeanDefinition parseProperty(Element sElement, ParserContext sParserContext) {
    BeanDefinitionBuilder aBuilder = BeanDefinitionBuilder
            .genericBeanDefinition(PropertyDefinitionFactoryBean.class);
    aBuilder.addPropertyValue("localName", sElement.getAttribute(PROPDEF_LOCALNAME));

    String aConverterID = sElement.getAttribute(PROPDEF_CONVERTERREF);
    if (StringUtils.hasText(aConverterID)) {
        aBuilder.addPropertyReference("converter", aConverterID);
    }/*from   ww  w  . j av  a 2s . c  o m*/

    List<Element> aValuesElements = DomUtils.getChildElementsByTagName(sElement, PROPDEF_VALUE);
    ManagedList<String> aValues = new ManagedList<String>(aValuesElements.size());

    for (Element aElement : aValuesElements) {
        aValues.add(aElement.getTextContent());
    }

    aBuilder.addPropertyValue("values", aValues);

    return aBuilder.getBeanDefinition();
}

From source file:de.codecentric.batch.jsr352.CustomJsrJobOperator.java

@Override
public long start(String jobName, Properties params) throws JobStartException, JobSecurityException {
    final JsrXmlApplicationContext batchContext = new JsrXmlApplicationContext(params);
    batchContext.setValidating(false);/*from   ww w .  j av a  2  s.  c  o  m*/

    Resource batchXml = new ClassPathResource("/META-INF/batch.xml");
    String jobConfigurationLocation = "/META-INF/batch-jobs/" + jobName + ".xml";
    Resource jobXml = new ClassPathResource(jobConfigurationLocation);

    if (batchXml.exists()) {
        batchContext.load(batchXml);
    }

    if (jobXml.exists()) {
        batchContext.load(jobXml);
    }

    AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder
            .genericBeanDefinition("org.springframework.batch.core.jsr.JsrJobContextFactoryBean")
            .getBeanDefinition();
    beanDefinition.setScope(BeanDefinition.SCOPE_SINGLETON);
    batchContext.registerBeanDefinition(JSR_JOB_CONTEXT_BEAN_NAME, beanDefinition);

    batchContext.setParent(parentContext);

    try {
        batchContext.refresh();
    } catch (BeanCreationException e) {
        throw new JobStartException(e);
    }

    Assert.notNull(jobName, "The job name must not be null.");

    final org.springframework.batch.core.JobExecution jobExecution;

    try {
        JobParameters jobParameters = jobParametersConverter.getJobParameters(params);
        String[] jobNames = batchContext.getBeanNamesForType(Job.class);

        if (jobNames == null || jobNames.length <= 0) {
            throw new BatchRuntimeException("No Job defined in current context");
        }

        org.springframework.batch.core.JobInstance jobInstance = jobRepository.createJobInstance(jobNames[0],
                jobParameters);
        jobExecution = jobRepository.createJobExecution(jobInstance, jobParameters, jobConfigurationLocation);
    } catch (Exception e) {
        throw new JobStartException(e);
    }

    try {
        final Semaphore semaphore = new Semaphore(1);
        final List<Exception> exceptionHolder = Collections.synchronizedList(new ArrayList<Exception>());
        semaphore.acquire();

        taskExecutor.execute(new Runnable() {

            @Override
            public void run() {
                JsrJobContextFactoryBean factoryBean = null;
                try {
                    factoryBean = (JsrJobContextFactoryBean) batchContext
                            .getBean("&" + JSR_JOB_CONTEXT_BEAN_NAME);
                    factoryBean.setJobExecution(jobExecution);
                    final AbstractJob job = batchContext.getBean(AbstractJob.class);
                    addListenerToJobService.addListenerToJob(job);
                    semaphore.release();
                    // Initialization of the JobExecution for job level dependencies
                    jobRegistry.register(job, jobExecution);
                    job.execute(jobExecution);
                    jobRegistry.remove(jobExecution);
                } catch (Exception e) {
                    exceptionHolder.add(e);
                } finally {
                    if (factoryBean != null) {
                        factoryBean.close();
                    }

                    batchContext.close();

                    if (semaphore.availablePermits() == 0) {
                        semaphore.release();
                    }
                }
            }
        });

        semaphore.acquire();
        if (exceptionHolder.size() > 0) {
            semaphore.release();
            throw new JobStartException(exceptionHolder.get(0));
        }
    } catch (Exception e) {
        if (jobRegistry.exists(jobExecution.getId())) {
            jobRegistry.remove(jobExecution);
        }
        jobExecution.upgradeStatus(BatchStatus.FAILED);
        if (jobExecution.getExitStatus().equals(ExitStatus.UNKNOWN)) {
            jobExecution.setExitStatus(ExitStatus.FAILED.addExitDescription(e));
        }
        jobRepository.update(jobExecution);

        if (batchContext.isActive()) {
            batchContext.close();
        }

        throw new JobStartException(e);
    }
    return jobExecution.getId();
}

From source file:org.jdal.beans.TableBeanDefinitionParser.java

/**
 * {@inheritDoc}/*from   www . j  av a  2  s. co  m*/
 */
@SuppressWarnings("rawtypes")
public BeanDefinition parse(Element element, ParserContext parserContext) {
    // Defaults
    String entity = null;

    if (element.hasAttribute(ENTITY))
        entity = element.getAttribute(ENTITY);

    String name = StringUtils.uncapitalize(StringUtils.substringAfterLast(entity, "."));

    if (element.hasAttribute(ID))
        name = element.getAttribute(ID);

    parserContext.pushContainingComponent(
            new CompositeComponentDefinition(name, parserContext.extractSource(element)));

    // Bean names
    String tableModelBeanName = name + LIST_TABLE_MODEL_SUFFIX;
    String tablePanelBeanName = name + TABLE_PANEL_SUFFIX;
    String pageableTableBeanName = name + PAGEABLE_TABLE_SUFFIX;
    String dataSource = name + SERVICE_SUFFIX;
    String paginator = PAGINATOR_VIEW;
    String editor = name + EDITOR_SUFFIX;
    String actions = DefaultsBeanDefinitionParser.DEFAULT_TABLE_ACTIONS;
    String guiFactory = DefaultsBeanDefinitionParser.DEFAULT_GUI_FACTORY;
    String scope = BeanDefinition.SCOPE_PROTOTYPE;

    if (element.hasAttribute(SERVICE_ATTRIBUTE))
        dataSource = element.getAttribute(SERVICE_ATTRIBUTE);

    if (element.hasAttribute(PAGINATOR))
        paginator = element.getAttribute(PAGINATOR);

    if (element.hasAttribute(ACTIONS))
        actions = element.getAttribute(ACTIONS);

    if (element.hasAttribute(GUI_FACTORY))
        guiFactory = element.getAttribute(GUI_FACTORY);

    if (element.hasAttribute(EDITOR))
        editor = element.getAttribute(EDITOR);

    if (element.hasAttribute(SCOPE))
        scope = element.getAttribute(SCOPE);

    // create ListTableModel
    BeanDefinitionBuilder bdb = BeanDefinitionBuilder.genericBeanDefinition(ListTableModel.class);
    bdb.setScope(scope);
    bdb.addPropertyValue("modelClass", entity);
    NodeList nl = element.getElementsByTagNameNS(element.getNamespaceURI(), COLUMNS);

    if (nl.getLength() > 0) {
        List columns = parserContext.getDelegate().parseListElement((Element) nl.item(0),
                bdb.getRawBeanDefinition());
        bdb.addPropertyValue(COLUMNS, columns);
    }
    registerBeanDefinition(element, parserContext, tableModelBeanName, bdb);

    // create PageableTable
    bdb = BeanDefinitionBuilder.genericBeanDefinition(PageableTable.class);
    bdb.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    bdb.addPropertyReference(DATA_SOURCE, dataSource);
    bdb.addPropertyReference(PAGINATOR_VIEW, paginator);
    bdb.addPropertyReference(TABLE_MODEL, tableModelBeanName);
    bdb.addPropertyValue(NAME, pageableTableBeanName);

    if (element.hasAttribute(TABLE_SERVICE))
        bdb.addPropertyReference(Conventions.attributeNameToPropertyName(TABLE_SERVICE),
                element.getAttribute(TABLE_SERVICE));

    if (element.hasAttribute(FILTER))
        bdb.addPropertyReference(FILTER, element.getAttribute(FILTER));

    if (element.hasAttribute(SHOW_MENU))
        bdb.addPropertyValue(Conventions.attributeNameToPropertyName(SHOW_MENU),
                element.getAttribute(SHOW_MENU));

    if (element.hasAttribute(MESSAGE_SOURCE))
        bdb.addPropertyReference(MESSAGE_SOURCE, element.getAttribute(MESSAGE_SOURCE));

    registerBeanDefinition(element, parserContext, pageableTableBeanName, bdb);

    // create TablePanel
    String tablePanelClassName = "org.jdal.swing.table.TablePanel";
    if (element.hasAttribute(TABLE_PANEL_CLASS))
        tablePanelClassName = element.getAttribute(TABLE_PANEL_CLASS);

    bdb = BeanDefinitionBuilder.genericBeanDefinition(tablePanelClassName);
    bdb.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    bdb.addPropertyReference(TABLE, pageableTableBeanName);
    bdb.addPropertyReference(GUI_FACTORY, guiFactory);
    bdb.addPropertyValue(EDITOR_NAME, editor);
    bdb.addPropertyReference(PERSISTENT_SERVICE, dataSource);

    if (element.hasAttribute(FILTER_VIEW))
        bdb.addPropertyReference(Conventions.attributeNameToPropertyName(FILTER_VIEW),
                element.getAttribute(FILTER_VIEW));

    if (!element.hasAttribute(USE_ACTIONS) || "true".equals(element.getAttribute(USE_ACTIONS)))
        bdb.addPropertyReference(ACTIONS, actions);

    registerBeanDefinition(element, parserContext, tablePanelBeanName, bdb);

    parserContext.popAndRegisterContainingComponent();

    return null;
}

From source file:io.cloudslang.schema.EngineBeanDefinitionParser.java

@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
    registerBeans(parserContext);/*from  w ww.  java2  s. co  m*/

    loadContexts(element, parserContext.getRegistry());

    registerSpecialBeans(element, parserContext);

    return BeanDefinitionBuilder.genericBeanDefinition(ScoreImpl.class).getBeanDefinition();
}

From source file:org.jsconf.core.impl.BeanFactory.java

private BeanDefinitionBuilder buildBeanFromInterface() {
    try {/* w  ww. jav  a2  s .  c  om*/
        Class<?> classBean = Class.forName(this.interfaceName);
        if (!classBean.isInterface()) {
            throw new BeanCreationException(
                    String.format("Interface is not an interface : %s", this.interfaceName));
        }
        return BeanDefinitionBuilder.genericBeanDefinition(VirtualBean.class).setFactoryMethod("factory")//
                .addConstructorArgValue(classBean).addConstructorArgValue(getAllProperties(this.properties));
    } catch (ClassNotFoundException e) {
        throw new BeanCreationException(String.format("Class not found : %s", this.interfaceName), e);
    }
}

From source file:org.jdal.beans.DefaultsBeanDefinitionParser.java

/**
 * Register CollectionTableCellRenderer//from w ww . j  av a2  s .c  o  m
 * @param element
 * @param parserContext
 * @return
 */
private ComponentDefinition registerCollectionTableCellRenderer(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder bdb = BeanDefinitionBuilder.genericBeanDefinition(CollectionTableCellRenderer.class);
    bdb.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    BeanComponentDefinition bcd = new BeanComponentDefinition(bdb.getBeanDefinition(),
            COLLECTION_TABLE_CELL_RENDERER_BEAN_NAME);
    registerBeanComponentDefinition(element, parserContext, bcd);
    return bcd;
}

From source file:net.phoenix.thrift.xml.ArgsBeanDefinitionParser.java

/**
 *  InetSocketAddress server socket?/*from   w  w  w  .  j a v  a 2 s  . c  o m*/
 *
 * @param element
 * @param parserContext
 * @return
 */
protected AbstractBeanDefinition buildInetSocketAddress(Element element) {
    int port = Integer.parseInt(element.getAttribute("port"));
    String hostname = element.getAttribute("hostname");
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(InetSocketAddress.class);
    if (!StringUtils.isEmpty(hostname))
        builder.addConstructorArgValue(hostname);
    builder.addConstructorArgValue(port);
    return builder.getBeanDefinition();
}

From source file:org.carewebframework.shell.plugins.PluginXmlParser.java

/**
 * Parse the resource list.//from  ww  w  .ja  v a 2 s .com
 * 
 * @param element Root resource tag.
 * @param builder Bean definition builder.
 * @param resourceList List of resources to build.
 */
private void parseResources(Element element, BeanDefinitionBuilder builder,
        ManagedList<AbstractBeanDefinition> resourceList) {
    NodeList resources = element.getChildNodes();

    for (int i = 0; i < resources.getLength(); i++) {
        Node node = resources.item(i);

        if (!(node instanceof Element)) {
            continue;
        }

        Element resource = (Element) resources.item(i);
        Class<? extends IPluginResource> resourceClass = null;

        switch (getResourceType(getNodeName(resource))) {
        case button:
            resourceClass = PluginResourceButton.class;
            break;

        case help:
            resourceClass = PluginResourceHelp.class;
            break;

        case menu:
            resourceClass = PluginResourceMenu.class;
            break;

        case property:
            resourceClass = PluginResourcePropertyGroup.class;
            break;

        case css:
            resourceClass = PluginResourceCSS.class;
            break;

        case bean:
            resourceClass = PluginResourceBean.class;
            break;

        case command:
            resourceClass = PluginResourceCommand.class;
            break;

        case action:
            resourceClass = PluginResourceAction.class;
            break;
        }

        if (resourceClass != null) {
            BeanDefinitionBuilder resourceBuilder = BeanDefinitionBuilder.genericBeanDefinition(resourceClass);
            addProperties(resource, resourceBuilder);
            resourceList.add(resourceBuilder.getBeanDefinition());
        }
    }
}

From source file:com.alibaba.citrus.service.form.impl.FormServiceDefinitionParser.java

private Object parseImportGroup(Element element, ParserContext parserContext,
        BeanDefinitionBuilder groupConfigBuilder) {
    BeanDefinitionBuilder importBuilder = BeanDefinitionBuilder.genericBeanDefinition(ImportImpl.class);

    importBuilder.addConstructorArgValue(element.getAttribute("group"));
    importBuilder.addConstructorArgValue(element.getAttribute("field"));

    return importBuilder.getBeanDefinition();
}