Search in sources :

Example 1 with Constraint

use of com.att.cdp.openstack.heat.model.Constraint in project AJSC by att.

the class Unmarshaller method determineImplementationClass.

/**
 * This method checks the specified bean property to locate the implementation class to be used. If the
 * implementation class supports polymorphic mappings, then the implementation class is further inspected for
 * JsonTypeInfo annotations. If the annotation is present, then the annotation directs the processing. Because the
 * processing of the annotation may be data-dependent, the mapped data to be unmarshalled must be provided as well.
 *
 * @param beanProperty
 *            The bean property we are mapping the contents into
 * @param obj
 *            The object that contains the mappings
 * @return The class to be constructed to contain the mappings
 * @throws UnmarshallException
 *             If the json type info uses unsupported metadata determination
 */
@SuppressWarnings("unchecked")
private Class<? extends ModelObject> determineImplementationClass(BeanProperty beanProperty, Object obj) throws UnmarshallException {
    Class<? extends ModelObject> implClass = null;
    JsonTypeInfo typeInfo = null;
    JsonSubTypes subTypes = null;
    Class<?> defaultImplClass = null;
    /*
         * First, try to determine the implementation class to be created based on the bean property type. If the bean
         * property is a scalar type, then use it as is. If it is a list, then use the first generic type. If it is a
         * map, use the second generic type (maps are always assumed to be keyed by strings).
         */
    implClass = (Class<? extends ModelObject>) beanProperty.getPropertyType();
    if (beanProperty.isList()) {
        implClass = (Class<? extends ModelObject>) beanProperty.getCollectionTypes()[0];
    } else if (beanProperty.isMap()) {
        implClass = (Class<? extends ModelObject>) beanProperty.getCollectionTypes()[1];
    }
    /*
         * HACK: If the implClass is Constraint, then we have a special case. We need to examine the first key in the
         * map to determine the type of constraint and return that name.
         */
    if (implClass.equals(Constraint.class)) {
        if (obj instanceof List) {
            Map<String, Object> map = (Map<String, Object>) ((List<Object>) obj).get(0);
            String constraintType = (String) map.keySet().toArray()[0];
            for (int index = 0; index < CONSTRAINT_TYPES.length; index++) {
                if (CONSTRAINT_TYPES[index].equals(constraintType)) {
                    implClass = CONSTRAINT_CLASSES[index];
                    return implClass;
                }
            }
        }
    }
    /*
         * If typeInfo annotation is present on the property type class, then check it to get the actual implementation
         * class. Otherwise, we will instantiate the property defined.
         */
    typeInfo = (JsonTypeInfo) ObjectHelper.getClassAnnotation(implClass, JsonTypeInfo.class);
    if (typeInfo != null) {
        subTypes = (JsonSubTypes) ObjectHelper.getClassAnnotation(implClass, JsonSubTypes.class);
        defaultImplClass = typeInfo.defaultImpl();
        JsonTypeInfo.Id use = typeInfo.use();
        JsonTypeInfo.As include = typeInfo.include();
        if (use.equals(JsonTypeInfo.Id.NAME) && include.equals(JsonTypeInfo.As.PROPERTY)) {
            if (obj instanceof Map) {
                Map<String, Object> map = (Map<String, Object>) obj;
                String property = typeInfo.property();
                String propertyValue = (String) map.get(property);
                implClass = (Class<? extends ModelObject>) defaultImplClass;
                if (propertyValue != null) {
                    JsonSubTypes.Type[] types = subTypes.value();
                    for (JsonSubTypes.Type type : types) {
                        if (type.name().equals(propertyValue)) {
                            implClass = (Class<? extends ModelObject>) type.value();
                            break;
                        }
                    }
                }
            }
        } else if (use.equals(JsonTypeInfo.Id.CUSTOM)) {
            JsonDeserialize deserializeAnnotation = (JsonDeserialize) ObjectHelper.getClassAnnotation(implClass, JsonDeserialize.class);
            Class<? extends JsonDeserializer> deserializer = deserializeAnnotation.using();
        } else {
            throw new UnmarshallException(String.format("Only JsonTypeInfo use=\"NAME\" and include=\"PROPERTY\" " + " or use=\"CUSTOM\" with custom deserializer are supported.  The mapping specified " + "use=\"%s\" and include=\"%s\"", use.name(), include.name()));
        }
    }
    return implClass;
}
Also used : JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) PatternConstraint(com.att.cdp.openstack.heat.model.PatternConstraint) RangeConstraint(com.att.cdp.openstack.heat.model.RangeConstraint) Constraint(com.att.cdp.openstack.heat.model.Constraint) ValuesConstraint(com.att.cdp.openstack.heat.model.ValuesConstraint) CustomConstraint(com.att.cdp.openstack.heat.model.CustomConstraint) LengthConstraint(com.att.cdp.openstack.heat.model.LengthConstraint) ModelObject(com.att.cdp.openstack.heat.model.ModelObject) ArrayList(java.util.ArrayList) List(java.util.List) ModelObject(com.att.cdp.openstack.heat.model.ModelObject) UnmarshallException(com.att.cdp.openstack.exception.UnmarshallException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

UnmarshallException (com.att.cdp.openstack.exception.UnmarshallException)1 Constraint (com.att.cdp.openstack.heat.model.Constraint)1 CustomConstraint (com.att.cdp.openstack.heat.model.CustomConstraint)1 LengthConstraint (com.att.cdp.openstack.heat.model.LengthConstraint)1 ModelObject (com.att.cdp.openstack.heat.model.ModelObject)1 PatternConstraint (com.att.cdp.openstack.heat.model.PatternConstraint)1 RangeConstraint (com.att.cdp.openstack.heat.model.RangeConstraint)1 ValuesConstraint (com.att.cdp.openstack.heat.model.ValuesConstraint)1 JsonSubTypes (com.fasterxml.jackson.annotation.JsonSubTypes)1 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)1 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)1 JsonDeserialize (com.fasterxml.jackson.databind.annotation.JsonDeserialize)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1