Search in sources :

Example 1 with Consumer

use of org.simpleflatmapper.util.Consumer in project SimpleFlatMapper by arnaudroger.

the class ConverterService method getConverterFactories.

private static List<ConverterFactory> getConverterFactories() {
    final List<ConverterFactory> converterFactories = new ArrayList<ConverterFactory>();
    Consumer<ConverterFactory<?, ?>> factoryConsumer = new Consumer<ConverterFactory<?, ?>>() {

        @Override
        public void accept(ConverterFactory<?, ?> converterFactory) {
            converterFactories.add(converterFactory);
        }
    };
    new JavaBaseConverterFactoryProducer().produce(factoryConsumer);
    // IFJAVA8_START
    new JavaTimeConverterFactoryProducer().produce(factoryConsumer);
    // IFJAVA8_END
    ProducerServiceLoader.produceFromServiceLoader(ConverterFactoryProducer.class, factoryConsumer);
    return converterFactories;
}
Also used : Consumer(org.simpleflatmapper.util.Consumer) JavaBaseConverterFactoryProducer(org.simpleflatmapper.converter.impl.JavaBaseConverterFactoryProducer) JavaTimeConverterFactoryProducer(org.simpleflatmapper.converter.impl.time.JavaTimeConverterFactoryProducer) ArrayList(java.util.ArrayList)

Example 2 with Consumer

use of org.simpleflatmapper.util.Consumer in project SimpleFlatMapper by arnaudroger.

the class AliasProviderTest method testFactoryJPANotPresent.

@Test
public void testFactoryJPANotPresent() throws Exception {
    final ClassLoader original = Thread.currentThread().getContextClassLoader();
    ClassLoader cl = new ClassLoader(ClassLoader.getSystemClassLoader().getParent()) {

        @Override
        protected Class<?> findClass(String name) throws ClassNotFoundException {
            if (!name.startsWith("javax.persistence")) {
                InputStream resourceAsStream = original.getResourceAsStream(name.replace(".", "/") + ".class");
                if (resourceAsStream == null) {
                    throw new ClassNotFoundException(name);
                }
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                try {
                    int i;
                    while ((i = resourceAsStream.read()) != -1) {
                        bos.write(i);
                    }
                    byte[] bytes = bos.toByteArray();
                    return defineClass(name, bytes, 0, bytes.length);
                } catch (IOException e) {
                    throw new ClassNotFoundException(e.getMessage(), e);
                } finally {
                    try {
                        resourceAsStream.close();
                    } catch (IOException e) {
                    }
                }
            } else {
                throw new ClassNotFoundException(name);
            }
        }
    };
    Thread.currentThread().setContextClassLoader(cl);
    try {
        Class<?> jpa = cl.loadClass(JpaAliasProviderFactory.class.getName());
        Class<?> consumerClass = cl.loadClass(Consumer.class.getName());
        Object consumer = cl.loadClass(ListCollector.class.getName()).newInstance();
        jpa.getMethod("produce", consumerClass).invoke(jpa.getConstructor().newInstance(), consumer);
        List<String> list = (List<String>) consumer.getClass().getMethod("getList").invoke(consumer);
        assertTrue(list.isEmpty());
    } finally {
        Thread.currentThread().setContextClassLoader(original);
    }
}
Also used : InputStream(java.io.InputStream) JpaAliasProviderFactory(org.simpleflatmapper.jdbc.impl.JpaAliasProviderFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Consumer(org.simpleflatmapper.util.Consumer) List(java.util.List) Test(org.junit.Test)

Example 3 with Consumer

use of org.simpleflatmapper.util.Consumer in project SimpleFlatMapper by arnaudroger.

the class ProducerServiceLoaderTest method testServiceLoader.

@Test
public void testServiceLoader() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, MalformedURLException {
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        final List<String> data = new ArrayList<String>();
        Consumer<String> consumer = new Consumer<String>() {

            @Override
            public void accept(String s) {
                data.add(s);
            }
        };
        URLClassLoader loader = new URLClassLoader(new URL[0], getClass().getClassLoader());
        Thread.currentThread().setContextClassLoader(loader);
        ProducerServiceLoader.produceFromServiceLoader(ServiceProducer.class, consumer);
        assertArrayEquals(new String[0], data.toArray(new String[0]));
        data.clear();
        loader = new URLClassLoader(new URL[] { new URL("file:src/test/resources/sl1/") }, getClass().getClassLoader());
        Thread.currentThread().setContextClassLoader(loader);
        ProducerServiceLoader.produceFromServiceLoader(ServiceProducer.class, consumer);
        assertArrayEquals(new String[] { "ValidServiceProducer" }, data.toArray(new String[0]));
    } finally {
        Thread.currentThread().setContextClassLoader(contextClassLoader);
    }
}
Also used : Consumer(org.simpleflatmapper.util.Consumer) URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL) Test(org.junit.Test)

