/** * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, * javax.servlet.ServletResponse, javax.servlet.FilterChain) */ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // Set thread's class loader Thread.currentThread().setContextClassLoader(OpenmrsClassLoader.getInstance()); // Carry on up the chain chain.doFilter(request, response); }
private OpenmrsClassScanner() { this.metadataReaderFactory = new SimpleMetadataReaderFactory(OpenmrsClassLoader.getInstance()); this.resourceResolver = new PathMatchingResourcePatternResolver(OpenmrsClassLoader.getInstance()); }
/** * Determine whether a class was loaded by this class loader or one of its child class loaders. <br> * <br> * Borrowed from Tomcat's WebappClassLoader */ protected static boolean loadedByThisOrChild(Class<?> clazz) { boolean result = false; for (ClassLoader classLoader = clazz.getClassLoader(); null != classLoader; classLoader = classLoader.getParent()) { if (classLoader.equals(getInstance())) { result = true; break; } } return result; }
/** * Loads a class with an instance of the OpenmrsClassLoader. Convenience method equivalent to * OpenmrsClassLoader.getInstance().loadClass(className); * * @param className the class to load * @return the class that was loaded * @throws ClassNotFoundException * @should load class with the OpenmrsClassLoader */ public static Class<?> loadClass(String className) throws ClassNotFoundException { return OpenmrsClassLoader.getInstance().loadClass(className); }
if (classLoader == getInstance()) { continue; thread.setContextClassLoader(getInstance());
/** * @see org.springframework.web.servlet.FrameworkServlet#initFrameworkServlet() */ @Override protected void initFrameworkServlet() throws ServletException, BeansException { // refresh the application context to look for module xml config files as well Thread.currentThread().setContextClassLoader(OpenmrsClassLoader.getInstance()); log.debug("Framework being initialized"); WebModuleUtil.setDispatcherServlet(this); super.initFrameworkServlet(); }
/** * @see org.springframework.web.servlet.FrameworkServlet#initFrameworkServlet() */ @Override protected void initFrameworkServlet() throws ServletException, BeansException { Thread.currentThread().setContextClassLoader(OpenmrsClassLoader.getInstance()); log.info("Framework being initialized for static content"); WebModuleUtil.setStaticDispatcherServlet(this); super.initFrameworkServlet(); }
/** * Called by the ModuleUtil after adding in a new, updating, starting, or stopping a module. * This needs to be called because each spring dispatcher servlet creates a new application * context, which therefore needs to be refreshed too. * * @throws ServletException */ public void refreshApplicationContext() throws ServletException { log.info("Application context for the static content dispatcher servlet is being refreshed"); Thread.currentThread().setContextClassLoader(OpenmrsClassLoader.getInstance()); ((XmlWebApplicationContext) getWebApplicationContext()).setClassLoader(OpenmrsClassLoader.getInstance()); refresh(); }
/** * Searches the filesystem for message properties files. ABKTODO: consider caching this, rather * than searching every time * * @return an array of property file names */ private Resource[] findPropertiesFiles() { Set<Resource> resourceSet = new HashSet<>(); try { String pattern = "classpath*:messages*.properties"; ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(OpenmrsClassLoader.getInstance()); Resource[] propertiesFiles = resourceResolver.getResources(pattern); Collections.addAll(resourceSet, propertiesFiles); for (ModuleClassLoader moduleClassLoader : ModuleFactory.getModuleClassLoaders()) { resourceResolver = new PathMatchingResourcePatternResolver(moduleClassLoader); propertiesFiles = resourceResolver.getResources(pattern); Collections.addAll(resourceSet, propertiesFiles); } } catch (IOException e) { log.error("Error generated", e); } if (log.isWarnEnabled() && (resourceSet.isEmpty())) { log.warn("No properties files found."); } return resourceSet.toArray(new Resource[resourceSet.size()]); }
private DefaultMessageSourceServiceImpl() { InputStream stream = OpenmrsClassLoader.getInstance().getResourceAsStream("messages.properties"); if (stream != null) { OpenmrsUtil.loadProperties(props, stream); } }
/** * @see org.openmrs.api.ObsService#registerHandler(String, String) */ @Override public void registerHandler(String key, String handlerClass) throws APIException { try { Class<?> loadedClass = OpenmrsClassLoader.getInstance().loadClass(handlerClass); registerHandler(key, (ComplexObsHandler) loadedClass.newInstance()); } catch (Exception e) { throw new APIException("unable.load.and.instantiate.handler", null, e); } }
/** * Called by the ModuleUtil after adding in a new module. This needs to be called because the * new mappings and advice that a new module adds in are cached by Spring's DispatcherServlet. * This method will reload that cache. * * @throws ServletException */ public void reInitFrameworkServlet() throws ServletException { log.debug("Framework being REinitialized"); Thread.currentThread().setContextClassLoader(OpenmrsClassLoader.getInstance()); ((XmlWebApplicationContext) getWebApplicationContext()).setClassLoader(OpenmrsClassLoader.getInstance()); refresh(); // the spring context gets reset by the framework servlet, so we need to // reload the advice points that were lost when refreshing Spring for (Module module : ModuleFactory.getStartedModules()) { ModuleFactory.loadAdvice(module); } }
instance = OpenmrsClassLoader.getInstance().loadClass(handlerGlobalValue).newInstance();
/** * Basic constructor for the super class to all openmrs api unit tests. This constructor sets up * the classloader and the properties file so that by the type spring gets around to finally * starting, the openmrs runtime properties are already in place A static load count is kept to * count the number of times this class has been loaded. * * @see #getLoadCount() */ public BaseContextSensitiveTest() { Thread.currentThread().setContextClassLoader(OpenmrsClassLoader.getInstance()); Properties props = getRuntimeProperties(); if (log.isDebugEnabled()) log.debug("props: " + props); Context.setRuntimeProperties(props); loadCount++; instance = this; }
Class<?> taskClass = OpenmrsClassLoader.getInstance().loadClass(taskDefinition.getTaskClass());
@Test(expected = ClassNotFoundException.class) public void shouldNotResolveInvalidClass() throws Exception { String className = "org.openmrs.scheduler.tasks.InvalidTask"; Class<?> c = OpenmrsClassLoader.getInstance().loadClass(className); Object o = c.newInstance(); if (o instanceof Task) fail("Class " + className + " is not supposed to be a valid Task"); else assertTrue("Class " + className + " is not a valid Task", true); }
@Test public void shouldResolveValidTaskClass() throws Exception { String className = "org.openmrs.scheduler.tasks.TestTask"; Class<?> c = OpenmrsClassLoader.getInstance().loadClass(className); Object o = c.newInstance(); if (o instanceof Task) assertTrue("Class " + className + " is a valid Task", true); else fail("Class " + className + " is not a valid Task"); }
/** * Interface used for callbacks when updating the database. Implement this interface and pass it * to {@link DatabaseUpdater#executeChangelog(String, Map, ChangeSetExecutorCallback)} */ public interface ChangeSetExecutorCallback { /** * This method is called after each changeset is executed. * * @param changeSet the liquibase changeset that was just run * @param numChangeSetsToRun the total number of changesets in the current file */ public void executing(ChangeSet changeSet, int numChangeSetsToRun); }
@Test public void shouldUpgradeModule() { Module module = ModuleFactory.getModuleById(MODULE3_ID); assertTrue(module.getVersion().equals("1.0-SNAPSHOT")); URL url = OpenmrsClassLoader.getInstance().getResource("org/openmrs/module/include/test3-2.0-SNAPSHOT.omod"); module.setDownloadURL("file:" + url.getFile()); createWebInfFolderIfNotExist(); ModuleFactory.stopModule(module, false, true); // stop the module with these parameters so that mandatory modules can be upgraded WebModuleUtil.stopModule(module, ((XmlWebApplicationContext) applicationContext).getServletContext()); Module newModule = ModuleFactory.updateModule(module); WebModuleUtil.startModule(newModule, ((XmlWebApplicationContext) applicationContext).getServletContext(), false); //module3 should have upgraded from version 1.0 to 2.0 module = ModuleFactory.getModuleById(MODULE3_ID); assertTrue(module.getVersion().equals("2.0-SNAPSHOT")); }
OpenmrsClassLoader loader = OpenmrsClassLoader.getInstance(); Class<?> module1ServiceClass = loader.loadClass("org.openmrs.module.test1.api.Test1Service"); Class<?> module2ServiceClass = loader.loadClass("org.openmrs.module.test2.api.Test2Service");