Example usage for org.springframework.beans.factory BeanFactory containsBean

List of usage examples for org.springframework.beans.factory BeanFactory containsBean

Introduction

In this page you can find the example usage for org.springframework.beans.factory BeanFactory containsBean.

Prototype

boolean containsBean(String name);

Source Link

Document

Does this bean factory contain a bean definition or externally registered singleton instance with the given name?

Usage

From source file:lodsve.core.condition.ConditionEvaluationReport.java

private static void locateParent(BeanFactory beanFactory, ConditionEvaluationReport report) {
    if (beanFactory != null && report.parent == null && beanFactory.containsBean(BEAN_NAME)) {
        report.parent = beanFactory.getBean(BEAN_NAME, ConditionEvaluationReport.class);
    }// w  ww.j  a v  a  2  s .c  o  m
}

From source file:com.taobao.itest.listener.TransactionalListener.java

/**
 * Obtain a PlatformTransactionManager from the given BeanFactory, matching
 * the given qualifier.//from  w w  w  . j a va  2  s. co  m
 * 
 * @param beanFactory
 *            the BeanFactory to get the PlatformTransactionManager bean
 *            from
 * @param qualifier
 *            the qualifier for selecting between multiple
 *            PlatformTransactionManager matches
 * @return the chosen PlatformTransactionManager (never <code>null</code>)
 * @throws IllegalStateException
 *             if no matching PlatformTransactionManager bean found
 */
@SuppressWarnings("unused")
private static PlatformTransactionManager getTransactionManager(BeanFactory beanFactory, String qualifier) {
    if (beanFactory instanceof ConfigurableListableBeanFactory) {
        // Full qualifier matching supported.
        return getTransactionManager((ConfigurableListableBeanFactory) beanFactory, qualifier);
    } else if (beanFactory.containsBean(qualifier)) {
        // Fallback: PlatformTransactionManager at least found by bean name.
        return (PlatformTransactionManager) beanFactory.getBean(qualifier, PlatformTransactionManager.class);
    } else {
        throw new IllegalStateException("No matching PlatformTransactionManager bean found for bean name '"
                + qualifier + "'! (Note: Qualifier matching not supported because given BeanFactory does not "
                + "implement ConfigurableListableBeanFactory.)");
    }
}

From source file:com.foreveross.modules.jsf.SpringBeanELResolver.java

@Override
public boolean isReadOnly(ELContext elContext, Object base, Object property) throws ELException {
    if (base == null) {
        String beanName = property.toString();
        BeanFactory bf = getBeanFactory(elContext);
        if (bf.containsBean(beanName)) {
            return true;
        }//  w w w .j  a va 2s  .c  o  m
    }
    return false;
}

From source file:com.foreveross.modules.jsf.SpringBeanELResolver.java

@Override
public Class<?> getType(ELContext elContext, Object base, Object property) throws ELException {
    if (base == null) {
        String beanName = property.toString();
        BeanFactory bf = getBeanFactory(elContext);
        if (bf.containsBean(beanName)) {
            elContext.setPropertyResolved(true);
            return bf.getType(beanName);
        }/*from   w  w w  . jav  a 2s .  co m*/
    }
    return null;
}

From source file:com.foreveross.modules.jsf.SpringBeanELResolver.java

@Override
public void setValue(ELContext elContext, Object base, Object property, Object value) throws ELException {
    if (base == null) {
        String beanName = property.toString();
        BeanFactory bf = getBeanFactory(elContext);
        if (bf.containsBean(beanName)) {
            if (value == bf.getBean(beanName)) {
                // Setting the bean reference to the same value is alright - can simply be ignored...
                elContext.setPropertyResolved(true);
            } else {
                throw new PropertyNotWritableException("Variable '" + beanName
                        + "' refers to a Spring bean which by definition is not writable");
            }/*from w  w  w .  j  a  v  a 2s  .  c  o  m*/
        }
    }
}

From source file:com.foreveross.modules.jsf.SpringBeanELResolver.java

@Override
public Object getValue(ELContext elContext, Object base, Object property) throws ELException {
    if (base == null) {
        String beanName = property.toString();
        BeanFactory bf = getBeanFactory(elContext);
        if (bf.containsBean(beanName)) {
            if (logger.isTraceEnabled()) {
                logger.trace("Successfully resolved variable '" + beanName + "' in Spring BeanFactory");
            }//w  w w  .  j ava  2  s.c o  m
            elContext.setPropertyResolved(true);
            Object bean = bf.getBean(beanName);
            String className = bean.getClass().getName();
            if (className.startsWith("$Proxy")) {//Spring aop proxy for groovy bean
                try {
                    Object o = com.foreveross.infra.util.mybatis.plugin.ReflectHelper.getValueByFieldName(bean,
                            "h");
                    o = com.foreveross.infra.util.mybatis.plugin.ReflectHelper.getValueByFieldName(o,
                            "advised");
                    o = com.foreveross.infra.util.mybatis.plugin.ReflectHelper.getValueByFieldName(o,
                            "targetSource");
                    org.springframework.aop.TargetSource ts = (org.springframework.aop.TargetSource) o;
                    bean = ts.getTarget();
                } catch (Exception e) {
                    Exceptions.runtime(FormatableCharSequence.get("get [{0}.{1}.{2}.{3}] Error!", className,
                            "h", "advised", "targetSource"), e);
                }
            }
            return bean;
        }
    }
    return null;
}

From source file:org.malaguna.cmdit.service.commands.usrmgt.LoadUser.java

public LoadUser(BeanFactory bf) {
    super(bf);//from  ww w.  j  av  a 2 s  .co m
    setDao(getUserDao());
    setAction(ActionHelper.LOAD_USER);

    if (bf.containsBean(BeanNames.USER_LDAP))
        usuarioLdap = (UserLDAP) bf.getBean(BeanNames.USER_LDAP);

    setReadOnly((usuarioLdap != null) ? (!(usuarioLdap.importarUsuarios() || usuarioLdap.refrescarUsuarios()))
            : true);
}

From source file:org.toobsframework.doitref.DoItRefJob.java

public void execute(JobExecutionContext context) throws JobExecutionException {
    if (isRunning()) {
        log.info("DoItRef job already running");
        return;//from   w w  w.j  a va 2  s .  co  m
    }
    try {
        setRunning(true);
        log.info("Starting DoItRef processing");
        BeanFactoryLocator bfl = org.springframework.beans.factory.access.SingletonBeanFactoryLocator
                .getInstance();
        BeanFactory beanFactory = bfl.useBeanFactory("beanRefFactory").getFactory();
        if (!beanFactory.containsBean("IDoItRefMessageReceiver")
                || !beanFactory.containsBean("IDoItRefMessageSender")) {
            log.info("JMS Email disabled");
            return;
        }

        IDoItRefMessageReceiver dimr = (IDoItRefMessageReceiver) beanFactory.getBean("IDoItRefMessageReceiver");
        IDoItRefMessageSender dims = (IDoItRefMessageSender) beanFactory.getBean("IDoItRefMessageSender");

        IDoItRefQueue queueRunner = (IDoItRefQueue) beanFactory.getBean("IDoItRefQueue");
        DoItRefBean doItRef = null;
        while ((doItRef = dimr.recieve()) != null) {
            try {
                if (log.isDebugEnabled()) {
                    log.debug("Sending doItRef: " + doItRef.toString());
                }
                queueRunner.runDoItRef(doItRef);
            } catch (Exception e) {
                log.info("Send Email failed " + e.getMessage(), e);
                doItRef.setFailureCause(e.getMessage());
                doItRef.setAttempts(doItRef.getAttempts() + 1);
                if (doItRef.getAttempts() < retries) {
                    dims.send(doItRef);
                } else {
                    // Send to failed queue
                    //emf.send(email);
                }
            }
        }
    } catch (Exception e) {
        log.error("Error running email job", e);
    } finally {
        setRunning(false);
    }
}

From source file:org.jdtaus.core.container.mojo.SpringDescriptorMojo.java

public void execute() throws MojoExecutionException, MojoFailureException {
    final ClassLoader mavenLoader = Thread.currentThread().getContextClassLoader();

    Writer writer = null;/*from w  ww  .  jav a2 s . com*/
    OutputStream out = null;

    try {
        final ClassLoader runtimeLoader = this.getRuntimeClassLoader(mavenLoader);

        Thread.currentThread().setContextClassLoader(runtimeLoader);
        enableThreadContextClassLoader();

        final BeansElement springModel = this.getModelManager().getSpringModel(this.getFactoryBean(),
                ModelFactory.newModel().getModules());

        if (springModel.getImportOrAliasOrBean().size() > 0) {
            if (!this.getSpringDescriptorFile().getParentFile().exists()) {
                this.getSpringDescriptorFile().getParentFile().mkdirs();
            }

            out = new FileOutputStream(this.getSpringDescriptorFile());

            this.getModelManager().getSpringMarshaller().marshal(springModel, out);

            out.close();
            out = null;

            this.getLog().info(SpringDescriptorMojoBundle.getInstance().getGeneratedDescriptorMessage(
                    Locale.getDefault(), this.getSpringDescriptorFile().getCanonicalPath()));

            final BeanFactory beanFactory = new XmlBeanFactory(
                    new FileSystemResource(this.getSpringDescriptorFile()));

            beanFactory.containsBean("TEST");

            final JavaArtifact artifact = new JavaArtifact(this.getFactoryBean());

            final File source = new File(this.getSourceRoot(),
                    artifact.getPackagePath() + File.separator + artifact.getName() + ".java");

            if (!source.getParentFile().exists() && !source.getParentFile().mkdirs()) {
                throw new MojoExecutionException(
                        SpringDescriptorMojoBundle.getInstance().getCannotCreateDirectoryMessage(
                                Locale.getDefault(), source.getParentFile().getAbsolutePath()));

            }

            if (this.getEncoding() == null) {
                writer = new FileWriter(source);
            } else {
                writer = new OutputStreamWriter(new FileOutputStream(source), this.getEncoding());

            }

            final VelocityContext ctx = new VelocityContext();
            ctx.put("artifact", artifact);
            ctx.put("project", this.getMavenProject());

            this.getVelocity().mergeTemplate(FACTORY_BEAN_TEMPLATE_LOCATION, "UTF-8", ctx, writer);

            writer.close();
            writer = null;

            this.getMavenProject().addCompileSourceRoot(this.getSourceRoot().getAbsolutePath());

            this.getLog().info(SpringDescriptorMojoBundle.getInstance()
                    .getGeneratedFactoryBeanMessage(Locale.getDefault(), source.getCanonicalPath()));

        } else {
            this.getLog().info(SpringDescriptorMojoBundle.getInstance()
                    .getEmptyDescriptorMessage(Locale.getDefault(), this.springDescriptor.getCanonicalPath()));

        }
    } catch (final ContextError e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } catch (final ContainerError e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } catch (final ModelError e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } catch (final Exception e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } finally {
        disableThreadContextClassLoader();
        Thread.currentThread().setContextClassLoader(mavenLoader);

        try {
            if (out != null) {
                out.close();
            }
        } catch (final IOException e) {
            this.getLog().error(e);
        }

        try {
            if (writer != null) {
                writer.close();
            }
        } catch (final IOException e) {
            this.getLog().error(e);
        }
    }
}

