/** * Convenience method that returns a {@link java.lang.Class} object for the associated * javaClassName * * @return The Java class as {@link java.lang.Class} * @throws APIException */ public Class getJavaClass() { try { return Context.loadClass(javaClassName); } catch (ClassNotFoundException e) { //re throw as a runtime exception throw new APIException("OrderType.failed.load.class", new Object[] { javaClassName }, e); } }
/** * @see org.openmrs.api.SerializationService#getDefaultSerializer() */ @Override @Transactional(readOnly = true) public OpenmrsSerializer getDefaultSerializer() { String prop = Context.getAdministrationService().getGlobalProperty( OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_SERIALIZER); if (StringUtils.isNotEmpty(prop)) { try { Class<?> clazz = Context.loadClass(prop); if (clazz != null && OpenmrsSerializer.class.isAssignableFrom(clazz)) { return (OpenmrsSerializer) clazz.newInstance(); } } catch (Exception e) { log.info("Cannot create an instance of " + prop + " - using builtin SimpleXStreamSerializer."); } } else { log.info("No default serializer specified - using builtin SimpleXStreamSerializer."); } return serializerMap.get(SimpleXStreamSerializer.class); }
/** * @see org.openmrs.api.PatientService#getIdentifierValidator(java.lang.String) */ @Override @SuppressWarnings("unchecked") @Transactional(readOnly = true) public IdentifierValidator getIdentifierValidator(String pivClassName) { if (StringUtils.isBlank(pivClassName)) { return null; } try { return getIdentifierValidator((Class<IdentifierValidator>) Context.loadClass(pivClassName)); } catch (ClassNotFoundException e) { log.error("Could not find patient identifier validator " + pivClassName, e); return getDefaultIdentifierValidator(); } }
/** * @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); }
/** * Loop over the given module's advice objects and load them into the Context This needs to be * called for all started modules after every restart of the Spring Application Context * * @param module */ public static void loadAdvice(Module module) { for (AdvicePoint advice : module.getAdvicePoints()) { Class<?> cls; try { cls = Context.loadClass(advice.getPoint()); Object aopObject = advice.getClassInstance(); if (Advisor.class.isInstance(aopObject)) { log.debug("adding advisor: " + aopObject.getClass()); Context.addAdvisor(cls, (Advisor) aopObject); } else { log.debug("Adding advice: " + aopObject.getClass()); Context.addAdvice(cls, (Advice) aopObject); } } catch (ClassNotFoundException e) { log.warn("Could not load advice point: " + advice.getPoint(), e); } } }
/** * @throws ClassNotFoundException * @see Context#loadClass(String) */ @Test public void loadClass_shouldLoadClassWithOpenmrsClassLoader() throws ClassNotFoundException { Class<?> c = Context.loadClass("org.openmrs.module.test1.api.Test1Service"); Assert.assertTrue("Should be loaded by OpenmrsClassLoader", c.getClassLoader() instanceof ModuleClassLoader); }
/** * @param datatypeClassname * @param datatypeConfig * @return a configured datatype with the given classname and configuration */ public static CustomDatatype<?> getDatatype(String datatypeClassname, String datatypeConfig) { try { Class dtClass = Context.loadClass(datatypeClassname); CustomDatatype<?> ret = (CustomDatatype<?>) Context.getDatatypeService().getDatatype(dtClass, datatypeConfig); if (ret == null) { throw new CustomDatatypeException("Can't find datatype: " + datatypeClassname); } return ret; } catch (Exception ex) { throw new CustomDatatypeException("Error loading " + datatypeClassname + " and configuring it with " + datatypeConfig, ex); } }
@Test public void shouldStartModules() throws ClassNotFoundException { Class<?> atdServiceClass = Context.loadClass("org.openmrs.module.atdproducer.service.ATDService"); Class<?> dssServiceClass = Context.loadClass("org.openmrs.module.dssmodule.DssService"); Assert.assertNotNull(atdServiceClass); Assert.assertNotNull(dssServiceClass); Assert.assertNotNull(Context.getService(atdServiceClass)); Assert.assertNotNull(Context.getService(dssServiceClass)); } }
Class cls; try { cls = Context.loadClass(advice.getPoint()); Object aopObject = advice.getClassInstance(); if (Advisor.class.isInstance(aopObject)) {
.loadClass("org.openmrs.module.examplehl7handlers.AlternateORUR01Handler"); Application classInstance = c.newInstance(); HashMap<String, Application> map = new HashMap<>();
/** * @see SerializedObjectDAO#convertSerializedObject(Class, SerializedObject) */ @Override @SuppressWarnings("unchecked") public <T extends OpenmrsObject> T convertSerializedObject(Class<T> clazz, SerializedObject serializedObject) throws DAOException { if (serializedObject == null) { return null; } OpenmrsSerializer serializer = getSerializer(serializedObject); T obj; try { Class<?> subtype = Context.loadClass(serializedObject.getSubtype()); obj = (T) serializer.deserialize(serializedObject.getSerializedData(), subtype); } catch (Exception e) { ExceptionUtil.rethrowAPIAuthenticationException(e); throw new DAOException("Unable to deserialize object: " + serializedObject, e); } if (obj == null) { // it's probably impossible to reach this code branch throw new DAOException("Unable to deserialize object: " + serializedObject); } obj.setId(serializedObject.getId()); obj.setUuid(serializedObject.getUuid()); return obj; }
Class<Application> c = (Class<Application>) Context.loadClass("org.openmrs.module.examplehl7handlers.ADRHandler"); Application classInstance = c.newInstance(); HashMap<String, Application> map = new HashMap<>();
private static Object getProviderRoleBUuid(String providerRoleUuid) { // we have to fetch the provider roles by reflection, since the provider management module is not a required dependency try { Class<?> providerManagementServiceClass = Context.loadClass("org.openmrs.module.providermanagement.api.ProviderManagementService"); Object providerManagementService = Context.getService(providerManagementServiceClass); Method getProviderRoleByUuid = providerManagementServiceClass.getMethod("getProviderRoleByUuid", String.class); return getProviderRoleByUuid.invoke(providerManagementService, providerRoleUuid); } catch(Exception e) { throw new RuntimeException("Unable to get provider role by uuid; the Provider Management module needs to be installed if using the providerRoles attribute", e); } }
private static Object getProviderRoleById(Integer providerRoleId) { // we have to fetch the provider role by reflection, since the provider management module is not a required dependency try { Class<?> providerManagementServiceClass = Context.loadClass("org.openmrs.module.providermanagement.api.ProviderManagementService"); Object providerManagementService = Context.getService(providerManagementServiceClass); Method getProviderRole = providerManagementServiceClass.getMethod("getProviderRole", Integer.class); return getProviderRole.invoke(providerManagementService, providerRoleId); } catch(Exception e) { throw new RuntimeException("Unable to get provider role by id; the Provider Management module needs to be installed if using the providerRoles attribute", e); } }
/** * Evaluates the specified Java constant using reflection * @param fqn the fully qualified name of the constant * @return the constant value */ protected static String evaluateStaticConstant(String fqn) { int lastPeriod = fqn.lastIndexOf("."); String clazzName = fqn.substring(0, lastPeriod); String constantName = fqn.substring(lastPeriod + 1); try { Class<?> clazz = Context.loadClass(clazzName); Field constantField = clazz.getField(constantName); Object val = constantField.get(null); return val != null ? String.valueOf(val) : null; } catch (Exception ex) { throw new IllegalArgumentException("Unable to evaluate " + fqn, ex); } }
@Override public void handleSubmission(FormEntrySession session, HttpServletRequest submission) { try { Class<?> actionClass = Context.loadClass(className); CustomFormSubmissionAction actionInstance = (CustomFormSubmissionAction) actionClass.newInstance(); session.getSubmissionActions().addCustomFormSubmissionAction(actionInstance); } catch (Exception ex) { throw new IllegalStateException("Error loading/instantiating post submission action class " + className, ex); } }
/** * @return results of parsing the {@link HtmlFormEntryConstants#GP_CLASSES_NOT_TO_EXPORT_WITH_MDS} global property */ private Set<Class<?>> getClassesNotToExport() { Set<Class<?>> ret = new HashSet<Class<?>>(); String gp = Context.getAdministrationService().getGlobalProperty(HtmlFormEntryConstants.GP_CLASSES_NOT_TO_EXPORT_WITH_MDS); if (StringUtils.isNotBlank(gp)) { for (StringTokenizer st = new StringTokenizer(gp, ", "); st.hasMoreTokens(); ) { String className = st.nextToken(); try { ret.add(Context.loadClass(className)); } catch (ClassNotFoundException ex) { // pass } } } return ret; }
private List<CustomDatatypeRepresentation> getAllCustomDatatypes() { List<String> datatypeClassnames = CustomDatatypeUtil.getDatatypeClassnames(); List<CustomDatatypeRepresentation> datatypes = new ArrayList<CustomDatatypeRepresentation>(); for (String datatypeClassname : datatypeClassnames) { CustomDatatypeRepresentation datatype = new CustomDatatypeRepresentation(); datatypes.add(datatype); datatype.setDatatypeClassname(datatypeClassname); Class<CustomDatatype<?>> datatypeClass; try { datatypeClass = (Class<CustomDatatype<?>>) Context.loadClass(datatypeClassname); } catch (ClassNotFoundException e) { throw new ConversionException("Failed to load datatype class", e); } List<Class<? extends CustomDatatypeHandler>> handlerClasses = Context.getDatatypeService().getHandlerClasses( datatypeClass); for (Class<? extends CustomDatatypeHandler> handlerClass : handlerClasses) { datatype.getHandlers().add(new CustomDatatypeHandlerRepresentation(datatype, handlerClass.getName())); } } return datatypes; }
@Test public void setValue_shouldSetThePassedValueForNonAttributableClasses() throws ClassNotFoundException { PersonAttributeType type = personService.getPersonAttributeTypeByName("Race"); PersonAttribute attribute = new PersonAttribute(type, null); Assert.assertNull(attribute.getValue()); Assert.assertEquals("java.lang.String", type.getFormat()); Assert.assertFalse(Attributable.class.isAssignableFrom(Context.loadClass(type.getFormat()))); resource.setValue(attribute, "arab"); Assert.assertEquals("arab", attribute.getValue()); } }
@Test public void setValue_shouldSetPassedValueIfCouldNotBeConvertedToAttributable() throws ClassNotFoundException { PersonAttributeType type = new PersonAttributeType(); type.setFormat("org.openmrs.Location"); type.setName("Second Home"); type.setDescription("Testing Attributable domain objects"); type.setSortWeight(5.5); type.setSearchable(false); type = personService.savePersonAttributeType(type); String nonExistenceLocationUuid = "this-uuid-does-not-exist-of-course"; PersonAttribute attribute = new PersonAttribute(type, null); Assert.assertNull(attribute.getValue()); Assert.assertTrue(Attributable.class.isAssignableFrom(Context.loadClass(type.getFormat()))); resource.setValue(attribute, nonExistenceLocationUuid); Assert.assertEquals(nonExistenceLocationUuid, attribute.getValue()); }