Last active
July 14, 2016 02:50
-
-
Save chrismrgn/ed1c13c3ec72e8c6a813705b8bfc2317 to your computer and use it in GitHub Desktop.
DataBinder, based on the DD4T 2 Java pattern, to get other types of pages from SDL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface CustomDataBinder { | |
<T extends Sitemap> T buildSitemap(String source, final Class<T> aClass) throws SerializationException; | |
} | |
public class CustomDataBinderImpl implements CustomDataBinder { | |
private static final Logger LOG = LoggerFactory.getLogger(CustomDataBinderImpl.class); | |
private static final ObjectMapper GENERIC_MAPPER = new ObjectMapper(); | |
static { | |
GENERIC_MAPPER.configure(MapperFeature.IGNORE_DUPLICATE_MODULE_REGISTRATIONS,true); | |
GENERIC_MAPPER.registerModule(new JodaModule()); | |
GENERIC_MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); | |
} | |
private static final CustomDataBinderImpl INSTANCE = new CustomDataBinderImpl(); | |
private CustomDataBinderImpl () { | |
LOG.info("CustomDataBinderImpl init."); | |
} | |
public static CustomDataBinderImpl getInstance () { | |
if (null == INSTANCE) { | |
LOG.error("DataBinderImpl not properly instantiated!"); | |
} | |
return INSTANCE; | |
} | |
@Override | |
public <T extends Sitemap> T buildSitemap(String source, final Class<T> aClass) throws SerializationException { | |
try { | |
return GENERIC_MAPPER.readValue(source, aClass); | |
} catch (IOException e) { | |
LOG.error(DataBindConstants.MESSAGE_ERROR_DESERIALIZING, e); | |
throw new SerializationException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment