• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmrs / openmrs-core / 20784346756

07 Jan 2026 02:13PM UTC coverage: 63.283%. First build
20784346756

push

github

web-flow
TRUNK-6418 Run liquibase checks and data imports only when version of core or modules changes (#5603)

* TRUNK-6418: Run liquibase checks and data imports only when version of core or modules changes

(cherry picked from commit 4723e71c3)

* TRUNK-6418: Follow up adjustments

---------

Co-authored-by: IamMujuziMoses <mujuzimoses@gmail.com>

49 of 112 new or added lines in 9 files covered. (43.75%)

23080 of 36471 relevant lines covered (63.28%)

0.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.86
/api/src/main/java/org/openmrs/module/ModuleFactory.java
1
/**
2
 * This Source Code Form is subject to the terms of the Mozilla Public License,
3
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
4
 * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5
 * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6
 * 
7
 * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8
 * graphic logo is a trademark of OpenMRS Inc.
9
 */
10
package org.openmrs.module;
11

12
import java.io.File;
13
import java.io.IOException;
14
import java.io.InputStream;
15
import java.net.MalformedURLException;
16
import java.net.URL;
17
import java.util.ArrayList;
18
import java.util.Arrays;
19
import java.util.Collection;
20
import java.util.Collections;
21
import java.util.Comparator;
22
import java.util.HashMap;
23
import java.util.HashSet;
24
import java.util.LinkedHashSet;
25
import java.util.List;
26
import java.util.Map;
27
import java.util.Map.Entry;
28
import java.util.Set;
29
import java.util.SortedMap;
30
import java.util.concurrent.ConcurrentHashMap;
31
import java.util.concurrent.ExecutionException;
32

33
import com.google.common.cache.Cache;
34
import com.google.common.cache.CacheBuilder;
35
import org.aopalliance.aop.Advice;
36
import org.openmrs.GlobalProperty;
37
import org.openmrs.Privilege;
38
import org.openmrs.api.APIException;
39
import org.openmrs.api.AdministrationService;
40
import org.openmrs.api.OpenmrsService;
41
import org.openmrs.api.context.Context;
42
import org.openmrs.api.context.Daemon;
43
import org.openmrs.module.Extension.MEDIA_TYPE;
44
import org.openmrs.util.CycleException;
45
import org.openmrs.util.DatabaseUpdater;
46
import org.openmrs.util.Graph;
47
import org.openmrs.util.InputRequiredException;
48
import org.openmrs.util.OpenmrsClassLoader;
49
import org.openmrs.util.OpenmrsConstants;
50
import org.openmrs.util.OpenmrsUtil;
51
import org.openmrs.util.PrivilegeConstants;
52
import org.slf4j.Logger;
53
import org.slf4j.LoggerFactory;
54
import org.springframework.aop.Advisor;
55
import org.springframework.context.support.AbstractRefreshableApplicationContext;
56
import org.springframework.util.StringUtils;
57

58
import liquibase.Contexts;
59

60
/**
61
 * Methods for loading, starting, stopping, and storing OpenMRS modules
62
 */
63
public class ModuleFactory {
64
        
65
        private ModuleFactory() {
66
        }
67
        
68
        private static final Logger log = LoggerFactory.getLogger(ModuleFactory.class);
1✔
69
        
70
        protected static final Cache<String, Module> loadedModules = CacheBuilder.newBuilder()
1✔
71
                .softValues().build();
1✔
72
        
73
        protected static final Cache<String, Module> startedModules = CacheBuilder.newBuilder()
1✔
74
                .softValues().build();
1✔
75
        
76
        protected static final Map<String, List<Extension>> extensionMap = new HashMap<>();
1✔
77
        
78
        // maps to keep track of the memory and objects to free/close
79
        protected static final Cache<Module, ModuleClassLoader> moduleClassLoaders = CacheBuilder.newBuilder().weakKeys()
1✔
80
                .softValues().build();
1✔
81
        
82
        private static final Map<String, Set<ModuleClassLoader>> providedPackages = new ConcurrentHashMap<>();
1✔
83
        
84
        // the name of the file within a module file
85
        private static final String MODULE_CHANGELOG_FILENAME = "liquibase.xml";
86
        
87
        private static final Cache<String, DaemonToken> daemonTokens = CacheBuilder.newBuilder().softValues().build();
1✔
88
        
89
        private static final Set<String> actualStartupOrder = new LinkedHashSet<>();
1✔
90
        
91
        /**
92
         * Add a module (in the form of a jar file) to the list of openmrs modules Returns null if an error
93
         * occurred and/or module was not successfully loaded
94
         *
95
         * @param moduleFile
96
         * @return Module
97
         */
98
        public static Module loadModule(File moduleFile) throws ModuleException {
99
                
100
                return loadModule(moduleFile, true);
×
101
                
102
        }
103
        
104
        /**
105
         * Add a module (in the form of a jar file) to the list of openmrs modules Returns null if an error
106
         * occurred and/or module was not successfully loaded
107
         *
108
         * @param moduleFile
109
         * @param replaceIfExists unload a module that has the same moduleId if one is loaded already
110
         * @return Module
111
         */
112
        public static Module loadModule(File moduleFile, Boolean replaceIfExists) throws ModuleException {
113
                Module module = new ModuleFileParser(Context.getMessageSourceService()).parse(moduleFile);
1✔
114
                
115
                if (module != null) {
1✔
116
                        loadModule(module, replaceIfExists);
1✔
117
                }
118
                
119
                return module;
1✔
120
        }
121
        
122
        /**
123
         * Add a module to the list of openmrs modules
124
         *
125
         * @param module
126
         * @param replaceIfExists unload a module that has the same moduleId if one is loaded already
127
         *            <strong>Should</strong> load module if it is currently not loaded
128
         *            <strong>Should</strong> not load module if already loaded <strong>Should</strong>
129
         *            always load module if replacement is wanted <strong>Should</strong> not load an older
130
         *            version of the same module <strong>Should</strong> load a newer version of the same
131
         *            module
132
         * @return module the module that was loaded or if the module exists already with the same version,
133
         *         the old module
134
         */
135
        public static Module loadModule(Module module, Boolean replaceIfExists) throws ModuleException {
136
                
137
                log.debug("Adding module {} to the module queue", module.getName());
1✔
138
                
139
                Module oldModule = getLoadedModulesMap().get(module.getModuleId());
1✔
140
                if (oldModule != null) {
1✔
141
                        int versionComparison = ModuleUtil.compareVersion(oldModule.getVersion(), module.getVersion());
1✔
142
                        if (versionComparison < 0) {
1✔
143
                                // if oldModule version is lower, unload it and use the new
144
                                unloadModule(oldModule);
1✔
145
                        } else if (versionComparison == 0) {
1✔
146
                                if (replaceIfExists) {
1✔
147
                                        // if the versions are the same and we're told to replaceIfExists, use the new
148
                                        unloadModule(oldModule);
1✔
149
                                } else {
150
                                        // if the versions are equal and we're not told to replaceIfExists, jump out of here in a bad way
151
                                        throw new ModuleException("A module with the same id and version already exists", module.getModuleId());
1✔
152
                                }
153
                        } else {
154
                                // if the older (already loaded) module is newer, keep that original one that was loaded. return that one.
155
                                return oldModule;
1✔
156
                        }
157
                }
158
                
159
                getLoadedModulesMap().put(module.getModuleId(), module);
1✔
160
                
161
                return module;
1✔
162
        }
163
        
164
        /**
165
         * Load OpenMRS modules from <code>OpenmrsUtil.getModuleRepository()</code>
166
         */
167
        public static void loadModules() {
168
                
169
                // load modules from the user's module repository directory
170
                File modulesFolder = ModuleUtil.getModuleRepository();
×
171
                
172
                log.debug("Loading modules from: {}", modulesFolder.getAbsolutePath());
×
173
                
174
                File[] files = modulesFolder.listFiles();
×
175
                if (modulesFolder.isDirectory() && files != null) {
×
176
                        loadModules(Arrays.asList(files));
×
177
                } else {
178
                        log.error("modules folder: '" + modulesFolder.getAbsolutePath() + "' is not a directory or IO error occurred");
×
179
                }
180
        }
×
181
        
182
        /**
183
         * Attempt to load the given files as OpenMRS modules
184
         *
185
         * @param modulesToLoad the list of files to try and load <strong>Should</strong> not crash when
186
         *            file is not found or broken <strong>Should</strong> setup requirement mappings for
187
         *            every module <strong>Should</strong> not start the loaded modules
188
         */
189
        public static void loadModules(List<File> modulesToLoad) {
190
                // loop over the modules and load all the modules that we can
191
                for (File f : modulesToLoad) {
1✔
192
                        if (f.exists()) {
1✔
193
                                // ignore .svn folder and the like
194
                                if (!f.getName().startsWith(".")) {
1✔
195
                                        try {
196
                                                // last module loaded wins
197
                                                Module mod = loadModule(f, true);
1✔
198
                                                log.debug("Loaded module: " + mod + " successfully");
1✔
199
                                        }
200
                                        catch (Exception e) {
×
201
                                                log.error("Unable to load file in module directory: " + f + ". Skipping file.", e);
×
202
                                        }
1✔
203
                                }
204
                        } else {
205
                                log.error("Could not find file in module directory: " + f);
1✔
206
                        }
207
                }
1✔
208
                
209
                //inform modules, that they can't start before other modules
210
                
211
                Map<String, Module> loadedModulesMap = getLoadedModulesMapPackage();
1✔
212
                for (Module m : loadedModulesMap.values()) {
1✔
213
                        Map<String, String> startBeforeModules = m.getStartBeforeModulesMap();
1✔
214
                        if (startBeforeModules.size() > 0) {
1✔
215
                                for (String s : startBeforeModules.keySet()) {
1✔
216
                                        Module mod = loadedModulesMap.get(s);
1✔
217
                                        if (mod != null) {
1✔
218
                                                mod.addRequiredModule(m.getPackageName(), m.getVersion());
1✔
219
                                        }
220
                                }
1✔
221
                        }
222
                }
1✔
223
        }
1✔
224
        
225
        /**
226
         * Try to start all of the loaded modules that have the global property <i>moduleId</i>.started is
227
         * set to "true" or the property does not exist. Otherwise, leave it as only "loaded"<br>
228
         * <br>
229
         * Modules that are already started will be skipped.
230
         */
231
        public static void startModules() {
232
                
233
                // loop over and try starting each of the loaded modules
234
                if (!getLoadedModules().isEmpty()) {
1✔
235
                        
236
                        List<Module> modules = getModulesThatShouldStart();
1✔
237
                        
238
                        try {
239
                                modules = getModulesInStartupOrder(modules);
1✔
240
                        }
241
                        catch (CycleException ex) {
×
242
                                String message = getCyclicDependenciesMessage(ex.getMessage());
×
243
                                log.error(message, ex);
×
244
                                notifySuperUsersAboutCyclicDependencies(ex);
×
245
                                modules = (List<Module>) ex.getExtraData();
×
246
                        }
1✔
247
                        
248
                        // try and start the modules that should be started
249
                        for (Module mod : modules) {
1✔
250
                                
251
                                if (mod.isStarted()) {
1✔
252
                                        // skip over modules that are already started
253
                                        continue;
×
254
                                }
255
                                
256
                                // Skip module if required ones are not started
257
                                if (!requiredModulesStarted(mod)) {
1✔
258
                                        String message = getFailedToStartModuleMessage(mod);
×
259
                                        log.error(message);
×
260
                                        mod.setStartupErrorMessage(message);
×
261
                                        notifySuperUsersAboutModuleFailure(mod);
×
262
                                        continue;
×
263
                                }
264
                                
265
                                try {
266
                                        log.debug("starting module: {}", mod.getModuleId());
1✔
267
                                        startModule(mod);
1✔
268
                                }
269
                                catch (Exception e) {
×
270
                                        log.error("Error while starting module: " + mod.getName(), e);
×
271
                                        mod.setStartupErrorMessage("Error while starting module", e);
×
272
                                        notifySuperUsersAboutModuleFailure(mod);
×
273
                                }
1✔
274
                        }
1✔
275
                }
276
        }
1✔
277
        
278
        /**
279
         * Obtain the list of modules that should be started
280
         *
281
         * @return list of modules
282
         */
283
        private static List<Module> getModulesThatShouldStart() {
284
                List<Module> modules = new ArrayList<>();
1✔
285
                
286
                AdministrationService adminService = Context.getAdministrationService();
1✔
287
                
288
                for (Module mod : getLoadedModules()) {
1✔
289
                        
290
                        String key = mod.getModuleId() + ".started";
1✔
291
                        String startedProp = adminService.getGlobalProperty(key, null);
1✔
292
                        String mandatoryProp = adminService.getGlobalProperty(mod.getModuleId() + ".mandatory", null);
1✔
293
                        
294
                        // if a 'moduleid.started' property doesn't exist, start the module anyway
295
                        // as this is probably the first time they are loading it
296
                        if (startedProp == null || "true".equals(startedProp) || "true".equalsIgnoreCase(mandatoryProp)
1✔
297
                                || mod.isMandatory()) {
×
298
                                modules.add(mod);
1✔
299
                        }
300
                }
1✔
301
                return modules;
1✔
302
        }
303
        
304
        /**
305
         * Sort modules in startup order based on required and aware-of dependencies
306
         *
307
         * @param modules list of modules to sort
308
         * @return list of modules sorted by dependencies
309
         * @throws CycleException
310
         */
311
        public static List<Module> getModulesInStartupOrder(Collection<Module> modules) throws CycleException {
312
                Graph<Module> graph = new Graph<>();
1✔
313
                
314
                for (Module mod : modules) {
1✔
315
                        
316
                        graph.addNode(mod);
1✔
317
                        
318
                        // Required dependencies
319
                        for (String key : mod.getRequiredModules()) {
1✔
320
                                Module module = getModuleByPackage(key);
1✔
321
                                Module fromNode = graph.getNode(module);
1✔
322
                                if (fromNode == null) {
1✔
323
                                        fromNode = module;
1✔
324
                                }
325
                                
326
                                if (fromNode != null) {
1✔
327
                                        graph.addEdge(graph.new Edge(
1✔
328
                                                fromNode,
329
                                                mod));
330
                                }
331
                        }
1✔
332
                        
333
                        // Aware-of dependencies
334
                        for (String key : mod.getAwareOfModules()) {
1✔
335
                                Module module = getModuleByPackage(key);
1✔
336
                                Module fromNode = graph.getNode(module);
1✔
337
                                if (fromNode == null) {
1✔
338
                                        fromNode = module;
1✔
339
                                }
340
                                
341
                                if (fromNode != null) {
1✔
342
                                        graph.addEdge(graph.new Edge(
×
343
                                                fromNode,
344
                                                mod));
345
                                }
346
                        }
1✔
347
                }
1✔
348
                
349
                return graph.topologicalSort();
1✔
350
        }
351
        
352
        /**
353
         * Send an Alert to all super users that the given module did not start successfully.
354
         *
355
         * @param mod The Module that failed
356
         */
357
        private static void notifySuperUsersAboutModuleFailure(Module mod) {
358
                try {
359
                        // Add the privileges necessary for notifySuperUsers
360
                        Context.addProxyPrivilege(PrivilegeConstants.MANAGE_ALERTS);
×
361
                        
362
                        // Send an alert to all administrators
363
                        Context.getAlertService().notifySuperUsers("Module.startupError.notification.message", null, mod.getName());
×
364
                }
365
                catch (Exception e) {
×
366
                        log.error("Unable to send an alert to the super users", e);
×
367
                }
368
                finally {
369
                        // Remove added privileges
370
                        Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_ALERTS);
×
371
                }
372
        }
×
373
        
374
        /**
375
         * Send an Alert to all super users that modules did not start due to cyclic dependencies
376
         */
377
        private static void notifySuperUsersAboutCyclicDependencies(Exception ex) {
378
                try {
379
                        Context.addProxyPrivilege(PrivilegeConstants.MANAGE_ALERTS);
×
380
                        Context.getAlertService().notifySuperUsers("Module.error.cyclicDependencies", ex, ex.getMessage());
×
381
                }
382
                catch (Exception e) {
×
383
                        log.error("Unable to send an alert to the super users", e);
×
384
                }
385
                finally {
386
                        Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_ALERTS);
×
387
                }
388
        }
