Package org.apache.felix.scrplugin.description

Examples of org.apache.felix.scrplugin.description.ComponentDescription


        // generate ComponentDescription if required
        if (generateComponent) {
            String nameOfAnnotatedClass = classDescription.getDescribedClass().getName();
           
            final ComponentDescription cd = new ComponentDescription(cad);
            cd.setName(cad.getStringValue("componentName", nameOfAnnotatedClass));
            cd.setConfigurationPolicy(ComponentConfigurationPolicy.valueOf(cad.getEnumValue("configurationPolicy",
                    ComponentConfigurationPolicy.OPTIONAL.name())));
            cd.setSetMetatypeFactoryPid(cad.getBooleanValue("configurationFactory", false));

            String nameFromAnnotation = (String) cad.getValue("name");
            String defaultLabel = "Sling Health Check: " + (nameFromAnnotation!=null ? nameFromAnnotation : nameOfAnnotatedClass);
            cd.setLabel(cad.getStringValue("label", defaultLabel));
            cd.setDescription(cad.getStringValue("description", "Health Check Configuration"));

            cd.setCreateMetatype(metatype);

            classDescription.add(cd);
        }

        // generate ServiceDescription if required
View Full Code Here


        // generate ComponentDescription if required
        final boolean generateComponent = cad.getBooleanValue("generateComponent", true);
        final boolean metatype = cad.getBooleanValue("metatype", !generateComponent);

        if (generateComponent) {
            final ComponentDescription cd = new ComponentDescription(cad);
            cd.setName(cad.getStringValue("name", classDescription.getDescribedClass().getName()));
            cd.setConfigurationPolicy(ComponentConfigurationPolicy.OPTIONAL);

            cd.setLabel(cad.getStringValue("label", null));
            cd.setDescription(cad.getStringValue("description", null));

            cd.setCreateMetatype(metatype);

            classDescription.add(cd);
        }

        // generate ServiceDescription if required
View Full Code Here

        // generate ComponentDescription if required
        final boolean generateComponent = cad.getBooleanValue("generateComponent", true);
        final boolean metatype = cad.getBooleanValue("metatype", !generateComponent);

        if (generateComponent) {
            final ComponentDescription cd = new ComponentDescription(cad);
            cd.setName(cad.getStringValue("name", classDescription.getDescribedClass().getName()));
            cd.setConfigurationPolicy(ComponentConfigurationPolicy.OPTIONAL);

            cd.setLabel(cad.getStringValue("label", null));
            cd.setDescription(cad.getStringValue("description", null));

            cd.setCreateMetatype(metatype);

            classDescription.add(cd);
        }

        // generate ServiceDescription if required
View Full Code Here

            describedClass.add(createComponent(cad, scannedClass));
        }

        // search for the component descriptions and use the first one
        final List<ComponentDescription> componentDescs = describedClass.getDescriptions(ComponentDescription.class);
        ComponentDescription found = null;
        if (!componentDescs.isEmpty()) {
            found = componentDescs.get(0);
        }

        if (found != null) {
            final ComponentDescription cd = found;

            // search for methods
            final List<MethodAnnotation> methodTags = scannedClass.getMethodAnnotations(null);
            for (final MethodAnnotation m : methodTags) {
                if (m.getName().equals(Activate.class.getName())) {
                    cd.setActivate(m.getAnnotatedMethod().getName());
                    scannedClass.processed(m);
                } else if (m.getName().equals(Deactivate.class.getName())) {
                    cd.setDeactivate(m.getAnnotatedMethod().getName());
                    scannedClass.processed(m);
                } else if (m.getName().equals(Modified.class.getName())) {
                    cd.setModified(m.getAnnotatedMethod().getName());
                    scannedClass.processed(m);
                }
            }

        }
