Search in sources :

Example 81 with BeansException

use of org.springframework.beans.BeansException in project opennms by OpenNMS.

the class DefaultPluginRegistry method getPluginInstance.

/**
 * {@inheritDoc}
 */
@Override
public <T> T getPluginInstance(Class<T> pluginClass, PluginConfig pluginConfig) {
    T pluginInstance = beanWithNameOfType(pluginConfig.getPluginClass(), pluginClass);
    if (pluginInstance == null) {
        return null;
    }
    Map<String, String> parameters = new HashMap<String, String>(pluginConfig.getParameterMap());
    BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(pluginInstance);
    try {
        wrapper.setPropertyValues(parameters);
    } catch (BeansException e) {
        error(e, "Could not set properties on report definition: {}", e.getMessage());
    }
    return pluginInstance;
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) HashMap(java.util.HashMap) BeansException(org.springframework.beans.BeansException)

Example 82 with BeansException

use of org.springframework.beans.BeansException in project webanno by webanno.

the class PreferencesUtil method loadPreferences.

/**
 * Set annotation preferences of users for a given project such as window size, annotation
 * layers,... reading from the file system.
 *
 * @param aUsername
 *            The {@link User} for whom we need to read the preference (preferences are stored
 *            per user)
 * @param aRepositoryService the repository service.
 * @param aAnnotationService the annotation service.
 * @param aBModel
 *            The {@link AnnotatorState} that will be populated with preferences from the
 *            file
 * @param aMode the mode.
 * @throws BeansException hum?
 * @throws IOException hum?
 */
