Package org.apache.felix.ipojo

Examples of org.apache.felix.ipojo.ConfigurationException


        String aggregate = elem.getAttribute("aggregate");
        boolean agg = aggregate != null && aggregate.equalsIgnoreCase("true");

        if (dep == null && !opt) {
            throw new ConfigurationException("Service Providing: The requirement " + elem.getAttribute("specification") + " is not present in the implementation and is declared as a mandatory service-level requirement");
        }

        if (dep != null && dep.isAggregate() && !agg) {
            throw new ConfigurationException("Service Providing: The requirement " + elem.getAttribute("specification") + " is aggregate in the implementation and is declared as a simple service-level requirement");
        }

        String filter = elem.getAttribute("filter");
        if (dep != null && filter != null) {
            String filter2 = dep.getFilter();
            if (filter2 == null || !filter2.equalsIgnoreCase(filter)) {
                throw new ConfigurationException("Service Providing: The specification requirement " + elem.getAttribute("specification") + " has not the same filter as declared in the service-level requirement");
            }
        }
    }
View Full Code Here


            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

        String field = dep.getField();
        DependencyCallback[] callbacks = dep.getCallbacks();
        int index = dep.getConstructorParameterIndex();

        if (callbacks == null && field == null  && index == -1) {
            throw new ConfigurationException("A service requirement requires at least binding methods, " +
                "a field or a constructor parameter");
        }

        for (int i = 0; callbacks != null && i < callbacks.length; i++) {
            MethodMetadata[] mets = manipulation.getMethods(callbacks[i].getMethodName());
            if (mets.length == 0) {
                debug("A requirement callback " + callbacks[i].getMethodName() + " does not exist in the implementation class, will try the super classes");
            } else {
                if (mets[0].getMethodArguments().length > 2) {
                    throw new ConfigurationException("Requirement Callback : A requirement callback "
                            + callbacks[i].getMethodName()
                            + " must have 0, 1 or 2 arguments");
                }

                callbacks[i].setArgument(mets[0].getMethodArguments());

                if (mets[0].getMethodArguments().length == 1) {
                    if (!mets[0].getMethodArguments()[0].equals(ServiceReference.class.getName())) {
                        // The callback receives the service object.
                        setSpecification(dep, mets[0].getMethodArguments()[0], false); // Just warn if a mismatch is discovered.
                    }
                } else if (mets[0].getMethodArguments().length == 2) {
                    // The callback receives service object, service reference. Check that the second argument is a service reference
                    if (!(mets[0].getMethodArguments()[1].equals(ServiceReference.class.getName()) // callback with (service object, service reference)
                           || mets[0].getMethodArguments()[1].equals(Dictionary.class.getName()) // callback with (service object, service properties in a dictionary)
                           || mets[0].getMethodArguments()[1].equals(Map.class.getName()))) { // callback with (service object, service properties in a map)
                        String message =
                                "The requirement callback " + callbacks[i].getMethodName() + " must have a ServiceReference, a Dictionary or a Map as the second argument";
                        throw new ConfigurationException(message);
                    }
                    setSpecification(dep, mets[0].getMethodArguments()[0], false); // Just warn if a mismatch is discovered.
                }
            }

        }

        if (field != null) {
            FieldMetadata meta = manipulation.getField(field);
            if (meta == null) {
                throw new ConfigurationException("Requirement Callback : A requirement field "
                        + field
                        + " does not exist in the implementation class");
            }
            String type = meta.getFieldType();
            if (type.endsWith("[]")) {
                if (dep.isProxy()) {
                    info("Arrays cannot be used for proxied dependencies - Disabling the proxy mode");
                    dep.setProxy(false);
                }
                // Set the dependency to multiple
                dep.setAggregate(true);
                type = type.substring(0, type.length() - 2);
            } else if (type.equals(List.class.getName()) || type.equals(Collection.class.getName())) {
                dep.setType(LIST);
                type = null;
            } else if (type.equals(Vector.class.getName())) {
                dep.setType(VECTOR);
                if (dep.isProxy()) {
                    warn("Vectors cannot be used for proxied dependencies - Disabling the proxy mode");
                    dep.setProxy(false);
                }
                type = null;
            } else if (type.equals(Set.class.getName())) {
                dep.setType(SET);
                type = null;
            } else {
                if (dep.isAggregate()) {
                    throw new ConfigurationException("A required service is not correct : the field "
                            + meta.getFieldName()
                            + " must be an array to support aggregate injections");
                }
            }
            setSpecification(dep, type, true); // Throws an exception if the field type mismatch.
        }

        // Constructor parameter
        if (index != -1) {
          if (! dep.isProxy()) {
            throw new ConfigurationException("Services injected into constructor must be proxied");
          }

        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 (cts.length > && cts[0].getMethodArguments().length > index) {
            String type = cts[0].getMethodArguments()[index];
            if (type.endsWith("[]")) {
                    throw new ConfigurationException("Services injected into constructor cannot be arrays");
                } else if (type.equals(List.class.getName()) || type.equals(Collection.class.getName())) {
                    dep.setType(LIST);
                    type = null;
                } else if (type.equals(Vector.class.getName())) {
                  throw new ConfigurationException("Services injected into constructor cannot be Vectors");
                } else if (type.equals(Set.class.getName())) {
                    dep.setType(SET);
                    type = null;
                } else {
                    if (dep.isAggregate()) {
                        throw new ConfigurationException("A required service is not correct : the constructor parameter "
                                + index
                                + " must be an aggregate type to support aggregate injections");
                    }
                }
                setSpecification(dep, type, true); // Throws an exception if the field type mismatch.
          } else {
            throw new ConfigurationException("Cannot determine the specification of the dependency " + index +
                ", please use the specification attribute");
          }
        }

        // Disable proxy on scalar dependency targeting non-interface specification
