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

openmrs / openmrs-core / 16357938490

17 Jul 2025 11:06PM UTC coverage: 65.244% (-0.1%) from 65.359%
16357938490

push

github

web-flow
TRUNK-6318: Add S3 Storage Service (#5110)

111 of 156 new or added lines in 5 files covered. (71.15%)

48 existing lines in 8 files now uncovered.

23568 of 36123 relevant lines covered (65.24%)

0.65 hits per line

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

50.87
/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✔
UNCOV
258
                                        String message = getFailedToStartModuleMessage(mod);
×
UNCOV
259
                                        log.error(message);
×
UNCOV
260
                                        mod.setStartupErrorMessage(message);
×
UNCOV
261
                                        notifySuperUsersAboutModuleFailure(mod);
×
UNCOV
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
UNCOV
360
                        Context.addProxyPrivilege(PrivilegeConstants.MANAGE_ALERTS);
×
361
                        
362
                        // Send an alert to all administrators
UNCOV
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
UNCOV
370
                        Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_ALERTS);
×
371
                }
UNCOV
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) {
UNCOV
400
                List<String> ret = new ArrayList<>();
×
UNCOV
401
                for (String moduleName : module.getRequiredModules()) {
×
UNCOV
402
                        boolean started = false;
×
UNCOV
403
                        for (Module mod : getStartedModules()) {
×
UNCOV
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
                                }
UNCOV
411
                        }
×
412
                        
UNCOV
413
                        if (!started) {
×
UNCOV
414
                                String moduleVersion = module.getRequiredModuleVersion(moduleName);
×
UNCOV
415
                                moduleName = moduleName.replace("org.openmrs.module.", "").replace("org.openmrs.", "");
×
UNCOV
416
                                ret.add(moduleName + (moduleVersion != null ? " " + moduleVersion : ""));
×
417
                        }
UNCOV
418
                }
×
UNCOV
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
                                // run module's optional liquibase.xml immediately after sqldiff.xml
695
                                runLiquibase(module);
1✔
696
                                
697
                                // effectively mark this module as started successfully
698
                                getStartedModulesMap().put(moduleId, module);
1✔
699

700
                                actualStartupOrder.add(moduleId);
1✔
701
                                
702
                                try {
703
                                        // save the state of this module for future restarts
704
                                        saveGlobalProperty(moduleId + ".started", "true", getGlobalPropertyStartedDescription(moduleId));
1✔
705
                                        
706
                                        // save the mandatory status
707
                                        saveGlobalProperty(moduleId + ".mandatory", String.valueOf(module.isMandatory()),
1✔
708
                                                getGlobalPropertyMandatoryModuleDescription(moduleId));
1✔
709
                                }
710
                                catch (Exception e) {
×
711
                                        // pass over errors because this doesn't really concern startup
712
                                        // passing over this also allows for multiple of the same-named modules
713
                                        // to be loaded in junit tests that are run within one session
714
                                        log.debug("Got an error when trying to set the global property on module startup", e);
×
715
                                }
1✔
716
                                
717
                                // (this must be done after putting the module in the started
718
                                // list)
719
                                // if this module defined any privileges or global properties,
720
                                // make sure they are added to the database
721
                                // (Unfortunately, placing the call here will duplicate work
722
                                // done at initial app startup)
723
                                if (!module.getPrivileges().isEmpty() || !module.getGlobalProperties().isEmpty()) {
1✔
724
                                        log.debug("Updating core dataset");
×
725
                                        Context.checkCoreDataset();
×
726
                                        // checkCoreDataset() currently doesn't throw an error. If
727
                                        // it did, it needs to be
728
                                        // caught and the module needs to be stopped and given a
729
                                        // startup error
730
                                }
731
                                
732
                                // should be near the bottom so the module has all of its stuff
733
                                // set up for it already.
734
                                try {
735
                                        if (module.getModuleActivator() != null) {
1✔
736
                                                // if extends BaseModuleActivator
737
                                                module.getModuleActivator().willStart();
1✔
738
                                        }
739
                                }
740
                                catch (ModuleException e) {
×
741
                                        // just rethrow module exceptions. This should be used for a
742
                                        // module marking that it had trouble starting
743
                                        throw e;
×
744
                                }
745
                                catch (Exception e) {
×
746
                                        throw new ModuleException("Error while calling module's Activator.startup()/willStart() method", e);
×
747
                                }
