/addon-jpa/addon/src/main/java/org/springframework/roo/addon/jpa/addon/AbstractIdentifierServiceAwareMetadataProvider.java

http://github.com/SpringSource/spring-roo · Java · 77 lines · 44 code · 7 blank · 26 comment · 3 complexity · 2078dd03bb834d1a77ca835237012513 MD5 · raw file

  1. package org.springframework.roo.addon.jpa.addon;
  2. import java.util.HashSet;
  3. import java.util.List;
  4. import java.util.Set;
  5. import org.apache.felix.scr.annotations.Component;
  6. import org.apache.felix.scr.annotations.Reference;
  7. import org.apache.felix.scr.annotations.ReferenceCardinality;
  8. import org.apache.felix.scr.annotations.ReferencePolicy;
  9. import org.apache.felix.scr.annotations.ReferenceStrategy;
  10. import org.springframework.roo.addon.jpa.addon.identifier.Identifier;
  11. import org.springframework.roo.addon.jpa.addon.identifier.IdentifierService;
  12. import org.springframework.roo.classpath.itd.AbstractItdMetadataProvider;
  13. import org.springframework.roo.classpath.persistence.PersistenceMemberLocator;
  14. import org.springframework.roo.model.JavaType;
  15. /**
  16. * Abstract class to make {@link IdentifierService} collection available to
  17. * subclasses.
  18. *
  19. * @author Alan Stewart
  20. * @author Ben Alex
  21. * @since 1.1
  22. */
  23. @Component(componentAbstract = true)
  24. @Reference(name = "identifierService", strategy = ReferenceStrategy.EVENT,
  25. policy = ReferencePolicy.DYNAMIC, referenceInterface = IdentifierService.class,
  26. cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE)
  27. public abstract class AbstractIdentifierServiceAwareMetadataProvider extends
  28. AbstractItdMetadataProvider {
  29. private final Set<IdentifierService> identifierServices = new HashSet<IdentifierService>();
  30. protected void bindIdentifierService(final IdentifierService identifierService) {
  31. synchronized (identifierServices) {
  32. identifierServices.add(identifierService);
  33. }
  34. }
  35. /**
  36. * Locates any {@link Identifier} that is applicable to this
  37. * {@link JavaType}.
  38. * <p>
  39. * See {@link IdentifierService#getIdentifiers(JavaType)} for the full
  40. * contract of what this method returns. Note this method simply returns the
  41. * first non-null result of invoking
  42. * {@link IdentifierService#getIdentifiers(JavaType)}. It returns null if no
  43. * provider is authoritative.
  44. *
  45. * @param javaType the entity or PK identifier class for which column
  46. * information is desired (required)
  47. * @return the applicable identifiers, or null if no registered
  48. * {@link IdentifierService} was authoritative for this type TODO
  49. * made obsolete by {@link PersistenceMemberLocator}?
  50. */
  51. protected List<Identifier> getIdentifiersForType(final JavaType javaType) {
  52. List<Identifier> identifierServiceResult = null;
  53. synchronized (identifierServices) {
  54. for (final IdentifierService service : identifierServices) {
  55. identifierServiceResult = service.getIdentifiers(javaType);
  56. if (identifierServiceResult != null) {
  57. // Someone has authoritatively indicated the fields for this
  58. // PK, so we don't need to continue looping
  59. break;
  60. }
  61. }
  62. }
  63. return identifierServiceResult;
  64. }
  65. protected void unbindIdentifierService(final IdentifierService identifierService) {
  66. synchronized (identifierServices) {
  67. identifierServices.remove(identifierService);
  68. }
  69. }
  70. }