All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.contextfw.web.commons.cloud.internal.serializer.ProvidedConverter Maven / Gradle / Ivy

The newest version!
package net.contextfw.web.commons.cloud.internal.serializer;

import net.contextfw.web.application.WebApplicationException;

import com.google.inject.Injector;
import com.thoughtworks.xstream.converters.SingleValueConverter;

public class ProvidedConverter implements SingleValueConverter {

    private final Injector injector;
    
    private final String type;

    private ClassLoader classLoader;
    
    public ProvidedConverter(Injector injector, Class type, ClassLoader classLoader) {
        this.type = type.getCanonicalName();
        this.injector = injector;
        this.classLoader = classLoader;
    }
    
    @Override
    public boolean canConvert(@SuppressWarnings("rawtypes") Class type) {
        return true;
    }

    @Override
    public String toString(Object obj) {
        return type;
    }

    @Override
    public Object fromString(String str) {
        try {
            return injector.getInstance(classLoader.loadClass(type));
        } catch (ClassNotFoundException e) {
            throw new WebApplicationException(e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy