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

com.cedarsoft.gdao.async.AsynchronousDaoManager Maven / Gradle / Ivy

package com.cedarsoft.gdao.async;

import com.cedarsoft.async.AsyncCallSupport;
import com.cedarsoft.async.CallbackCaller;
import com.cedarsoft.gdao.GenericDao;
import com.cedarsoft.gdao.GenericDaoManager;
import com.cedarsoft.gdao.LockProvider;
import com.cedarsoft.utils.Cache;
import com.cedarsoft.utils.HashedCache;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Iterator;

/**
 * Special implementation of {@link GenericDaoManager} that
 * returns {@link AsynchronousDao}s.
 */
public final class AsynchronousDaoManager implements GenericDaoManager {
  @NonNls
  @NotNull
  private static final Log log = LogFactory.getLog( AsynchronousDaoManager.class );
  @NotNull
  private final AsyncCallSupport> asyncCallSupport = new AsyncCallSupport>();

  @SuppressWarnings( {"MismatchedQueryAndUpdateOfCollection"} )
  @NotNull
  private final Cache, AsynchronousDao> daoCache = new HashedCache, AsynchronousDao>(
      new Cache.Factory, AsynchronousDao>() {
        @NotNull
        public AsynchronousDao create( @NotNull Class key ) {
          return asyncCallSupport.invoke( new AsyncDaoCreator( key ) );
        }
      }
  );

  public AsynchronousDaoManager( @NotNull final String description, @NotNull final GenericDaoManager delegatingManager ) {
    asyncCallSupport.initializeWorker( new CallbackCaller>() {
      public Object call( @NotNull AsyncDaoCreator callback ) throws Exception {
        log.debug( "executing callback" );
        return callback.execute( delegatingManager );
      }

      @NotNull
      public String getDescription() {
        return description;
      }
    } );
  }

  /**
   * Returns the asynchronous Dao
   *
   * @param type the type
   * @return the asynchronous dao for the given type
   */
  @NotNull
  public  AsynchronousDao getDao( @NotNull Class type ) {
    return ( AsynchronousDao ) daoCache.get( type );
  }

  @NotNull
  public  GenericDao getDao( @NotNull Class type, @Nullable LockProvider lockProvider ) {
    if ( lockProvider == null ) {
      return getDao( type );
    } else {
      throw new UnsupportedOperationException( "Lock provider not supported" );
    }
  }

  public void shutdown() {
    for ( Iterator> it = daoCache.values().iterator(); it.hasNext(); ) {
      AsynchronousDao dao = it.next();
      dao.shutdown();
      it.remove();
    }

    asyncCallSupport.shutdown();
  }

  private static class AsyncDaoCreator {
    @NotNull
    private final Class key;

    private AsyncDaoCreator( @NotNull Class key ) {
      this.key = key;
    }

    @NotNull
    public AsynchronousDao execute( @NotNull GenericDaoManager delegatingManager ) {
      log.debug( "Creating a new asynchronious dao for " + key.getName() );
      GenericDao dao = delegatingManager.getDao( key );
      AsynchronousDao asynchronousDao = new AsynchronousDao();
      asynchronousDao.initializeDelegatingDao( dao );
      return asynchronousDao;
    }
  }
}