×
389

390
        /**
391
         * Convenience method to return a List of Strings containing a description of which modules the
392
         * passed module requires but which are not started. The returned description of each module is the
393
         * moduleId followed by the required version if one is specified
394
         *
395
         * @param module the module to check required modules for
396
         * @return List&lt;String&gt; of module names + optional required versions: "org.openmrs.formentry
397
         *         1.8, org.rg.patientmatching"
398
         */
399
        private static List<String> getMissingRequiredModules(Module module) {
400
                List<String> ret = new ArrayList<>();
×
401
                for (String moduleName : module.getRequiredModules()) {
×
402
                        boolean started = false;
×
403
                        for (Module mod : getStartedModules()) {
×
404
                                if (mod.getPackageName().equals(moduleName)) {
×
405
                                        String reqVersion = module.getRequiredModuleVersion(moduleName);
×
406
                                        if (reqVersion == null || ModuleUtil.compareVersion(mod.getVersion(), reqVersion) >= 0) {
×
407
                                                started = true;
×
408
                                        }
409
                                        break;
410
                                }
411
                        }
×
412
                        
413
                        if (!started) {
×
414
                                String moduleVersion = module.getRequiredModuleVersion(moduleName);
×
415
                                moduleName = moduleName.replace("org.openmrs.module.", "").replace("org.openmrs.", "");
×
416
                                ret.add(moduleName + (moduleVersion != null ? " " + moduleVersion : ""));
×
417
                        }
418
                }
×
419
                return ret;
×
420
        }
