Line data Source code
1 : // Copyright (C) 2017 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.api.plugins; 16 : 17 : import static com.google.gerrit.server.api.ApiUtil.asRestApiException; 18 : 19 : import com.google.gerrit.extensions.api.plugins.InstallPluginInput; 20 : import com.google.gerrit.extensions.api.plugins.PluginApi; 21 : import com.google.gerrit.extensions.api.plugins.Plugins; 22 : import com.google.gerrit.extensions.common.PluginInfo; 23 : import com.google.gerrit.extensions.restapi.Response; 24 : import com.google.gerrit.extensions.restapi.RestApiException; 25 : import com.google.gerrit.extensions.restapi.TopLevelResource; 26 : import com.google.gerrit.server.plugins.InstallPlugin; 27 : import com.google.gerrit.server.plugins.ListPlugins; 28 : import com.google.gerrit.server.plugins.PluginsCollection; 29 : import com.google.inject.Inject; 30 : import com.google.inject.Provider; 31 : import com.google.inject.Singleton; 32 : import java.util.SortedMap; 33 : 34 : @Singleton 35 : public class PluginsImpl implements Plugins { 36 : private final PluginsCollection plugins; 37 : private final Provider<ListPlugins> listProvider; 38 : private final Provider<InstallPlugin> installProvider; 39 : private final PluginApiImpl.Factory pluginApi; 40 : 41 : @Inject 42 : PluginsImpl( 43 : PluginsCollection plugins, 44 : Provider<ListPlugins> listProvider, 45 : Provider<InstallPlugin> installProvider, 46 149 : PluginApiImpl.Factory pluginApi) { 47 149 : this.plugins = plugins; 48 149 : this.listProvider = listProvider; 49 149 : this.installProvider = installProvider; 50 149 : this.pluginApi = pluginApi; 51 149 : } 52 : 53 : @Override 54 : public PluginApi name(String name) throws RestApiException { 55 1 : return pluginApi.create(plugins.parse(name)); 56 : } 57 : 58 : @Override 59 : public ListRequest list() { 60 1 : return new ListRequest() { 61 : @Override 62 : public SortedMap<String, PluginInfo> getAsMap() throws RestApiException { 63 : try { 64 1 : return listProvider.get().request(this).apply(TopLevelResource.INSTANCE).value(); 65 1 : } catch (Exception e) { 66 1 : throw asRestApiException("Cannot list plugins", e); 67 : } 68 : } 69 : }; 70 : } 71 : 72 : @Override 73 : @Deprecated 74 : public PluginApi install( 75 : String name, com.google.gerrit.extensions.common.InstallPluginInput input) 76 : throws RestApiException { 77 1 : return install(name, convertInput(input)); 78 : } 79 : 80 : @SuppressWarnings("deprecation") 81 : private InstallPluginInput convertInput( 82 : com.google.gerrit.extensions.common.InstallPluginInput input) { 83 1 : InstallPluginInput result = new InstallPluginInput(); 84 1 : result.url = input.url; 85 1 : result.raw = input.raw; 86 1 : return result; 87 : } 88 : 89 : @Override 90 : public PluginApi install(String name, InstallPluginInput input) throws RestApiException { 91 : try { 92 3 : Response<PluginInfo> created = 93 3 : installProvider.get().setName(name).apply(TopLevelResource.INSTANCE, input); 94 3 : return pluginApi.create(plugins.parse(created.value().id)); 95 1 : } catch (Exception e) { 96 1 : throw asRestApiException("Cannot install plugin", e); 97 : } 98 : } 99 : }