List of usage examples for org.springframework.beans.factory.config PropertyPlaceholderConfigurer getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:com.apdplat.platform.compass.APDPlatLocalCompassBean.java
public void afterPropertiesSet() throws Exception { CompassConfiguration config = this.config; if (config == null) { config = newConfiguration();//from w ww .j a va2 s .c o m } 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("apdplatcompass2"); 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("apdplatcompass3"); 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.addMappingResover(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); } } } if (postProcessor != null) { postProcessor.process(config); } this.compass = newCompass(config); this.compass = (Compass) Proxy.newProxyInstance(SpringCompassInvocationHandler.class.getClassLoader(), new Class[] { InternalCompass.class }, new SpringCompassInvocationHandler(this.compass)); }
From source file:org.compass.spring.LocalCompassBean.java
public void afterPropertiesSet() throws Exception { CompassConfiguration config = this.config; if (config == null) { config = newConfiguration();//from w w w . j a v a 2 s.c o m } 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) { for (Resource resourceJarLocation : resourceJarLocations) { config.addJar(resourceJarLocation.getFile()); } } if (classMappings != null) { for (String classMapping : classMappings) { config.addClass(ClassUtils.forName(classMapping, getClassLoader())); } } if (resourceDirectoryLocations != null) { for (Resource resourceDirectoryLocation : resourceDirectoryLocations) { File file = resourceDirectoryLocation.getFile(); if (!file.isDirectory()) { throw new IllegalArgumentException("Resource directory location [" + resourceDirectoryLocation + "] does not denote a directory"); } config.addDirectory(file); } } if (mappingResolvers != null) { for (InputStreamMappingResolver mappingResolver : mappingResolvers) { config.addMappingResolver(mappingResolver); } } 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); } } } // we do this after the proeprties placeholder hack, so if other Compass beans are created beacuse // of it, we still maintain the correct thread local setting of the data source and transaction manager 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 (postProcessor != null) { postProcessor.process(config); } this.compass = newCompass(config); this.compass = (Compass) Proxy.newProxyInstance(SpringCompassInvocationHandler.class.getClassLoader(), new Class[] { InternalCompass.class }, new SpringCompassInvocationHandler(this.compass)); }
From source file:org.sakaiproject.util.SakaiProperties.java
/** * Dereferences property placeholders in the given {@link Properties} * in exactly the same way the {@link BeanFactoryPostProcessor}s in this * object perform their placeholder dereferencing. Unfortunately, this * process is not readily decoupled from the act of processing a * bean factory in the Spring libraries. Hence the reflection. * //from www. j a v a 2 s .co m * @param srcProperties a collection of name-value pairs * @return a new collection of properties. If <code>srcProperties</code> * is <code>null</code>, returns null. If <code>srcProperties</code> * is empty, returns a reference to same object. * @throws RuntimeException if any aspect of processing fails */ private Properties dereferenceProperties(Properties srcProperties) throws RuntimeException { if (srcProperties == null) { return null; } if (srcProperties.isEmpty()) { return srcProperties; } try { Properties parsedProperties = new Properties(); PropertyPlaceholderConfigurer resolver = new PropertyPlaceholderConfigurer(); resolver.setIgnoreUnresolvablePlaceholders(true); Method parseStringValue = resolver.getClass().getDeclaredMethod("parseStringValue", String.class, Properties.class, Set.class); parseStringValue.setAccessible(true); for (Map.Entry<Object, Object> propEntry : srcProperties.entrySet()) { String parsedPropValue = (String) parseStringValue.invoke(resolver, (String) propEntry.getValue(), srcProperties, new HashSet<Object>()); parsedProperties.setProperty((String) propEntry.getKey(), parsedPropValue); } return parsedProperties; } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException("Failed to dereference properties", e); } }