Line data Source code
1 : // Copyright (C) 2014 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.pgm; 16 : 17 : import static com.google.common.collect.ImmutableList.toImmutableList; 18 : import static com.google.gerrit.pgm.init.InitPlugins.JAR; 19 : import static com.google.gerrit.pgm.init.InitPlugins.PLUGIN_DIR; 20 : 21 : import com.google.common.flogger.FluentLogger; 22 : import com.google.gerrit.launcher.GerritLauncher; 23 : import com.google.gerrit.pgm.init.PluginsDistribution; 24 : import com.google.inject.Singleton; 25 : import java.io.File; 26 : import java.io.FileNotFoundException; 27 : import java.io.IOException; 28 : import java.io.InputStream; 29 : import java.util.List; 30 : import java.util.zip.ZipEntry; 31 : import java.util.zip.ZipFile; 32 : 33 : @Singleton 34 15 : public class WarDistribution implements PluginsDistribution { 35 15 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 36 : 37 : @Override 38 : public void foreach(Processor processor) throws IOException { 39 1 : File myWar = GerritLauncher.getDistributionArchive(); 40 1 : if (myWar.isFile()) { 41 1 : try (ZipFile zf = new ZipFile(myWar)) { 42 1 : for (ZipEntry ze : entriesOf(zf)) { 43 1 : if (ze.isDirectory()) { 44 1 : continue; 45 : } 46 : 47 1 : if (ze.getName().startsWith(PLUGIN_DIR) && ze.getName().endsWith(JAR)) { 48 0 : String pluginJarName = new File(ze.getName()).getName(); 49 0 : String pluginName = pluginJarName.substring(0, pluginJarName.length() - JAR.length()); 50 0 : try (InputStream in = zf.getInputStream(ze)) { 51 0 : processor.process(pluginName, in); 52 0 : } catch (IOException ioe) { 53 0 : logger.atSevere().log("Error opening plugin %s: %s", ze.getName(), ioe.getMessage()); 54 0 : } 55 : } 56 1 : } 57 : } 58 : } 59 1 : } 60 : 61 : @Override 62 : public List<String> listPluginNames() throws FileNotFoundException { 63 : // not yet used 64 0 : throw new UnsupportedOperationException(); 65 : } 66 : 67 : private static Iterable<? extends ZipEntry> entriesOf(ZipFile zipFile) { 68 1 : return zipFile.stream().collect(toImmutableList()); 69 : } 70 : }