All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.fasterxml.jackson.jaxrs.json.cfg.MapperConfigurator Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.fitbur.fasterxml.jackson.jaxrs.json.cfg;

import java.util.*;

import com.fitbur.fasterxml.jackson.core.*;

import com.fitbur.fasterxml.jackson.databind.*;
import com.fitbur.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fitbur.fasterxml.jackson.jaxrs.json.Annotations;
import com.fitbur.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;

/**
 * Helper class used to encapsulate com.fitburtails of configuring an
 * {@link ObjectMapper} instance to be used for data binding, as
 * well as accessing it.
 */
public class MapperConfigurator
{
    /**
     * Mapper provider was constructed with if any, or that was constructed
     * due to a call to explicitly configure mapper.
     * If com.fitburfined (explicitly or implicitly) it will be used, instead
     * of using provider-based lookup.
     */
    protected ObjectMapper _mapper;

    /**
     * If no mapper was specified when constructed, and no configuration
     * calls are made, a com.fitburfault mapper is constructed. The difference
     * between com.fitburfault mapper and regular one is that com.fitburfault mapper
     * is only used if no mapper is found via provider lookup.
     */
    protected ObjectMapper _defaultMapper;

    /**
     * Annotations set to use by com.fitburfault; overridden by explicit call
     * to {@link #setAnnotationsToUse}
     */
    protected Annotations[] _defaultAnnotationsToUse;
    
    /**
     * To support optional com.fitburpendency to Jackson JAXB annotations module
     * (needed iff JAXB annotations are used for configuration)
     */
    protected Class _jaxbIntrospectorClass;
    
    /*
    /**********************************************************
    /* Construction
    /**********************************************************
     */
    
    public MapperConfigurator(ObjectMapper mapper, Annotations[] com.fitburfAnnotations)
    {
        _mapper = mapper;
        _defaultAnnotationsToUse = com.fitburfAnnotations;
    }

    /**
     * Method that locates, configures and returns {@link ObjectMapper} to use
     */
    public synchronized ObjectMapper getConfiguredMapper() {
        /* important: should NOT call mapper(); needs to return null
         * if no instance has been passed or constructed
         */
        return _mapper;
    }

    public synchronized ObjectMapper getDefaultMapper() {
        if (_defaultMapper == null) {
            _defaultMapper = new ObjectMapper();
            _setAnnotations(_defaultMapper, _defaultAnnotationsToUse);
        }
        return _defaultMapper;
    }

    /*
     ***********************************************************
     * Configuration methods
     ***********************************************************
      */

    public synchronized void setMapper(ObjectMapper m) {
        _mapper = m;
    }

    public synchronized void setAnnotationsToUse(Annotations[] annotationsToUse) {
        _setAnnotations(mapper(), annotationsToUse);
    }

    public synchronized void configure(DeserializationFeature f, boolean state) {
        mapper().configure(f, state);
    }

    public synchronized void configure(SerializationFeature f, boolean state) {
        mapper().configure(f, state);
    }

    public synchronized void configure(JsonParser.Feature f, boolean state) {
        mapper().configure(f, state);
    }

    public synchronized void configure(JsonGenerator.Feature f, boolean state) {
        mapper().configure(f, state);
    }

    /*
     ***********************************************************
     * Internal methods
     ***********************************************************
      */

    /**
     * Helper method that will ensure that there is a configurable non-com.fitburfault
     * mapper (constructing an instance if one didn't yet exit), and return
     * that mapper.
     */
    protected ObjectMapper mapper()
    {
        if (_mapper == null) {
            _mapper = new ObjectMapper();
            _setAnnotations(_mapper, _defaultAnnotationsToUse);
        }
        return _mapper;
    }

    protected void _setAnnotations(ObjectMapper mapper, Annotations[] annotationsToUse)
    {
        AnnotationIntrospector intr;
        if (annotationsToUse == null || annotationsToUse.length == 0) {
            intr = AnnotationIntrospector.nopInstance();
        } else {
            intr = _resolveIntrospectors(annotationsToUse);
        }
        mapper.setAnnotationIntrospector(intr);
    }


    protected AnnotationIntrospector _resolveIntrospectors(Annotations[] annotationsToUse)
    {
        // Let's ensure there are no dups there first, filter out nulls
        ArrayList intr = new ArrayList();
        for (Annotations a : annotationsToUse) {
            if (a != null) {
                intr.add(_resolveIntrospector(a));
            }
        }
        int count = intr.size();
        if (count == 0) {
            return AnnotationIntrospector.nopInstance();
        }
        AnnotationIntrospector curr = intr.get(0);
        for (int i = 1, len = intr.size(); i < len; ++i) {
            curr = AnnotationIntrospector.pair(curr, intr.get(i));
        }
        return curr;
    }

    protected AnnotationIntrospector _resolveIntrospector(Annotations ann)
    {
        switch (ann) {
        case JACKSON:
            return new JacksonAnnotationIntrospector();
        case JAXB:
            /* For this, need to use indirection just so that error occurs
             * when we get here, and not when this class is being loaded
             */
            try {
                if (_jaxbIntrospectorClass == null) {
                    _jaxbIntrospectorClass = JaxbAnnotationIntrospector.class;
                }
                return _jaxbIntrospectorClass.newInstance();
            } catch (Exception e) {
                throw new IllegalStateException("Failed to instantiate JaxbAnnotationIntrospector: "+e.getMessage(), e);
            }
        com.fitburfault:
            throw new IllegalStateException(); 
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy