<commit_before>/*
 * Copyright 2014 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.addon.ui.impl.command;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.jboss.forge.addon.ui.command.CommandFactory;
import org.jboss.forge.addon.ui.command.CommandProvider;
import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.util.Commands;
import org.jboss.forge.addon.ui.wizard.UIWizardStep;
import org.jboss.forge.furnace.addons.AddonRegistry;
import org.jboss.forge.furnace.services.Imported;

/**
 * Creates and manages commands
 *
 * @author <a href="mailto:<EMAIL>"><NAME>, III</a>
 * @author <a href="<EMAIL>"><NAME></a>
 */
@Singleton
public class CommandFactoryImpl implements CommandFactory
{
   @Inject
   private AddonRegistry registry;

   private static final Logger log = Logger.getLogger(CommandFactoryImpl.class.getName());

   @Override
   public Iterable<UICommand> getCommands()
   {
      Set<UICommand> result = new HashSet<>();
      synchronized (this)
      {
         Imported<CommandProvider> instances = registry.getServices(CommandProvider.class);
         for (CommandProvider provider : instances)
         {
            Iterable<UICommand> commands = provider.getCommands();
            Iterator<UICommand> iterator = commands.iterator();
            while (iterator.hasNext())
            {
               UICommand command = iterator.next();
               if (!(command instanceof UIWizardStep))
               {
                  result.add(command);
               }
            }
            instances.release(provider);
         }
      }
      return result;
   }

   @Override
   public Set<String> getEnabledCommandNames(UIContext context)
   {
      Set<String> commands = new TreeSet<>();
      Iterable<UICommand> allCommands = getCommands();
      for (UICommand cmd : allCommands)
      {
         try
         {
            if (Commands.isEnabled(cmd, context))
            {
               commands.add(getCommandName(context, cmd));
            }
         }
         catch (Exception e)
         {
            log.log(Level.SEVERE, "Error while checking if command " + cmd + " isEnabled", e);
         }
      }
      return commands;
   }
<commit_msg>FORGE-1781: Do not fail if services cannot be constructed<commit_after>/*
 * Copyright 2014 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.addon.ui.impl.command;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.jboss.forge.addon.ui.command.CommandFactory;
import org.jboss.forge.addon.ui.command.CommandProvider;
import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.util.Commands;
import org.jboss.forge.addon.ui.wizard.UIWizardStep;
import org.jboss.forge.furnace.addons.AddonRegistry;
import org.jboss.forge.furnace.services.Imported;

/**
 * Creates and manages commands
 *
 * @author <a href="mailto:<EMAIL>"><NAME>, III</a>
 * @author <a href="<EMAIL>"><NAME></a>
 */
@Singleton
public class CommandFactoryImpl implements CommandFactory
{
   @Inject
   private AddonRegistry registry;

   private static final Logger log = Logger.getLogger(CommandFactoryImpl.class.getName());

   @Override
   public Iterable<UICommand> getCommands()
   {
      Set<UICommand> result = new HashSet<>();
      synchronized (this)
      {
         Imported<CommandProvider> instances = registry.getServices(CommandProvider.class);
         for (CommandProvider provider : instances)
         {
            Iterable<UICommand> commands = provider.getCommands();
            Iterator<UICommand> iterator = commands.iterator();
            while (iterator.hasNext())
            {
               try
               {
                  UICommand command = iterator.next();
                  if (!(command instanceof UIWizardStep))
                  {
                     result.add(command);
                  }
               }
               catch (Exception e)
               {
                  log.log(Level.SEVERE, "Error while retrieving command instance", e);
               }
            }
            instances.release(provider);
         }
      }
      return result;
   }

   @Override
   public Set<String> getEnabledCommandNames(UIContext context)
   {
      Set<String> commands = new TreeSet<>();
      Iterable<UICommand> allCommands = getCommands();
      for (UICommand cmd : allCommands)
      {
         try
         {
            if (Commands.isEnabled(cmd, context))
            {
               commands.add(getCommandName(context, cmd));
            }
         }
         catch (Exception e)
         {
            log.log(Level.SEVERE, "Error while checking if command " + cmd + " isEnabled", e);
         }
      }
      return commands;
   }

{'fraction_non_alphanumeric': 0.11240588024381498, 'fraction_numerical': 0.0035855145213338113, 'mean_word_length': 2.4695273631840795, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, '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': '6443871', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1618, 'n_words': 330}