421
        
422
        /**
423
         * Returns all modules found/loaded into the system (started and not started)
424
         *
425
         * @return <code>Collection&lt;Module&gt;</code> of the modules loaded into the system
426
         */
427
        public static Collection<Module> getLoadedModules() {
428
                if (getLoadedModulesMap().size() > 0) {
1✔
429
                        return getLoadedModulesMap().values();
1✔
430
                }
431
                
432
                return Collections.emptyList();
1✔
433
        }
434
        
435
        /**
436
         * Returns all modules found/loaded into the system (started and not started) in the form of a
437
         * map&lt;ModuleId, Module&gt;
438
         *
439
         * @return map&lt;ModuleId, Module&gt;
440
         */
441
        public static Map<String, Module> getLoadedModulesMap() {
442
                return loadedModules.asMap();
1✔
443
        }
444
        
445
        /**
446
         * Returns all modules found/loaded into the system (started and not started) in the form of a
447
         * map&lt;PackageName, Module&gt;
448
         *
449
         * @return map&lt;PackageName, Module&gt;
450
         */
451
        public static Map<String, Module> getLoadedModulesMapPackage() {
452
                Map<String, Module> map = new HashMap<>();
1✔
453
                for (Module loadedModule : getLoadedModulesMap().values()) {
1✔
454
                        map.put(loadedModule.getPackageName(), loadedModule);
1✔
455
                }
1✔
456
                return map;
1✔
457
        }
458
        
459
        /**
460
         * Returns the modules that have been successfully started
461
         *
462
         * @return <code>Collection&lt;Module&gt;</code> of the started modules
463
         */
464
        public static Collection<Module> getStartedModules() {
465
                if (getStartedModulesMap().size() > 0) {
1✔
466
                        return getStartedModulesMap().values();
1✔
467
                }
468
                
469
                return Collections.emptyList();
1✔
470
        }
471
        
472
        public static List<Module> getStartedModulesInOrder() {
473
                List<Module> modules = new ArrayList<>();
×
474
                if (actualStartupOrder != null) {
×
475
                        for (String moduleId : actualStartupOrder) {
×
476
                                modules.add(getStartedModulesMap().get(moduleId));
×
477
                        }
×
478
                } else {
479
                        modules.addAll(getStartedModules());
×
480
                }
481
                return modules;
×
482
        }
483
        
484
        /**
485
         * Returns the modules that have been successfully started in the form of a map&lt;ModuleId,
486
         * Module&gt;
487
         *
488
         * @return Map&lt;ModuleId, Module&gt;
489
         */
490
        public static Map<String, Module> getStartedModulesMap() {
491
                return startedModules.asMap();
1✔
492
        }
493
        
494
        /**
495
         * @param moduleId
496
         * @return Module matching module id or null if none
497
         */
498
        public static Module getModuleById(String moduleId) {
499
                return getLoadedModulesMap().get(moduleId);
1✔
500
        }
501
        
502
        /**
503
         * @param moduleId
504
         * @return Module matching moduleId, if it is started or null otherwise
505
         */
506
        public static Module getStartedModuleById(String moduleId) {
507
                return getStartedModulesMap().get(moduleId);
1✔
508
        }
509
        
510
        /**
511
         * @param modulePackage
512
         * @return Module matching module package or null if none
513
         */
514
        public static Module getModuleByPackage(String modulePackage) {
515
                for (Module mod : getLoadedModulesMap().values()) {
1✔
516
                        if (mod.getPackageName().equals(modulePackage)) {
1✔
517
                                return mod;
1✔
518
                        }
519
                }
1✔
520
                return null;
1✔
521
        }
522
        
523
        /**
524
         * @see #startModule(Module, boolean, AbstractRefreshableApplicationContext)
525
         * @see #startModuleInternal(Module)
526
         * @see Daemon#startModule(Module)
527
         */
528
        public static Module startModule(Module module) throws ModuleException {
529
                return startModule(module, false, null);
1✔
530
        }
531
        
532
        /**
533
         * Runs through extensionPoints and then calls {@link BaseModuleActivator#willStart()} on the
534
         * Module's activator. This method is run in a new thread and is authenticated as the Daemon user.
535
         * If a non null application context is passed in, it gets refreshed to make the module's services
536
         * available
537
         *
538
         * @param module Module to start
539
         * @param isOpenmrsStartup Specifies whether this module is being started at application startup or
540
         *            not, this argument is ignored if a null application context is passed in
541
         * @param applicationContext the spring application context instance to refresh
542
         * @throws ModuleException if the module throws any kind of error at startup or in an activator
543
         * @see #startModuleInternal(Module, boolean, AbstractRefreshableApplicationContext)
544
         * @see Daemon#startModule(Module, boolean, AbstractRefreshableApplicationContext)
545
         */
546
        public static Module startModule(Module module, boolean isOpenmrsStartup,
547
                AbstractRefreshableApplicationContext applicationContext) throws ModuleException {
548
                
549
                if (!requiredModulesStarted(module)) {
1✔
550
                        int missingModules = 0;
1✔
551
                        
552
                        for (String packageName : module.getRequiredModulesMap().keySet()) {
1✔
553
                                Module mod = getModuleByPackage(packageName);
1✔
554
                                
555
                                // mod not installed
556
                                if (mod == null) {
1✔
557
                                        missingModules++;
×
558
                                        continue;
×
559
                                }
560
                                
561
                                if (!mod.isStarted()) {
1✔
562
                                        startModule(mod);
1✔
563
                                }
564
                        }
1✔
565
                        
566
                        if (missingModules > 0) {
1✔
567
                                String message = getFailedToStartModuleMessage(module);
×
568
                                log.error(message);
×
569
                                module.setStartupErrorMessage(message);
×
570
                                notifySuperUsersAboutModuleFailure(module);
×
571
                                // instead of return null, i realized that Daemon.startModule() always returns a Module
572
                                // object,irrespective of whether the startup succeeded
573
                                return module;
×
574
                        }
575
                }
576
                return Daemon.startModule(module, isOpenmrsStartup, applicationContext);
1✔
577
        }
578
        
579
        /**
580
         * This method should not be called directly.<br>
581
         * <br>
582
         * The {@link #startModule(Module)} (and hence {@link Daemon#startModule(Module)}) calls this method
583
         * in a new Thread and is authenticated as the {@link Daemon} user<br>
584
         * <br>
585
         * Runs through extensionPoints and then calls {@link BaseModuleActivator#willStart()} on the
586
         * Module's activator.
587
         *
588
         * @param module Module to start
589
         */
590
        public static Module startModuleInternal(Module module) throws ModuleException {
591
                return startModuleInternal(module, false, null);
×
592
        }
593
        
594
        /**
595
         * This method should not be called directly.<br>
596
         * <br>
597
         * The {@link #startModule(Module)} (and hence {@link Daemon#startModule(Module)}) calls this method
598
         * in a new Thread and is authenticated as the {@link Daemon} user<br>
599
         * <br>
600
         * Runs through extensionPoints and then calls {@link BaseModuleActivator#willStart()} on the
601
         * Module's activator. <br>
602
         * <br>
603
         * If a non null application context is passed in, it gets refreshed to make the module's services
604
         * available
605
         *
606
         * @param module Module to start
607
         * @param isOpenmrsStartup Specifies whether this module is being started at application startup or
608
         *            not, this argument is ignored if a null application context is passed in
609
         * @param applicationContext the spring application context instance to refresh
610
         */
