001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.server.startup;
021    
022    import org.apache.commons.io.FileUtils;
023    import org.apache.commons.io.IOUtils;
024    import org.apache.commons.lang.CharUtils;
025    import org.sonar.api.platform.PluginMetadata;
026    import org.sonar.core.plugins.DefaultPluginMetadata;
027    import org.sonar.core.plugins.RemotePlugin;
028    import org.sonar.server.platform.DefaultServerFileSystem;
029    import org.sonar.server.plugins.DefaultServerPluginRepository;
030    
031    import java.io.File;
032    import java.io.FileWriter;
033    import java.io.IOException;
034    
035    /**
036     * @since 2.11
037     */
038    public final class GeneratePluginIndex {
039    
040      private DefaultServerFileSystem fileSystem;
041      private DefaultServerPluginRepository repository;
042    
043      public GeneratePluginIndex(DefaultServerFileSystem fileSystem, DefaultServerPluginRepository repository) {
044        this.fileSystem = fileSystem;
045        this.repository = repository;
046      }
047    
048      public void start() throws IOException {
049        writeIndex(fileSystem.getPluginIndex());
050      }
051    
052      void writeIndex(File indexFile) throws IOException {
053        FileUtils.forceMkdir(indexFile.getParentFile());
054        FileWriter writer = new FileWriter(indexFile, false);
055        try {
056          for (PluginMetadata metadata : repository.getMetadata()) {
057            if (!repository.isDisabled(metadata.getKey())) {
058              writer.append(RemotePlugin.create((DefaultPluginMetadata) metadata).marshal());
059              writer.append(CharUtils.LF);
060            }
061          }
062          writer.flush();
063    
064        } finally {
065          IOUtils.closeQuietly(writer);
066        }
067      }
068    }