<commit_before>/*
 * Copyright 2013 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.jboss.forge.bootstrap.listener;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.jboss.forge.addon.manager.InstallRequest;
import org.jboss.forge.addon.manager.impl.AddonManagerImpl;
import org.jboss.forge.container.AddonId;
import org.jboss.forge.container.AddonRepository;
import org.jboss.forge.container.Forge;
import org.jboss.forge.container.exception.ContainerException;
import org.jboss.forge.container.spi.ContainerLifecycleListener;
import org.jboss.forge.dependencies.Coordinate;
import org.jboss.forge.dependencies.builder.CoordinateBuilder;
import org.jboss.forge.dependencies.builder.DependencyQueryBuilder;
import org.jboss.forge.maven.dependencies.FileResourceFactory;
import org.jboss.forge.maven.dependencies.MavenContainer;
import org.jboss.forge.maven.dependencies.MavenDependencyResolver;

/**
 * Installs the required addons
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 *
 */
public class InstallRequiredAddonsListener implements ContainerLifecycleListener
{
   private Logger logger = Logger.getLogger(getClass().getName());

   private MavenDependencyResolver resolver = new MavenDependencyResolver(new FileResourceFactory(),
            new MavenContainer());

   /**
    * These addons will be installed when forge starts
    */
   private static final String[] REQUIRED_ADDON_COORDINATES = {
            "ui",
            "maven",
            "convert",
            "dependencies",
            "facets",
            "ui-hints",
            "addon-manager",
            "projects",
            "resources"
   };

   @Override
   public void beforeStart(Forge forge) throws ContainerException
   {
      AddonRepository repository = forge.getRepository();
      List<AddonId> addons = new ArrayList<AddonId>();
      for (String addonCoordinate : REQUIRED_ADDON_COORDINATES)
      {
         AddonId addonId = toAddonId(addonCoordinate);
         if (repository.isDeployed(addonId))
         {
            logger.fine("Addon " + addonId + " is already deployed");
         }
         else
         {
            addons.add(addonId);
         }
      }
      install(forge, addons);
   }

   @Override
   public void beforeStop(Forge forge) throws ContainerException
   {
      // Do nothing
   }

   @Override
   public void afterStop(Forge forge) throws ContainerException
   {
      // Do nothing
   }

   private void install(Forge forge, Iterable<AddonId> addons)
   {
      try
      {
         AddonManagerImpl addonManager = new AddonManagerImpl(forge.getRepository(), resolver);

         for (AddonId addon : addons)
         {
            InstallRequest request = addonManager.install(addon);
            logger.info(request.toString());
            request.perform();
         }<commit_msg>Introduced skipPreInstall system variable to skip pre-installation of addons<commit_after>/*
 * Copyright 2013 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.jboss.forge.bootstrap.listener;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.jboss.forge.addon.manager.InstallRequest;
import org.jboss.forge.addon.manager.impl.AddonManagerImpl;
import org.jboss.forge.container.AddonId;
import org.jboss.forge.container.AddonRepository;
import org.jboss.forge.container.Forge;
import org.jboss.forge.container.exception.ContainerException;
import org.jboss.forge.container.spi.ContainerLifecycleListener;
import org.jboss.forge.dependencies.Coordinate;
import org.jboss.forge.dependencies.builder.CoordinateBuilder;
import org.jboss.forge.dependencies.builder.DependencyQueryBuilder;
import org.jboss.forge.maven.dependencies.FileResourceFactory;
import org.jboss.forge.maven.dependencies.MavenContainer;
import org.jboss.forge.maven.dependencies.MavenDependencyResolver;

/**
 * Installs the required addons
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 *
 */
public class InstallRequiredAddonsListener implements ContainerLifecycleListener
{
   private Logger logger = Logger.getLogger(getClass().getName());

   private MavenDependencyResolver resolver = new MavenDependencyResolver(new FileResourceFactory(),
            new MavenContainer());

   /**
    * These addons will be installed when forge starts
    */
   private static final String[] REQUIRED_ADDON_COORDINATES = {
            "ui",
            "maven",
            "convert",
            "dependencies",
            "facets",
            "ui-hints",
            "addon-manager",
            "projects",
            "resources"
   };

   @Override
   public void beforeStart(Forge forge) throws ContainerException
   {
      if (Boolean.getBoolean("skipPreInstall"))
      {
         logger.info("Skipping pre-installation of required addons");
      }
      else
      {
         logger.info("Pre-installing required addons...");
         AddonRepository repository = forge.getRepository();
         List<AddonId> addons = new ArrayList<AddonId>();
         for (String addonCoordinate : REQUIRED_ADDON_COORDINATES)
         {
            AddonId addonId = toAddonId(addonCoordinate);
            if (repository.isDeployed(addonId))
            {
               logger.fine("Addon " + addonId + " is already deployed");
            }
            else
            {
               addons.add(addonId);
            }
         }
         install(forge, addons);
      }
   }

   @Override
   public void beforeStop(Forge forge) throws ContainerException
   {
      // Do nothing
   }

   @Override
   public void afterStop(Forge forge) throws ContainerException
   {
      // Do nothing
   }

   private void install(Forge forge, Iterable<AddonId> addons)
   {
      try
      {
         AddonManagerImpl addonManager = new AddonManagerImpl(forge.getRepository(), resolver);

         for (AddonId addon : addons)
         {
            logger.info("Installing " + addon);
            InstallRequest request = addonManager.install(addon);
            logger.info(request.toString());
            request.perform();
         }
{'fraction_non_alphanumeric': 0.10103871576959396, 'fraction_numerical': 0.0025180988353792886, 'mean_word_length': 2.8398791540785497, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5111658', 'n_tokens_mistral': 1898, 'n_tokens_neox': 1781, 'n_words': 363}