1✔
748
                                
749
                                // erase any previous startup error
750
                                module.clearStartupError();
1✔
751
                        }
UNCOV
752
                        catch (Exception e) {
×
UNCOV
753
                                log.warn("Error while trying to start module: " + moduleId, e);
×
UNCOV
754
                                module.setStartupErrorMessage("Error while trying to start module", e);
×
UNCOV
755
                                notifySuperUsersAboutModuleFailure(module);
×
756
                                // undo all of the actions in startup
757
                                try {
UNCOV
758
                                        boolean skipOverStartedProperty = false;
×
759
                                        
UNCOV
760
                                        if (e instanceof ModuleMustStartException) {
×
761
                                                skipOverStartedProperty = true;
×
762
                                        }
763
                                        
UNCOV
764
                                        stopModule(module, skipOverStartedProperty, true);
×
765
                                }
766
                                catch (Exception e2) {
×
767
                                        // this will probably occur about the same place as the
768
                                        // error in startup
769
                                        log.debug("Error while stopping module: " + moduleId, e2);
×
UNCOV
770
                                }
×
771
                        }
1✔
772
                        
773
                }
774
                
775
                if (applicationContext != null) {
1✔
776
                        ModuleUtil.refreshApplicationContext(applicationContext, isOpenmrsStartup, module);
×
777
                }
778
                
779
                return module;
1✔
780
        }
781
        
782
        private static void registerProvidedPackages(ModuleClassLoader moduleClassLoader) {
783
                for (String providedPackage : moduleClassLoader.getProvidedPackages()) {
1✔
784
                        Set<ModuleClassLoader> newSet = new HashSet<>();
1✔
785
                        
786
                        Set<ModuleClassLoader> set = providedPackages.get(providedPackage);
1✔
787
                        if (set != null) {
1✔
788
                                newSet.addAll(set);
1✔
789
                        }
790
                        
791
                        newSet.add(moduleClassLoader);
1✔
792
                        providedPackages.put(providedPackage, newSet);
1✔
793
                }
1✔
794
        }
1✔
795
        
796
        private static void unregisterProvidedPackages(ModuleClassLoader moduleClassLoader) {
797
                for (String providedPackage : moduleClassLoader.getProvidedPackages()) {
1✔
798
                        Set<ModuleClassLoader> newSet = new HashSet<>();
1✔
799
                        
800
                        Set<ModuleClassLoader> set = providedPackages.get(providedPackage);
1✔
801
                        if (set != null) {
1✔
802
                                newSet.addAll(set);
1✔
803
                        }
804
                        newSet.remove(moduleClassLoader);
1✔
805
                        
806
                        providedPackages.put(providedPackage, newSet);
1✔
807
                }
1✔
808
        }
1✔
809
        
810
        public static Set<ModuleClassLoader> getModuleClassLoadersForPackage(String packageName) {
811
                Set<ModuleClassLoader> set = providedPackages.get(packageName);
1✔
812
                if (set == null) {
1✔
813
                        return Collections.emptySet();
1✔
814
                } else {
815
                        return new HashSet<>(set);
1✔
816
                }
817
        }
818
        
819
        /**
820
         * Gets the error message of a module which fails to start.
821
         *
822
         * @param module the module that has failed to start.
823
         * @return the message text.
824
         */
825
        private static String getFailedToStartModuleMessage(Module module) {
UNCOV
826
                String[] params = { module.getName(), String.join(",", getMissingRequiredModules(module)) };
×
UNCOV
827
                return Context.getMessageSourceService().getMessage("Module.error.moduleCannotBeStarted", params,
×
UNCOV
828
                        Context.getLocale());
×
829
        }
830
        
831
        /**
832
         * Gets the error message of cyclic dependencies between modules
833
         *
834
         * @return the message text.
835
         */
836
        private static String getCyclicDependenciesMessage(String message) {
837
                return Context.getMessageSourceService().getMessage("Module.error.cyclicDependencies", new Object[] { message },
×
838
                        Context.getLocale());
×
839
        }
840
        
841
        /**
842
         * Loop over the given module's advice objects and load them into the Context This needs to be
843
         * called for all started modules after every restart of the Spring Application Context
844
         *
845
         * @param module
846
         */
