/**
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

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;

/**
 * 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);
    }
  }

  @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);
    }
  }
}

@@ -1683,16 +1683,110 @@
 tName;%0A%0A
+import java.util.concurrent.Executors;%0Aimport java.util.concurrent.ScheduledExecutorService;%0A%0A
 /**%0A * G
@@ -2381,16 +2381,747 @@
  %7D%0A  %7D%0A%0A
+  // NOTE:%0A  // This is a fairly important thread pool: It's currently used by the bundling%0A  // signer... see DeltaSignerProvider.java.%0A  // For each bundle the thread will perform the signature and call the%0A  // listener for each signing request, this will do work like submit deltas%0A  // to wavelets and broadcast results of the submit. Depending on the timeout%0A  // on the bundling accumulation delay and bundle size parameters, the thread%0A  // pool size will affect the performance of your server when it comes to%0A  // dealing with client submits. (see WaveServerImpl.submitDelta).%0A%0A  @Provides%0A  private static ScheduledExecutorService provideScheduledExecutorService() %7B%0A    return Executors.newScheduledThreadPool(4);%0A  %7D%0A%0A
   @Overr

Create thread pool in Main().