Search in sources :

Example 1 with MetadataProvider

use of org.springframework.roo.metadata.MetadataProvider in project spring-roo by spring-projects.

the class MemberDetailsScannerImpl method getMemberDetails.

public final MemberDetails getMemberDetails(final String requestingClass, ClassOrInterfaceTypeDetails cid) {
    if (metadataService == null) {
        metadataService = getMetadataService();
    }
    if (providers.isEmpty()) {
        bindProviders();
    }
    if (decorators.isEmpty()) {
        bindDecorators();
    }
    if (cid == null) {
        return null;
    }
    synchronized (lock) {
        // Create a list of discovered members
        final List<MemberHoldingTypeDetails> memberHoldingTypeDetails = new ArrayList<MemberHoldingTypeDetails>();
        // Build a List representing the class hierarchy, where the first
        // element is the absolute superclass
        final List<ClassOrInterfaceTypeDetails> cidHierarchy = new ArrayList<ClassOrInterfaceTypeDetails>();
        while (cid != null) {
            // Note to the top of the list
            cidHierarchy.add(0, cid);
            cid = cid.getSuperclass();
        }
        // Now we add this governor, plus all of its superclasses
        for (final ClassOrInterfaceTypeDetails currentClass : cidHierarchy) {
            memberHoldingTypeDetails.add(currentClass);
            // thus MemberHoldingTypeDetails information
            for (final MetadataProvider mp : providers) {
                // Skip non-ITD providers
                if (!(mp instanceof ItdMetadataProvider)) {
                    continue;
                }
                // Skip myself
                if (mp.getClass().getName().equals(requestingClass)) {
                    continue;
                }
                // Determine the key the ITD provider uses for this
                // particular type
                final String key = ((ItdMetadataProvider) mp).getIdForPhysicalJavaType(currentClass.getDeclaredByMetadataId());
                Validate.isTrue(MetadataIdentificationUtils.isIdentifyingInstance(key), "ITD metadata provider '%s' returned an illegal key ('%s')", mp, key);
                // Get the metadata and ensure we have ITD type details
                // available
                final MetadataItem metadataItem = metadataService.get(key);
                if (metadataItem == null || !metadataItem.isValid()) {
                    continue;
                }
                Validate.isInstanceOf(ItdTypeDetailsProvidingMetadataItem.class, metadataItem, "ITD metadata provider '%s' failed to return the correct metadata type", mp);
                final ItdTypeDetailsProvidingMetadataItem itdTypeDetailsMd = (ItdTypeDetailsProvidingMetadataItem) metadataItem;
                if (itdTypeDetailsMd.getMemberHoldingTypeDetails() == null) {
                    continue;
                }
                // Capture the member details
                memberHoldingTypeDetails.add(itdTypeDetailsMd.getMemberHoldingTypeDetails());
            }
        }
        // Turn out list of discovered members into a result
        MemberDetails result = new MemberDetailsImpl(memberHoldingTypeDetails);
        // Loop until such time as we complete a full loop where no changes
        // are made to the result
        boolean additionalLoopRequired = true;
        while (additionalLoopRequired) {
            additionalLoopRequired = false;
            for (final MemberDetailsDecorator decorator : decorators) {
                final MemberDetails newResult = decorator.decorate(requestingClass, result);
                Validate.isTrue(newResult != null, "Decorator '%s' returned an illegal result", decorator.getClass().getName());
                if (newResult != null && !newResult.equals(result)) {
                    additionalLoopRequired = true;
                }
                result = newResult;
            }
        }
        return result;
    }
}
Also used : ItdTypeDetailsProvidingMetadataItem(org.springframework.roo.classpath.itd.ItdTypeDetailsProvidingMetadataItem) ArrayList(java.util.ArrayList) ItdMetadataProvider(org.springframework.roo.classpath.itd.ItdMetadataProvider) MetadataProvider(org.springframework.roo.metadata.MetadataProvider) MemberHoldingTypeDetails(org.springframework.roo.classpath.details.MemberHoldingTypeDetails) ItdMetadataProvider(org.springframework.roo.classpath.itd.ItdMetadataProvider) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) MetadataItem(org.springframework.roo.metadata.MetadataItem) ItdTypeDetailsProvidingMetadataItem(org.springframework.roo.classpath.itd.ItdTypeDetailsProvidingMetadataItem)

Example 2 with MetadataProvider

use of org.springframework.roo.metadata.MetadataProvider in project spring-roo by spring-projects.

the class MemberDetailsScannerImpl method bindProviders.

protected void bindProviders() {
    synchronized (lock) {
        // Get all Services implement MetadataProvider interface
        try {
            ServiceReference<?>[] references = this.context.getAllServiceReferences(MetadataProvider.class.getName(), null);
            for (ServiceReference<?> ref : references) {
                MetadataProvider provider = (MetadataProvider) this.context.getService(ref);
                Validate.notNull(provider, "Metadata provider required");
                final String mid = provider.getProvidesType();
                Validate.isTrue(MetadataIdentificationUtils.isIdentifyingClass(mid), "Metadata provider '%s' violated interface contract by returning '%s'", provider, mid);
                providers.add(provider);
            }
        } catch (InvalidSyntaxException e) {
            LOGGER.warning("Cannot load MetadataProvider on MemberDetailsScannerImpl.");
        }
    }
}
Also used : ItdMetadataProvider(org.springframework.roo.classpath.itd.ItdMetadataProvider) MetadataProvider(org.springframework.roo.metadata.MetadataProvider) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

ItdMetadataProvider (org.springframework.roo.classpath.itd.ItdMetadataProvider)2 MetadataProvider (org.springframework.roo.metadata.MetadataProvider)2 ArrayList (java.util.ArrayList)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 ServiceReference (org.osgi.framework.ServiceReference)1 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)1 MemberHoldingTypeDetails (org.springframework.roo.classpath.details.MemberHoldingTypeDetails)1 ItdTypeDetailsProvidingMetadataItem (org.springframework.roo.classpath.itd.ItdTypeDetailsProvidingMetadataItem)1 MetadataItem (org.springframework.roo.metadata.MetadataItem)1