847
        public static void loadAdvice(Module module) {
848
                for (AdvicePoint advice : module.getAdvicePoints()) {
×
849
                        Class<?> cls;
850
                        try {
851
                                cls = Context.loadClass(advice.getPoint());
×
852
                                Object aopObject = advice.getClassInstance();
×
853
                                if (aopObject instanceof Advisor) {
×
854
                                        log.debug("adding advisor [{}]", aopObject.getClass());
×
855
                                        Context.addAdvisor(cls, (Advisor) aopObject);
×
856
                                } else if (aopObject != null) {
×
857
                                        log.debug("adding advice [{}]", aopObject.getClass());
×
858
                                        Context.addAdvice(cls, (Advice) aopObject);
×
859
                                } else {
860
                                        log.debug("Could not load advice class for {} [{}]", advice.getPoint(), advice.getClassName());
×
861
                                }
862
                        }
863
                        catch (ClassNotFoundException | NoClassDefFoundError e) {
×
864
                                log.warn("Could not load advice point [{}]", advice.getPoint(), e);
×
865
                        }
×
866
                }
×
867
        }
×
868
        
869
        /**
870
         * Execute the given sql diff section for the given module
871
         *
872
         * @param module the module being executed on
873
         * @param version the version of this sql diff
874
         * @param sql the actual sql statements to run (separated by semi colons)
875
         */
876
        private static void runDiff(Module module, String version, String sql) {
877
                AdministrationService as = Context.getAdministrationService();
×
878
                
879
                String key = module.getModuleId() + ".database_version";
×
880
                GlobalProperty gp = as.getGlobalPropertyObject(key);
×
881
                
882
                boolean executeSQL = false;
×
883
                
884
                // check given version against current version
885
                if (gp != null && StringUtils.hasLength(gp.getPropertyValue())) {
×
886
                        String currentDbVersion = gp.getPropertyValue();
×
887
                        if (log.isDebugEnabled()) {
×
888
                                log.debug("version:column {}:{}", version, currentDbVersion);
×
889
                                log.debug("compare: {}", ModuleUtil.compareVersion(version, currentDbVersion));
×
890
                        }
891
                        if (ModuleUtil.compareVersion(version, currentDbVersion) > 0) {
×
892
                                executeSQL = true;
×
893
                        }
894
                } else {
×
895
                        executeSQL = true;
×
896
                }
897
                
898
                // version is greater than the currently installed version. execute this update.
899
                if (executeSQL) {
×
900
                        try {
901
                                Context.addProxyPrivilege(PrivilegeConstants.SQL_LEVEL_ACCESS);
×
902
                                log.debug("Executing sql: " + sql);
×
903
                                String[] sqlStatements = sql.split(";");
×
904
                                for (String sqlStatement : sqlStatements) {
×
905
                                        if (sqlStatement.trim().length() > 0) {
×
906
                                                as.executeSQL(sqlStatement, false);
×
907
                                        }
908
                                }
909
                        }
910
                        finally {
911
                                Context.removeProxyPrivilege(PrivilegeConstants.SQL_LEVEL_ACCESS);
×
912
                        }
913
                        
914
                        // save the global property
915
                        try {
916
                                Context.addProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES);
×
917
                                
918
                                String description = "DO NOT MODIFY.  Current database version number for the " + module.getModuleId()
×
919
                                        + " module.";
920
                                
921
                                if (gp == null) {
×
922
                                        log.info("Global property " + key + " was not found. Creating one now.");
×
923
                                        gp = new GlobalProperty(key, version, description);
×
924
                                        as.saveGlobalProperty(gp);
×
925
                                } else if (!gp.getPropertyValue().equals(version)) {
×
926
                                        log.info("Updating global property " + key + " to version: " + version);
×
927
                                        gp.setDescription(description);
×
928
                                        gp.setPropertyValue(version);
×
929
                                        as.saveGlobalProperty(gp);
×
930
                                } else {
931
                                        log.error("Should not be here. GP property value and sqldiff version should not be equal");
×
932
                                }
933
                                
934
                        }
935
                        finally {
936
                                Context.removeProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES);
×
937
                        }
938
                        
939
                }
940
                
941
        }
×
942
        
943
        /**
944
         * Execute all not run changeSets in liquibase.xml for the given module
945
         *
946
         * @param module the module being executed on
947
         */
