PageRenderTime 65ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/addon-email/addon/src/main/java/org/springframework/roo/addon/email/addon/MailCommands.java

http://github.com/SpringSource/spring-roo
Java | 287 lines | 227 code | 43 blank | 17 comment | 20 complexity | de56f9c9d26f30d1b1272064a2549871 MD5 | raw file
  1. package org.springframework.roo.addon.email.addon;
  2. import static org.springframework.roo.shell.OptionContexts.APPLICATION_FEATURE_INCLUDE_CURRENT_MODULE;
  3. import org.apache.felix.scr.annotations.Component;
  4. import org.apache.felix.scr.annotations.Reference;
  5. import org.apache.felix.scr.annotations.Service;
  6. import org.osgi.framework.BundleContext;
  7. import org.osgi.service.component.ComponentContext;
  8. import org.springframework.roo.classpath.ModuleFeatureName;
  9. import org.springframework.roo.classpath.TypeLocationService;
  10. import org.springframework.roo.model.JavaType;
  11. import org.springframework.roo.project.ProjectOperations;
  12. import org.springframework.roo.project.maven.Pom;
  13. import org.springframework.roo.shell.CliAvailabilityIndicator;
  14. import org.springframework.roo.shell.CliCommand;
  15. import org.springframework.roo.shell.CliOption;
  16. import org.springframework.roo.shell.CliOptionAutocompleteIndicator;
  17. import org.springframework.roo.shell.CliOptionMandatoryIndicator;
  18. import org.springframework.roo.shell.CliOptionVisibilityIndicator;
  19. import org.springframework.roo.shell.CommandMarker;
  20. import org.springframework.roo.shell.ShellContext;
  21. import org.springframework.roo.support.logging.HandlerUtils;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.logging.Logger;
  25. /**
  26. * Commands for the 'email' add-on to be used by the Roo shell.
  27. *
  28. * @author Stefan Schmidt
  29. * @author Juan Carlos GarcĂ­a
  30. * @author Manuel Iborra
  31. * @since 1.0
  32. */
  33. @Component
  34. @Service
  35. public class MailCommands implements CommandMarker {
  36. private static final Logger LOGGER = HandlerUtils.getLogger(MailCommands.class);
  37. @Reference
  38. private MailOperations mailOperations;
  39. @Reference
  40. private TypeLocationService typeLocationService;
  41. @Reference
  42. private ProjectOperations projectOperations;
  43. // ------------ OSGi component attributes ----------------
  44. private BundleContext context;
  45. protected void activate(final ComponentContext context) {
  46. this.context = context.getBundleContext();
  47. }
  48. protected void deactivate(final ComponentContext context) {
  49. this.context = null;
  50. }
  51. @CliAvailabilityIndicator("email sender setup")
  52. public boolean isInstallSenderEmailAvailable() {
  53. return mailOperations.isEmailInstallationPossible();
  54. }
  55. @CliOptionVisibilityIndicator(command = "email sender setup", params = {"jndiName"},
  56. help = "jndiName parameter is not available if any of host, "
  57. + "port, protocol, username, password or starttls are selected.")
  58. public boolean isJndiVisibleSenderSetup(ShellContext shellContext) {
  59. Map<String, String> params = shellContext.getParameters();
  60. // If user define host, port, protocol, username, password or starttls
  61. // parameters, jndiName should not be visible.
  62. if (params.containsKey("host") || params.containsKey("port") || params.containsKey("protocol")
  63. || params.containsKey("username") || params.containsKey("password")
  64. || params.containsKey("starttls")) {
  65. return false;
  66. }
  67. return true;
  68. }
  69. @CliOptionVisibilityIndicator(command = "email sender setup", params = {"host", "port",
  70. "protocol", "username", "password", "starttls"},
  71. help = "Connection parameters are not available if jndiName is specified.")
  72. public boolean areConnectionParamsVisibleSenderSetup(ShellContext shellContext) {
  73. Map<String, String> params = shellContext.getParameters();
  74. // If user define jndiName parameter, connection parameters should not
  75. // be visible
  76. if (params.containsKey("jndiName")) {
  77. return false;
  78. }
  79. return true;
  80. }
  81. @CliOptionVisibilityIndicator(command = "email sender setup", params = {"module"},
  82. help = "Module parameter is not available if there is only one application module")
  83. public boolean isModuleVisibleSenderSetup(ShellContext shellContext) {
  84. if (typeLocationService.getModuleNames(ModuleFeatureName.APPLICATION).size() > 1) {
  85. return true;
  86. }
  87. return false;
  88. }
  89. @CliOptionMandatoryIndicator(command = "email sender setup", params = "module")
  90. public boolean isModuleRequiredSenderSetup(ShellContext shellContext) {
  91. Pom module = projectOperations.getFocusedModule();
  92. if (!isModuleVisibleSenderSetup(shellContext)
  93. || typeLocationService.hasModuleFeature(module, ModuleFeatureName.APPLICATION)) {
  94. return false;
  95. }
  96. return true;
  97. }
  98. @CliOptionAutocompleteIndicator(
  99. command = "email sender setup",
  100. param = "service",
  101. help = "--service parameter parameter is the service where will be added the support to send emails")
  102. public List<String> returnServicesImpl(ShellContext shellContext) {
  103. return mailOperations.getAllServiceImpl(shellContext.getParameters().get("service"));
  104. }
  105. @CliCommand(value = "email sender setup",
  106. help = "Install a Spring JavaMailSender in your project")
  107. public void installSenderEmail(
  108. @CliOption(key = {"host"}, mandatory = false, help = "The host server. "
  109. + "This option is not available if `--jndiName` has already been specified.") final String host,
  110. @CliOption(key = {"port"}, mandatory = false, help = "The port used by mail server. "
  111. + "This option is not available if `--jndiName` has already been specified.") final String port,
  112. @CliOption(key = {"protocol"}, mandatory = false, help = "The protocol used by mail server. "
  113. + "This option is not available if `--jndiName` has already been specified.") final String protocol,
  114. @CliOption(key = {"username"}, mandatory = false, help = "The mail account username. "
  115. + "This option is not available if `--jndiName` has already been specified.") final String username,
  116. @CliOption(key = {"password"}, mandatory = false, help = "The mail account password. "
  117. + "This option is not available if `--jndiName` has already been specified.") final String password,
  118. @CliOption(key = {"starttls"}, mandatory = false,
  119. help = "If true, enables the use of the STARTTLS command. "
  120. + "This option is not available if `--jndiName` has already been specified.") final Boolean starttls,
  121. @CliOption(key = {"jndiName"}, mandatory = false,
  122. help = "The jndi name where the mail configuration has been defined. "
  123. + "This option is not available if any of `--host`, `--port`, "
  124. + "`--protocol`, `--username`, `--password` or `--starttls` has "
  125. + "been specified before.") final String jndiName,
  126. @CliOption(key = {"profile"}, mandatory = false,
  127. help = "The profile where the properties will be set.") final String profile,
  128. @CliOption(
  129. key = "module",
  130. mandatory = true,
  131. help = "The application module where generate the integration test. "
  132. + "This option is mandatory if the focus is not set in an 'application' module and there "
  133. + "are more than one 'application' modules, that is, a module containing an "
  134. + "`@SpringBootApplication` class. "
  135. + "This option is available only if there are more than one application module and none of"
  136. + " them is focused. "
  137. + "Default if option not present: the unique 'application' module, or focused 'application'"
  138. + " module.", unspecifiedDefaultValue = ".",
  139. optionContext = APPLICATION_FEATURE_INCLUDE_CURRENT_MODULE) Pom module,
  140. @CliOption(
  141. key = "service",
  142. mandatory = false,
  143. help = "The service where include an instance of JavaMailSender, which is a service that "
  144. + "have methods to send emails.") final JavaType service, ShellContext shellContext) {
  145. mailOperations.installSendEmailSupport(host, port, protocol, username, password, starttls,
  146. jndiName, profile, module, service, shellContext.isForce());
  147. }
  148. @CliAvailabilityIndicator("email receiver setup")
  149. public boolean isInstallReceiverEmailAvailable() {
  150. return mailOperations.isEmailInstallationPossible();
  151. }
  152. @CliOptionVisibilityIndicator(command = "email receiver setup", params = {"jndiName"},
  153. help = "jndiName parameter is not available if any of host, "
  154. + "port, protocol, username, password or starttls are selected.")
  155. public boolean isJndiVisibleReceiverSetup(ShellContext shellContext) {
  156. Map<String, String> params = shellContext.getParameters();
  157. // If user define host, port, protocol, username, password or starttls
  158. // parameters, jndiName should not be visible.
  159. if (params.containsKey("host") || params.containsKey("port") || params.containsKey("protocol")
  160. || params.containsKey("username") || params.containsKey("password")
  161. || params.containsKey("starttls")) {
  162. return false;
  163. }
  164. return true;
  165. }
  166. @CliOptionVisibilityIndicator(command = "email receiver setup", params = {"host", "port",
  167. "protocol", "username", "password", "starttls"},
  168. help = "Connection parameters are not available if jndiName is specified.")
  169. public boolean areConnectionParamsVisibleReceiverSetup(ShellContext shellContext) {
  170. Map<String, String> params = shellContext.getParameters();
  171. // If user define jndiName parameter, connection parameters should not
  172. // be visible
  173. if (params.containsKey("jndiName")) {
  174. return false;
  175. }
  176. return true;
  177. }
  178. @CliOptionVisibilityIndicator(command = "email receiver setup", params = {"module"},
  179. help = "Module parameter is not available if there is only one application module")
  180. public boolean isModuleVisibleReceiverSetup(ShellContext shellContext) {
  181. if (typeLocationService.getModuleNames(ModuleFeatureName.APPLICATION).size() > 1) {
  182. return true;
  183. }
  184. return false;
  185. }
  186. @CliOptionMandatoryIndicator(command = "email receiver setup", params = "module")
  187. public boolean isModuleRequiredReceiverSetup(ShellContext shellContext) {
  188. Pom module = projectOperations.getFocusedModule();
  189. if (!isModuleVisibleReceiverSetup(shellContext)
  190. || typeLocationService.hasModuleFeature(module, ModuleFeatureName.APPLICATION)) {
  191. return false;
  192. }
  193. return true;
  194. }
  195. @CliOptionAutocompleteIndicator(
  196. command = "email receiver setup",
  197. param = "service",
  198. help = "--service parameter parameter is the service where will be added the support to receive emails")
  199. public List<String> returnServicesImplReceiverSetup(ShellContext shellContext) {
  200. return mailOperations.getAllServiceImpl(shellContext.getParameters().get("service"));
  201. }
  202. @CliCommand(value = "email receiver setup",
  203. help = "Installs a Spring JavaMailReceiver in your project.")
  204. public void installReceiverEmail(
  205. @CliOption(key = {"host"}, mandatory = false, help = "The host server. "
  206. + "This option is not available if `--jndiName` has already been specified.") final String host,
  207. @CliOption(key = {"port"}, mandatory = false, help = "The port used by mail server. "
  208. + "This option is not available if `--jndiName` has already been specified.") final String port,
  209. @CliOption(key = {"protocol"}, mandatory = false, help = "The protocol used by mail server. "
  210. + "This option is not available if `--jndiName` has already been specified.") final String protocol,
  211. @CliOption(key = {"username"}, mandatory = false, help = "The mail account username. "
  212. + "This option is not available if `--jndiName` has already been specified.") final String username,
  213. @CliOption(key = {"password"}, mandatory = false, help = "The mail account password. "
  214. + "This option is not available if `--jndiName` has already been specified.") final String password,
  215. @CliOption(key = {"starttls"}, mandatory = false,
  216. help = "If true, enables the use of the STARTTLS command. "
  217. + "This option is not available if `--jndiName` has already been specified.") final Boolean starttls,
  218. @CliOption(key = {"jndiName"}, mandatory = false,
  219. help = "The jndi name where the mail configuration has been defined. "
  220. + "This option is not available if any of `--host`, `--port`, "
  221. + "`--protocol`, `--username`, `--password` or `--starttls` has "
  222. + "been specified before.") final String jndiName,
  223. @CliOption(key = {"profile"}, mandatory = false,
  224. help = "The profile where the properties will be set.") final String profile,
  225. @CliOption(
  226. key = "module",
  227. mandatory = true,
  228. help = "The application module where generate the integration test. "
  229. + "This option is mandatory if the focus is not set in an 'application' module and there "
  230. + "are more than one 'application' modules, that is, a module containing an "
  231. + "`@SpringBootApplication` class. "
  232. + "This option is available only if there are more than one application module and none of"
  233. + " them is focused. "
  234. + "Default if option not present: the unique 'application' module, or focused 'application'"
  235. + " module.", unspecifiedDefaultValue = ".",
  236. optionContext = APPLICATION_FEATURE_INCLUDE_CURRENT_MODULE) Pom module, @CliOption(
  237. key = "service", mandatory = false,
  238. help = "The service where include an instance of MailReceiverService, which is a "
  239. + "service that have methods to receive emails.") final JavaType service,
  240. ShellContext shellContext) {
  241. mailOperations.installReceiveEmailSupport(host, port, protocol, username, password, starttls,
  242. jndiName, profile, module, service, shellContext.isForce());
  243. }
  244. }