View Full Code Here

                    id = dep.getField();
                    if (id == null) {
                      id = Integer.toString(dep.getConstructorParameterIndex());
                    }
                  }
                    throw new ConfigurationException("Cannot discover the required specification for " + id);
                } else {
                    // If the specification is different, warn that we will override it.
                    info("Cannot discover the required specification for " + dep.getField());
                }
            }
        } else { // In all other case, className is not null.
            if (dep.getSpecification() == null || !dep.getSpecification().getName().equals(className)) {
                if (dep.getSpecification() != null) {
                    if (error) {
                        throw new ConfigurationException("A required service is not correct : the discovered type ["
                            + className
                            + "] and the specified (or already discovered)  service interface ["
                            + dep.getSpecification().getName()
                            + "] are not the same");
                    } else {
                        // If the specification is different, warn that we will override it.
                        warn("["
                            + getInstanceManager().getInstanceName()
                            + "] The field type ["
                            + className
                            + "] and the required service interface ["
                            + dep.getSpecification()
                            + "] are not the same");
                    }
                }

                try {
                    dep.setSpecification(getInstanceManager().getContext().getBundle().loadClass(className));
                } catch (ClassNotFoundException e) {
                    throw new ConfigurationException("The required service interface cannot be loaded : " + e.getMessage());
                }
            }
        }
    }
