/runtime/application-config/src/main/java/org/springframework/roo/application/config/ApplicationConfigServiceImpl.java

http://github.com/SpringSource/spring-roo · Java · 280 lines · 206 code · 43 blank · 31 comment · 15 complexity · 83da10c0abb8f04b8e8df2c28319d7a0 MD5 · raw file

  1. package org.springframework.roo.application.config;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.SortedSet;
  6. import java.util.TreeSet;
  7. import org.apache.commons.lang3.StringUtils;
  8. import org.apache.commons.lang3.Validate;
  9. import org.apache.felix.scr.annotations.Component;
  10. import org.apache.felix.scr.annotations.Reference;
  11. import org.apache.felix.scr.annotations.Service;
  12. import org.springframework.roo.classpath.ModuleFeatureName;
  13. import org.springframework.roo.classpath.TypeLocationService;
  14. import org.springframework.roo.file.monitor.event.FileDetails;
  15. import org.springframework.roo.process.manager.FileManager;
  16. import org.springframework.roo.project.LogicalPath;
  17. import org.springframework.roo.project.Path;
  18. import org.springframework.roo.project.PathResolver;
  19. import org.springframework.roo.project.maven.Pom;
  20. import org.springframework.roo.propfiles.manager.PropFilesManagerService;
  21. import org.springframework.roo.settings.project.ProjectSettingsService;
  22. /**
  23. * Provides a service to manage all necessary properties located on application
  24. * configuration files.
  25. *
  26. * @author Juan Carlos García
  27. * @author Paula Navarro
  28. * @since 2.0
  29. */
  30. @Component
  31. @Service
  32. public class ApplicationConfigServiceImpl implements ApplicationConfigService {
  33. private static final Path DEFAULT_APPLICATION_CONFIG_FILE_LOCATION = Path.SRC_MAIN_RESOURCES;
  34. private static final String DEFAULT_APPLICATION_CONFIG_FILE_NAME = "application";
  35. private static final String DEFAULT_APPLICATION_CONFIG_FILE_EXTENSION = ".properties";
  36. // ROO-3706: Spring Roo Configuration Settings
  37. private static final String CONFIG_FILE_LOCATION_SHELL_PROPERTY =
  38. "spring.roo.configuration.location";
  39. private static final String CONFIG_FILE_NAME_SHELL_PROPERTY = "spring.roo.configuration.name";
  40. @Reference
  41. private PropFilesManagerService propFilesManager;
  42. @Reference
  43. private PathResolver pathResolver;
  44. @Reference
  45. private FileManager fileManager;
  46. @Reference
  47. private ProjectSettingsService settingsService;
  48. @Reference
  49. private TypeLocationService typeLocationService;
  50. @Override
  51. public void addProperty(final String moduleName, final String key, final String value,
  52. String profile, boolean force) {
  53. // Get application modules
  54. propFilesManager.addPropertyIfNotExists(getApplicationConfigFileLocation(moduleName),
  55. getAppliCationConfigFileName(profile), key, value, true, force);
  56. }
  57. @Override
  58. public void addProperty(final String moduleName, final String prefix, final String key,
  59. final String value, String profile, boolean force) {
  60. // Get application modules
  61. propFilesManager.addPropertyIfNotExists(getApplicationConfigFileLocation(moduleName),
  62. getAppliCationConfigFileName(profile), prefix, key, value, true, force);
  63. }
  64. @Override
  65. public void addProperties(final String moduleName, final Map<String, String> properties,
  66. String profile, boolean force) {
  67. // Get application modules
  68. propFilesManager.addProperties(getApplicationConfigFileLocation(moduleName),
  69. getAppliCationConfigFileName(profile), properties, true, force);
  70. }
  71. @Override
  72. public void addProperties(final String moduleName, final String prefix,
  73. final Map<String, String> properties, String profile, boolean force) {
  74. // Get application modules
  75. propFilesManager.addProperties(getApplicationConfigFileLocation(moduleName),
  76. getAppliCationConfigFileName(profile), prefix, properties, true, force);
  77. }
  78. @Override
  79. public void updateProperty(final String moduleName, final String key, final String value,
  80. String profile, boolean force) {
  81. propFilesManager.changeProperty(getApplicationConfigFileLocation(moduleName),
  82. getAppliCationConfigFileName(profile), key, value, true, force);
  83. }
  84. @Override
  85. public void updateProperty(final String moduleName, final String prefix, final String key,
  86. final String value, String profile, boolean force) {
  87. propFilesManager.changeProperty(getApplicationConfigFileLocation(moduleName),
  88. getAppliCationConfigFileName(profile), prefix, key, value, true, force);
  89. }
  90. @Override
  91. public void updateProperties(final String moduleName, final Map<String, String> properties,
  92. String profile, boolean force) {
  93. propFilesManager.changeProperties(getApplicationConfigFileLocation(moduleName),
  94. getAppliCationConfigFileName(profile), properties, true, force);
  95. }
  96. @Override
  97. public void updateProperties(final String moduleName, final String prefix,
  98. final Map<String, String> properties, String profile, boolean force) {
  99. propFilesManager.changeProperties(getApplicationConfigFileLocation(moduleName),
  100. getAppliCationConfigFileName(profile), prefix, properties, true, force);
  101. }
  102. @Override
  103. public List<String> getApplicationProfiles(String moduleName) {
  104. List<String> profiles = new ArrayList<String>();
  105. final String applicationConfigFilename =
  106. StringUtils.removeEnd(getSpringConfigLocation(moduleName),
  107. DEFAULT_APPLICATION_CONFIG_FILE_EXTENSION);
  108. // Find application config files
  109. for (final FileDetails applicationConfig : fileManager
  110. .findMatchingAntPath(applicationConfigFilename + "*"
  111. + DEFAULT_APPLICATION_CONFIG_FILE_EXTENSION)) {
  112. final String applicationConfigPath = applicationConfig.getCanonicalPath();
  113. if (!fileManager.exists(applicationConfigPath)) {
  114. continue;
  115. }
  116. // Extract profile
  117. String profile =
  118. StringUtils.substringBetween(applicationConfigPath, applicationConfigFilename,
  119. ".properties");
  120. profiles.add(StringUtils.removeStart(profile, "-"));
  121. }
  122. return profiles;
  123. }
  124. @Override
  125. public Map<String, String> getProperties(String moduleName, String profile) {
  126. return propFilesManager.getProperties(getApplicationConfigFileLocation(moduleName),
  127. getAppliCationConfigFileName(profile));
  128. }
  129. @Override
  130. public SortedSet<String> getPropertyKeys(final String moduleName, boolean includeValues,
  131. String profile) {
  132. return propFilesManager.getPropertyKeys(getApplicationConfigFileLocation(moduleName),
  133. getAppliCationConfigFileName(profile), includeValues);
  134. }
  135. @Override
  136. public SortedSet<String> getPropertyKeys(final String moduleName, String prefix,
  137. boolean includeValues, String profile) {
  138. return propFilesManager.getPropertyKeys(getApplicationConfigFileLocation(moduleName),
  139. getAppliCationConfigFileName(profile), prefix, includeValues);
  140. }
  141. @Override
  142. public String getProperty(final String moduleName, final String key, String profile) {
  143. return propFilesManager.getProperty(getApplicationConfigFileLocation(moduleName),
  144. getAppliCationConfigFileName(profile), key);
  145. }
  146. @Override
  147. public String getProperty(final String moduleName, final String prefix, final String key,
  148. String profile) {
  149. return propFilesManager.getProperty(getApplicationConfigFileLocation(moduleName),
  150. getAppliCationConfigFileName(profile), prefix, key);
  151. }
  152. @Override
  153. public void removeProperty(final String moduleName, final String key, String profile) {
  154. if (existsSpringConfigFile(moduleName, profile)) {
  155. propFilesManager.removeProperty(getApplicationConfigFileLocation(moduleName),
  156. getAppliCationConfigFileName(profile), key);
  157. }
  158. }
  159. @Override
  160. public void removeProperty(final String moduleName, final String prefix, String key,
  161. String profile) {
  162. if (existsSpringConfigFile(moduleName, profile)) {
  163. propFilesManager.removeProperty(getApplicationConfigFileLocation(moduleName),
  164. getAppliCationConfigFileName(profile), prefix, key);
  165. }
  166. }
  167. @Override
  168. public void removeProperties(final String moduleName, List<String> keys, String profile) {
  169. for (String key : keys) {
  170. removeProperty(moduleName, key, profile);
  171. }
  172. }
  173. @Override
  174. public void removePropertiesByPrefix(final String moduleName, String prefix, String profile) {
  175. if (existsSpringConfigFile(moduleName, profile)) {
  176. propFilesManager.removePropertiesByPrefix(getApplicationConfigFileLocation(moduleName),
  177. getAppliCationConfigFileName(profile), prefix);
  178. }
  179. }
  180. @Override
  181. public String getSpringConfigLocation(String moduleName) {
  182. return pathResolver.getIdentifier(getApplicationConfigFileLocation(moduleName),
  183. getAppliCationConfigFileName(null));
  184. }
  185. @Override
  186. public String getSpringConfigLocation(String moduleName, String profile) {
  187. return pathResolver.getIdentifier(getApplicationConfigFileLocation(moduleName),
  188. getAppliCationConfigFileName(profile));
  189. }
  190. @Override
  191. public boolean existsSpringConfigFile(String moduleName) {
  192. return fileManager.exists(getSpringConfigLocation(moduleName));
  193. }
  194. @Override
  195. public boolean existsSpringConfigFile(String moduleName, String profile) {
  196. return fileManager.exists(getSpringConfigLocation(moduleName, profile));
  197. }
  198. /**
  199. * Method that generates application config file name using application
  200. * config file name profile parameter if exists, and config file extension.
  201. *
  202. * @param profile
  203. * @return
  204. */
  205. private String getAppliCationConfigFileName(String profile) {
  206. String fileName = DEFAULT_APPLICATION_CONFIG_FILE_NAME;
  207. // ROO-3706: Check if exists some specific configuration to the
  208. // name of application config properties file
  209. String fileNameShellConfig = settingsService.getProperty(CONFIG_FILE_NAME_SHELL_PROPERTY);
  210. if (fileNameShellConfig != null && StringUtils.isNotBlank(fileNameShellConfig)) {
  211. fileName = fileNameShellConfig;
  212. }
  213. if (profile != null && StringUtils.isNotBlank(profile)) {
  214. fileName = fileName.concat("-").concat(profile);
  215. }
  216. fileName = fileName.concat(DEFAULT_APPLICATION_CONFIG_FILE_EXTENSION);
  217. return fileName;
  218. }
  219. /**
  220. * Method that get application config file location
  221. *
  222. * @return Path where application config file is located
  223. */
  224. private LogicalPath getApplicationConfigFileLocation(String moduleName) {
  225. Validate.notNull(moduleName, "Module name is required");
  226. LogicalPath location =
  227. LogicalPath.getInstance(DEFAULT_APPLICATION_CONFIG_FILE_LOCATION, moduleName);
  228. // ROO-3706: Check if exists some specific configuration to the
  229. // location of application config properties file
  230. String fileLocationShellConfig =
  231. settingsService.getProperty(CONFIG_FILE_LOCATION_SHELL_PROPERTY);
  232. if (fileLocationShellConfig != null && StringUtils.isNotBlank(fileLocationShellConfig)) {
  233. location = LogicalPath.getInstance(Path.ROOT, fileLocationShellConfig);
  234. }
  235. return location;
  236. }
  237. }