611
        public static Module startModuleInternal(Module module, boolean isOpenmrsStartup,
612
                AbstractRefreshableApplicationContext applicationContext) throws ModuleException {
613
                
614
                if (module != null) {
1✔
615
                        String moduleId = module.getModuleId();
1✔
616
                        
617
                        try {
618
                                
619
                                // check to be sure this module can run with our current version
620
                                // of OpenMRS code
621
                                String requireVersion = module.getRequireOpenmrsVersion();
1✔
622
                                ModuleUtil.checkRequiredVersion(OpenmrsConstants.OPENMRS_VERSION_SHORT, requireVersion);
1✔
623
                                
624
                                // check for required modules
625
                                if (!requiredModulesStarted(module)) {
1✔
626
                                        throw new ModuleException(getFailedToStartModuleMessage(module));
×
627
                                }
628
                                
629
                                // fire up the classloader for this module
630
                                ModuleClassLoader moduleClassLoader = new ModuleClassLoader(module, ModuleFactory.class.getClassLoader());
1✔
631
                                getModuleClassLoaderMap().put(module, moduleClassLoader);
1✔
632
                                registerProvidedPackages(moduleClassLoader);
1✔
633
                                
634
                                // don't load the advice objects into the Context
635
                                // At startup, the spring context isn't refreshed until all modules
636
                                // have been loaded.  This causes errors if called here during a
637
                                // module's startup if one of these advice points is on another
638
                                // module because that other module's service won't have been loaded
639
                                // into spring yet.  All advice for all modules must be reloaded
640
                                // a spring context refresh anyway
641
                                
642
                                // map extension point to a list of extensions for this module only
643
                                Map<String, List<Extension>> moduleExtensionMap = new HashMap<>();
1✔
644
                                for (Extension ext : module.getExtensions()) {
1✔
645
                                        
646
                                        String extId = ext.getExtensionId();
×
647
                                        List<Extension> tmpExtensions = moduleExtensionMap.computeIfAbsent(extId, k -> new ArrayList<>());
×
648
                                        
649
                                        tmpExtensions.add(ext);
×
650
                                }
×
651
                                
652
                                // Sort this module's extensions, and merge them into the full extensions map
653
                                Comparator<Extension> sortOrder = (e1, e2) -> Integer.valueOf(e1.getOrder()).compareTo(e2.getOrder());
1✔
654
                                for (Map.Entry<String, List<Extension>> moduleExtensionEntry : moduleExtensionMap.entrySet()) {
1✔
655
                                        // Sort this module's extensions for current extension point
656
                                        List<Extension> sortedModuleExtensions = moduleExtensionEntry.getValue();
×
657
                                        sortedModuleExtensions.sort(sortOrder);
×
658
                                        
659
                                        // Get existing extensions, and append the ones from the new module
660
                                        List<Extension> extensions = getExtensionMap().computeIfAbsent(moduleExtensionEntry.getKey(),
×
661
                                                k -> new ArrayList<>());
×
662
                                        for (Extension ext : sortedModuleExtensions) {
×
663
                                                log.debug("Adding to mapping ext: " + ext.getExtensionId() + " ext.class: " + ext.getClass());
×
664
                                                extensions.add(ext);
×
665
                                        }
×
666
                                }
×
667
                                
668
                                // run the module's sql update script
669
                                // This and the property updates are the only things that can't
670
                                // be undone at startup, so put these calls after any other
671
                                // calls that might hinder startup
672
                                SortedMap<String, String> diffs = SqlDiffFileParser.getSqlDiffs(module);
1✔
673
                                
674
                                try {
675
                                        // this method must check and run queries against the database.
676
                                        // to do this, it must be "authenticated".  Give the current
677
                                        // "user" the proxy privilege so this can be done. ("user" might
678
                                        // be nobody because this is being run at startup)
679
                                        Context.addProxyPrivilege("");
1✔
680
                                        
681
                                        for (Map.Entry<String, String> entry : diffs.entrySet()) {
1✔
682
                                                String version = entry.getKey();
×
683
                                                String sql = entry.getValue();
×
684
                                                if (StringUtils.hasText(sql)) {
×
685
                                                        runDiff(module, version, sql);
×
686
                                                }
687
                                        }
×
688
                                }
689
                                finally {
690
                                        // take the "authenticated" privilege away from the current "user"
691
                                        Context.removeProxyPrivilege("");
1✔
692
                                }
693
                                
694
                                if (Context.getAdministrationService().isModuleSetupOnVersionChangeNeeded(module.getModuleId())) {
1✔
695
                                        log.info("Module {} changed, running setup.", module.getModuleId());
1✔
696
                                        Context.getAdministrationService().runModuleSetupOnVersionChange(module);
1✔
697
                                }
698
                                
699
                                // effectively mark this module as started successfully
700
                                getStartedModulesMap().put(moduleId, module);
1✔
701

702
                                actualStartupOrder.add(moduleId);
1✔
703
                                
704
                                try {
705
                                        // save the state of this module for future restarts
706
                                        saveGlobalProperty(moduleId + ".started", "true", getGlobalPropertyStartedDescription(moduleId));
1✔
707
                                        
708
                                        // save the mandatory status
709
                                        saveGlobalProperty(moduleId + ".mandatory", String.valueOf(module.isMandatory()),
1✔
710
                                                getGlobalPropertyMandatoryModuleDescription(moduleId));
1✔
711
                                }
712
                                catch (Exception e) {
×
713
                                        // pass over errors because this doesn't really concern startup
714
                                        // passing over this also allows for multiple of the same-named modules
715
                                        // to be loaded in junit tests that are run within one session
716
                                        log.debug("Got an error when trying to set the global property on module startup", e);
×
717
                                }
1✔
718
                                
719
                                // (this must be done after putting the module in the started
720
                                // list)
721
                                // if this module defined any privileges or global properties,
722
                                // make sure they are added to the database
723
                                // (Unfortunately, placing the call here will duplicate work
724
                                // done at initial app startup)
725
                                if (!module.getPrivileges().isEmpty() || !module.getGlobalProperties().isEmpty()) {
1✔
726
                                        log.debug("Updating core dataset");
×
727
                                        Context.checkCoreDataset();
×
728
                                        // checkCoreDataset() currently doesn't throw an error. If
729
                                        // it did, it needs to be
730
                                        // caught and the module needs to be stopped and given a
731
                                        // startup error
732
                                }
733
                                
734
                                // should be near the bottom so the module has all of its stuff
735
                                // set up for it already.
736
                                try {
737
                                        if (module.getModuleActivator() != null) {
1✔
738
                                                // if extends BaseModuleActivator
739
                                                module.getModuleActivator().willStart();
1✔
740
                                        }
741
                                }
742
                                catch (ModuleException e) {
×
743
                                        // just rethrow module exceptions. This should be used for a
744
                                        // module marking that it had trouble starting
745
                                        throw e;
×
746
                                }
747
                                catch (Exception e) {
×
748
                                        throw new ModuleException("Error while calling module's Activator.startup()/willStart() method", e);
×
749
                                }
1✔
750
                                
751
                                // erase any previous startup error
752
                                module.clearStartupError();
1✔
753
                        }
754
                        catch (Exception e) {
×
NEW
755
                                log.error("Error while trying to start module: {}", moduleId, e);
×
756
                                module.setStartupErrorMessage("Error while trying to start module", e);
×
757
                                notifySuperUsersAboutModuleFailure(module);
×
758
                                // undo all of the actions in startup
759
                                try {
760
                                        boolean skipOverStartedProperty = false;
×
761
                                        
762
                                        if (e instanceof ModuleMustStartException) {
×
763
                                                skipOverStartedProperty = true;
×
764
                                        }
765
                                        
766
                                        stopModule(module, skipOverStartedProperty, true);
×
767
                                }
768
                                catch (Exception e2) {
×
769
                                        // this will probably occur about the same place as the
770
                                        // error in startup
NEW
771
                                        log.debug("Error while stopping module: {}", moduleId, e2);
×
772
                                }
×
773
                        }
1✔
774
                        
775
                }
776
                
777
                if (applicationContext != null) {
1✔
778
                        ModuleUtil.refreshApplicationContext(applicationContext, isOpenmrsStartup, module);
×
779
                }
780
                
781
                return module;
1✔
782
        }
783
        
784
        private static void registerProvidedPackages(ModuleClassLoader moduleClassLoader) {
785
                for (String providedPackage : moduleClassLoader.getProvidedPackages()) {
1✔
786
                        Set<ModuleClassLoader> newSet = new HashSet<>();
1✔
787
                        
788
                        Set<ModuleClassLoader> set = providedPackages.get(providedPackage);
1✔
789
                        if (set != null) {
1✔
790
                                newSet.addAll(set);
1✔
791
                        }
792
                        
793
                        newSet.add(moduleClassLoader);
1✔
794
                        providedPackages.put(providedPackage, newSet);
1✔
795
                }
1✔
796
        }
1✔
797
        
798
        private static void unregisterProvidedPackages(ModuleClassLoader moduleClassLoader) {
799
                for (String providedPackage : moduleClassLoader.getProvidedPackages()) {
1✔
800
                        Set<ModuleClassLoader> newSet = new HashSet<>();
1✔
801
                        
802
                        Set<ModuleClassLoader> set = providedPackages.get(providedPackage);
1✔
803
                        if (set != null) {
1✔
804
                                newSet.addAll(set);
1✔
805
                        }
806
                        newSet.remove(moduleClassLoader);
1✔
807
                        
808
                        providedPackages.put(providedPackage, newSet);
1✔
809
                }
1✔
810
        }