View Full Code Here

            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)) {
View Full Code Here

      throws ConfigurationException {
    if (requiresFiltersValue != null
        && requiresFiltersValue.getClass().isArray()) {
      String[] filtersArray = (String[]) requiresFiltersValue;
      if (filtersArray.length % 2 != 0) {
        throw new ConfigurationException(
            "A requirement filter is invalid : "
                + requiresFiltersValue);
      }
      Dictionary requiresFilters = new Hashtable();
      for (int i = 0; i < filtersArray.length; i += 2) {
View Full Code Here

        Element[] elem = metadata.getElements("properties", NAMESPACE); // Get all example.handler.properties:properties element

        switch (elem.length) {
            case 0:
                // No matching element in metadata, throw a configuration error.
                throw new ConfigurationException("No properties found");
            case 1:
                // One 'properties' found, get attributes.
                m_file = elem[0].getAttribute("file");
                if (m_file == null) {
                    // if file is null, throw a configuration error.
                    throw new ConfigurationException("Malformed properties element : file attribute must be set");
                }
                break;
            default:
                // To simplify we handle only one properties element.
                throw new ConfigurationException("Only one properties element is supported");
        }

        // Look if the instance overrides file location :
        String instanceFile = (String) configuration.get("properties.file");
        if (instanceFile != null) {
            m_file = instanceFile;
        }

        // Load properties
        try {
            loadProperties();
        } catch (IOException e) {
            throw new ConfigurationException("Error when reading the " + m_file + " file : " + e.getMessage());
        }

        // 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");
            }

            // Then check that the field is a String field
            if (!fm.getFieldType().equals(String.class.getName())) {
                throw new ConfigurationException("The field " + field + " exists in the pojo, but is not a String");
            }

            // All checks are ok, register the interceptor.
            getInstanceManager().register(fm, this);
        }
View Full Code Here

    public void configure(Element arg0, Dictionary arg1)
            throws ConfigurationException {
        Element[] elements = arg0.getElements(NAME, NAMESPACE);
        if (elements.length > 1) {
            throw new ConfigurationException("The handler " + NAMESPACE + ":" + NAME + " cannot be declared several times");
        }

        String field = elements[0].getAttribute(FIELD_ATTRIBUTE);
        if (field != null) {
            FieldMetadata meta = getPojoMetadata().getField(field);
            if (meta == null) {
                throw new ConfigurationException("The transaction field does not exist in the pojo class : " + field);
            }
            if (! meta.getFieldType().equals(Transaction.class.getName())) {
                throw new ConfigurationException("The transaction field type must be " + Transaction.class.getName());
            }
            // Register the interceptor
            getInstanceManager().register(meta, this);

        }

        String oncommit = elements[0].getAttribute(ONCOMMIT_ATTRIBUTE);
        if (oncommit != null) {
            m_onCommit = new Callback(oncommit, new String[] { Transaction.class.getName() }, false, getInstanceManager());
        }

        String onrollback = elements[0].getAttribute(ONROLLBACK_ATTRIBUTE);
        if (onrollback != null) {
            m_onRollback = new Callback(onrollback, new String[] { Transaction.class.getName() }, false, getInstanceManager());
        }


        Element[] sub = elements[0].getElements(TRANSACTIONAL_ELEMENT);
        if (sub == null  || sub.length == 0) {
            throw new ConfigurationException("The handler " + NAMESPACE + ":" + NAME + " must have " + TRANSACTIONAL_ELEMENT + " subelement");
        }

        for (int i = 0; i < sub.length; i++) {
            String method = sub[i].getAttribute(METHOD_ATTRIBUTE);
            String to = sub[i].getAttribute(TIMEOUT_ATTRIBUTE);
            String propa = sub[i].getAttribute(PROPAGATION_ATTRIBUTE);
            String nrbf = sub[i].getAttribute(NOROLLBACKFOR_ATTRIBUTE);
            String eorb = sub[i].getAttribute(EXCEPTIONONROLLBACK_ATTRIBUTE);

            if (method == null) {
                throw new ConfigurationException("A transactionnal element must specified the method attribute");
            }
            MethodMetadata meta = this.getPojoMetadata().getMethod(method);
            if (meta == null) {
                throw new ConfigurationException("A transactionnal method is not in the pojo class : " + method);
            }

            int timeout = 0;
            if (to != null) {
                timeout = new Integer(to).intValue();
View Full Code Here

        } else if (propa.equalsIgnoreCase("requiresnew")) {
            return TransactionnalMethod.REQUIRES_NEW;
        }

       throw new ConfigurationException("Unknown propgation policy : " + propa);
    }
View Full Code Here

            } else if (element.containsAttribute("field")) {
                String field = element.getAttribute("field");
                final BundleContext injected = bc;
                FieldMetadata fm = getFactory().getPojoMetadata().getField(field);
                if (fm == null) {
                    throw new ConfigurationException("Cannot inject the bundle context in the field " + field + " - " +
                            "reason: the field does not exist in " + getInstanceManager().getClassName());
                }
                if (!BundleContext.class.getName().equals(fm.getFieldType())) {
                    throw new ConfigurationException("Cannot inject the bundle context in the field " + field + " - " +
                            "reason: the field " + field + " from " + getInstanceManager().getClassName() + " is not " +
                            "from the BundleContext type");
                }
                getInstanceManager().register(fm, new FieldInterceptor() {
                    public void onSet(Object pojo, String fieldName, Object value) {
View Full Code Here

TOP

Related Classes of org.apache.felix.ipojo.ConfigurationException

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.