public static void loadPreferences(String aUsername, SettingsService aSettingsService, ProjectService aRepositoryService, AnnotationSchemaService aAnnotationService, AnnotatorState aBModel, Mode aMode) throws BeansException, IOException {
    AnnotationPreference preference = new AnnotationPreference();
    BeanWrapper wrapper = new BeanWrapperImpl(preference);
    // get annotation preference from file system
    try {
        Properties props = aRepositoryService.loadUserSettings(aUsername, aBModel.getProject());
        for (Entry<Object, Object> entry : props.entrySet()) {
            String property = entry.getKey().toString();
            int index = property.indexOf(".");
            String propertyName = property.substring(index + 1);
            String mode = property.substring(0, index);
            if (wrapper.isWritableProperty(propertyName) && mode.equals(aMode.getName())) {
                if (AnnotationPreference.class.getDeclaredField(propertyName).getGenericType() instanceof ParameterizedType) {
                    if (entry.getValue().toString().startsWith("[")) {
                        // its a list
                        List<String> value = Arrays.asList(StringUtils.replaceChars(entry.getValue().toString(), "[]", "").split(","));
                        if (!value.get(0).equals("")) {
                            wrapper.setPropertyValue(propertyName, value);
                        }
                    } else if (entry.getValue().toString().startsWith("{")) {
                        // its a map
                        String s = StringUtils.replaceChars(entry.getValue().toString(), "{}", "");
                        Map<String, String> value = Arrays.stream(s.split(",")).map(x -> x.split("=")).collect(Collectors.toMap(x -> x[0], x -> x[1]));
                        wrapper.setPropertyValue(propertyName, value);
                    }
                } else {
                    wrapper.setPropertyValue(propertyName, entry.getValue());
                }
            }
        }
        // set layers according to preferences
        List<AnnotationLayer> enabledLayers = aAnnotationService.listAnnotationLayer(aBModel.getProject()).stream().filter(// only allow enabled layers
        l -> l.isEnabled()).collect(Collectors.toList());
        List<Long> hiddenLayerIds = preference.getHiddenAnnotationLayerIds();
        enabledLayers = enabledLayers.stream().filter(l -> !hiddenLayerIds.contains(l.getId())).collect(Collectors.toList());
        aBModel.setAnnotationLayers(enabledLayers);
        // Get color preferences for each layer, init with legacy if not found
        Map<Long, ColoringStrategyType> colorPerLayer = preference.getColorPerLayer();
        for (AnnotationLayer layer : aAnnotationService.listAnnotationLayer(aBModel.getProject())) {
            if (!colorPerLayer.containsKey(layer.getId())) {
                colorPerLayer.put(layer.getId(), ColoringStrategyType.LEGACY);
            }
        }
    }// no preference found
     catch (Exception e) {
        // If no layer preferences are defined,
        // then just assume all enabled layers are preferred
        List<AnnotationLayer> enabledLayers = aAnnotationService.listAnnotationLayer(aBModel.getProject()).stream().filter(// only allow enabled layers
        l -> l.isEnabled()).collect(Collectors.toList());
        aBModel.setAnnotationLayers(enabledLayers);
        preference.setWindowSize(aSettingsService.getNumberOfSentences());
        // add default coloring strategy
        Map<Long, ColoringStrategyType> colorPerLayer = new HashMap<>();
        for (AnnotationLayer layer : aBModel.getAnnotationLayers()) {
            colorPerLayer.put(layer.getId(), ColoringStrategy.getBestInitialStrategy(aAnnotationService, layer, preference));
        }
        preference.setColorPerLayer(colorPerLayer);
    }
    aBModel.setPreferences(preference);
}
Also used : Arrays(java.util.Arrays) Properties(java.util.Properties) ProjectService(de.tudarmstadt.ukp.clarin.webanno.api.ProjectService) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) BeanWrapper(org.springframework.beans.BeanWrapper) IOException(java.io.IOException) HashMap(java.util.HashMap) BeansException(org.springframework.beans.BeansException) ColoringStrategy(de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringStrategy) Mode(de.tudarmstadt.ukp.clarin.webanno.model.Mode) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) SettingsService(de.tudarmstadt.ukp.clarin.webanno.api.SettingsService) AnnotationSchemaService(de.tudarmstadt.ukp.clarin.webanno.api.AnnotationSchemaService) List(java.util.List) ParameterizedType(java.lang.reflect.ParameterizedType) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) AnnotationPreference(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotationPreference) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) Map(java.util.Map) Entry(java.util.Map.Entry) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) ColoringStrategyType(de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringStrategy.ColoringStrategyType) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Properties(java.util.Properties) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) ParameterizedType(java.lang.reflect.ParameterizedType) BeanWrapper(org.springframework.beans.BeanWrapper) List(java.util.List) AnnotationPreference(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotationPreference) HashMap(java.util.HashMap) Map(java.util.Map) ColoringStrategyType(de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringStrategy.ColoringStrategyType)

Example 83 with BeansException

use of org.springframework.beans.BeansException in project SearchServices by Alfresco.

the class AlfrescoSolrDataModel method getQNameFilter.

/**
 * TODO: Fix to load type filter/exclusions from somewhere sensible
 * @return QNameFilter
 */
private QNameFilter getQNameFilter() {
    QNameFilter qnameFilter = null;
    FileSystemXmlApplicationContext ctx = null;
    File resourceDirectory = getResourceDirectory();
    // If we ever need to filter out models in the future, then we must put a filter somewhere.
    // Currently, we do not need to filter any models, so this filter does not exist.
    File filterContext = new File(resourceDirectory, "opencmis-qnamefilter-context.xml");
    if (!filterContext.exists()) {
        log.info("No type filter context found at " + filterContext.getAbsolutePath() + ", no type filtering");
        return qnameFilter;
    }
    try {
        ctx = new FileSystemXmlApplicationContext(new String[] { "file:" + filterContext.getAbsolutePath() }, false);
        ctx.setClassLoader(this.getClass().getClassLoader());
        ctx.refresh();
        qnameFilter = (QNameFilter) ctx.getBean("cmisTypeExclusions");
        if (qnameFilter == null) {
            log.warn("Unable to find type filter at " + filterContext.getAbsolutePath() + ", no type filtering");
        }
    } catch (BeansException e) {
        log.warn("Unable to parse type filter at " + filterContext.getAbsolutePath() + ", no type filtering");
    } finally {
        if (ctx != null && ctx.getBeanFactory() != null && ctx.isActive()) {
            ctx.close();
        }
    }
    return qnameFilter;
}
Also used : FileSystemXmlApplicationContext(org.springframework.context.support.FileSystemXmlApplicationContext) QNameFilter(org.alfresco.opencmis.dictionary.QNameFilter) File(java.io.File) BeansException(org.springframework.beans.BeansException)