View Full Code Here

     *            The component annotation for the class.
     * @param scannedClass
     *            The scanned class.
     */
    private ComponentDescription createComponent(final ClassAnnotation cad, final ScannedClass scannedClass) {
        final ComponentDescription component = new ComponentDescription(cad);
        final boolean classIsAbstract = Modifier.isAbstract(scannedClass.getScannedClass().getModifiers());
        component.setAbstract(cad.getBooleanValue("componentAbstract", classIsAbstract));

        component.setCreatePid(cad.getBooleanValue("createPid", true));

        component.setName(cad.getStringValue("name", scannedClass.getScannedClass().getName()));

        component.setLabel(cad.getStringValue("label", null));
        component.setDescription(cad.getStringValue("description", null));

        component.setCreateDs(cad.getBooleanValue("ds", true));

        component.setCreateMetatype(cad.getBooleanValue("metatype", false));

        if (cad.getValue("enabled") != null) {
            component.setEnabled(cad.getBooleanValue("enabled", true));
        }
        if (cad.getValue("specVersion") != null) {
            component.setSpecVersion(SpecVersion.fromName(cad.getValue("specVersion").toString()));
        }
        component.setFactory(cad.getStringValue("factory", null));
        // FELIX-593: immediate attribute does not default to true all the
        // times hence we only set it if declared in the tag
        if (cad.getValue("immediate") != null) {
            component.setImmediate(cad.getBooleanValue("immediate", false));
        }
        component.setInherit(cad.getBooleanValue("inherit", true));
        component.setConfigurationPolicy(ComponentConfigurationPolicy.valueOf(cad.getEnumValue("policy",
                        ComponentConfigurationPolicy.OPTIONAL.name())));
        component.setSetMetatypeFactoryPid(cad.getBooleanValue("configurationFactory", false));

        // Version 1.2
        component.setConfigurationPid(cad.getStringValue("configurationPid", null));

        return component;
    }
View Full Code Here

     * Validate the component description. If errors occur a message is added to
     * the issues list, warnings can be added to the warnings list.
     */
    public void validate()
    throws SCRDescriptorException {
        final ComponentDescription component = this.container.getComponentDescription();

        // nothing to check if this is ignored
        if (!component.isCreateDs()) {
            return;
        }

        final int currentIssueCount = iLog.getNumberOfErrors();

        // if the component is abstract, we do not validate everything
        if (!component.isAbstract()) {
            // if configuration pid is set and different from name, we need 1.2
            if ( component.getConfigurationPid() != null && !component.getConfigurationPid().equals(component.getName())
                 && options.getSpecVersion().ordinal() < SpecVersion.VERSION_1_2.ordinal() ) {
                this.logError(component, "Different configuration pid requires "
                                + SpecVersion.VERSION_1_2.getName() + " or higher.");
            }

            // ensure non-abstract, public class
            if (!Modifier.isPublic(this.container.getClassDescription().getDescribedClass().getModifiers())) {
                this.logError(component, "Class must be public: "
                                + this.container.getClassDescription().getDescribedClass().getName());
            }
            if (Modifier.isAbstract(this.container.getClassDescription().getDescribedClass().getModifiers())
                            || this.container.getClassDescription().getDescribedClass().isInterface()) {
                this.logError(component, "Class must be concrete class (not abstract or interface) : "
                                + this.container.getClassDescription().getDescribedClass().getName());
            }

            // no errors so far, let's continue
            if (iLog.getNumberOfErrors() == currentIssueCount) {

                final String activateName = component.getActivate() == null ? "activate" : component.getActivate();
                final String deactivateName = component.getDeactivate() == null ? "deactivate" : component.getDeactivate();

                // check activate and deactivate methods
                this.checkLifecycleMethod(activateName, true, component.getActivate() != null);
                this.checkLifecycleMethod(deactivateName, false, component.getDeactivate() != null);

                if (component.getModified() != null) {
                    if ( this.options.getSpecVersion().ordinal() >= SpecVersion.VERSION_1_1.ordinal() ) {
                        this.checkLifecycleMethod(component.getModified(), true, true);
                    } else {
                        this.logError(component, "If modified version is specified, spec version must be " +
                            SpecVersion.VERSION_1_1.name() + " or higher : " + component.getModified());
                    }
                }

                // ensure public default constructor
                boolean constructorFound = true;
                Constructor<?>[] constructors = this.container.getClassDescription().getDescribedClass().getDeclaredConstructors();
                for (int i = 0; constructors != null && i < constructors.length; i++) {
                    // if public default, succeed
                    if (Modifier.isPublic(constructors[i].getModifiers())
                        && (constructors[i].getParameterTypes() == null || constructors[i].getParameterTypes().length == 0)) {
                        constructorFound = true;
                        break;
                    }

                    // non-public/non-default constructor found, must have
                    // explicit
                    constructorFound = false;
                }

                if (!constructorFound) {
                    this.logError(component, "Class must have public default constructor: " + this.container.getClassDescription().getDescribedClass().getName());
                }

                // verify properties
                for (final PropertyDescription prop : this.container.getProperties().values()) {
                    this.validateProperty(prop);
                }

                // verify service
                boolean isServiceFactory = false;
                if (this.container.getServiceDescription() != null) {
                    if (this.container.getServiceDescription().getInterfaces().size() == 0) {
                        this.logError(component, "Service interface information is missing!");
                    }
                    this.validateService(this.container.getServiceDescription());
                    isServiceFactory = this.container.getServiceDescription().isServiceFactory();
                }

                // serviceFactory must not be true for immediate of component factory
                if (isServiceFactory && component.getImmediate() != null && component.getImmediate().booleanValue()
                    && component.getFactory() != null) {
                    this.logError(component,
                        "Component must not be a ServiceFactory, if immediate and/or component factory: "
                        + this.container.getClassDescription().getDescribedClass().getName());
                }

                // immediate must not be true for component factory
                if (component.getImmediate() != null && component.getImmediate().booleanValue() && component.getFactory() != null) {
                    this.logError(component,
                        "Component must not be immediate if component factory: " + this.container.getClassDescription().getDescribedClass().getName());
                }
            }

            // additional check for metatype (FELIX-4035)
            if ( this.container.getMetatypeContainer() != null ) {
                if ( this.container.getMetatypeContainer().getProperties().size() == 0 ) {
                    this.logWarn(component, "Component is defined to generate metatype information, however no properties have been " +
                        "defined; in case no properties are wanted, consider to use 'metatype=false'");
                }
            }
            if (iLog.getNumberOfErrors() == currentIssueCount) {
                // verify references
                for (final ReferenceDescription ref : this.container.getReferences().values()) {
                    this.validateReference(ref, component.isAbstract());
                }
            }
        }
    }
