Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package de.zalando.sprocwrapper.globalobjecttransformer;
  
  import java.util.HashMap;
  import java.util.Map;
  import java.util.Set;
  
  
 
 
 
 
 
 
 
     private static final Logger LOG = LoggerFactory.getLogger(GlobalObjectTransformerLoader.class);
 
     private static final String DEFAULT_NAMESPACE = "de.zalando";
 
     private static final String GLOBAL_OBJECT_TRANSFORMER_SEARCH_NAMESPACE =
         "global.object.transformer.search.namespace";
 
     // you need to set the namespace to a valid value like: org.doodlejump
     private static volatile String namespaceToScan = null;
     private static volatile ImmutableMap<Class<?>, ObjectMapper<?>> register;
 
     public static <T> ObjectMapper<T> getObjectMapperForClass(final Class<T> genericTypethrows InstantiationException,
         IllegalAccessException {
         Preconditions.checkNotNull(genericType"genericType");
 
         // performance improvement. Volatile field is read only once in the commons scenario.
         ImmutableMap<Class<?>, ObjectMapper<?>> localRegister = ;
         if (localRegister == null) {
 
             synchronized (GlobalObjectTransformerLoader.class) {
                 localRegister = ;
                 if (localRegister == null) {
                      = localRegister = buildMappers();
                 }
             }
         }
 
         return (ObjectMapper<T>) localRegister.get(genericType);
     }
 
     private static ImmutableMap<Class<?>, ObjectMapper<?>> buildMappers() throws InstantiationException,
         IllegalAccessException {
 
         final Map<Class<?>, ObjectMapper<?>> mappers = new HashMap<>();
 
         for (final Class<?> foundGlobalObjectTransformer : findObjectMappers()) {
 
             if (ObjectMapper.class.isAssignableFrom(foundGlobalObjectTransformer)) {
 
                 final ObjectMapper<?> mapper = (ObjectMapper<?>) foundGlobalObjectTransformer.newInstance();
                 final Class<?> valueTransformerReturnType = mapper.getType();
 
                 final ObjectMapper<?> previousMapper = mappers.put(valueTransformerReturnTypemapper);
 
                 if (previousMapper == null) {
                     .debug("Global Object Mapper [{}] for type [{}] registered. ",
                         foundGlobalObjectTransformer.getSimpleName(), valueTransformerReturnType.getSimpleName());
                 } else {
                     .error("Found multiple global object mappers for type [{}]. [{}] replaced by [{}]",
                         new Object[] {
                             valueTransformerReturnTypepreviousMapper.getClass().getSimpleName(),
                             valueTransformerReturnType.getSimpleName()
                         });
                 }
             } else {
                 .error("Object mapper [{}] should extend [{}]"foundGlobalObjectTransformer.getSimpleName(),
                     ObjectMapper.class.getSimpleName());
             }
         }
 
         return ImmutableMap.copyOf(mappers);
 
     }
 
     private static Set<Class<?>> findObjectMappers() {
         final Predicate<Stringfilter = new Predicate<String>() {
             @Override
             public boolean apply(final String input) {
                 return GlobalObjectMapper.class.getCanonicalName().equals(input);
             }
         };
        final String myNameSpaceToScan = getNameSpace();
        final Reflections reflections = new Reflections(new ConfigurationBuilder().filterInputsBy(
                    new FilterBuilder.Include(FilterBuilder.prefix(myNameSpaceToScan))).setUrls(
                    ClasspathHelper.forPackage(myNameSpaceToScan)).setScanners(new TypeAnnotationsScanner()
                        .filterResultsBy(filter), new SubTypesScanner()));
        final Set<Class<?>> objectMapper = reflections.getTypesAnnotatedWith(GlobalObjectMapper.class);
        return objectMapper;
    }
    private static String getNameSpace() {
        String myNameSpaceToScan = ;
        if (myNameSpaceToScan == null) {
            try {
                myNameSpaceToScan = System.getenv();
            } catch (final Exception e) {
                // ignore - e.g. if a security manager exists and permissions are denied.
                .warn("Could not get the value of environment variable: {}. Using: {}",
                    new Object[] {e});
            }
            if (myNameSpaceToScan == null) {
                myNameSpaceToScan = ;
            }
        }
        return myNameSpaceToScan;
    }

    
Use this static function to set the namespace to scan.

Parameters:
newNamespace the new namespace to be searched for de.zalando.sprocwrapper.globalvaluetransformer.annotation.GlobalValueTransformer
    public static void changeNamespaceToScan(final String newNamespace) {
         = Preconditions.checkNotNull(newNamespace"newNamespace");
         = null;
    }
New to GrepCode? Check out our FAQ X