

package org.waveprotocol.wave.examples.fedone.waveserver;

import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.name.Named;

import org.waveprotocol.wave.crypto.CachedCertPathValidator;
import org.waveprotocol.wave.crypto.CertPathStore;
import org.waveprotocol.wave.crypto.DefaultCacheImpl;
import org.waveprotocol.wave.crypto.DefaultCertPathStore;
import org.waveprotocol.wave.crypto.DefaultTimeSource;
import org.waveprotocol.wave.crypto.DefaultTrustRootsProvider;
import org.waveprotocol.wave.crypto.DisabledCertPathValidator;
import org.waveprotocol.wave.crypto.TimeSource;
import org.waveprotocol.wave.crypto.TrustRootsProvider;
import org.waveprotocol.wave.crypto.VerifiedCertChainCache;
import org.waveprotocol.wave.crypto.WaveCertPathValidator;
import org.waveprotocol.wave.crypto.WaveSignatureVerifier;
import org.waveprotocol.wave.crypto.WaveSigner;
import org.waveprotocol.wave.examples.fedone.waveserver.WaveClientRpc.ProtocolWaveClientRpc;
import org.waveprotocol.wave.model.id.WaveletName;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * Guice Module for the prototype Server.
 *
 *
 */
public class WaveServerModule extends AbstractModule {

  private static class LocalWaveletContainerFactory implements LocalWaveletContainer.Factory {
    @Override
    public LocalWaveletContainer create(WaveletName waveletName) {
      return new LocalWaveletContainerImpl(waveletName);
    }
  }

  private static class RemoteWaveletContainerFactory implements RemoteWaveletContainer.Factory {
    @Override
    public RemoteWaveletContainer create(WaveletName waveletName) {
      return new RemoteWaveletContainerImpl(waveletName);
    }
  }

  // NOTE:
  // This is a fairly important thread pool: It's currently used by the bundling
  // signer... see DeltaSignerProvider.java.
  // For each bundle the thread will perform the signature and call the
  // listener for each signing request, this will do work like submit deltas
  // to wavelets and broadcast results of the submit. Depending on the timeout
  // on the bundling accumulation delay and bundle size parameters, the thread
  // pool size will affect the performance of your server when it comes to
  // dealing with client submits. (see WaveServerImpl.submitDelta).

  @Provides
  private static ScheduledExecutorService provideScheduledExecutorService() {
    return Executors.newScheduledThreadPool(4);
  }

  @Override
  protected void configure() {
    bind(CertPathStore.class).to(DefaultCertPathStore.class).in(Singleton.class);
    bind(TimeSource.class).to(DefaultTimeSource.class).in(Singleton.class);
    bind(WaveSigner.class).toProvider(WaveSignerProvider.class);

    try {
      bind(WaveSignatureVerifier.class).toConstructor(WaveSignatureVerifier.class.getConstructor(
          WaveCertPathValidator.class, CertPathStore.class));
      bind(VerifiedCertChainCache.class).to(DefaultCacheImpl.class).in(Singleton.class);
      bind(DefaultCacheImpl.class).toConstructor(
          DefaultCacheImpl.class.getConstructor(TimeSource.class));
    } catch (NoSuchMethodException e) {
      throw new IllegalStateException(e);
    }

    bind(TrustRootsProvider.class).to(DefaultTrustRootsProvider.class).in(Singleton.class);
    bind(CertificateManager.class).to(CertificateManagerImpl.class).in(Singleton.class);
    bind(WaveletProvider.class).to(WaveServer.class);
    bind(WaveServer.class).to(WaveServerImpl.class).in(Singleton.class);
    bind(ClientFrontend.class).to(ClientFrontendImpl.class).in(Singleton.class);
    bind(ProtocolWaveClientRpc.Interface.class).to(WaveClientRpcImpl.class).in(Singleton.class);
    bind(LocalWaveletContainer.Factory.class).to(LocalWaveletContainerFactory.class)
        .in(Singleton.class);
    bind(RemoteWaveletContainer.Factory.class).to(RemoteWaveletContainerFactory.class)
        .in(Singleton.class);
  }

  /**
   * Guice provider of {@code WaveCertPathValidator}s.
   */
  @Provides
  protected WaveCertPathValidator provideWaveCertPathValidator(
      @Named("waveserver_disable_signer_verification") boolean disableSignerVerification,
      TimeSource timeSource, VerifiedCertChainCache certCache,
      TrustRootsProvider trustRootsProvider) {
    if (disableSignerVerification) {
      return new DisabledCertPathValidator();
    } else {
      return new CachedCertPathValidator(certCache, timeSource, trustRootsProvider);
    }
  }
}