Example 84 with BeansException

use of org.springframework.beans.BeansException in project turbo-rpc by hank-whu.

the class TurboClientStarter method postProcessBeanFactory.

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    this.beanFactory = beanFactory;
    try {
        ClientConfig clientConfig = ClientConfig.parse("turbo-client.conf");
        turboClient = new TurboClient(clientConfig);
    } catch (com.typesafe.config.ConfigException configException) {
        if (logger.isErrorEnabled()) {
            logger.error("turbo-client.conf 格式错误,无法开启TurboClient!", configException);
        }
        throw configException;
    } catch (Exception e) {
        if (logger.isErrorEnabled()) {
            logger.error("类路径中找不到 turbo-client.conf,无法开启TurboClient!", e);
        }
        throw e;
    }
    Collection<Class<?>> turboClassList = extractTurboServiceClassList(beanFactory);
    for (Class<?> turboClass : turboClassList) {
        registerTurboService(turboClass);
    }
}
Also used : TurboClient(rpc.turbo.client.TurboClient) ClientConfig(rpc.turbo.config.client.ClientConfig) BeansException(org.springframework.beans.BeansException)

Example 85 with BeansException

use of org.springframework.beans.BeansException in project RestyPass by darren-fu.

the class RestyProxyBeanFactory method getBean.

/**
 * 从spring容器获取执行类型bean或者获取默认实现
 *
 * @param clz bean class 类型
 * @param <T>
 * @return bean
 */
private <T> T getBean(Class<T> clz) {
    T t = null;
    try {
        if (this.applicationContext != null) {
            t = this.applicationContext.getBean(clz);
        }
        if (t != null) {
            log.info("{}使用Spring注入:{}", clz.getSimpleName(), t.getClass());
        } else {
            t = DefaultRestyPassFactory.getDefaultBean(clz);
            log.info("{}使用默认配置:{}", clz.getSimpleName(), t.getClass());
            if (t == null) {
                throw new IllegalArgumentException("无法获取Bean:" + clz);
            }
            if (t instanceof ApplicationContextAware) {
                ApplicationContextAware contextAware = (ApplicationContextAware) serverContext;
                contextAware.setApplicationContext(this.applicationContext);
            }
        }
    } catch (BeansException ex) {
        t = DefaultRestyPassFactory.getDefaultBean(clz);
        log.info("{}使用默认配置:{}", clz.getSimpleName(), t.getClass());
    }
    return t;
}
Also used : ApplicationContextAware(org.springframework.context.ApplicationContextAware) BeansException(org.springframework.beans.BeansException)

Aggregations

BeansException (org.springframework.beans.BeansException)126 Test (org.junit.Test)24 ApplicationContext (org.springframework.context.ApplicationContext)22 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)18 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)16 Map (java.util.Map)15 ArrayList (java.util.ArrayList)13 BeanCreationException (org.springframework.beans.factory.BeanCreationException)11 HashMap (java.util.HashMap)10 BeanWrapper (org.springframework.beans.BeanWrapper)10 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)10 MalformedURLException (java.net.MalformedURLException)9 File (java.io.File)8 IOException (java.io.IOException)8 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)8 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)8 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)8 List (java.util.List)7 Method (java.lang.reflect.Method)6 LinkedHashSet (java.util.LinkedHashSet)6