Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package de.zalando.sprocwrapper.globalvaluetransformer;
 
 import java.util.Set;
 
 
 
    private static final String GLOBAL_VALUE_TRANSFORMER_SEARCH_NAMESPACE = "global.value.transformer.search.namespace";
    // you need to set the namespace to a valid value like: org.doodlejump
    private static String namespaceToScan = "de.zalando";
    private static final Logger LOG = LoggerFactory.getLogger(GlobalValueTransformerLoader.class);
    private static boolean scannedClasspath = false;
    public static synchronized ValueTransformer<?, ?> getValueTransformerForClass(final Class<?> genericType)
        // did we already scanned the classpath for global value transformers?
        if ( == false) {
            final Predicate<Stringfilter = new Predicate<String>() {
                @Override
                public boolean apply(final String input) {
                    return GlobalValueTransformer.class.getCanonicalName().equals(input);
                }
            };
            // last to get the namespace from the system environment
            String myNameSpaceToScan = null;
            try {
                myNameSpaceToScan = System.getenv();
            } catch (final Exception e) {
                // ignore - e.g. if a security manager exists and permissions are denied.
            }
            if (Strings.isNullOrEmpty(myNameSpaceToScan)) {
                // last to use the given namespace
                myNameSpaceToScan = ;
            }
            if (!Strings.isNullOrEmpty(myNameSpaceToScan)) {
                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<?>> typesAnnotatedWith = reflections.getTypesAnnotatedWith(
                        GlobalValueTransformer.class);
                for (final Class<?> foundGlobalValueTransformer : typesAnnotatedWith) {
                    final Class<?> valueTransformerReturnType = ValueTransformerUtils.getUnmarshalFromDbClass(
                            foundGlobalValueTransformer);
                    if (valueTransformerReturnType != null) {
                        GlobalValueTransformerRegistry.register(valueTransformerReturnType,
                            (ValueTransformer<?, ?>) foundGlobalValueTransformer.newInstance());
                        .debug("Global Value Transformer [{}] for type [{}] registered. ",
                            foundGlobalValueTransformer.getSimpleName(), valueTransformerReturnType.getSimpleName());
                    } else {
                        .error(
                            "Could add global transformer [{}] to global registry. Could not find method unmarshalFromDb.",
                            foundGlobalValueTransformer);
                    }
                }
            }
             = true;
        }
        return GlobalValueTransformerRegistry.getValueTransformerForClass(genericType);
    }

    
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) {
         = newNamespace;
         = false;
    }
New to GrepCode? Check out our FAQ X