
pl.bristleback.server.bristle.config.ClassConfigurationUtil Maven / Gradle / Ivy
// Bristleback plugin - Copyright (c) 2010 bristleback.googlecode.com
// ---------------------------------------------------------------------------
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
// This library is distributed in the hope that it will be useful,
// but without any warranty; without even the implied warranty of merchantability
// or fitness for a particular purpose.
// You should have received a copy of the GNU Lesser General Public License along
// with this program; if not, see .
// ---------------------------------------------------------------------------
package pl.bristleback.server.bristle.config;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import pl.bristleback.server.bristle.exceptions.ImplementationResolvingException;
import pl.bristleback.server.bristle.exceptions.MissingPluginConfigurationElementException;
import pl.bristleback.server.bristle.integration.spring.SpringIntegrationUtil;
import pl.bristleback.server.bristle.utils.ResolverUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* This class provides functions which can be used in user implementations of resolvers of various configuration elements.
*
* Created on: 2010-10-14 21:11:19
*
* @author Wojciech Niemiec
*/
public final class ClassConfigurationUtil {
private static Logger log = Logger.getLogger(ClassConfigurationUtil.class.getName());
public static final String SPRING_REF_PART = "_ref_";
public static final String CLASS_TYPE_PART = "_class_";
public static final String CLASS_NAME_PART = "_name_";
private ClassConfigurationUtil() {
throw new UnsupportedOperationException();
}
/**
* Gets list of class elements taken from plugin settings with prefix given in constructor,
* each bound in {@link pl.bristleback.server.bristle.config.ClassConfiguration} class.
* Special convention of names is used here, user should provide only name of setting property.
* For example, to get message sender classes, name of property prefix is: 'messageSender'.
*
* @param settingPrefix prefix first part of setting property.
* @param pluginSettings map of settings from plugin configuration.
* @return list of class configuration.
*/
public static List getClassConfigurationsFromSettingsMap(String settingPrefix, Map pluginSettings) {
List classConfigurations = new ArrayList();
int index = 0;
String className = getClassNameForIndex(settingPrefix, pluginSettings, index);
while (StringUtils.isNotEmpty(className)) {
ClassConfiguration information = getClassConfigurationForIndex(settingPrefix, className, pluginSettings, index);
classConfigurations.add(information);
index++;
className = getClassNameForIndex(settingPrefix, pluginSettings, index);
}
return classConfigurations;
}
private static ClassConfiguration getClassConfigurationForIndex(String settingPrefix, String className, Map pluginSettings, int index) {
String springRef = getSenderSpringRefForIndex(settingPrefix, pluginSettings, index);
String classType = getClassTypeForIndex(settingPrefix, pluginSettings, index);
return createClassConfigurationInstance(className, springRef, classType);
}
private static ClassConfiguration createClassConfigurationInstance(String className, String springRef, String classType) {
String type = getClassInstanceType(classType, springRef);
boolean isSpringRef = StringUtils.isNotEmpty(springRef);
return new ClassConfiguration(className, type, isSpringRef);
}
private static String getClassInstanceType(String senderClass, String senderSpringRef) {
if (StringUtils.isNotEmpty(senderClass)) {
return senderClass;
} else if (StringUtils.isNotEmpty(senderSpringRef)) {
return senderSpringRef;
} else {
throw new MissingPluginConfigurationElementException("message sender class or Spring reference");
}
}
private static String getClassNameForIndex(String settingPrefix, Map pluginSettings, int index) {
return pluginSettings.get(settingPrefix + CLASS_NAME_PART + index);
}
private static String getClassTypeForIndex(String settingPrefix, Map pluginSettings, int index) {
return pluginSettings.get(settingPrefix + CLASS_TYPE_PART + index);
}
private static String getSenderSpringRefForIndex(String settingPrefix, Map pluginSettings, int index) {
return pluginSettings.get(settingPrefix + SPRING_REF_PART + index);
}
public static T getInstanceFromConfiguration(Class type, ClassConfiguration configuration) {
if (configuration.isSpringType()) {
return SpringIntegrationUtil.getSpringBean(configuration.getType(), type);
} else {
return retrieveInstance(type, configuration);
}
}
/**
* Returns instance of class from given class configuration. Method checks the way in which class should be retrieved.
* Depending on configuration, it creates new instance of class or gets instance from Spring context.
*
* @param type type of class.
* @param configuration information about class.
* @param type of instance.
* @return instance of class taken from configuration.
*/
@SuppressWarnings("unchecked")
private static T retrieveInstance(Class type, ClassConfiguration configuration) {
try {
ResolverUtil resolverUtil = new ResolverUtil();
Class senderClass = resolverUtil.getImplementation(type, configuration.getType());
return senderClass.newInstance();
} catch (Exception e) {
throw new ImplementationResolvingException(configuration.getType(), type);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy