Package org.jvnet.hk2.component

Examples of org.jvnet.hk2.component.ComponentException


    public DomDocument parse(URL source, DomDocument document, Dom parent) {
        try {
            parse(xif.createXMLStreamReader(new StreamSource(source.toString())), document, parent);
            return document;
        } catch (XMLStreamException e) {
            throw new ComponentException("Failed to parse "+source,e);
        }
    }
View Full Code Here


     * @param description
     *      The description of the model as written in {@link InhabitantsFile the inhabitants file}.
     */
    public ConfigModel(DomDocument document, Inhabitant<? extends ConfigInjector> injector, MultiMap<String,String> description) {
        if(description==null)
            throw new ComponentException("%s doesn't have any metadata",injector.type());

        document.models.put(injector,this); // register now so that cyclic references are handled correctly.
        this.injector = injector;
        String targetTypeName=null,indexTypeName=null;
        String key = null;
        for (Map.Entry<String, List<String>> e : description.entrySet()) {
            String name = e.getKey();
            String value = e.getValue().size()>0 ? e.getValue().get(0) : null;
            if(name.startsWith("@")) {
                // TODO: handle value.equals("optional") and value.equals("required") distinctively.
                String attributeName = name.substring(1);
                String dv = getMetadataFieldKeyedAs(e.getValue(), "default:")//default value
                String dt = getMetadataFieldKeyedAs(e.getValue(), "datatype:"); //type
                AttributeLeaf leaf = null;
                if (dv == null) {
                    if (e.getValue().contains("reference")) {
                        leaf = new ReferenceAttributeLeaf(attributeName, dt);
                    } else {
                        leaf = new AttributeLeaf(attributeName, dt);
                    }
                } else
                    leaf = new AttributeLeafWithDefaultValue(attributeName, dt, dv);
                attributes.put(attributeName, leaf);
            } else
            if(name.startsWith("<")) {
                if (e.getValue().size()>0) {
                    String elementName = name.substring(1, name.length() - 1);
                   elements.put(elementName,parseValue(elementName,document,e.getValue()));
                }
            } else
            if(name.equals(ConfigMetadata.TARGET))
                targetTypeName = value;
            else
            if(name.equals(ConfigMetadata.KEYED_AS))
                indexTypeName = value;
            else
            if(name.equals(ConfigMetadata.KEY))
                key = value;
        }
        if(targetTypeName==null)
            throw new ComponentException("%s doesn't have the mandatory '%s' metadata", injector.type(), ConfigMetadata.TARGET);
        if(key==null ^ indexTypeName==null)
            throw new ComponentException("%s has inconsistent '%s=%s' and '%s=%s' metadata",
                ConfigMetadata.KEY, key, ConfigMetadata.TARGET, indexTypeName);
        this.targetTypeName = targetTypeName;
        this.keyedAs = indexTypeName;
        this.key = key;
        this.contracts = description.get(ConfigMetadata.TARGET_CONTRACTS);
View Full Code Here

                    "GenericCrudCommand.too_many_indexes",
                    "The metadata for this generic implementation has more than one index {0}",
                    sb.toString());
            Object[] params = new Object[] { sb.toString()};
            logger.log(Level.SEVERE, "GenericCrudCommand.too_many_indexes", params);
            throw new ComponentException(msg);
        }
        String index = indexes.get(0);
        if (index.indexOf(":")==-1) {
            String msg = localStrings.getLocalString(GenericCrudCommand.class,
                    "GenericCrudCommand.unamed_service",
                    "The service {0} is un-named, for generic command, the service name is the command name and must be provided",
                    index);
            Object[] params = new Object[] { index};
            logger.log(Level.SEVERE, "GenericCrudCommand.unamed_service", params);
            throw new ComponentException(msg);           
        }
        commandName = index.substring(index.indexOf(":")+1);
        String parentTypeName = myself.metadata().get(InhabitantsFile.TARGET_TYPE).get(0);
        if (logger.isLoggable(level)) {
            logger.log(level,"Generic method parent targeted type is " + parentTypeName);
        }

        try {
            parentType = (Class<ConfigBeanProxy>) loadClass(parentTypeName);
        } catch(ClassNotFoundException e) {
            String msg = localStrings.getLocalString(GenericCrudCommand.class,
                    "GenericCrudCommand.configbean_not_found",
                    "The Config Bean {0} cannot be loaded by the generic command implementation : {1}",
                    parentTypeName, e.getMessage());
            Object[] params = new Object[] { parentTypeName, e.getMessage()};
            logger.log(Level.SEVERE, "GenericCrudCommand.configbean_not_found",params);
            throw new ComponentException(msg, e);
        }

        // find now the accessor method.
        String methodName = myself.metadata().get("method-name").get(0);
        targetMethod=null;
        for (Method m : parentType.getMethods()) {
            if (m.getName().equals(methodName)) {
                targetMethod=m;
                break;
            }
        }

        if (targetMethod==null) {
            String msg = localStrings.getLocalString(GenericCrudCommand.class,
                    "GenericCrudCommand.configbean_not_found",
                    "The Config Bean {0} cannot be loaded by the generic command implementation : {1}",
                    parentTypeName, methodName);
            Object[] params = new Object[] { parentTypeName, methodName};
            logger.log(Level.SEVERE,"GenericCrudCommand.configbean_not_found", params);
            throw new ComponentException(msg);
        }

        if (targetMethod.getParameterTypes().length==0) {
            // return type matters.
            targetType = Types.erasure(Types.getTypeArgument(
View Full Code Here

                                    "GenericCrudCommand.invalid_type",
                                    "Invalid annotated type {0} passed to InjectionResolver:getValue()",
                                    annotated.getClass().toString());
                            Object[] params = new Object[] { annotated.getClass().toString()};
                            logger.log(Level.SEVERE, "GenericCrudCommand.invalid_type", params);
                            throw new ComponentException(msg);
                        }
                    } catch (IllegalAccessException e) {
                        String msg = localStrings.getLocalString(GenericCrudCommand.class,
                                "GenericCrudCommand.invocation_failure",
                                "Failure {0} while getting List<?> values from component",
                                e.getMessage());
                        Object[] params = new Object[] { e.getMessage()};
                        logger.log(Level.SEVERE, "GenericCrudCommand.invocation_failure", params);
                        throw new ComponentException(msg, e);
                    } catch (InvocationTargetException e) {
                        String msg = localStrings.getLocalString(GenericCrudCommand.class,
                                "GenericCrudCommand.invocation_failure",
                                "Failure {0} while getting List<?> values from component",
                                e.getMessage());
                        Object[] params = new Object[] { e.getMessage()};
                        logger.log(Level.SEVERE, "GenericCrudCommand.invocation_failure", params);
                        throw new ComponentException(msg, e);
                    }
                    Object value = delegate.getValue(component, null, annotated, genericType, type);
                    if (value==null) {
                        if (logger.isLoggable(level)) {
                            logger.log(level, "Value of " + annotated.toString() + " is null");
                        }
                        return null;
                    }
                    final Class<? extends ConfigBeanProxy> itemType = Types.erasure(Types.getTypeArgument(
                            annotated instanceof Method?
                            ((Method) annotated).getGenericReturnType():((Field) annotated).getGenericType(), 0));
                    if (logger.isLoggable(level)) {
                        logger.log(level, "Found that List<?> really is a List<" + itemType.toString() + ">");
                    }
                    if (itemType==null) {
                            String msg = localStrings.getLocalString(GenericCrudCommand.class,
                                    "GenericCrudCommand.nongeneric_type",
                                    "The List type returned by {0} must be a generic type",
                                    annotated.toString());
                            Object[] params = new Object[] {annotated.toString()};
                            logger.log(Level.SEVERE, "GenericCrudCommand.nongeneric_type", params);
                            throw new ComponentException(msg);
                    }
                    if (!ConfigBeanProxy.class.isAssignableFrom(itemType)) {
                        String msg = localStrings.getLocalString(GenericCrudCommand.class,
                                "GenericCrudCommand.wrong_type",
                                "The generic type {0} is not supported, only List<? extends ConfigBeanProxy> is",
                                annotated.toString());
                        Object[] params = new Object[] { annotated.toString()};
                        logger.log(Level.SEVERE, "GenericCrudCommand.wrong_type", params);
                        throw new ComponentException(msg);
                       
                    }
                    Properties props = convertStringToProperties(value.toString(), ':');
                    if (logger.isLoggable(level)) {
                        for (Map.Entry<Object, Object> entry : props.entrySet()) {
                            logger.log(level, "Subtype " + itemType + " key:" + entry.getKey() + " value:" + entry.getValue());
                        }
                    }
                    final BeanInfo beanInfo;
                    try {
                        beanInfo = Introspector.getBeanInfo(itemType);
                    } catch (IntrospectionException e) {
                        String msg = localStrings.getLocalString(GenericCrudCommand.class,
                                "GenericCrudCommand.introspection_failure",
                                "Failure {0} while instrospecting {1} to find all getters and setters",
                                e.getMessage(), itemType.getName());
                        Object[] params = new Object[] { e.getMessage(), itemType.getName()};
                        logger.log(Level.SEVERE, "GenericCrudCommand.introspection_failure", params);
                        throw new ComponentException(msg, e);
                    }
                    for (final Map.Entry<Object, Object> entry : props.entrySet()) {
                        ConfigBeanProxy child = (ConfigBeanProxy) component;
                        try {
                            ConfigBeanProxy cc = child.createChild(itemType);
                            new InjectionManager().inject(cc, itemType, new InjectionResolver<Attribute>(Attribute.class) {

                                @Override
                                public boolean isOptional(AnnotatedElement annotated, Attribute annotation) {
                                    return true;   
                                }

                                @Override
                                public Method getSetterMethod(Method annotated, Attribute annotation) {
                                    // Attribute annotation are always annotated on the getter, we need to find the setter
                                    // variant.
                                    for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
                                        if (pd.getReadMethod().equals(annotated)) {
                                            return pd.getWriteMethod();
                                        }
                                    }
                                    return annotated;
                                }

                                @Override
                                public <V> V getValue(Object component, Inhabitant<?> onBehalfOf, AnnotatedElement annotated, Type genericType, Class<V> type) throws ComponentException {
                                    String name = annotated.getAnnotation(Attribute.class).value();
                                    if (name==null || name.length()==0) {

                                        // maybe there is a better way to do this...
                                        name = ((Method) annotated).getName().substring(3);

                                        if (name.equalsIgnoreCase("name") || name.equalsIgnoreCase("key")) {
                                            return type.cast(entry.getKey());
                                        }
                                        if (name.equalsIgnoreCase("value")) {
                                            return type.cast(entry.getValue());
                                        }
                                    }
                                    return null;
                                }
                            });
                            values.add(cc);
                        } catch (TransactionFailure transactionFailure) {
                            String msg = localStrings.getLocalString(GenericCrudCommand.class,
                                "GenericCrudCommand.transactionException",
                                "Transaction exception {0} while injecting {1}",
                                transactionFailure.getMessage(), itemType);
                            Object[] params = new Object[] { transactionFailure.getMessage(), itemType};
                            logger.log(Level.SEVERE, "GenericCrudCommand.transactionException", params);
                            throw new ComponentException(msg, transactionFailure);
                        }

                    }
                    return null;
                }
View Full Code Here

            habitat.initialized();
           
            return habitat;
        } catch (Exception e) {
            throw new ComponentException("Failed to create a habitat",e);
        }
    }
View Full Code Here

                    "GenericCrudCommand.too_many_indexes",
                    "The metadata for this generic implementation has more than one index {0}",
                    sb.toString());
            Object[] params = new Object[] { sb.toString()};
            logger.log(Level.SEVERE, "GenericCrudCommand.too_many_indexes", params);
            throw new ComponentException(msg);
        }
        String index = indexes.get(0);
        if (index.indexOf(":")==-1) {
            String msg = localStrings.getLocalString(GenericCrudCommand.class,
                    "GenericCrudCommand.unamed_service",
                    "The service {0} is un-named, for generic command, the service name is the command name and must be provided",
                    index);
            Object[] params = new Object[] { index};
            logger.log(Level.SEVERE, "GenericCrudCommand.unamed_service", params);
            throw new ComponentException(msg);           
        }
        commandName = index.substring(index.indexOf(":")+1);
        String parentTypeName = myself.metadata().get(InhabitantsFile.TARGET_TYPE).get(0);
        if (logger.isLoggable(level)) {
            logger.log(level,"Generic method parent targeted type is " + parentTypeName);
        }

        try {
            parentType = (Class<ConfigBeanProxy>) loadClass(parentTypeName);
        } catch(ClassNotFoundException e) {
            String msg = localStrings.getLocalString(GenericCrudCommand.class,
                    "GenericCrudCommand.configbean_not_found",
                    "The Config Bean {0} cannot be loaded by the generic command implementation : {1}",
                    parentTypeName, e.getMessage());
            Object[] params = new Object[] { parentTypeName, e.getMessage()};
            logger.log(Level.SEVERE, "GenericCrudCommand.configbean_not_found",params);
            throw new ComponentException(msg, e);
        }

        // find now the accessor method.
        String methodName = myself.metadata().get("method-name").get(0);
        targetMethod=null;
        methodlookup:
        for (Method m : parentType.getMethods()) {
            if (m.getName().equals(methodName)) {
                // Make sure that this method is annotated with an annotation
                // that is annotated with InhabitantAnnotation (such as @Create).
                // This makes sure that we have found the method we are looking for
                // in case there is a like-named method that is not annotated.
                for (Annotation a : m.getAnnotations()) {
                    if (a.annotationType().getAnnotation(InhabitantAnnotation.class) != null) {
                        targetMethod=m;
                        break methodlookup;
                    }
                }
            }
        }

        if (targetMethod==null) {
            String msg = localStrings.getLocalString(GenericCrudCommand.class,
                    "GenericCrudCommand.configbean_not_found",
                    "The Config Bean {0} cannot be loaded by the generic command implementation : {1}",
                    parentTypeName, methodName);
            Object[] params = new Object[] { parentTypeName, methodName};
            logger.log(Level.SEVERE,"GenericCrudCommand.configbean_not_found", params);
            throw new ComponentException(msg);
        }

        if (targetMethod.getParameterTypes().length==0) {
            // return type matters.
            targetType = Types.erasure(Types.getTypeArgument(
View Full Code Here

                                    "GenericCrudCommand.invalid_type",
                                    "Invalid annotated type {0} passed to InjectionResolver:getValue()",
                                    annotated.getClass().toString());
                            Object[] params = new Object[] { annotated.getClass().toString()};
                            logger.log(Level.SEVERE, "GenericCrudCommand.invalid_type", params);
                            throw new ComponentException(msg);
                        }
                    } catch (IllegalAccessException e) {
                        String msg = localStrings.getLocalString(GenericCrudCommand.class,
                                "GenericCrudCommand.invocation_failure",
                                "Failure {0} while getting List<?> values from component",
                                e.getMessage());
                        Object[] params = new Object[] { e.getMessage()};
                        logger.log(Level.SEVERE, "GenericCrudCommand.invocation_failure", params);
                        throw new ComponentException(msg, e);
                    } catch (InvocationTargetException e) {
                        String msg = localStrings.getLocalString(GenericCrudCommand.class,
                                "GenericCrudCommand.invocation_failure",
                                "Failure {0} while getting List<?> values from component",
                                e.getMessage());
                        Object[] params = new Object[] { e.getMessage()};
                        logger.log(Level.SEVERE, "GenericCrudCommand.invocation_failure", params);
                        throw new ComponentException(msg, e);
                    }
                    Object value = delegate.getValue(component, null, annotated, genericType, type);
                    if (value==null) {
                        if (logger.isLoggable(level)) {
                            logger.log(level, "Value of " + annotated.toString() + " is null");
                        }
                        return null;
                    }
                    final Class<? extends ConfigBeanProxy> itemType = Types.erasure(Types.getTypeArgument(
                            annotated instanceof Method?
                            ((Method) annotated).getGenericReturnType():((Field) annotated).getGenericType(), 0));
                    if (logger.isLoggable(level)) {
                        logger.log(level, "Found that List<?> really is a List<" + itemType.toString() + ">");
                    }
                    if (itemType==null) {
                            String msg = localStrings.getLocalString(GenericCrudCommand.class,
                                    "GenericCrudCommand.nongeneric_type",
                                    "The List type returned by {0} must be a generic type",
                                    annotated.toString());
                            Object[] params = new Object[] {annotated.toString()};
                            logger.log(Level.SEVERE, "GenericCrudCommand.nongeneric_type", params);
                            throw new ComponentException(msg);
                    }
                    if (!ConfigBeanProxy.class.isAssignableFrom(itemType)) {
                        String msg = localStrings.getLocalString(GenericCrudCommand.class,
                                "GenericCrudCommand.wrong_type",
                                "The generic type {0} is not supported, only List<? extends ConfigBeanProxy> is",
                                annotated.toString());
                        Object[] params = new Object[] { annotated.toString()};
                        logger.log(Level.SEVERE, "GenericCrudCommand.wrong_type", params);
                        throw new ComponentException(msg);
                       
                    }
                    Properties props = convertStringToProperties(value.toString(), ':');
                    if (logger.isLoggable(level)) {
                        for (Map.Entry<Object, Object> entry : props.entrySet()) {
                            logger.log(level, "Subtype " + itemType + " key:" + entry.getKey() + " value:" + entry.getValue());
                        }
                    }
                    final BeanInfo beanInfo;
                    try {
                        beanInfo = Introspector.getBeanInfo(itemType);
                    } catch (IntrospectionException e) {
                        String msg = localStrings.getLocalString(GenericCrudCommand.class,
                                "GenericCrudCommand.introspection_failure",
                                "Failure {0} while instrospecting {1} to find all getters and setters",
                                e.getMessage(), itemType.getName());
                        Object[] params = new Object[] { e.getMessage(), itemType.getName()};
                        logger.log(Level.SEVERE, "GenericCrudCommand.introspection_failure", params);
                        throw new ComponentException(msg, e);
                    }
                    for (final Map.Entry<Object, Object> entry : props.entrySet()) {
                        ConfigBeanProxy child = (ConfigBeanProxy) component;
                        try {
                            ConfigBeanProxy cc = child.createChild(itemType);
                            new InjectionManager().inject(cc, itemType, new InjectionResolver<Attribute>(Attribute.class) {

                                @Override
                                public boolean isOptional(AnnotatedElement annotated, Attribute annotation) {
                                    return true;   
                                }

                                @Override
                                public Method getSetterMethod(Method annotated, Attribute annotation) {
                                    // Attribute annotation are always annotated on the getter, we need to find the setter
                                    // variant.
                                    for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
                                        if (pd.getReadMethod().equals(annotated)) {
                                            return pd.getWriteMethod();
                                        }
                                    }
                                    return annotated;
                                }

                                @Override
                                public <V> V getValue(Object component, Inhabitant<?> onBehalfOf, AnnotatedElement annotated, Type genericType, Class<V> type) throws ComponentException {
                                    String name = annotated.getAnnotation(Attribute.class).value();
                                    if (name==null || name.length()==0) {

                                        // maybe there is a better way to do this...
                                        name = ((Method) annotated).getName().substring(3);

                                        if (name.equalsIgnoreCase("name") || name.equalsIgnoreCase("key")) {
                                            return type.cast(entry.getKey());
                                        }
                                        if (name.equalsIgnoreCase("value")) {
                                            return type.cast(entry.getValue());
                                        }
                                    }
                                    return null;
                                }
                            });
                            values.add(cc);
                        } catch (TransactionFailure transactionFailure) {
                            String msg = localStrings.getLocalString(GenericCrudCommand.class,
                                "GenericCrudCommand.transactionException",
                                "Transaction exception {0} while injecting {1}",
                                transactionFailure.getMessage(), itemType);
                            Object[] params = new Object[] { transactionFailure.getMessage(), itemType};
                            logger.log(Level.SEVERE, "GenericCrudCommand.transactionException", params);
                            throw new ComponentException(msg, transactionFailure);
                        }

                    }
                    return null;
                }
View Full Code Here

            String msg = localStrings.getLocalString(GenericCrudCommand.class,
                    "GenericCreateCommand.command_model_exception",
                    "Exception while creating the command model for the generic command {0} : {1}",
                    commandName, e.getMessage());
            logger.severe(msg);
            throw new ComponentException(msg, e);

        }

    }
View Full Code Here

                    "GenericCrudCommand.configbean_not_found",
                    "The Config Bean {0} cannot be loaded by the generic command implementation : {1}",
                    parentTypeName, e.getMessage());
            Object[] params = new Object[] { parentTypeName, e.getMessage()};
            logger.log(Level.SEVERE, "GenericCrudCommand.configbean_not_found",params);
            throw new ComponentException(msg, e);
        }

        // find now the accessor method.
        String methodName = getOne(GenerateServiceFromMethod.METHOD_NAME, myself.getMetadata());
        targetMethod=null;
        methodlookup:
        for (Method m : parentType.getMethods()) {
            if (m.getName().equals(methodName)) {
                // Make sure that this method is annotated with an annotation
                // that is annotated with InhabitantAnnotation (such as @Create).
                // This makes sure that we have found the method we are looking for
                // in case there is a like-named method that is not annotated.
                for (Annotation a : m.getAnnotations()) {
                    if (a.annotationType().getAnnotation(GenerateServiceFromMethod.class) != null) {
                        targetMethod=m;
                        break methodlookup;
                    }
                }
            }
        }

        if (targetMethod==null) {
            String msg = localStrings.getLocalString(GenericCrudCommand.class,
                    "GenericCrudCommand.configbean_not_found",
                    "The Config Bean {0} cannot be loaded by the generic command implementation : {1}",
                    parentTypeName, methodName);
            Object[] params = new Object[] { parentTypeName, methodName};
            logger.log(Level.SEVERE,"GenericCrudCommand.configbean_not_found", params);
            throw new ComponentException(msg);
        }
       
        String targetTypeName = getOne(GenerateServiceFromMethod.METHOD_ACTUAL, myself.getMetadata());
        try {
            targetType = loadClass(targetTypeName);
        }
        catch(ClassNotFoundException e) {
            String msg = localStrings.getLocalString(GenericCrudCommand.class,
                    "GenericCrudCommand.configbean_not_found",
                    "The Config Bean {0} cannot be loaded by the generic command implementation : {1}",
                    targetTypeName, e.getMessage());
            Object[] params = new Object[] { targetTypeName, e.getMessage()};
            logger.log(Level.SEVERE, "GenericCrudCommand.configbean_not_found",params);
            throw new ComponentException(msg, e);
        }
    }
View Full Code Here

                                    "GenericCrudCommand.invalid_type",
                                    "Invalid annotated type {0} passed to InjectionResolver:getValue()",
                                    annotated.getClass().toString());
                            Object[] params = new Object[] { annotated.getClass().toString()};
                            logger.log(Level.SEVERE, "GenericCrudCommand.invalid_type", params);
                            throw new ComponentException(msg);
                        }
                    } catch (IllegalAccessException e) {
                        String msg = localStrings.getLocalString(GenericCrudCommand.class,
                                "GenericCrudCommand.invocation_failure",
                                "Failure {0} while getting List<?> values from component",
                                e.getMessage());
                        Object[] params = new Object[] { e.getMessage()};
                        logger.log(Level.SEVERE, "GenericCrudCommand.invocation_failure", params);
                        throw new ComponentException(msg, e);
                    } catch (InvocationTargetException e) {
                        String msg = localStrings.getLocalString(GenericCrudCommand.class,
                                "GenericCrudCommand.invocation_failure",
                                "Failure {0} while getting List<?> values from component",
                                e.getMessage());
                        Object[] params = new Object[] { e.getMessage()};
                        logger.log(Level.SEVERE, "GenericCrudCommand.invocation_failure", params);
                        throw new ComponentException(msg, e);
                    }
                    Object value = delegate.getValue(component, annotated, genericType, type);
                    if (value==null) {
                        if (logger.isLoggable(level)) {
                            logger.log(level, "Value of " + annotated.toString() + " is null");
                        }
                        return null;
                    }
                    Type genericReturnType=null;
                    if (annotated instanceof Method) {
                        genericReturnType = ((Method) annotated).getGenericReturnType();
                    } else if (annotated instanceof Field) {
                        genericReturnType = ((Field) annotated).getGenericType();
                    }
                    if (genericReturnType==null) {
                        throw new ComponentException("Cannot determine parametized type from " + annotated.toString());
                    }

                    final Class<? extends ConfigBeanProxy> itemType = Types.erasure(Types.getTypeArgument(genericReturnType, 0));
                    if (logger.isLoggable(level)) {
                        logger.log(level, "Found that List<?> really is a List<" + itemType.toString() + ">");
                    }
                    if (itemType==null) {
                            String msg = localStrings.getLocalString(GenericCrudCommand.class,
                                    "GenericCrudCommand.nongeneric_type",
                                    "The List type returned by {0} must be a generic type",
                                    annotated.toString());
                            Object[] params = new Object[] {annotated.toString()};
                            logger.log(Level.SEVERE, "GenericCrudCommand.nongeneric_type", params);
                            throw new ComponentException(msg);
                    }
                    if (!ConfigBeanProxy.class.isAssignableFrom(itemType)) {
                        String msg = localStrings.getLocalString(GenericCrudCommand.class,
                                "GenericCrudCommand.wrong_type",
                                "The generic type {0} is not supported, only List<? extends ConfigBeanProxy> is",
                                annotated.toString());
                        Object[] params = new Object[] { annotated.toString()};
                        logger.log(Level.SEVERE, "GenericCrudCommand.wrong_type", params);
                        throw new ComponentException(msg);
                       
                    }
                    Properties props = convertStringToProperties(value.toString(), ':');
                    if (logger.isLoggable(level)) {
                        for (Map.Entry<Object, Object> entry : props.entrySet()) {
                            logger.log(level, "Subtype " + itemType + " key:" + entry.getKey() + " value:" + entry.getValue());
                        }
                    }
                    final BeanInfo beanInfo;
                    try {
                        beanInfo = Introspector.getBeanInfo(itemType);
                    } catch (IntrospectionException e) {
                        String msg = localStrings.getLocalString(GenericCrudCommand.class,
                                "GenericCrudCommand.introspection_failure",
                                "Failure {0} while instrospecting {1} to find all getters and setters",
                                e.getMessage(), itemType.getName());
                        Object[] params = new Object[] { e.getMessage(), itemType.getName()};
                        logger.log(Level.SEVERE, "GenericCrudCommand.introspection_failure", params);
                        throw new ComponentException(msg, e);
                    }
                    for (final Map.Entry<Object, Object> entry : props.entrySet()) {
                        ConfigBeanProxy child = (ConfigBeanProxy) component;
                        try {
                            ConfigBeanProxy cc = child.createChild(itemType);
                            new InjectionManager().inject(cc, itemType, new InjectionResolver<Attribute>(Attribute.class) {

                                @Override
                                public boolean isOptional(AnnotatedElement annotated, Attribute annotation) {
                                    return true;   
                                }

                                @Override
                                public Method getSetterMethod(Method annotated, Attribute annotation) {
                                    // Attribute annotation are always annotated on the getter, we need to find the setter
                                    // variant.
                                    for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
                                        if (pd.getReadMethod().equals(annotated)) {
                                            return pd.getWriteMethod();
                                        }
                                    }
                                    return annotated;
                                }

                                @Override
                                public <V> V getValue(Object component, AnnotatedElement annotated, Type genericType, Class<V> type) throws ComponentException {
                                    String name = annotated.getAnnotation(Attribute.class).value();
                                    if ((name==null || name.length()==0) && annotated instanceof Method) {

                                        // maybe there is a better way to do this...
                                        name = ((Method) annotated).getName().substring(3);

                                        if (name.equalsIgnoreCase("name") || name.equalsIgnoreCase("key")) {
                                            return type.cast(entry.getKey());
                                        }
                                        if (name.equalsIgnoreCase("value")) {
                                            return type.cast(entry.getValue());
                                        }
                                    }
                                    return null;
                                }
                            });
                            values.add(cc);
                        } catch (TransactionFailure transactionFailure) {
                            String msg = localStrings.getLocalString(GenericCrudCommand.class,
                                "GenericCrudCommand.transactionException",
                                "Transaction exception {0} while injecting {1}",
                                transactionFailure.getMessage(), itemType);
                            Object[] params = new Object[] { transactionFailure.getMessage(), itemType};
                            logger.log(Level.SEVERE, "GenericCrudCommand.transactionException", params);
                            throw new ComponentException(msg, transactionFailure);
                        }

                    }
                    return null;
                }
View Full Code Here

TOP

Related Classes of org.jvnet.hk2.component.ComponentException

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.