From source file:com.doculibre.constellio.services.ConnectorManagerServicesImpl.java

@Override
public void synchronizeWithDatabase(ConnectorManager connectorManager) {
    ConnectorTypeServices connectorTypeServices = ConstellioSpringUtils.getConnectorTypeServices();

    // Add missing connector types
    List<String> connectorTypeNames = getConnectorTypes(connectorManager);
    for (String connectorTypeName : connectorTypeNames) {
        ConnectorType connectorType = connectorTypeServices.get(connectorTypeName);
        if (connectorType == null) {
            connectorType = new ConnectorType();
            connectorType.setName(connectorTypeName);

            File connectorsDir = ConstellioSpringUtils.getGoogleConnectorsDir();
            File connectorTypeDir = new File(connectorsDir, connectorType.getName());

            File iconFile = new File(connectorTypeDir, "icon.gif");
            if (iconFile.exists()) {
                try {
                    byte[] iconBytes = FileUtils.readFileToByteArray(iconFile);
                    connectorType.setIconFileContent(iconBytes);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }//from w w  w.j a  v  a 2 s  . co m
            }

            File connectorTypeMetaXmlFile = new File(connectorTypeDir, "connectorTypeMeta.xml");
            if (connectorTypeMetaXmlFile.exists()) {
                String path = connectorTypeMetaXmlFile.toURI().toString();
                BeanFactory connectorTypeMeta = new FileSystemXmlApplicationContext(path);
                if (connectorTypeMeta.containsBean("searchResultPanelClassName")) {
                    String searchResultPanelClassName = (String) connectorTypeMeta
                            .getBean("searchResultPanelClassName");
                    connectorType.setSearchResultPanelClassName(searchResultPanelClassName);
                }
                if (connectorTypeMeta.containsBean("initInstanceHandlerClassName")) {
                    String initInstancePluginClassName = (String) connectorTypeMeta
                            .getBean("initInstanceHandlerClassName");
                    connectorType.setInitInstanceHandlerClassName(initInstancePluginClassName);
                }
            }

            connectorType.setConnectorManager(connectorManager);
            connectorTypeServices.makePersistent(connectorType);
        }
    }

    // Remove deleted connector types
    List<ConnectorType> dbConnectorType = connectorTypeServices.list();
    for (ConnectorType connectorType : dbConnectorType) {
        if (!connectorTypeNames.contains(connectorType.getName())) {
            connectorTypeServices.makeTransient(connectorType);
        }
    }

    ConnectorInstanceServices connectorInstanceServices = ConstellioSpringUtils.getConnectorInstanceServices();
    BackupServices backupServices = ConstellioSpringUtils.getBackupServices();
    List<ConnectorInstance> connectorInstances = connectorInstanceServices.list();
    for (ConnectorInstance connectorInstance : connectorInstances) {
        String connectorName = connectorInstance.getName();
        String connectorTypeName = connectorInstance.getConnectorType().getName();
        boolean existingConnector = isExistingConnector(connectorManager, connectorName);
        boolean hasConfigBackup = backupServices.hasConfigBackup(connectorName, connectorTypeName);
        if (!existingConnector && hasConfigBackup) {
            backupServices.restoreConfigBackup(connectorName, connectorTypeName);
        } else if (existingConnector && !hasConfigBackup) {
            backupServices.backupConfig(connectorName, connectorTypeName);
        }
    }
}