1✔
811
        
812
        public static Set<ModuleClassLoader> getModuleClassLoadersForPackage(String packageName) {
813
                Set<ModuleClassLoader> set = providedPackages.get(packageName);
1✔
814
                if (set == null) {
1✔
815
                        return Collections.emptySet();
1✔
816
                } else {
817
                        return new HashSet<>(set);
1✔
818
                }
819
        }
820
        
821
        /**
822
         * Gets the error message of a module which fails to start.
823
         *
824
         * @param module the module that has failed to start.
825
         * @return the message text.
826
         */
827
        private static String getFailedToStartModuleMessage(Module module) {
828
                String[] params = { module.getName(), String.join(",", getMissingRequiredModules(module)) };
×
829
                return Context.getMessageSourceService().getMessage("Module.error.moduleCannotBeStarted", params,
×
830
                        Context.getLocale());
×
831
        }
832
        
833
        /**
834
         * Gets the error message of cyclic dependencies between modules
835
         *
836
         * @return the message text.
837
         */
838
        private static String getCyclicDependenciesMessage(String message) {
839
                return Context.getMessageSourceService().getMessage("Module.error.cyclicDependencies", new Object[] { message },
×
840
                        Context.getLocale());
×
841
        }
842
        
843
        /**
844
         * Loop over the given module's advice objects and load them into the Context This needs to be
845
         * called for all started modules after every restart of the Spring Application Context
846
         *
847
         * @param module
848
         */
849
        public static void loadAdvice(Module module) {
850
                for (AdvicePoint advice : module.getAdvicePoints()) {
×
851
                        Class<?> cls;
852
                        try {
853
                                cls = Context.loadClass(advice.getPoint());
×
854
                                Object aopObject = advice.getClassInstance();
×
855
                                if (aopObject instanceof Advisor) {
×
856
                                        log.debug("adding advisor [{}]", aopObject.getClass());
×
857
                                        Context.addAdvisor(cls, (Advisor) aopObject);
×
858
                                } else if (aopObject != null) {
×
859
                                        log.debug("adding advice [{}]", aopObject.getClass());
×
860
                                        Context.addAdvice(cls, (Advice) aopObject);
×
861
                                } else {
862
                                        log.debug("Could not load advice class for {} [{}]", advice.getPoint(), advice.getClassName());
×
863
                                }
864
                        }
865
                        catch (ClassNotFoundException | NoClassDefFoundError e) {
×
866
                                log.warn("Could not load advice point [{}]", advice.getPoint(), e);
×
867
                        }
×
868
                }
×
869
        }
×
870
        
871
        /**
872
         * Execute the given sql diff section for the given module
873
         *
874
         * @param module the module being executed on
875
         * @param version the version of this sql diff
876
         * @param sql the actual sql statements to run (separated by semi colons)
877
         */
878
        private static void runDiff(Module module, String version, String sql) {
879
                AdministrationService as = Context.getAdministrationService();
×
880
                
881
                String key = module.getModuleId() + ".database_version";
×
882
                GlobalProperty gp = as.getGlobalPropertyObject(key);
×
883
                
884
                boolean executeSQL = false;
×
885
                
886
                // check given version against current version
887
                if (gp != null && StringUtils.hasLength(gp.getPropertyValue())) {
×
888
                        String currentDbVersion = gp.getPropertyValue();
×
889
                        if (log.isDebugEnabled()) {
×
890
                                log.debug("version:column {}:{}", version, currentDbVersion);
×
891
                                log.debug("compare: {}", ModuleUtil.compareVersion(version, currentDbVersion));
×
892
                        }
893
                        if (ModuleUtil.compareVersion(version, currentDbVersion) > 0) {
×
894
                                executeSQL = true;
×
895
                        }
896
                } else {
×
897
                        executeSQL = true;
×
898
                }
899
                
900
                // version is greater than the currently installed version. execute this update.
901
                if (executeSQL) {
×
902
                        try {
903
                                Context.addProxyPrivilege(PrivilegeConstants.SQL_LEVEL_ACCESS);
×
904
                                log.debug("Executing sql: " + sql);
×
905
                                String[] sqlStatements = sql.split(";");
×
906
                                for (String sqlStatement : sqlStatements) {
×
907
                                        if (sqlStatement.trim().length() > 0) {
×
908
                                                as.executeSQL(sqlStatement, false);
×
909
                                        }
910
                                }
911
                        }
912
                        finally {
913
                                Context.removeProxyPrivilege(PrivilegeConstants.SQL_LEVEL_ACCESS);
×
914
                        }
915
                        
916
                        // save the global property
917
                        try {
918
                                Context.addProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES);
×
919
                                
920
                                String description = "DO NOT MODIFY.  Current database version number for the " + module.getModuleId()
×
921
                                        + " module.";
922
                                
923
                                if (gp == null) {
×
924
                                        log.info("Global property " + key + " was not found. Creating one now.");
×
925
                                        gp = new GlobalProperty(key, version, description);
×
926
                                        as.saveGlobalProperty(gp);
×
927
                                } else if (!gp.getPropertyValue().equals(version)) {
×
928
                                        log.info("Updating global property " + key + " to version: " + version);
×
929
                                        gp.setDescription(description);
×
930
                                        gp.setPropertyValue(version);
×
931
                                        as.saveGlobalProperty(gp);
×
932
                                } else {
933
                                        log.error("Should not be here. GP property value and sqldiff version should not be equal");
×
934
                                }
935
                                
936
                        }
937
                        finally {
938
                                Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES);
×
939
                        }
940
                        
941
                }
942
                
943
        }
×
944

945
        /**
946
         * This is a convenience method that exposes the private {@link #runLiquibase(Module)} method.
947
         * @since 2.9.0
948
         */
949
        public static void runLiquibaseForModule(Module module) {
950
                runLiquibase(module);
1✔
951
        }
1✔
952
        
953
        /**
954
         * Execute all not run changeSets in liquibase.xml for the given module
955
         *
956
         * @param module the module being executed on
957
         */
958
        private static void runLiquibase(Module module) {
959
                ModuleClassLoader moduleClassLoader = getModuleClassLoader(module);
1✔
960
                boolean liquibaseFileExists = false;
1✔
961
                
962
                if (moduleClassLoader != null) {
1✔
963
                        try (InputStream inStream = moduleClassLoader.getResourceAsStream(MODULE_CHANGELOG_FILENAME)) {
1✔
964
                                liquibaseFileExists = (inStream != null);
1✔
965
                        }
966
                        catch (IOException ignored) {
×
967
                                
968
                        }
1✔
969
                }
970
                
971
                if (liquibaseFileExists) {
1✔
972
                        try {
973
                                // run liquibase.xml by Liquibase API
974
                                DatabaseUpdater.executeChangelog(MODULE_CHANGELOG_FILENAME, new Contexts(), null, moduleClassLoader);
×
975
                        }
976
                        catch (InputRequiredException e) {
×
977
                                // the user would be stepped through the questions returned here.
978
                                throw new ModuleException("Input during database updates is not yet implemented.", module.getName(), e);
×
979
                        }
980
                        catch (Exception e) {
×
981
                                throw new ModuleException("Unable to update data model using " + MODULE_CHANGELOG_FILENAME + ".",
×
982
                                        module.getName(), e);
×
983
                        }
×
984
                }
985
        }
1✔
986
        
987
        /**
988
         * Runs through the advice and extension points and removes from api. <br>
989
         * Also calls mod.Activator.shutdown()
990
         *
991
         * @param mod module to stop
992
         * @see ModuleFactory#stopModule(Module, boolean, boolean)
993
         */
994
        public static void stopModule(Module mod) {
995
                stopModule(mod, false, false);
1✔
996
        }
1✔
997
        
998
        /**
999
         * Runs through the advice and extension points and removes from api.<br>
1000
         * Also calls mod.Activator.shutdown()
1001
         *
1002
         * @param mod the module to stop
1003
         * @param isShuttingDown true if this is called during the process of shutting down openmrs
1004
         * @see #stopModule(Module, boolean, boolean)
1005
         */
1006
        public static void stopModule(Module mod, boolean isShuttingDown) {
1007
                stopModule(mod, isShuttingDown, false);
1✔
1008
        }
1✔
1009
        
