View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.json.transformers;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.config.transformer.AbstractAnnotatedTransformerArgumentResolver;
11  
12  import org.codehaus.jackson.map.ObjectMapper;
13  
14  /**
15   * This resolver is used by the transform engine to inject a {@link org.codehaus.jackson.map.ObjectMapper} into a method that requires it.
16   * A shared ObjectMapper context can be created for the application and stored in the registry, this will get injected
17   * into any transform methods that add {@link org.codehaus.jackson.map.ObjectMapper} to the method signature.
18   * <p/>
19   * If there is no shared Object Mapper one will be created for the transformer using the return type as the Json root element.
20   *
21   * @since 3.0
22   */
23  public class JsonMapperResolver extends AbstractAnnotatedTransformerArgumentResolver
24  {
25      public static final String ANNOTATIONS_PACKAGE_NAME = "org.codehaus.jackson";
26  
27      /**
28       * {@inheritDoc}
29       */
30      @Override
31      protected Class<?> getArgumentClass()
32      {
33          return ObjectMapper.class;
34      }
35  
36      /**
37       * {@inheritDoc}
38       */
39      @Override
40      protected Object createArgument(Class<?> annotatedType, MuleContext context) throws Exception
41      {
42          return new ObjectMapper();
43      }
44  
45      /**
46       * {@inheritDoc}
47       */
48      @Override
49      protected String getAnnotationsPackageName()
50      {
51          return ANNOTATIONS_PACKAGE_NAME;
52      }
53  }