1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.opencastproject.plugin.command;
23
24 import org.opencastproject.plugin.PluginManager;
25 import org.opencastproject.plugin.command.completers.PluginNameCompleter;
26 import org.opencastproject.plugin.impl.PluginManagerImpl;
27
28 import org.apache.karaf.config.core.ConfigRepository;
29 import org.apache.karaf.shell.api.action.Action;
30 import org.apache.karaf.shell.api.action.Argument;
31 import org.apache.karaf.shell.api.action.Command;
32 import org.apache.karaf.shell.api.action.Completion;
33 import org.apache.karaf.shell.api.action.Option;
34 import org.apache.karaf.shell.api.action.lifecycle.Reference;
35 import org.apache.karaf.shell.api.action.lifecycle.Service;
36 import org.osgi.service.cm.Configuration;
37
38 import java.util.Collections;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Set;
42 import java.util.function.Function;
43 import java.util.stream.Collectors;
44
45 @Service
46 @Command(scope = "opencast", name = "plugin-disable", description = "Disable a plugin with the specified name")
47 public class PluginDisable implements Action {
48
49 @Reference
50 private ConfigRepository configRepository;
51
52 @Reference
53 private PluginManager pluginManager;
54
55 @Argument(name = "plugins", description = "The name of the plugin to disable.", required = true, multiValued = true)
56 @Completion(PluginNameCompleter.class)
57 private List<String> plugins;
58
59 @Option(name = "--persist", description = "Persist changes", required = false, multiValued = false)
60 private boolean persist = false;
61
62 @Override
63 public Object execute() throws Exception {
64 var properties = configRepository.getConfigAdmin()
65 .getConfiguration(PluginManagerImpl.class.getName()).getProperties();
66
67 Set<String> available = pluginManager.listAvailablePlugins();
68 for (String plugin : plugins) {
69 if (!available.contains(plugin)) {
70 throw new IllegalArgumentException("Plugin " + plugin + " is invalid");
71 } else {
72 properties.put(plugin, "off");
73 }
74 }
75
76 if (persist) {
77 Map<String, Object> propertiesMap = Collections.list(properties.keys())
78 .stream()
79 .collect(Collectors.toMap(Function.identity(), properties::get));
80 configRepository.update(PluginManagerImpl.class.getName(), propertiesMap);
81 } else {
82 Configuration c = configRepository.getConfigAdmin().getConfiguration(PluginManagerImpl.class.getName());
83 c.update(properties);
84 }
85 return null;
86 }
87 }