Line data Source code
1 : // Copyright (C) 2015 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.plugins; 16 : 17 : import com.google.common.base.MoreObjects; 18 : import com.google.gerrit.extensions.annotations.PluginCanonicalWebUrl; 19 : import com.google.gerrit.extensions.annotations.PluginData; 20 : import com.google.gerrit.extensions.annotations.PluginName; 21 : import com.google.gerrit.lifecycle.LifecycleModule; 22 : import com.google.gerrit.metrics.MetricMaker; 23 : import com.google.gerrit.server.PluginUser; 24 : import com.google.inject.AbstractModule; 25 : import com.google.inject.Provides; 26 : import com.google.inject.ProvisionException; 27 : import java.io.File; 28 : import java.io.IOException; 29 : import java.nio.file.Files; 30 : import java.nio.file.Path; 31 : 32 : class ServerPluginInfoModule extends AbstractModule { 33 : private final ServerPlugin plugin; 34 : private final Path dataDir; 35 : 36 : private volatile boolean ready; 37 : private final MetricMaker serverMetrics; 38 : 39 13 : ServerPluginInfoModule(ServerPlugin plugin, MetricMaker serverMetrics) { 40 13 : this.plugin = plugin; 41 13 : this.dataDir = plugin.getDataDir(); 42 13 : this.serverMetrics = serverMetrics; 43 13 : } 44 : 45 : @Override 46 : protected void configure() { 47 13 : bind(PluginUser.class).toInstance(plugin.getPluginUser()); 48 13 : bind(String.class).annotatedWith(PluginName.class).toInstance(plugin.getName()); 49 13 : bind(String.class) 50 13 : .annotatedWith(PluginCanonicalWebUrl.class) 51 13 : .toInstance(plugin.getPluginCanonicalWebUrl()); 52 13 : bind(Plugin.class).toInstance(plugin); 53 : 54 13 : install( 55 13 : new LifecycleModule() { 56 : @Override 57 : public void configure() { 58 13 : PluginMetricMaker metrics = 59 : new PluginMetricMaker( 60 : serverMetrics, 61 13 : MoreObjects.firstNonNull( 62 13 : plugin.getMetricsPrefix(), String.format("plugins/%s/", plugin.getName()))); 63 13 : bind(MetricMaker.class).toInstance(metrics); 64 13 : listener().toInstance(metrics); 65 13 : } 66 : }); 67 13 : } 68 : 69 : @Provides 70 : @PluginData 71 : Path getPluginData() { 72 0 : if (!ready) { 73 0 : synchronized (dataDir) { 74 0 : if (!ready) { 75 : try { 76 0 : Files.createDirectories(dataDir); 77 0 : } catch (IOException e) { 78 0 : throw new ProvisionException( 79 0 : String.format( 80 0 : "Cannot create %s for plugin %s", dataDir.toAbsolutePath(), plugin.getName()), 81 : e); 82 0 : } 83 0 : ready = true; 84 : } 85 0 : } 86 : } 87 0 : return dataDir; 88 : } 89 : 90 : @Provides 91 : @PluginData 92 : File getPluginDataAsFile(@PluginData Path pluginData) { 93 0 : return pluginData.toFile(); 94 : } 95 : }