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;
}
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);
}
}
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);
}
}
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]);
}
Aggregations