public void setCompassConfiguration(CompassConfiguration config) {
this.config = config;
}
public void afterPropertiesSet() throws Exception {
CompassConfiguration config = this.config;
if (config == null) {
config = newConfiguration();
}
if (classLoader != null) {
config.setClassLoader(getClassLoader());
}
if (this.configLocation != null) {
config.configure(this.configLocation.getURL());
}
if (this.configLocations != null) {
for (Resource configLocation1 : configLocations) {
config.configure(configLocation1.getURL());
}
}
if (this.mappingScan != null) {
config.addScan(this.mappingScan);
}
if (this.compassSettings != null) {
config.getSettings().addSettings(this.compassSettings);
}
if (this.settings != null) {
config.getSettings().addSettings(this.settings);
}
if (resourceLocations != null) {
for (Resource resourceLocation : resourceLocations) {
config.addInputStream(resourceLocation.getInputStream(), resourceLocation.getFilename());
}
}
if (resourceJarLocations != null && !"".equals(resourceJarLocations.trim())) {
log.info("开始执行apdplat对compass的定制修改2");
log.info("compass resourceJarLocations:" + resourceJarLocations);
String[] jars = resourceJarLocations.split(",");
for (String jar : jars) {
try {
FileSystemResource resource = new FileSystemResource(FileUtils.getAbsolutePath(jar));
config.addJar(resource.getFile());
log.info("compass resourceJarLocations find:" + jar);
} catch (Exception e) {
log.info("compass resourceJarLocations not exists:" + jar);
}
}
}
if (classMappings != null) {
for (String classMapping : classMappings) {
config.addClass(ClassUtils.forName(classMapping, getClassLoader()));
}
}
if (resourceDirectoryLocations != null && !"".equals(resourceDirectoryLocations.trim())) {
log.info("开始执行apdplat对compass的定制修改3");
log.info("compass resourceDirectoryLocations:" + resourceDirectoryLocations);
String[] dirs = resourceDirectoryLocations.split(",");
for (String dir : dirs) {
ClassPathResource resource = new ClassPathResource(dir);
try {
File file = resource.getFile();
if (!file.isDirectory()) {
log.info("Resource directory location ["
+ dir + "] does not denote a directory");
} else {
config.addDirectory(file);
}
log.info("compass resourceDirectoryLocations find:" + dir);
} catch (Exception e) {
log.info("compass resourceDirectoryLocations not exists:" + dir);
}
}
}
if (mappingResolvers != null) {
for (InputStreamMappingResolver mappingResolver : mappingResolvers) {
config.addMappingResolver(mappingResolver);
}
}
if (dataSource != null) {
ExternalDataSourceProvider.setDataSource(dataSource);
if (config.getSettings().getSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.CLASS) == null) {
config.getSettings().setSetting(LuceneEnvironment.JdbcStore.DataSourceProvider.CLASS,
ExternalDataSourceProvider.class.getName());
}
}
String compassTransactionFactory = config.getSettings().getSetting(CompassEnvironment.Transaction.FACTORY);
if (compassTransactionFactory == null && transactionManager != null) {
// if the transaciton manager is set and a transcation factory is not set, default to the SpringSync one.
config.getSettings().setSetting(CompassEnvironment.Transaction.FACTORY, SpringSyncTransactionFactory.class.getName());
}
if (compassTransactionFactory != null && compassTransactionFactory.equals(SpringSyncTransactionFactory.class.getName())) {
if (transactionManager == null) {
throw new IllegalArgumentException("When using SpringSyncTransactionFactory the transactionManager property must be set");
}
}
SpringSyncTransactionFactory.setTransactionManager(transactionManager);
if (convertersByName != null) {
for (Map.Entry<String, Converter> entry : convertersByName.entrySet()) {
config.registerConverter(entry.getKey(), entry.getValue());
}
}
if (config.getSettings().getSetting(CompassEnvironment.NAME) == null) {
config.getSettings().setSetting(CompassEnvironment.NAME, beanName);
}
if (config.getSettings().getSetting(CompassEnvironment.CONNECTION) == null && connection != null) {
config.getSettings().setSetting(CompassEnvironment.CONNECTION, connection.getFile().getAbsolutePath());
}
if (applicationContext != null) {
String[] names = applicationContext.getBeanNamesForType(PropertyPlaceholderConfigurer.class);
for (String name : names) {
try {
PropertyPlaceholderConfigurer propConfigurer = (PropertyPlaceholderConfigurer) applicationContext.getBean(name);
Method method = findMethod(propConfigurer.getClass(), "mergeProperties");
method.setAccessible(true);
Properties props = (Properties) method.invoke(propConfigurer);
method = findMethod(propConfigurer.getClass(), "convertProperties", Properties.class);
method.setAccessible(true);
method.invoke(propConfigurer, props);
method = findMethod(propConfigurer.getClass(), "parseStringValue", String.class, Properties.class, Set.class);
method.setAccessible(true);
String nullValue = null;
try {
Field field = propConfigurer.getClass().getDeclaredField("nullValue");
field.setAccessible(true);
nullValue = (String) field.get(propConfigurer);
} catch (NoSuchFieldException e) {
// no field (old spring version)
}
for (Map.Entry entry : config.getSettings().getProperties().entrySet()) {
String key = (String) entry.getKey();
String value = (String) entry.getValue();
value = (String) method.invoke(propConfigurer, value, props, new HashSet());
config.getSettings().setSetting(key, value.equals(nullValue) ? null : value);
}
} catch (Exception e) {
log.debug("Failed to apply property placeholder defined in bean [" + name + "]", e);
}
}