
package de.ivu.elect.business.security.control;

import de.ivu.elect.business.konfiguration.entity.ElectKonfigurationWerte;
import de.ivu.elect.business.konfiguration.entity.KonfigurationWertGeaendertEvent;
import de.ivu.elect.util.ElectRandom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.enterprise.event.Observes;
import javax.enterprise.event.Reception;
import javax.enterprise.event.TransactionPhase;

public class RandomGeneratorKonfigurationChangeListener {

  private static final Logger LOG = LoggerFactory.getLogger(RandomGeneratorKonfigurationChangeListener.class);

  public void listen(@Observes(during = TransactionPhase.AFTER_COMPLETION, notifyObserver = Reception.ALWAYS)
                             KonfigurationWertGeaendertEvent konfigurationWertGeaendertEvent) {
    try {
      if (ElectKonfigurationWerte.ELECT_COMMON_RANDOM_GENERATOR_SECURE.equals(konfigurationWertGeaendertEvent.getKey())) {
        setNewRandom(Boolean.valueOf(konfigurationWertGeaendertEvent.getNeuerWert()));
      }
    } catch (Exception e) {
      LOG.warn("Fehler bei KonfigurationEvent: " + e.getMessage(), e);
    }

  }

  protected void setNewRandom(Boolean strongRandom) {
    LOG.info("SecureRandom.strong: " + strongRandom);
    ElectRandom.USE_STRONG_RANDOM_GENERATOR.set(strongRandom);
  }
}