1010
        /**
1011
         * Runs through the advice and extension points and removes from api.<br>
1012
         * <code>skipOverStartedProperty</code> should only be true when openmrs is stopping modules because
1013
         * it is shutting down. When normally stopping a module, use {@link #stopModule(Module)} (or leave
1014
         * value as false). This property controls whether the globalproperty is set for startup/shutdown.
1015
         * <br>
1016
         * Also calls module's {@link ModuleActivator#stopped()}
1017
         *
1018
         * @param mod module to stop
1019
         * @param skipOverStartedProperty true if we don't want to set &lt;moduleid&gt;.started to false
1020
         * @param isFailedStartup true if this is being called as a cleanup because of a failed module
1021
         *            startup
1022
         * @return list of dependent modules that were stopped because this module was stopped. This will
1023
         *         never be null.
1024
         */
1025
        public static List<Module> stopModule(Module mod, boolean skipOverStartedProperty, boolean isFailedStartup)
1026
                throws ModuleMustStartException {
1027
                
1028
                List<Module> dependentModulesStopped = new ArrayList<>();
1✔
1029
                
1030
                if (mod != null) {
1✔
1031
                        
1032
                        if (!ModuleFactory.isModuleStarted(mod)) {
1✔
1033
                                return dependentModulesStopped;
×
1034
                        }
1035
                        
1036
                        try {
1037
                                // if extends BaseModuleActivator
1038
                                if (mod.getModuleActivator() != null) {
1✔
1039
                                        mod.getModuleActivator().willStop();
1✔
1040
                                }
1041
                        }
1042
                        catch (Exception t) {
×
1043
                                log.warn("Unable to call module's Activator.willStop() method", t);
×
1044
                        }
1✔
1045
                        
1046
                        String moduleId = mod.getModuleId();
1✔
1047
                        
1048
                        // don't allow mandatory modules to be stopped
1049
                        // don't use database checks here because spring might be in a bad state
1050
                        if (!isFailedStartup && mod.isMandatory()) {
1✔
1051
                                throw new MandatoryModuleException(moduleId);
×
1052
                        }
1053

1054
                        String modulePackage = mod.getPackageName();
1✔
1055
                        
1056
                        // stop all dependent modules
1057
                        // copy modules to new list to avoid "concurrent modification exception"
1058
                        List<Module> startedModulesCopy = new ArrayList<>(getStartedModules());
1✔
1059
                        for (Module dependentModule : startedModulesCopy) {
1✔
1060
                                if (dependentModule != null && !dependentModule.equals(mod)
1✔
1061
                                        && isModuleRequiredByAnother(dependentModule, modulePackage)) {
1✔
1062
                                        dependentModulesStopped.add(dependentModule);
1✔
1063
                                        dependentModulesStopped.addAll(stopModule(dependentModule, skipOverStartedProperty, isFailedStartup));
1✔
1064
                                }
1065
                        }
1✔
1066
                        
1067
                        getStartedModulesMap().remove(moduleId);
1✔
1068
                        if (actualStartupOrder != null) {
1✔
1069
                                actualStartupOrder.remove(moduleId);
1✔
1070
                                for (Module depModule : dependentModulesStopped) {
1✔
1071
                                        actualStartupOrder.remove(depModule.getModuleId());
1✔
1072
                                }
1✔
1073
                        }
1074
                        
1075
                        if (!skipOverStartedProperty && !Context.isRefreshingContext()) {
1✔
1076
                                saveGlobalProperty(moduleId + ".started", "false", getGlobalPropertyStartedDescription(moduleId));
1✔
1077
                        }
1078
                        
1079
                        ModuleClassLoader moduleClassLoader = getModuleClassLoaderMap().get(mod);
1✔
1080
                        if (moduleClassLoader != null) {
1✔
1081
                                unregisterProvidedPackages(moduleClassLoader);
1✔
1082
                                
1083
                                log.debug("Mod was in classloader map.  Removing advice and extensions.");
1✔
1084
                                // remove all advice by this module
1085
                                try {
1086
                                        for (AdvicePoint advice : mod.getAdvicePoints()) {
1✔
1087
                                                Class cls;
1088
                                                try {
1089
                                                        cls = Context.loadClass(advice.getPoint());
×
1090
                                                        Object aopObject = advice.getClassInstance();
×
1091
                                                        if (aopObject instanceof Advisor) {
×
1092
                                                                log.debug("adding advisor: " + aopObject.getClass());
×
1093
                                                                Context.removeAdvisor(cls, (Advisor) aopObject);
×
1094
                                                        } else {
1095
                                                                log.debug("Adding advice: " + aopObject.getClass());
×
1096
                                                                Context.removeAdvice(cls, (Advice) aopObject);
×
1097
                                                        }
1098
                                                }
1099
                                                catch (Exception t) {
×
1100
                                                        log.warn("Could not remove advice point: " + advice.getPoint(), t);
×
1101
                                                }
×
1102
                                        }
×
1103
                                }
1104
                                catch (Exception t) {
×
1105
                                        log.warn("Error while getting advicePoints from module: " + moduleId, t);
×
1106
                                }
1✔
1107
                                
1108
                                // remove all extensions by this module
1109
                                try {
1110
                                        for (Extension ext : mod.getExtensions()) {
1✔
1111
                                                String extId = ext.getExtensionId();
×
1112
                                                try {
1113
                                                        List<Extension> tmpExtensions = getExtensions(extId);
×
1114
                                                        tmpExtensions.remove(ext);
×
1115
                                                        getExtensionMap().put(extId, tmpExtensions);
×
1116
                                                }
1117
                                                catch (Exception exterror) {
×
1118
                                                        log.warn("Error while getting extension: " + ext, exterror);
×
1119
                                                }
×
1120
                                        }
×
1121
                                }
1122
                                catch (Exception t) {
×
1123
                                        log.warn("Error while getting extensions from module: " + moduleId, t);
×
1124
                                }
1✔
1125
                        }
1126
                        
1127
                        //Run the onShutdown() method for openmrs services in this module.
1128
                        List<OpenmrsService> services = Context.getModuleOpenmrsServices(modulePackage);
1✔
1129
                        if (services != null) {
1✔
1130
                                for (OpenmrsService service : services) {
1✔
1131
                                        service.onShutdown();
1✔
1132
                                }
1✔
1133
                        }
1134
                        
1135
                        try {
1136
                                if (mod.getModuleActivator() != null) {// extends BaseModuleActivator
1✔
1137
                                        mod.getModuleActivator().stopped();
1✔
1138
                                }
1139
                        }
1140
                        catch (Exception t) {
×
1141
                                log.warn("Unable to call module's Activator.shutdown() method", t);
×
1142
                        }
1✔
1143
                        
1144
                        //Since extensions are loaded by the module class loader which is about to be disposed,
1145
                        //we need to clear them, else we shall never be able to unload the class loader until
1146
                        //when we unload the module, hence resulting into two problems:
1147
                        // 1) Memory leakage for start/stop module.
1148
                        // 2) Calls to Context.getService(Service.class) which are made within these extensions 
1149
                        //          will throw APIException("Service not found: ") because their calls to Service.class
1150
                        //    will pass in a Class from the old module class loader (which loaded them) yet the
1151
                        //    ServiceContext will have new services from a new module class loader.
1152
                        //
1153
                        //Same thing applies to activator, moduleActivator and AdvicePoint classInstance.
1154
                        mod.getExtensions().clear();
1✔
1155
                        mod.setModuleActivator(null);
1✔
1156
                        mod.disposeAdvicePointsClassInstance();
1✔
1157
                        
1158
                        ModuleClassLoader cl = removeClassLoader(mod);
1✔
1159
                        if (cl != null) {
1✔
1160
                                cl.dispose();
1✔
1161
                                // remove files from lib cache
1162
                                File folder = OpenmrsClassLoader.getLibCacheFolder();
1✔
1163
                                File tmpModuleDir = new File(folder, moduleId);
1✔
1164
                                try {
1165
                                        OpenmrsUtil.deleteDirectory(tmpModuleDir);
1✔
1166
                                }
1167
                                catch (IOException e) {
×
1168
                                        log.warn("Unable to delete libcachefolder for " + moduleId);
×
1169
                                }
1✔
1170
                        }
1171
                }
1172
                
1173
                return dependentModulesStopped;
1✔
1174
        }
1175
        
1176
        /**
1177
         * Checks if a module is required by another
1178
         *
1179
         * @param dependentModule the module whose required modules are to be checked
1180
         * @param modulePackage the package of the module to check if required by another
1181
         * @return true if the module is required, else false
1182
         */
1183
        private static boolean isModuleRequiredByAnother(Module dependentModule, String modulePackage) {
1184
                return dependentModule.getRequiredModules() != null && dependentModule.getRequiredModules().contains(modulePackage);
1✔
1185
        }
1186
        
