<commit_before>/*
 * Copyright (c) 2012-2017 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Red Hat, Inc. - initial API and implementation
 */
package org.eclipse.che.ide.macro;

import static org.eclipse.che.api.core.model.workspace.WorkspaceStatus.RUNNING;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.web.bindery.event.shared.EventBus;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.eclipse.che.api.core.model.workspace.runtime.Machine;
import org.eclipse.che.api.core.model.workspace.runtime.Server;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.macro.BaseMacro;
import org.eclipse.che.ide.api.macro.Macro;
import org.eclipse.che.ide.api.macro.MacroRegistry;
import org.eclipse.che.ide.api.workspace.WsAgentServerUtil;
import org.eclipse.che.ide.api.workspace.event.WorkspaceRunningEvent;
import org.eclipse.che.ide.api.workspace.event.WorkspaceStoppedEvent;
import org.eclipse.che.ide.api.workspace.model.MachineImpl;
import org.eclipse.che.ide.bootstrap.BasicIDEInitializedEvent;

/**
 * For every server in dev-machine registers a {@link Macro} that returns server's URL.
 *
 * <p>Macro name: <code>${server.server_reference}</code>.
 *
 * @author <NAME>
 */
@Singleton
public class ServerAddressMacroRegistrar {

  private static final String MACRO_NAME_TEMPLATE = "${server.%}";

  private final Provider<MacroRegistry> macroRegistryProvider;
  private final AppContext appContext;
  private final WsAgentServerUtil wsAgentServerUtil;

  private Set<Macro> macros;

  @Inject
  public ServerAddressMacroRegistrar(
      EventBus eventBus,
      Provider<MacroRegistry> macroRegistryProvider,
      AppContext appContext,
      WsAgentServerUtil wsAgentServerUtil) {
    this.macroRegistryProvider = macroRegistryProvider;
    this.appContext = appContext;
    this.wsAgentServerUtil = wsAgentServerUtil;

    eventBus.addHandler(
        BasicIDEInitializedEvent.TYPE,
        e -> {
          if (appContext.getWorkspace().getStatus() == RUNNING) {
            registerMacros();
          }
        });

    eventBus.addHandler(WorkspaceRunningEvent.TYPE, e -> registerMacros());

    eventBus.addHandler(
        WorkspaceStoppedEvent.TYPE,
        e -> {
          if (macros  != null) {
            macros.forEach(macro -> macroRegistryProvider.get().unregister(macro));
            macros.clear();<commit_msg>Fix codestyle (#8046)<commit_after>/*
 * Copyright (c) 2012-2017 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Red Hat, Inc. - initial API and implementation
 */
package org.eclipse.che.ide.macro;

import static org.eclipse.che.api.core.model.workspace.WorkspaceStatus.RUNNING;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.web.bindery.event.shared.EventBus;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.eclipse.che.api.core.model.workspace.runtime.Machine;
import org.eclipse.che.api.core.model.workspace.runtime.Server;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.macro.BaseMacro;
import org.eclipse.che.ide.api.macro.Macro;
import org.eclipse.che.ide.api.macro.MacroRegistry;
import org.eclipse.che.ide.api.workspace.WsAgentServerUtil;
import org.eclipse.che.ide.api.workspace.event.WorkspaceRunningEvent;
import org.eclipse.che.ide.api.workspace.event.WorkspaceStoppedEvent;
import org.eclipse.che.ide.api.workspace.model.MachineImpl;
import org.eclipse.che.ide.bootstrap.BasicIDEInitializedEvent;

/**
 * For every server in dev-machine registers a {@link Macro} that returns server's URL.
 *
 * <p>Macro name: <code>${server.server_reference}</code>.
 *
 * @author <NAME>
 */
@Singleton
public class ServerAddressMacroRegistrar {

  private static final String MACRO_NAME_TEMPLATE = "${server.%}";

  private final Provider<MacroRegistry> macroRegistryProvider;
  private final AppContext appContext;
  private final WsAgentServerUtil wsAgentServerUtil;

  private Set<Macro> macros;

  @Inject
  public ServerAddressMacroRegistrar(
      EventBus eventBus,
      Provider<MacroRegistry> macroRegistryProvider,
      AppContext appContext,
      WsAgentServerUtil wsAgentServerUtil) {
    this.macroRegistryProvider = macroRegistryProvider;
    this.appContext = appContext;
    this.wsAgentServerUtil = wsAgentServerUtil;

    eventBus.addHandler(
        BasicIDEInitializedEvent.TYPE,
        e -> {
          if (appContext.getWorkspace().getStatus() == RUNNING) {
            registerMacros();
          }
        });

    eventBus.addHandler(WorkspaceRunningEvent.TYPE, e -> registerMacros());

    eventBus.addHandler(
        WorkspaceStoppedEvent.TYPE,
        e -> {
          if (macros != null) {
            macros.forEach(macro -> macroRegistryProvider.get().unregister(macro));
            macros.clear();
{'fraction_non_alphanumeric': 0.1087552549808079, 'fraction_numerical': 0.005117894352038019, 'mean_word_length': 3.9927007299270074, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 25, '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': '7325817', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1626, 'n_words': 335}