View Full Code Here

    /**
     * Create the SCR objects based on the descriptions
     */
    private ComponentContainer createComponent(final ClassDescription desc,
                    final IssueLog iLog) {
        final ComponentDescription componentDesc = desc.getDescription(ComponentDescription.class);

        final SpecVersion intitialComponentSpecVersion = componentDesc.getSpecVersion();

        // configuration pid in 1.2
        if ( componentDesc.getConfigurationPid() != null && !componentDesc.getConfigurationPid().equals(componentDesc.getName())) {
            componentDesc.setSpecVersion(SpecVersion.VERSION_1_2);
        }

        final ComponentContainer container = new ComponentContainer(desc, componentDesc);

        // Create metatype (if required)
        final MetatypeContainer ocd;
        if ( !componentDesc.isAbstract() && componentDesc.isCreateMetatype() ) {
            // OCD
            ocd = new MetatypeContainer();
            container.setMetatypeContainer( ocd );
            ocd.setId( componentDesc.getName() );
            if ( componentDesc.getLabel() != null ) {
                ocd.setName( componentDesc.getLabel() );
            }
            if ( componentDesc.getDescription() != null ) {
                ocd.setDescription( componentDesc.getDescription() );
            }

            // Factory pid
            if ( componentDesc.isSetMetatypeFactoryPid() ) {
                if ( componentDesc.getFactory() == null ) {
                    ocd.setFactoryPid( componentDesc.getName() );
                } else {
                    iLog.addWarning( "Component factory " + componentDesc.getName()
                        + " should not set metatype factory pid.", desc.getSource() );
                }
            }
        } else {
            ocd = null;
        }
        // metatype checks if metatype is not generated (FELIX-4033)
        if ( !componentDesc.isAbstract() && !componentDesc.isCreateMetatype() ) {
            if ( componentDesc.getLabel() != null && componentDesc.getLabel().trim().length() > 0 ) {
                iLog.addWarning(" Component " + componentDesc.getName() + " has set a label. However metatype is set to false. This label is ignored.",
                        desc.getSource());
            }
            if ( componentDesc.getDescription() != null && componentDesc.getDescription().trim().length() > 0 ) {
                iLog.addWarning(" Component " + componentDesc.getName() + " has set a description. However metatype is set to false. This description is ignored.",
                        desc.getSource());
            }
        }

        ClassDescription current = desc;
        boolean inherit;
        do {
            final ComponentDescription cd = current.getDescription(ComponentDescription.class);
            inherit = (cd == null ? true : cd.isInherit());

            if ( cd != null ) {
                if ( current != desc ) {
                    iLog.addWarning(" Component " + componentDesc.getName() + " is using the " +
                                    "deprecated inheritance feature and inherits from " + current.getDescribedClass().getName() +
                                    ". This feature will be removed in future versions.",
                                    desc.getSource());
                }
                // handle enabled and immediate
                if ( componentDesc.getEnabled() == null ) {
                    componentDesc.setEnabled(cd.getEnabled());
                }
                if ( componentDesc.getImmediate() == null ) {
                    componentDesc.setImmediate(cd.getImmediate());
                }

                // lifecycle methods
                if ( componentDesc.getActivate() == null && cd.getActivate() != null ) {
                    componentDesc.setActivate(cd.getActivate());
                }
                if ( componentDesc.getDeactivate() == null && cd.getDeactivate() != null ) {
                    componentDesc.setDeactivate(cd.getDeactivate());
                }
                if ( componentDesc.getModified() == null && cd.getModified() != null ) {
                    componentDesc.setModified(cd.getModified());
                }
                if ( componentDesc.getActivate() != null || componentDesc.getDeactivate() != null || componentDesc.getModified() != null ) {
                    // spec version must be at least 1.1
                    componentDesc.setSpecVersion(SpecVersion.VERSION_1_1);
                }
View Full Code Here

    protected static void generateXML(final String namespace,
            final DescriptionContainer module,
            final ComponentContainer container,
            final ContentHandler contentHandler)
                    throws SAXException {
        final ComponentDescription component = container.getComponentDescription();

        final AttributesImpl ai = new AttributesImpl();
        IOUtils.addAttribute(ai, COMPONENT_ATTR_ENABLED, component.getEnabled());
        IOUtils.addAttribute(ai, COMPONENT_ATTR_IMMEDIATE, component.getImmediate());
        IOUtils.addAttribute(ai, ATTR_NAME, component.getName());
        IOUtils.addAttribute(ai, COMPONENT_ATTR_FACTORY, component.getFactory());

        // attributes new in 1.1
        if (module.getOptions().getSpecVersion().ordinal() >= SpecVersion.VERSION_1_1.ordinal() ) {
            if ( component.getConfigurationPolicy() != null
                    && component.getConfigurationPolicy() != ComponentConfigurationPolicy.OPTIONAL ) {
                IOUtils.addAttribute(ai, COMPONENT_ATTR_POLICY, component.getConfigurationPolicy().name().toLowerCase());
            }
            IOUtils.addAttribute(ai, COMPONENT_ATTR_ACTIVATE, component.getActivate());
            IOUtils.addAttribute(ai, COMPONENT_ATTR_DEACTIVATE, component.getDeactivate());
            IOUtils.addAttribute(ai, COMPONENT_ATTR_MODIFIED, component.getModified());
        }
        // attributes new in 1.2
        if ( module.getOptions().getSpecVersion().ordinal() >= SpecVersion.VERSION_1_2.ordinal() ) {
            if ( component.getConfigurationPid() != null && !component.getConfigurationPid().equals(component.getName())) {
                IOUtils.addAttribute(ai, COMPONENT_ATTR_CONFIGURATION_PID, component.getConfigurationPid());
            }
        }
        IOUtils.indent(contentHandler, 1);
        contentHandler.startElement(namespace, ComponentDescriptorIO.COMPONENT, ComponentDescriptorIO.COMPONENT_QNAME, ai);
        IOUtils.newline(contentHandler);
View Full Code Here

            if (specVersion != null) {

                if (localName.equals(COMPONENT)) {
                    this.isComponent = true;

                    final ComponentDescription desc = new ComponentDescription(null);
                    desc.setName(attributes.getValue(ATTR_NAME));

                    // enabled attribute is optional
                    if (attributes.getValue(COMPONENT_ATTR_ENABLED) != null) {
                        desc.setEnabled(Boolean.valueOf(attributes.getValue(COMPONENT_ATTR_ENABLED)));
                    }

                    // immediate attribute is optional
                    if (attributes.getValue(COMPONENT_ATTR_IMMEDIATE) != null) {
                        desc.setImmediate(Boolean.valueOf(attributes.getValue(COMPONENT_ATTR_IMMEDIATE)));
                    }

                    desc.setFactory(attributes.getValue(COMPONENT_ATTR_FACTORY));

                    desc.setConfigurationPolicy(ComponentConfigurationPolicy.OPTIONAL);
                    // check for version 1.1 attributes
                    if (specVersion.ordinal() >= SpecVersion.VERSION_1_1.ordinal()) {
                        final String policy = attributes.getValue(COMPONENT_ATTR_POLICY);
                        if ( policy != null ) {
                            try {
                                desc.setConfigurationPolicy(ComponentConfigurationPolicy.valueOf(policy.toUpperCase()));
                            } catch (final IllegalArgumentException iae) {
                                iLog.addWarning("Invalid value for attribute " + COMPONENT_ATTR_POLICY + " : " + policy, this.location);
                            }
                        }
                        if ( attributes.getValue(COMPONENT_ATTR_ACTIVATE) != null ) {
                            desc.setActivate(attributes.getValue(COMPONENT_ATTR_ACTIVATE));
                        }
                        if ( attributes.getValue(COMPONENT_ATTR_DEACTIVATE) != null ) {
                            desc.setDeactivate(attributes.getValue(COMPONENT_ATTR_DEACTIVATE));
                        }
                        if ( attributes.getValue(COMPONENT_ATTR_MODIFIED) != null ) {
                            desc.setModified(attributes.getValue(COMPONENT_ATTR_MODIFIED));
                        }
                    }

                    this.currentComponent = desc;
                } else if (localName.equals(IMPLEMENTATION)) {
View Full Code Here

            this.createComponent(cad, describedClass, scannedClass);
        }

        // search for the component descriptions and use the first one
        final List<ComponentDescription> componentDescs = describedClass.getDescriptions(ComponentDescription.class);
        ComponentDescription found = null;
        if (!componentDescs.isEmpty()) {
            found = componentDescs.get(0);
        }

        if (found != null) {
            final ComponentDescription cd = found;

            // search for methods
            final List<MethodAnnotation> methodTags = scannedClass.getMethodAnnotations(null);
            for (final MethodAnnotation m : methodTags) {
                if (m.getName().equals(Activate.class.getName())) {
                    cd.setActivate(m.getAnnotatedMethod().getName());
                    scannedClass.processed(m);
                } else if (m.getName().equals(Deactivate.class.getName())) {
                    cd.setDeactivate(m.getAnnotatedMethod().getName());
                    scannedClass.processed(m);
                } else if (m.getName().equals(Modified.class.getName())) {
                    cd.setModified(m.getAnnotatedMethod().getName());
                    scannedClass.processed(m);
                } else if (m.getName().equals(Reference.class.getName()) ) {
                    this.processReference(describedClass, m);
                    scannedClass.processed(m);
                }
View Full Code Here

TOP

Related Classes of org.apache.felix.scrplugin.description.ComponentDescription

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.