View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
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  }