1187
        private static ModuleClassLoader removeClassLoader(Module mod) {
1188
                // create map if it is null
1189
                ModuleClassLoader cl = moduleClassLoaders.getIfPresent(mod);
1✔
1190
                if (cl == null) {
1✔
1191
                        log.warn("Module: " + mod.getModuleId() + " does not exist");
×
1192
                }
1193
                
1194
                moduleClassLoaders.invalidate(mod);
1✔
1195
                
1196
                return cl;
1✔
1197
        }
1198
        
1199
        /**
1200
         * Removes module from module repository
1201
         *
1202
         * @param mod module to unload
1203
         */
1204
        public static void unloadModule(Module mod) {
1205
                
1206
                // remove this module's advice and extensions
1207
                if (isModuleStarted(mod)) {
1✔
1208
                        stopModule(mod, true);
1✔
1209
                }
1210
                
1211
                // remove from list of loaded modules
1212
                getLoadedModules().remove(mod);
1✔
1213
                
1214
                if (mod != null) {
1✔
1215
                        // remove the file from the module repository
1216
                        File file = mod.getFile();
1✔
1217
                        
1218
                        boolean deleted = file.delete();
1✔
1219
                        if (!deleted) {
1✔
1220
                                file.deleteOnExit();
×
1221
                                log.warn("Could not delete " + file.getAbsolutePath());
×
1222
                        }
1223
                        
1224
                }
1225
        }
1✔
1226
        
1227
        /**
1228
         * Return all of the extensions associated with the given <code>pointId</code> Returns empty
1229
         * extension list if no modules extend this pointId
1230
         *
1231
         * @param pointId
1232
         * @return List of extensions
1233
         */
1234
        public static List<Extension> getExtensions(String pointId) {
1235
                List<Extension> extensions;
1236
                Map<String, List<Extension>> extensionMap = getExtensionMap();
×
1237
                
1238
                // get all extensions for this exact pointId
1239
                extensions = extensionMap.get(pointId);
×
1240
                if (extensions == null) {
×
1241
                        extensions = new ArrayList<>();
×
1242
                }
1243
                
1244
                // if this pointId doesn't contain the separator character, search
1245
                // for this point prepended with each MEDIA TYPE
1246
                if (!pointId.contains(Extension.EXTENSION_ID_SEPARATOR)) {
×
1247
                        for (MEDIA_TYPE mediaType : Extension.MEDIA_TYPE.values()) {
×
1248
                                
1249
                                // get all extensions for this type and point id
1250
                                List<Extension> tmpExtensions = extensionMap.get(Extension.toExtensionId(pointId, mediaType));
×
1251
                                
1252
                                // 'extensions' should be a unique list
1253
                                if (tmpExtensions != null) {
×
1254
                                        for (Extension ext : tmpExtensions) {
×
1255
                                                if (!extensions.contains(ext)) {
×
1256
                                                        extensions.add(ext);
×
1257
                                                }
1258
                                        }
×
1259
                                }
1260
                        }
1261
                }
1262
                
1263
                log.debug("Getting extensions defined by : " + pointId);
×
1264
                return extensions;
×
1265
        }
1266
        
1267
        /**
1268
         * Return all of the extensions associated with the given <code>pointId</code> Returns
1269
         * getExtension(pointId) if no modules extend this pointId for given media type
1270
         *
1271
         * @param pointId
1272
         * @param type Extension.MEDIA_TYPE
1273
         * @return List of extensions
1274
         */
1275
        public static List<Extension> getExtensions(String pointId, Extension.MEDIA_TYPE type) {
1276
                String key = Extension.toExtensionId(pointId, type);
×
1277
                List<Extension> extensions = getExtensionMap().get(key);
×
1278
                if (extensions != null) {
×
1279
                        log.debug("Getting extensions defined by : " + key);
×
1280
                        return extensions;
×
1281
                } else {
1282
                        return getExtensions(pointId);
×
1283
                }
1284
        }
1285
        
1286
        /**
1287
         * Get a list of required Privileges defined by the modules
1288
         *
1289
         * @return <code>List&lt;Privilege&gt;</code> of the required privileges
1290
         */
1291
        public static List<Privilege> getPrivileges() {
1292
                
1293
                List<Privilege> privileges = new ArrayList<>();
1✔
1294
                
1295
                for (Module mod : getStartedModules()) {
1✔
1296
                        privileges.addAll(mod.getPrivileges());
×
1297
                }
×
1298
                
1299
                log.debug(privileges.size() + " new privileges");
1✔
1300
                
1301
                return privileges;
1✔
1302
        }
1303
        
1304
        /**
1305
         * Get a list of required GlobalProperties defined by the modules
1306
         *
1307
         * @return <code>List&lt;GlobalProperty&gt;</code> object of the module's global properties
1308
         */
1309
        public static List<GlobalProperty> getGlobalProperties() {
1310
                
1311
                List<GlobalProperty> globalProperties = new ArrayList<>();
×
1312
                
1313
                for (Module mod : getStartedModules()) {
×
1314
                        globalProperties.addAll(mod.getGlobalProperties());
×
1315
                }
×
1316
                
1317
                log.debug(globalProperties.size() + " new global properties");
×
1318
                
1319
                return globalProperties;
×
1320
        }
1321
        
1322
        /**
1323
         * Checks whether the given module is activated
1324
         *
1325
         * @param mod Module to check
1326
         * @return true if the module is started, false otherwise
1327
         */
1328
        public static boolean isModuleStarted(Module mod) {
1329
                return getStartedModulesMap().containsValue(mod);
1✔
1330
        }
1331
        
1332
        /**
1333
         * Checks whether the given module, identified by its id, is started.
1334
         *
1335
         * @param moduleId module id. e.g formentry, logic
1336
         * @since 1.9
1337
         * @return true if the module is started, false otherwise
1338
         */
1339
        public static boolean isModuleStarted(String moduleId) {
1340
                return getStartedModulesMap().containsKey(moduleId);
1✔
1341
        }
1342
        
1343
        /**
1344
         * Get a module's classloader
1345
         *
1346
         * @param mod Module to fetch the class loader for
1347
         * @return ModuleClassLoader pertaining to this module. Returns null if the module is not started
1348
         * @throws ModuleException if the module does not have a registered classloader
1349
         */
1350
        public static ModuleClassLoader getModuleClassLoader(Module mod) throws ModuleException {
1351
                ModuleClassLoader mcl = getModuleClassLoaderMap().get(mod);
1✔
1352
                
1353
                if (mcl == null) {
1✔
1354
                        log.debug("Module classloader not found for module with id: " + mod.getModuleId());
1✔
1355
                }
1356
                
1357
                return mcl;
1✔
1358
        }
1359
        
1360
        /**
1361
         * Get a module's classloader via the module id
1362
         *
1363
         * @param moduleId <code>String</code> id of the module
1364
         * @return ModuleClassLoader pertaining to this module. Returns null if the module is not started
1365
         * @throws ModuleException if this module isn't started or doesn't have a classloader
1366
         * @see #getModuleClassLoader(Module)
1367
         */
1368
        public static ModuleClassLoader getModuleClassLoader(String moduleId) throws ModuleException {
1369
                Module mod = getStartedModulesMap().get(moduleId);
×
1370
                if (mod == null) {
×
1371
                        log.debug("Module id not found in list of started modules: " + moduleId);
×
1372
                }
1373
                
1374
                return getModuleClassLoader(mod);
×
1375
        }
1376
        
1377
        /**
1378
         * Returns all module classloaders This method will not return null
1379
         *
1380
         * @return Collection&lt;ModuleClassLoader&gt; all known module classloaders or empty list.
1381
         */
1382
        public static Collection<ModuleClassLoader> getModuleClassLoaders() {
1383
                Map<Module, ModuleClassLoader> classLoaders = getModuleClassLoaderMap();
1✔
1384
                if (classLoaders.size() > 0) {
1✔
1385
                        return classLoaders.values();
1✔
1386
                }
1387
                
1388
                return Collections.emptyList();
1✔
1389
        }
1390
        
1391
        /**
1392
         * Return all current classloaders keyed on module object
1393
         *
1394
         * @return Map&lt;Module, ModuleClassLoader&gt;
1395
         */
1396
        public static Map<Module, ModuleClassLoader> getModuleClassLoaderMap() {
1397
                // because the OpenMRS classloader depends on this static function, it is weirdly possible for this to get called
1398
                // as this classfile is loaded, in which case, the static final field can be null.
1399
                if (moduleClassLoaders == null) {
1✔
1400
                        return Collections.emptyMap();
×
1401
                }
1402
                
1403
                return moduleClassLoaders.asMap();
1✔
1404
        }
1405
        
1406
        /**
1407
         * Return the current extension map keyed on extension point id
1408
         *
1409
         * @return Map&lt;String, List&lt;Extension&gt;&gt;
1410
         */
1411
        public static Map<String, List<Extension>> getExtensionMap() {
1412
                return extensionMap;
×
1413
        }
1414
        
1415
        /**
1416
         * Tests whether all modules mentioned in module.requiredModules are loaded and started already (by
1417
         * being in the startedModules list)
1418
         *
1419
         * @param module
1420
         * @return true/false boolean whether this module's required modules are all started
1421
         */
1422
        private static boolean requiredModulesStarted(Module module) {
1423
                //required
1424
                for (String reqModPackage : module.getRequiredModules()) {
1✔
1425
                        boolean started = false;
1✔
1426
                        for (Module mod : getStartedModules()) {
1✔
1427
                                if (mod.getPackageName().equals(reqModPackage)) {
1✔
1428
                                        String reqVersion = module.getRequiredModuleVersion(reqModPackage);
1✔
1429
                                        if (reqVersion == null || ModuleUtil.compareVersion(mod.getVersion(), reqVersion) >= 0) {
1✔
1430
                                                started = true;
1✔
1431
                                        }
1432
                                        break;
1433
                                }
1434
                        }
×
1435
                        
1436
                        if (!started) {
1✔
1437
                                return false;
1✔
1438
                        }
1439
                }
1✔
1440
                
1441
                return true;
1✔
1442
        }
1443
        
1444
        /**
1445
         * Update the module: 1) Download the new module 2) Unload the old module 3) Load/start the new
1446
         * module
1447
         *
1448
         * @param mod
1449
         */
1450
        public static Module updateModule(Module mod) throws ModuleException {
1451
                if (mod.getDownloadURL() == null) {
×
1452
                        return mod;
×
1453
                }
1454
                
1455
                URL url;
1456
                try {
1457
                        url = new URL(mod.getDownloadURL());
×
1458
                }
1459
                catch (MalformedURLException e) {
×
1460
                        throw new ModuleException("Unable to download module update", e);
×
1461
                }
×
1462
                
1463
                unloadModule(mod);
×
1464
                
1465
                // copy content to a temporary file
1466
                InputStream inputStream = ModuleUtil.getURLStream(url);
×
1467
                log.warn("url pathname: " + url.getPath());
×
1468
                String filename = url.getPath().substring(url.getPath().lastIndexOf("/"));
×
1469
                File moduleFile = ModuleUtil.insertModuleFile(inputStream, filename);
×
1470
                
1471
                try {
1472
                        // load, and start the new module
1473
                        Module newModule = loadModule(moduleFile);
×
1474
                        startModule(newModule);
×
1475
                        return newModule;
×
1476
                }
1477
                catch (Exception e) {
×
1478
                        log.warn("Error while unloading old module and loading in new module");
×
1479
                        moduleFile.delete();
×
1480
                        return mod;
×
1481
                }
1482
                
1483
        }
1484
        
1485
        /**
1486
         * Validates the given token.
1487
         * <p>
1488
         * It is thread safe.
1489
         *
1490
         * @param token
1491
         * @since 1.9.2
1492
         */
1493
        public static boolean isTokenValid(DaemonToken token) {
1494
                if (token == null) {
×
1495
                        return false;
×
1496
                } else {
1497
                        //We need to synchronize to guarantee that the last passed token is valid.
1498
                        synchronized (daemonTokens) {
×
1499
                                DaemonToken validToken = daemonTokens.getIfPresent(token.getId());
×
1500
                                //Compare by reference to defend from overridden equals.
1501
                                return validToken != null && validToken == token;
×
1502
                        }
1503
                }
1504
        }
1505
        
1506
        /**
1507
         * Passes a daemon token to the given module.
1508
         * <p>
1509
         * The token is passed to that module's {@link ModuleActivator} if it implements
1510
         * {@link DaemonTokenAware}.
1511
         * <p>
1512
         * This method is called automatically before {@link ModuleActivator#contextRefreshed()} or
1513
         * {@link ModuleActivator#started()}. Note that it may be called multiple times and there is no
1514
         * guarantee that it will always pass the same token. The last passed token is valid, whereas
1515
         * previously passed tokens may be invalidated.
1516
         * <p>
1517
         * It is thread safe.
1518
         *
1519
         * @param module
1520
         * @since 1.9.2
1521
         */
1522
        static void passDaemonToken(Module module) {
1523
                ModuleActivator moduleActivator = module.getModuleActivator();
×
1524
                if (moduleActivator instanceof DaemonTokenAware) {
×
1525
                        DaemonToken daemonToken = getDaemonToken(module);
×
1526
                        ((DaemonTokenAware) module.getModuleActivator()).setDaemonToken(daemonToken);
×
1527
                }
1528
        }
×
1529
        
1530
        /**
1531
         * Gets a new or existing token. Uses weak references for tokens so that they are garbage collected
1532
         * when not needed.
1533
         * <p>
1534
         * It is thread safe.
1535
         *
1536
         * @param module
1537
         * @return the token
1538
         */
1539
        private static DaemonToken getDaemonToken(Module module) {
1540
                DaemonToken token;
1541
                try {
1542
                        token = daemonTokens.get(module.getModuleId(), () -> new DaemonToken(module.getModuleId()));
×
1543
                }
1544
                catch (ExecutionException e) {
×
1545
                        throw new APIException(e);
×
1546
                }
×
1547
                
1548
                return token;
×
1549
        }
1550
        
1551
        /**
1552
         * Returns the description for the [moduleId].started global property
1553
         *
1554
         * @param moduleId
1555
         * @return description to use for the .started property
1556
         */
1557
        private static String getGlobalPropertyStartedDescription(String moduleId) {
1558
                String ret = "DO NOT MODIFY. true/false whether or not the " + moduleId;
1✔
1559
                ret += " module has been started.  This is used to make sure modules that were running ";
1✔
1560
                ret += " prior to a restart are started again";
1✔
1561
                
1562
                return ret;
1✔
1563
        }
1564
        
1565
        /**
1566
         * Returns the description for the [moduleId].mandatory global property
1567
         *
1568
         * @param moduleId
1569
         * @return description to use for .mandatory property
1570
         */
1571
        private static String getGlobalPropertyMandatoryModuleDescription(String moduleId) {
1572
                String ret = "true/false whether or not the " + moduleId;
1✔
1573
                ret += " module MUST start when openmrs starts.  This is used to make sure that mission critical";
1✔
1574
                ret += " modules are always running if openmrs is running.";
1✔
1575
                
1576
                return ret;
1✔
1577
        }
1578
        
1579
        /**
1580
         * Convenience method to save a global property with the given value. Proxy privileges are added so
1581
         * that this can occur at startup.
1582
         *
1583
         * @param key the property for this global property
1584
         * @param value the value for this global property
1585
         * @param desc the description
1586
         * @see AdministrationService#saveGlobalProperty(GlobalProperty)
1587
         */
1588
        private static void saveGlobalProperty(String key, String value, String desc) {
1589
                try {
1590
                        AdministrationService as = Context.getAdministrationService();
1✔
1591
                        GlobalProperty gp = as.getGlobalPropertyObject(key);
1✔
1592
                        if (gp == null) {
1✔
1593
                                gp = new GlobalProperty(key, value, desc);
1✔
1594
                        } else {
1595
                                gp.setPropertyValue(value);
1✔
1596
                        }
1597
                        
1598
                        as.saveGlobalProperty(gp);
1✔
1599
                }
1600
                catch (Exception e) {
×
1601
                        log.warn("Unable to save the global property", e);
×
1602
                }
1✔
1603
        }
1✔
1604
        
1605
        /**
1606
         * Convenience method used to identify module interdependencies and alert the user before modules
1607
         * are shut down.
1608
         *
1609
         * @param moduleId the moduleId used to identify the module being validated
1610
         * @return List&lt;dependentModules&gt; the list of moduleId's which depend on the module about to
1611
         *         be shutdown.
1612
         * @since 1.10
1613
         */
1614
        public static List<String> getDependencies(String moduleId) {
1615
                List<String> dependentModules = null;
×
1616
                Module module = getModuleById(moduleId);
×
1617
                
1618
                Map<String, Module> startedModules = getStartedModulesMap();
×
1619
                String modulePackage = module.getPackageName();
×
1620
                
1621
                for (Entry<String, Module> entry : startedModules.entrySet()) {
×
1622
                        if (!moduleId.equals(entry.getKey()) && entry.getValue().getRequiredModules().contains(modulePackage)) {
×
1623
                                if (dependentModules == null) {
×
1624
                                        dependentModules = new ArrayList<>();
×
1625
                                }
1626
                                dependentModules.add(entry.getKey() + " " + entry.getValue().getVersion());
×
1627
                        }
1628
                }
×
1629
                return dependentModules;
×
1630
        }
1631
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc