All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cedarsoft.gdao.DefaultGenericServiceManager Maven / Gradle / Ivy

package com.cedarsoft.gdao;

import com.cedarsoft.utils.Cache;
import com.cedarsoft.utils.HashedCache;
import org.jetbrains.annotations.NotNull;
import org.springframework.transaction.support.AbstractPlatformTransactionManager;

/**
 * Manages the services
 */
public class DefaultGenericServiceManager implements GenericServiceManager {
  @NotNull
  private final GenericDaoManager daoManager;
  @NotNull
  private final AbstractPlatformTransactionManager transactionManager;

  @SuppressWarnings( {"MismatchedQueryAndUpdateOfCollection"} )
  @NotNull
  private final Cache, GenericService> serviceCache;

  /**
   * Should only be necessary for testing purposes
   *
   * @param transactionManager the transaction manager
   * @param daoManager         the dao manager
   * @param factory            the factory
   */
  protected DefaultGenericServiceManager( @NotNull AbstractPlatformTransactionManager transactionManager, @NotNull GenericDaoManager daoManager, @NotNull Cache.Factory, GenericService> factory ) {
    serviceCache = new HashedCache, GenericService>( factory );
    this.transactionManager = transactionManager;
    this.daoManager = daoManager;

  }

  /**
   * Creates a new service manager
   *
   * @param transactionManager the transaction manager
   * @param daoManager         the dao manager
   */
  public DefaultGenericServiceManager( @NotNull AbstractPlatformTransactionManager transactionManager, @NotNull GenericDaoManager daoManager ) {
    this.transactionManager = transactionManager;
    this.daoManager = daoManager;
    serviceCache = new HashedCache, GenericService>( new Cache.Factory, GenericService>() {
      @NotNull
      public GenericService create( @NotNull Class key ) {
        return new GenericServiceImpl( DefaultGenericServiceManager.this.daoManager.getDao( key ), DefaultGenericServiceManager.this.transactionManager );
      }
    } );
  }

  /**
   * Returns the service for the given type
   *
   * @param type the type
   * @return the service for the given type
   */
  @NotNull
  public  GenericService getService( @NotNull Class type ) {
    return ( GenericService ) serviceCache.get( type );
  }
}