Example 4 with Consumer

use of org.simpleflatmapper.util.Consumer in project SimpleFlatMapper by arnaudroger.

the class ReflectionService method getPredifinedClassMetaFactory.

@SuppressWarnings("unchecked")
private static UnaryFactory<ReflectionService, ClassMeta<?>>[] getPredifinedClassMetaFactory() {
    final List<UnaryFactory<ReflectionService, ClassMeta<?>>> list = new ArrayList<UnaryFactory<ReflectionService, ClassMeta<?>>>();
    Consumer<UnaryFactory<ReflectionService, ClassMeta<?>>> consumer = new Consumer<UnaryFactory<ReflectionService, ClassMeta<?>>>() {

        @Override
        public void accept(UnaryFactory<ReflectionService, ClassMeta<?>> reflectionServiceClassMetaUnaryFactory) {
            list.add(reflectionServiceClassMetaUnaryFactory);
        }
    };
    new JavaLangClassMetaFactoryProducer().produce(consumer);
    ProducerServiceLoader.produceFromServiceLoader(ClassMetaFactoryProducer.class, consumer);
    return list.toArray(new UnaryFactory[0]);
}
Also used : JavaLangClassMetaFactoryProducer(org.simpleflatmapper.reflect.impl.JavaLangClassMetaFactoryProducer) Consumer(org.simpleflatmapper.util.Consumer) OptionalClassMeta(org.simpleflatmapper.reflect.meta.OptionalClassMeta) ArrayClassMeta(org.simpleflatmapper.reflect.meta.ArrayClassMeta) ObjectClassMeta(org.simpleflatmapper.reflect.meta.ObjectClassMeta) TupleClassMeta(org.simpleflatmapper.reflect.meta.TupleClassMeta) FastTupleClassMeta(org.simpleflatmapper.reflect.meta.FastTupleClassMeta) ClassMeta(org.simpleflatmapper.reflect.meta.ClassMeta) MapClassMeta(org.simpleflatmapper.reflect.meta.MapClassMeta) UnaryFactory(org.simpleflatmapper.util.UnaryFactory)

Aggregations

Consumer (org.simpleflatmapper.util.Consumer)4 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 List (java.util.List)1 JavaBaseConverterFactoryProducer (org.simpleflatmapper.converter.impl.JavaBaseConverterFactoryProducer)1 JavaTimeConverterFactoryProducer (org.simpleflatmapper.converter.impl.time.JavaTimeConverterFactoryProducer)1 JpaAliasProviderFactory (org.simpleflatmapper.jdbc.impl.JpaAliasProviderFactory)1 JavaLangClassMetaFactoryProducer (org.simpleflatmapper.reflect.impl.JavaLangClassMetaFactoryProducer)1 ArrayClassMeta (org.simpleflatmapper.reflect.meta.ArrayClassMeta)1 ClassMeta (org.simpleflatmapper.reflect.meta.ClassMeta)1 FastTupleClassMeta (org.simpleflatmapper.reflect.meta.FastTupleClassMeta)1 MapClassMeta (org.simpleflatmapper.reflect.meta.MapClassMeta)1 ObjectClassMeta (org.simpleflatmapper.reflect.meta.ObjectClassMeta)1 OptionalClassMeta (org.simpleflatmapper.reflect.meta.OptionalClassMeta)1 TupleClassMeta (org.simpleflatmapper.reflect.meta.TupleClassMeta)1