Package org.apache.felix.ipojo.parser

Examples of org.apache.felix.ipojo.parser.PojoMetadata


        m_callbacks = new LifecycleCallback[0];

        String imm = metadata.getAttribute("immediate");
        m_immediate = imm != null && imm.equalsIgnoreCase("true");
       
        PojoMetadata meta = getFactory().getPojoMetadata();

        Element[] hooksMetadata = metadata.getElements("callback");
        for (int i = 0; hooksMetadata != null && i < hooksMetadata.length; i++) {
            String method = hooksMetadata[i].getAttribute("method");
            if (method == null) {
                throw new ConfigurationException("Lifecycle callback : A callback needs to contain a method attribute");
            }
           
            MethodMetadata met = meta.getMethod(method, new String[0]);
           
            int transition = -1;
            String trans = hooksMetadata[i].getAttribute("transition");
            if (trans == null) {
                throw new ConfigurationException("Lifecycle callback : the transition attribute is missing");
View Full Code Here


            }

            String value = configurables[i].getAttribute("value");

            // Detect the type of the property
            PojoMetadata manipulation = getFactory().getPojoMetadata();
            String type = null;
            if (methodName != null) {
                MethodMetadata[] method = manipulation.getMethods(methodName);
                if (method.length == 0) {
                    type = configurables[i].getAttribute("type");
                    if (type == null) {
                        throw new ConfigurationException("Malformed property : The type of the property cannot be discovered, add a 'type' attribute");
                    }
                } else {
                    if (method[0].getMethodArguments().length != 1) {
                        throw new ConfigurationException("Malformed property :  The method " + methodName + " does not have one argument");
                    }
                    type = method[0].getMethodArguments()[0];
                    configurables[i].addAttribute(new Attribute("type", type)); // Add the type to avoid configure checking
                }
            } else if (fieldName != null) {
                FieldMetadata field = manipulation.getField(fieldName);
                if (field == null) { throw new ConfigurationException("Malformed property : The field " + fieldName + " does not exist in the implementation class"); }
                type = field.getFieldType();
                configurables[i].addAttribute(new Attribute("type", type)); // Add the type to avoid configure checking
            } else if (paramIndex != null) {
                int index = Integer.parseInt(paramIndex);
                type = configurables[i].getAttribute("type");
                MethodMetadata[] cts = manipulation.getConstructors();
                // If we don't have a type, try to get the first constructor and get the type of the parameter
                // we the index 'index'.
                if (type == null && cts.length > && cts[0].getMethodArguments().length > index) {
                    type = cts[0].getMethodArguments()[index];
                } else if (type == null) { // Applied only if type was not determined.
View Full Code Here

     * @see org.apache.felix.ipojo.Handler#initializeComponentFactory(org.apache.felix.ipojo.architecture.ComponentTypeDescription, org.apache.felix.ipojo.metadata.Element)
     */
    public void initializeComponentFactory(ComponentTypeDescription desc, Element metadata) throws ConfigurationException {
        // Change ComponentInfo
        Element[] provides = metadata.getElements("provides");
        PojoMetadata manipulation = getFactory().getPojoMetadata();

        for (int i = 0; i < provides.length; i++) {
            // First : create the serviceSpecification list
            String[] serviceSpecification = manipulation.getInterfaces();
            String parent = manipulation.getSuperClass();
            Set interfaces = new HashSet();
            Set parentClasses = new HashSet();
            try {
                computeInterfacesAndSuperClasses(serviceSpecification, parent, desc.getBundleContext().getBundle(), interfaces, parentClasses);
                getLogger().log(Logger.INFO, "Collected interfaces from " + metadata.getAttribute("classname") + " : " + interfaces);
                getLogger().log(Logger.INFO, "Collected super classes from " + metadata.getAttribute("classname") + " : " + parentClasses);
            } catch (ClassNotFoundException e) {
                throw new ConfigurationException("An interface or parent class cannot be loaded : " + e.getMessage());
            }

            String serviceSpecificationStr = provides[i].getAttribute("specifications");
            if (serviceSpecificationStr == null) {
                serviceSpecificationStr = provides[i].getAttribute("interface");
                if (serviceSpecificationStr != null) {
                    warn("The 'interface' attribute is deprecated, use the 'specifications' attribute instead of 'interface'");
                }
            }

            if (serviceSpecificationStr != null) {
                List itfs = ParseUtils.parseArraysAsList(serviceSpecificationStr);
                for (int j = 0; j < itfs.size(); j++) {
                    if (! interfaces.contains(itfs.get(j))
                            && ! parentClasses.contains(itfs.get(j))
                            && ! desc.getFactory().getClassName().equals(itfs.get(j))) {
                            desc.getFactory().getLogger().log(Logger.ERROR, "The specification " + itfs.get(j) + " is not implemented by " + metadata.getAttribute("classname"));
                    }
                }
                interfaces = new HashSet(itfs);
            }

            if (interfaces.isEmpty()) {
                warn("No service interface found in the class hierarchy, use the implementation class");
                interfaces.add(desc.getFactory().getClassName());
            }

            StringBuffer specs = null;
            Set set = new HashSet(interfaces);
            set.remove(Pojo.class.getName()); // Remove POJO.
            Iterator iterator = set.iterator();
            while (iterator.hasNext()) {
                String spec = (String) iterator.next();
                desc.addProvidedServiceSpecification(spec);
                if (specs == null) {
                    specs = new StringBuffer("{");
                    specs.append(spec);
                } else {
                    specs.append(',');
                    specs.append(spec);
                }
            }

            specs.append('}');
            provides[i].addAttribute(new Attribute("specifications", specs.toString())); // Add interface attribute to avoid checking in the configure method

            Element[] props = provides[i].getElements("property");
            for (int j = 0; props != null && j < props.length; j++) {
                String name = props[j].getAttribute("name");
                String value = props[j].getAttribute("value");
                String type = props[j].getAttribute("type");
                String field = props[j].getAttribute("field");


                // Get property name :
                if (field != null && name == null) {
                    name = field;
                }

                // Check type if not already set
                if (type == null) {
                    if (field == null) {
                        throw new ConfigurationException("The property " + name + " has neither type nor field.");
                    }
                    FieldMetadata fieldMeta = manipulation.getField(field);
                    if (fieldMeta == null) {
                        throw new ConfigurationException("A declared property was not found in the implementation class : " + field);
                    }
                    type = fieldMeta.getFieldType();
                    props[j].addAttribute(new Attribute("type", type));
View Full Code Here

     * @see org.apache.felix.ipojo.Handler#configure(org.apache.felix.ipojo.InstanceManager, org.apache.felix.ipojo.metadata.Element,
     *      java.util.Dictionary)
     */
    public void configure(Element componentMetadata, Dictionary configuration) throws ConfigurationException {
        // getPojoMetadata();
        PojoMetadata manipulation = getFactory().getPojoMetadata();
        boolean atLeastOneField = false;

        // Create the dependency according to the component metadata
        Element[] deps = componentMetadata.getElements("Requires");

        // Get instance filters.
        Dictionary filtersConfiguration = getRequiresFilters(configuration.get("requires.filters"));
        Dictionary fromConfiguration = (Dictionary) configuration.get("requires.from");

        for (int i = 0; deps != null && i < deps.length; i++) {
            // Create the dependency metadata
            String field = deps[i].getAttribute("field");

            String serviceSpecification = deps[i].getAttribute("interface");
            // the 'interface' attribute is deprecated
            if (serviceSpecification != null) {
                warn("The 'interface' attribute is deprecated, use the 'specification' attribute instead");
            } else {
                serviceSpecification = deps[i].getAttribute("specification");
            }

            String filter = deps[i].getAttribute("filter");
            String opt = deps[i].getAttribute("optional");
            boolean optional = opt != null && opt.equalsIgnoreCase("true");
            String defaultImplem = deps[i].getAttribute("default-implementation");

            String agg = deps[i].getAttribute("aggregate");
            boolean aggregate = agg != null && agg.equalsIgnoreCase("true");
            String identitity = deps[i].getAttribute("id");

            String nul = deps[i].getAttribute("nullable");
            boolean nullable = nul == null || nul.equalsIgnoreCase("true");

            boolean isProxy = true;
            // Detect proxy default value.
            String setting = getInstanceManager().getContext().getProperty(PROXY_SETTINGS_PROPERTY);
            if (setting == null || PROXY_ENABLED.equals(setting)) { // If not set => Enabled
                isProxy = true;
            } else if (setting != null  && PROXY_DISABLED.equals(setting)) {
                isProxy = false;
            }

            String proxy = deps[i].getAttribute("proxy");
            // If proxy == null, use default value
            if (proxy != null) {
                if (proxy.equals("false")) {
                    isProxy = false;
                } else if (proxy.equals("true")) {
                    if (! isProxy) { // The configuration overrides the system setting
                        warn("The configuration of a service dependency overrides the proxy mode");
                    }
                    isProxy = true;
                }
            }

            String scope = deps[i].getAttribute("scope");
            BundleContext context = getInstanceManager().getContext(); // Get the default bundle context.
            if (scope != null) {
                // If we are not in a composite, the policy is set to global.
                if (scope.equalsIgnoreCase("global") || ((((IPojoContext) getInstanceManager().getContext()).getServiceContext()) == null)) {
                    context =
                            new PolicyServiceContext(getInstanceManager().getGlobalContext(), getInstanceManager().getLocalServiceContext(),
                                    PolicyServiceContext.GLOBAL);
                } else if (scope.equalsIgnoreCase("composite")) {
                    context =
                            new PolicyServiceContext(getInstanceManager().getGlobalContext(), getInstanceManager().getLocalServiceContext(),
                                    PolicyServiceContext.LOCAL);
                } else if (scope.equalsIgnoreCase("composite+global")) {
                    context =
                            new PolicyServiceContext(getInstanceManager().getGlobalContext(), getInstanceManager().getLocalServiceContext(),
                                    PolicyServiceContext.LOCAL_AND_GLOBAL);
                }
            }

            // Get instance filter if available
            if (filtersConfiguration != null && identitity != null && filtersConfiguration.get(identitity) != null) {
                filter = (String) filtersConfiguration.get(identitity);
            }

            // Compute the 'from' attribute
            String from = deps[i].getAttribute("from");
            if (fromConfiguration != null && identitity != null && fromConfiguration.get(identitity) != null) {
                from = (String) fromConfiguration.get(identitity);
            }
            if (from != null) {
                String fromFilter = "(|(instance.name=" + from + ")(service.pid=" + from + "))";
                if (aggregate) {
                    warn("The 'from' attribute is incompatible with aggregate requirements: only one provider will match : " + fromFilter);
                }
                if (filter != null) {
                    filter = "(&" + fromFilter + filter + ")"; // Append the two filters
                } else {
                    filter = fromFilter;
                }
            }

            Filter fil = null;
            if (filter != null) {
                try {
                    fil = getInstanceManager().getContext().createFilter(filter);
                } catch (InvalidSyntaxException e) {
                    throw new ConfigurationException("A requirement filter is invalid : " + filter + " - " + e.getMessage());
                }
            }


            Class spec = null;
            if (serviceSpecification != null) {
                spec = DependencyModel.loadSpecification(serviceSpecification, getInstanceManager().getContext());
            }

            int policy = DependencyModel.getPolicy(deps[i]);
            Comparator cmp = DependencyModel.getComparator(deps[i], getInstanceManager().getGlobalContext());


            Dependency dep = new Dependency(this, field, spec, fil, optional, aggregate, nullable, isProxy, identitity, context, policy, cmp, defaultImplem);

            // Look for dependency callback :
            Element[] cbs = deps[i].getElements("Callback");
            for (int j = 0; cbs != null && j < cbs.length; j++) {
                if (!cbs[j].containsAttribute("method") && cbs[j].containsAttribute("type")) {
                    throw new ConfigurationException("Requirement Callback : a dependency callback must contain a method and a type (bind or unbind) attribute");
                }
                String method = cbs[j].getAttribute("method");
                String type = cbs[j].getAttribute("type");
                int methodType = 0;
                if ("bind".equalsIgnoreCase(type)) {
                    methodType = DependencyCallback.BIND;
                } else if ("modified".equalsIgnoreCase(type)) {
                    methodType = DependencyCallback.MODIFIED;
                } else {
                    methodType = DependencyCallback.UNBIND;
                }

                DependencyCallback callback = new DependencyCallback(dep, method, methodType);
                dep.addDependencyCallback(callback);
            }

            // Add the constructor parameter if needed
            String paramIndex = deps[i].getAttribute("constructor-parameter");
            if (paramIndex != null) {
              int index = Integer.parseInt(paramIndex);
              dep.addConstructorInjection(index);
            }

            // Check the dependency :
            if (checkDependency(dep, manipulation)) {
                addDependency(dep);
                if (dep.getField() != null) {
                    getInstanceManager().register(manipulation.getField(dep.getField()), dep);
                    atLeastOneField = true;
                }
            }
        }

        if (atLeastOneField) { // Does register only if we have fields
            MethodMetadata[] methods = manipulation.getMethods();
            for (int i = 0; i < methods.length; i++) {
                for (int j = 0; j < m_dependencies.length; j++) {
                    getInstanceManager().register(methods[i], m_dependencies[j]);
                }
            }
View Full Code Here

        // Register fields
        // By convention, properties file entry are field name, so look for each property to get field list.

        //First get Pojo Metadata metadata :
        PojoMetadata pojoMeta = getPojoMetadata();
        Enumeration e = m_properties.keys();
        while (e.hasMoreElements()) {
            String field = (String) e.nextElement();
            FieldMetadata fm = pojoMeta.getField(field);

            if (fm == null) { // The field does not exist
                throw new ConfigurationException("The field " + field + " is declared in the properties file but does not exist in the pojo");
            }
View Full Code Here

     * @param metadata the component metadata
     * @param dict the instance configuration
     */
    public void configure(Element metadata, Dictionary dict) {

        PojoMetadata manipulation = getPojoMetadata();

        m_instanceManager = getInstanceManager();

        m_jmxConfigFieldMap = new JmxConfigFieldMap();

        // Build the hashmap
        Element[] mbeans = metadata.getElements(JMX_CONFIG_ELT, m_namespace);
        if (mbeans == null || mbeans.length == 0) {
            mbeans = metadata.getElements(JMX_CONFIG_ALT_ELT, m_namespace);
        }

        if (mbeans.length != 1) {
            error("A component must have exactly one " + JMX_CONFIG_ELT + " or " + JMX_CONFIG_ALT_ELT + " element.");
            error("The JMX handler configuration is ignored.");
            return;
        }

        Element mbean = mbeans[0];

        // retrieve kind of MBeanServer to use
        m_usesMOSGi = Boolean.parseBoolean(mbean.getAttribute(JMX_USES_MOSGI_ELT));

        // retrieve object name
        m_completeObjNameElt = mbean.getAttribute(JMX_OBJ_NAME_ELT);
        m_domainElt = mbean.getAttribute(JMX_OBJ_NAME_DOMAIN_ELT);
        m_objNameWODomainElt = mbean.getAttribute(JMX_OBJ_NAME_WO_DOMAIN_ELT);

        // test if Pojo is interested in registration callbacks
        m_registerCallbacks = manipulation
            .isInterfaceImplemented(MBeanRegistration.class.getName());
        if (m_registerCallbacks) {
            // don't need to check that methods exist, the pojo implements
            // MBeanRegistration interface
            String[] preRegisterParams = { MBeanServer.class.getName(),
                    ObjectName.class.getName() };
            m_preRegisterMeth = manipulation.getMethod(PRE_REGISTER_METH_NAME,
                preRegisterParams);

            String[] postRegisterParams = { Boolean.class.getName() };
            m_postRegisterMeth = manipulation.getMethod(
                POST_REGISTER_METH_NAME, postRegisterParams);

            m_preDeregisterMeth = manipulation.getMethod(
                PRE_DEREGISTER_METH_NAME, new String[0]);

            m_postDeregisterMeth = manipulation.getMethod(
                POST_DEREGISTER_METH_NAME, new String[0]);
        }

        // set property
        Element[] attributes = mbean.getElements(JMX_PROPERTY_ELT, m_namespace);
        Element[] attributesAlt = mbean.getElements(JMX_PROPERTY_ELT_ALT, m_namespace);
        List<Element> listOfAttributes = new ArrayList<Element>();
        if (attributes != null) {
          listOfAttributes.addAll(Arrays.asList(attributes));
        }
        if (attributesAlt != null) {
          listOfAttributes.addAll(Arrays.asList(attributesAlt));
        }

        Element[] attributesOld = mbeans[0].getElements(JMX_PROPERTY_ELT);
        if (attributesOld != null) {
            warn("The JMX property element should use the '" + m_namespace + "' namespace.");
            listOfAttributes.addAll(Arrays.asList(attributesOld));
        }

        for (Element attribute : listOfAttributes) {
            boolean notif = false;
            String rights;
            String name;
            String field = attribute.getAttribute(JMX_FIELD_ELT);

            if (attribute.containsAttribute(JMX_NAME_ELT)) {
                name = attribute.getAttribute(JMX_NAME_ELT);
            } else {
                name = field;
            }
            if (attribute.containsAttribute(JMX_RIGHTS_ELT)) {
                rights = attribute.getAttribute(JMX_RIGHTS_ELT);
            } else {
                rights = "r";
            }

            PropertyField property = new PropertyField(name, field, rights,
                getTypeFromAttributeField(field, manipulation));

            if (attribute.containsAttribute(JMX_NOTIFICATION_ELT)) {
                notif = Boolean.parseBoolean(attribute
                    .getAttribute(JMX_NOTIFICATION_ELT));
            }

            property.setNotifiable(notif);

            if (notif) {
                // add the new notifiable property in structure
                NotificationField notification = new NotificationField(
                    name, this.getClass().getName() + "." + field, null);
                m_jmxConfigFieldMap.addNotificationFromName(name,
                    notification);
            }
            m_jmxConfigFieldMap.addPropertyFromName(name, property);
            getInstanceManager().register(manipulation.getField(field),
                this);
            info("property exposed:" + name + " " + field + ":"
                    + getTypeFromAttributeField(field, manipulation) + " "
                    + rights + ", Notif=" + notif);
        }
View Full Code Here

        metadata.addAttribute(new Attribute("classname", classname));
        metadata.addElement(new Element("provides", ""));
        metadata.addElement(manipulation);
        manipulation.addAttribute(new Attribute("classname", classname));

        Mockito.when(factory.getPojoMetadata()).thenReturn(new PojoMetadata(metadata));
        Mockito.when(factory.getClassName()).thenReturn(classname);

        handler.initializeComponentFactory(desc, metadata);

        //Expected behavior: the implementation classname
View Full Code Here

        provides.addAttribute(new Attribute("specifications", "java.beans.SimpleBeanInfo"));
        metadata.addElement(provides);
        metadata.addElement(manipulation);
        manipulation.addAttribute(new Attribute("classname", classname));
        manipulation.addAttribute(new Attribute("super", "java.beans.SimpleBeanInfo"));
        Mockito.when(factory.getPojoMetadata()).thenReturn(new PojoMetadata(metadata));
        Mockito.when(factory.getClassName()).thenReturn(classname);

        handler.initializeComponentFactory(desc, metadata);

        System.out.println(metadata);
View Full Code Here

        provides.addAttribute(new Attribute("specifications", classname));
        metadata.addElement(provides);
        metadata.addElement(manipulation);
        manipulation.addAttribute(new Attribute("classname", classname));
        manipulation.addAttribute(new Attribute("super", "java.beans.SimpleBeanInfo"));
        Mockito.when(factory.getPojoMetadata()).thenReturn(new PojoMetadata(metadata));
        Mockito.when(factory.getClassName()).thenReturn(classname);

        handler.initializeComponentFactory(desc, metadata);

        System.out.println(metadata);
View Full Code Here

        metadata.addElement(provides);
        metadata.addElement(manipulation);
        manipulation.addAttribute(new Attribute("classname", classname));
        manipulation.addAttribute(new Attribute("super", "java.beans.MethodDescriptor"));

        Mockito.when(factory.getPojoMetadata()).thenReturn(new PojoMetadata(metadata));
        Mockito.when(factory.getClassName()).thenReturn(classname);

        handler.initializeComponentFactory(desc, metadata);

        System.out.println(metadata);
View Full Code Here

TOP

Related Classes of org.apache.felix.ipojo.parser.PojoMetadata

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.