/addon-layers-repository-jpa/addon/src/main/java/org/springframework/roo/addon/layers/repository/jpa/addon/RepositoryJpaConfigurationMetadataProviderImpl.java

http://github.com/SpringSource/spring-roo · Java · 145 lines · 98 code · 22 blank · 25 comment · 2 complexity · a1bb81b2d95f4b53020d714fabdd6583 MD5 · raw file

  1. package org.springframework.roo.addon.layers.repository.jpa.addon;
  2. import static org.springframework.roo.model.RooJavaType.ROO_JPA_REPOSITORY_CONFIGURATION;
  3. import java.util.Set;
  4. import java.util.logging.Logger;
  5. import org.apache.commons.lang3.Validate;
  6. import org.apache.felix.scr.annotations.Component;
  7. import org.apache.felix.scr.annotations.Service;
  8. import org.osgi.service.component.ComponentContext;
  9. import org.springframework.roo.classpath.PhysicalTypeIdentifier;
  10. import org.springframework.roo.classpath.PhysicalTypeMetadata;
  11. import org.springframework.roo.classpath.details.ItdTypeDetails;
  12. import org.springframework.roo.classpath.itd.AbstractMemberDiscoveringItdMetadataProvider;
  13. import org.springframework.roo.classpath.itd.ItdTypeDetailsProvidingMetadataItem;
  14. import org.springframework.roo.metadata.MetadataDependencyRegistry;
  15. import org.springframework.roo.metadata.internal.MetadataDependencyRegistryTracker;
  16. import org.springframework.roo.model.JavaType;
  17. import org.springframework.roo.model.RooJavaType;
  18. import org.springframework.roo.model.SpringJavaType;
  19. import org.springframework.roo.project.LogicalPath;
  20. import org.springframework.roo.project.ProjectOperations;
  21. import org.springframework.roo.support.logging.HandlerUtils;
  22. import org.springframework.roo.support.osgi.ServiceInstaceManager;
  23. /**
  24. * = RepositoryJpaConfigurationMetadataProviderImpl
  25. *
  26. * Implementation of {@link RepositoryJpaConfigurationMetadataProvider}.
  27. *
  28. * @author Sergio Clares
  29. * @since 2.0
  30. */
  31. @Component
  32. @Service
  33. public class RepositoryJpaConfigurationMetadataProviderImpl extends
  34. AbstractMemberDiscoveringItdMetadataProvider implements
  35. RepositoryJpaConfigurationMetadataProvider {
  36. protected final static Logger LOGGER = HandlerUtils
  37. .getLogger(RepositoryJpaConfigurationMetadataProviderImpl.class);
  38. protected MetadataDependencyRegistryTracker registryTracker = null;
  39. private ServiceInstaceManager serviceInstaceManager = new ServiceInstaceManager();
  40. /**
  41. * This service is being activated so setup it:
  42. * <ul>
  43. * <li>Create and open the {@link MetadataDependencyRegistryTracker}.</li>
  44. * <li>Registers {@link RooJavaType#ROO_JPA_REPOSITORY_CONFIGURATION} as additional
  45. * JavaType that will trigger metadata registration.</li>
  46. * <li>Set ensure the governor type details represent a class.</li>
  47. * </ul>
  48. */
  49. @Override
  50. protected void activate(final ComponentContext cContext) {
  51. super.activate(cContext);
  52. super.setDependsOnGovernorBeingAClass(true);
  53. serviceInstaceManager.activate(cContext.getBundleContext());
  54. this.registryTracker =
  55. new MetadataDependencyRegistryTracker(cContext.getBundleContext(), this,
  56. PhysicalTypeIdentifier.getMetadataIdentiferType(), getProvidesType());
  57. this.registryTracker.open();
  58. addMetadataTrigger(ROO_JPA_REPOSITORY_CONFIGURATION);
  59. }
  60. /**
  61. * This service is being deactivated so unregister upstream-downstream
  62. * dependencies, triggers, matchers and listeners.
  63. *
  64. * @param context
  65. */
  66. protected void deactivate(final ComponentContext context) {
  67. MetadataDependencyRegistry registry = this.registryTracker.getService();
  68. registry.removeNotificationListener(this);
  69. registry.deregisterDependency(PhysicalTypeIdentifier.getMetadataIdentiferType(),
  70. getProvidesType());
  71. this.registryTracker.close();
  72. removeMetadataTrigger(ROO_JPA_REPOSITORY_CONFIGURATION);
  73. }
  74. @Override
  75. protected String createLocalIdentifier(final JavaType javaType, final LogicalPath path) {
  76. return RepositoryJpaConfigurationMetadata.createIdentifier(javaType, path);
  77. }
  78. @Override
  79. protected String getGovernorPhysicalTypeIdentifier(final String metadataIdentificationString) {
  80. final JavaType javaType =
  81. RepositoryJpaConfigurationMetadata.getJavaType(metadataIdentificationString);
  82. final LogicalPath path =
  83. RepositoryJpaConfigurationMetadata.getPath(metadataIdentificationString);
  84. return PhysicalTypeIdentifier.createIdentifier(javaType, path);
  85. }
  86. public String getItdUniquenessFilenameSuffix() {
  87. return "Jpa_Repository_Configuration";
  88. }
  89. @Override
  90. protected String getLocalMidToRequest(final ItdTypeDetails itdTypeDetails) {
  91. return getLocalMid(itdTypeDetails);
  92. }
  93. @Override
  94. protected ItdTypeDetailsProvidingMetadataItem getMetadata(
  95. final String metadataIdentificationString, final JavaType aspectName,
  96. final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
  97. // Get application main class
  98. Set<JavaType> applicationTypes =
  99. getTypeLocationService().findTypesWithAnnotation(SpringJavaType.SPRING_BOOT_APPLICATION);
  100. String repositoryConfigurationModuleName = governorPhysicalTypeMetadata.getType().getModule();
  101. JavaType applicationMainType = null;
  102. for (JavaType applicationType : applicationTypes) {
  103. if (applicationType.getModule().equals(repositoryConfigurationModuleName)) {
  104. applicationMainType = applicationType;
  105. }
  106. }
  107. Validate.notNull(applicationMainType, "Unable to find a main application class on module %s",
  108. repositoryConfigurationModuleName);
  109. // Check if security is installed in module
  110. boolean isSpringletsSecurityEnabled =
  111. getProjectOperations()
  112. .isFeatureInstalled(applicationMainType.getModule(), "SPRINGLETS_JPA");
  113. return new RepositoryJpaConfigurationMetadata(metadataIdentificationString, aspectName,
  114. governorPhysicalTypeMetadata, applicationMainType, isSpringletsSecurityEnabled);
  115. }
  116. @Override
  117. public String getProvidesType() {
  118. return RepositoryJpaConfigurationMetadata.getMetadataIdentiferType();
  119. }
  120. protected ProjectOperations getProjectOperations() {
  121. return serviceInstaceManager.getServiceInstance(this, ProjectOperations.class);
  122. }
  123. }