Search in sources :

Example 26 with ConfigurationException

use of org.apache.felix.ipojo.ConfigurationException in project felix by apache.

the class SourceManager method parseSources.

/**
 * Parse the context-source attribute in order to create source tracker object.
 * @param sourceAtt : context-source attribute.
 * @param global : global bundle context.
 * @param parent : parent bundle context.
 * @param local : local bundle context.
 * @throws ConfigurationException : the context-source attribute is invalid.
 */
private void parseSources(String sourceAtt, BundleContext global, BundleContext parent, BundleContext local) throws ConfigurationException {
    String[] sources = ParseUtils.split(sourceAtt, ",");
    for (int i = 0; i < sources.length; i++) {
        String[] srcs = ParseUtils.split(sources[i], ":");
        if (srcs.length == 1) {
            // No prefix use local. //TODO choose default case.
            SourceTracker tracker = new SourceTracker(srcs[0], local);
            m_trackers.add(tracker);
        } else if (srcs.length == 2) {
            // According to prefix add the source in the good list.
            if (srcs[0].equalsIgnoreCase("parent")) {
                SourceTracker tracker = new SourceTracker(srcs[1], parent);
                m_trackers.add(tracker);
            } else if (srcs[0].equalsIgnoreCase("local")) {
                SourceTracker tracker = new SourceTracker(srcs[1], local);
                m_trackers.add(tracker);
            } else if (srcs[0].equalsIgnoreCase("global")) {
                SourceTracker tracker = new SourceTracker(srcs[1], global);
                m_trackers.add(tracker);
            } else {
                throw new ConfigurationException("Unknowns context scope : " + srcs[0]);
            }
        } else {
            throw new ConfigurationException("Malformed context source : " + sources[i]);
        }
    }
}
Also used : ConfigurationException(org.apache.felix.ipojo.ConfigurationException)

Example 27 with ConfigurationException

use of org.apache.felix.ipojo.ConfigurationException in project felix by apache.

the class Property method computeArrayType.

/**
 * Gets the Class object of a type array.
 * @param type the string descriptor of the type (must end by [] )
 * @param context the bundle context (used to load classes)
 * @return the Class object of the given type array.
 * @throws ConfigurationException if the class cannot be loaded
 */
private static Class computeArrayType(String type, BundleContext context) throws ConfigurationException {
    // Note: Harmony does't support the type[].class notation.
    // An empty array has to be created to get the class object.
    String internalType = type.substring(0, type.length() - 2);
    if ("string".equals(internalType) || "String".equals(internalType)) {
        return new String[0].getClass();
    }
    if ("boolean".equals(internalType)) {
        return new boolean[0].getClass();
    }
    if ("byte".equals(internalType)) {
        return new byte[0].getClass();
    }
    if ("short".equals(internalType)) {
        return new short[0].getClass();
    }
    if ("int".equals(internalType)) {
        return new int[0].getClass();
    }
    if ("long".equals(internalType)) {
        return new long[0].getClass();
    }
    if ("float".equals(internalType)) {
        return new float[0].getClass();
    }
    if ("double".equals(internalType)) {
        return new double[0].getClass();
    }
    if ("char".equals(internalType)) {
        return new char[0].getClass();
    }
    // Complex array type.
    try {
        Class clazz = context.getBundle().loadClass(internalType);
        Object[] object = (Object[]) Array.newInstance(clazz, 0);
        return object.getClass();
    } catch (ClassNotFoundException e) {
        throw new ConfigurationException("Class not found exception in setValue on " + internalType, e);
    } catch (SecurityException e) {
        throw new ConfigurationException("Security Exception in setValue on " + internalType, e);
    } catch (IllegalArgumentException e) {
        throw new ConfigurationException("Argument issue when calling the constructor of the type " + internalType, e);
    }
}
Also used : ConfigurationException(org.apache.felix.ipojo.ConfigurationException)

Example 28 with ConfigurationException

use of org.apache.felix.ipojo.ConfigurationException in project felix by apache.

the class PojoMetadataTest method getManipulationMetadataForComponent.

private PojoMetadata getManipulationMetadataForComponent(String comp_name) {
    Element elem = null;
    try {
        elem = ManifestMetadataParser.parseHeaderMetadata(header);
    } catch (ParseException e) {
        e.printStackTrace();
        fail("Parse Exception when parsing iPOJO-Component " + e);
    }
    assertNotNull("Check elem not null", elem);
    Element manip = getMetadataForComponent(elem, comp_name);
    assertNotNull("Check manipulation metadata not null for " + comp_name, manip);
    try {
        return new PojoMetadata(manip);
    } catch (ConfigurationException e) {
        fail("The creation of pojo metadata for " + comp_name + " has failed");
        return null;
    }
}
Also used : ConfigurationException(org.apache.felix.ipojo.ConfigurationException) Element(org.apache.felix.ipojo.metadata.Element)

Example 29 with ConfigurationException

use of org.apache.felix.ipojo.ConfigurationException in project felix by apache.

the class BundleContextHandler method configure.

/**
 * Configures the handler.
 * This method collects all `context` element.
 *
 * @param metadata      the metadata of the component
 * @param configuration the instance configuration
 * @throws org.apache.felix.ipojo.ConfigurationException if the metadata are not correct.
 */
@Override
public void configure(Element metadata, Dictionary configuration) throws ConfigurationException {
    Element[] contexts = metadata.getElements("context");
    for (Element element : contexts) {
        BundleContext bc = getBundleContextForConfiguration(element);
        if (element.containsAttribute("constructor-parameter")) {
            String idx = element.getAttribute("constructor-parameter");
            int index = Integer.parseInt(idx);
            final BundleContext injected = bc;
            getLogger().log(Log.DEBUG, "Registering bundle context injection for index " + index + " for instance" + " " + getInstanceManager().getInstanceName());
            getInstanceManager().register(index, new ConstructorInjector() {

                public Object getConstructorParameter(int index) {
                    return injected;
                }

                public Class getConstructorParameterType(int index) {
                    return BundleContext.class;
                }
            });
        } 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) {
                // Do nothing.
                }

                public Object onGet(Object pojo, String fieldName, Object value) {
                    return injected;
                }
            });
        } else if (element.containsAttribute("method")) {
            String method = element.getAttribute("method");
            MethodMetadata mm = getFactory().getPojoMetadata().getMethod(method, new String[] { BundleContext.class.getName() });
            if (mm == null) {
                getLogger().log(Log.WARNING, "Cannot find the method " + method + " in the class " + getInstanceManager().getClassName() + ", super classes lookup will be attempted");
            }
            Callback callback = new Callback(method, new Class[] { BundleContext.class }, false, getInstanceManager());
            m_methods.add(new BundleCallback(callback, bc));
        }
    }
}
Also used : FieldMetadata(org.apache.felix.ipojo.parser.FieldMetadata) Element(org.apache.felix.ipojo.metadata.Element) FieldInterceptor(org.apache.felix.ipojo.FieldInterceptor) Callback(org.apache.felix.ipojo.util.Callback) ConfigurationException(org.apache.felix.ipojo.ConfigurationException) ConstructorInjector(org.apache.felix.ipojo.ConstructorInjector) MethodMetadata(org.apache.felix.ipojo.parser.MethodMetadata) BundleContext(org.osgi.framework.BundleContext)

Example 30 with ConfigurationException

use of org.apache.felix.ipojo.ConfigurationException in project felix by apache.

the class DependencyConfigurationChecker method ensureThatTheConstructorParameterIsCoherent.

/**
 * Checks whether the constructor parameter injection is suitable. this check verified that the constructor has
 * enough parameter.
 * @param dependency the dependency
 * @param manipulation the manipulation metadata
 * @throws ConfigurationException if the constructor is not suitable
 */
private static void ensureThatTheConstructorParameterIsCoherent(Dependency dependency, PojoMetadata manipulation) throws ConfigurationException {
    if (dependency.getConstructorParameterIndex() != -1) {
        MethodMetadata[] constructors = manipulation.getConstructors();
        if (constructors == null || constructors.length == 0) {
            throw new ConfigurationException("The constructor parameter attribute of " + DependencyHandler.getDependencyIdentifier(dependency) + " is inconsistent - reason: there is no constructor in" + " the component class (" + dependency.getHandler().getInstanceManager().getClassName() + ")");
        }
        // TODO Consider only the first constructor. This is a limitation we should think about,
        // how to determine which constructor to use. Only one constructor should have annotations,
        // it could be use as hint.
        MethodMetadata constructor = constructors[0];
        if (!(constructor.getMethodArguments().length > dependency.getConstructorParameterIndex())) {
            throw new ConfigurationException("The constructor parameter attribute of " + DependencyHandler.getDependencyIdentifier(dependency) + " is inconsistent - reason: the constructor with the " + "signature " + Arrays.toString(constructor.getMethodArguments()) + " has not enough " + "parameters");
        }
    }
}
Also used : ConfigurationException(org.apache.felix.ipojo.ConfigurationException) MethodMetadata(org.apache.felix.ipojo.parser.MethodMetadata)

Aggregations

ConfigurationException (org.apache.felix.ipojo.ConfigurationException)34 Element (org.apache.felix.ipojo.metadata.Element)22 FieldMetadata (org.apache.felix.ipojo.parser.FieldMetadata)9 MethodMetadata (org.apache.felix.ipojo.parser.MethodMetadata)6 PojoMetadata (org.apache.felix.ipojo.parser.PojoMetadata)6 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)6 Properties (java.util.Properties)5 Filter (org.osgi.framework.Filter)5 MissingHandlerException (org.apache.felix.ipojo.MissingHandlerException)4 UnacceptableConfiguration (org.apache.felix.ipojo.UnacceptableConfiguration)4 BundleContext (org.osgi.framework.BundleContext)4 ArrayList (java.util.ArrayList)3 Comparator (java.util.Comparator)3 Dictionary (java.util.Dictionary)3 Enumeration (java.util.Enumeration)3 PropertyDescription (org.apache.felix.ipojo.architecture.PropertyDescription)3 ParseException (org.apache.felix.ipojo.parser.ParseException)3 Callback (org.apache.felix.ipojo.util.Callback)3 ComponentFactory (org.apache.felix.ipojo.ComponentFactory)2 Factory (org.apache.felix.ipojo.Factory)2