/** * @param dt the datatype that this handler should be for * @param preferredHandlerClassname * @param handlerConfig * @return a configured datatype handler with the given classname and configuration */ public static CustomDatatypeHandler getHandler(CustomDatatype<?> dt, String preferredHandlerClassname, String handlerConfig) { if (preferredHandlerClassname != null) { try { Class<? extends CustomDatatypeHandler> clazz = (Class<? extends CustomDatatypeHandler>) Context .loadClass(preferredHandlerClassname); CustomDatatypeHandler handler = clazz.newInstance(); if (handlerConfig != null) { handler.setHandlerConfiguration(handlerConfig); } return handler; } catch (Exception ex) { log.warn("Failed to instantiate and configure preferred handler with class " + preferredHandlerClassname + " and config " + handlerConfig, ex); } } // if we couldn't get the preferred handler (or none was specified) we get the default one by datatype return Context.getDatatypeService().getHandler(dt, handlerConfig); }
/** * @see DatatypeService#getHandler(CustomDatatype,String) */ @Test public void getHandler_shouldReturnAHandlerForTheSpecifiedDatatype() { DatatypeService service = Context.getDatatypeService(); CustomDatatype dateDatatype = CustomDatatypeUtil.getDatatype(DateDatatype.class.getName(), null); Assert.assertEquals(DateDatatypeHandler.class, service.getHandler(dateDatatype, null).getClass()); }
/** * @see DatatypeService#getHandler(CustomDatatype,String) */ @Test public void getHandler_shouldReturnAHandlerForADatatypeThatExtendsAGenericSuperclass() { DatatypeService service = Context.getDatatypeService(); CustomDatatype locationDatatype = CustomDatatypeUtil.getDatatype(LocationDatatype.class.getName(), null); Assert.assertEquals(LocationDatatypeHandler.class, service.getHandler(locationDatatype, null).getClass()); } }