948
        private static void runLiquibase(Module module) {
949
                ModuleClassLoader moduleClassLoader = getModuleClassLoader(module);
1✔
950
                boolean liquibaseFileExists = false;
1✔
951
                
952
                if (moduleClassLoader != null) {
1✔
953
                        try (InputStream inStream = moduleClassLoader.getResourceAsStream(MODULE_CHANGELOG_FILENAME)) {
1✔
954
                                liquibaseFileExists = (inStream != null);
1✔
955
                        }
956
                        catch (IOException ignored) {
×
957
                                
958
                        }
1✔
959
                }
960
                
961
                if (liquibaseFileExists) {
1✔
962
                        try {
963
                                // run liquibase.xml by Liquibase API
964
                                DatabaseUpdater.executeChangelog(MODULE_CHANGELOG_FILENAME, new Contexts(), null, moduleClassLoader);
×
965
                        }
966
                        catch (InputRequiredException e) {
×
967
                                // the user would be stepped through the questions returned here.
968
                                throw new ModuleException("Input during database updates is not yet implemented.", module.getName(), e);
×
969
                        }
970
                        catch (Exception e) {
×
971
                                throw new ModuleException("Unable to update data model using " + MODULE_CHANGELOG_FILENAME + ".",
×
972
                                        module.getName(), e);
×
973
                        }
×
974
                }
975
        }
1✔
976
        
977
        /**
978
         * Runs through the advice and extension points and removes from api. <br>
979
         * Also calls mod.Activator.shutdown()
980
         *
981
         * @param mod module to stop
982
         * @see ModuleFactory#stopModule(Module, boolean, boolean)
983
         */
984
        public static void stopModule(Module mod) {
985
                stopModule(mod, false, false);
1✔
986
        }
1✔
987
        
988
        /**
989
         * Runs through the advice and extension points and removes from api.<br>
990
         * Also calls mod.Activator.shutdown()
991
         *
992
         * @param mod the module to stop
993
         * @param isShuttingDown true if this is called during the process of shutting down openmrs
994
         * @see #stopModule(Module, boolean, boolean)
995
         */
996
        public static void stopModule(Module mod, boolean isShuttingDown) {
997
                stopModule(mod, isShuttingDown, false);
1✔
998
        }
1✔
999
        
1000
        /**
1001
         * Runs through the advice and extension points and removes from api.<br>
1002
         * <code>skipOverStartedProperty</code> should only be true when openmrs is stopping modules because
1003
         * it is shutting down. When normally stopping a module, use {@link #stopModule(Module)} (or leave
1004
         * value as false). This property controls whether the globalproperty is set for startup/shutdown.
1005
         * <br>
1006
         * Also calls module's {@link ModuleActivator#stopped()}
1007
         *
1008
         * @param mod module to stop
1009
         * @param skipOverStartedProperty true if we don't want to set &lt;moduleid&gt;.started to false
1010
         * @param isFailedStartup true if this is being called as a cleanup because of a failed module
1011
         *            startup
1012
         * @return list of dependent modules that were stopped because this module was stopped. This will
1013
         *         never be null.
1014
         */
1015
        public static List<Module> stopModule(Module mod, boolean skipOverStartedProperty, boolean isFailedStartup)
1016
                throws ModuleMustStartException {
1017
                
1018
                List<Module> dependentModulesStopped = new ArrayList<>();
1✔
1019
                
1020
                if (mod != null) {
1✔
1021
                        
1022
                        if (!ModuleFactory.isModuleStarted(mod)) {
1✔
UNCOV
1023
                                return dependentModulesStopped;
×
1024
                        }
1025
                        
1026
                        try {
1027
                                // if extends BaseModuleActivator
1028
                                if (mod.getModuleActivator() != null) {
1✔
1029
                                        mod.getModuleActivator().willStop();
1✔
1030
                                }
1031
                        }
1032
                        catch (Exception t) {
×
1033
                                log.warn("Unable to call module's Activator.willStop() method", t);
×
1034
                        }
1✔
1035
                        
1036
                        String moduleId = mod.getModuleId();
1✔
1037
                        
1038
                        // don't allow mandatory modules to be stopped
1039
                        // don't use database checks here because spring might be in a bad state
1040
                        if (!isFailedStartup && mod.isMandatory()) {
1✔
1041
                                throw new MandatoryModuleException(moduleId);
×
1042
                        }
1043

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