net.sourceforge.domian.repository
Class PartitionRepositoryInvocationHandler<T extends Entity>

java.lang.Object
  extended by net.sourceforge.domian.repository.AbstractRepository<T>
      extended by net.sourceforge.domian.repository.AbstractDomianCoreRepository<T>
          extended by net.sourceforge.domian.repository.PartitionRepositoryInvocationHandler<T>
All Implemented Interfaces:
java.lang.reflect.InvocationHandler, PartitionRepository<T>, PersistentRepository<T>, Repository<T>
Direct Known Subclasses:
PartitionRepositoryReuseInvocationHandler

public class PartitionRepositoryInvocationHandler<T extends Entity>
extends AbstractDomianCoreRepository<T>
implements java.lang.reflect.InvocationHandler, PartitionRepository<T>, PersistentRepository<T>

InvocationHandler implementation for partition repositories. All repositories either consisting of, or belonging to a PartitionRepository may use this repository wrapper.

Since:
0.4
Author:
Eirik Torske

Field Summary
protected  boolean executeOperationsExclusivelyOnly
           
protected  net.sourceforge.domian.repository.PartitionRepositoryInvocationHandler.IteratorRegistry iteratorRegistry
          A internal registry keeping track of all active iterators.
protected  Specification<T> partitionSpecification
           
protected  Repository<T> repositoryDelegate
           
protected  boolean repositoryDelegateIsAlreadyPartitioned
           
protected  java.util.Map<Specification<? extends T>,PartitionRepository> subPartitions
          All sub-partition repositories, indexed by a unique specification.
protected  PartitionRepository<? super T> superPartitionRepository
           
protected  Specification<? super T> superPartitionSpecification
           
protected  Synchronizer synchronizer
          A synchronizer used to control concurrent and exclusive access to repository methods.
 
Fields inherited from class net.sourceforge.domian.repository.AbstractDomianCoreRepository
DEFAULT_DOMIAN_ROOT_DIR_NAME, DEFAULT_DOMIAN_ROOT_PATH, log
 
Fields inherited from class net.sourceforge.domian.repository.AbstractRepository
supportsRecursiveIndexing, usesNativePartitioningSupport
 
Method Summary
protected
<V extends T>
PartitionRepository<V>
addPartition(Specification<V> partitionSpecification, PartitionRepository<? extends V> partitionRepository)
           
<V extends T>
PartitionRepository<V>
addPartitionFor(Specification<V> partitionSpecification)
          Adds a new partition to this repository.
<V extends T>
PartitionRepository<V>
addPartitionFor(Specification<V> partitionSpecification, Repository<? super V> partitionRepository)
          Adds a new partition to this repository.
<V extends T>
PartitionRepository<V>
addPartitionFor(Specification<V> partitionSpecification, java.lang.String repositoryId)
          Adds a new partition to this repository.
 void close()
          Closes the repository and releases all resources.
 void collectAllPartitions(java.util.Map<Specification<? extends T>,PartitionRepository> partitionMap)
          Populates the given map with a flattened and recursive view of all partitions in this repository.
<R extends Repository>
void
collectAllPartitionsWithRepositorySatisfying(Specification<R> specification, java.util.Map<Specification<? extends T>,PartitionRepository> partitionMap)
          Populates the given map with a flattened and recursive view of all partitions in this repository, satisfying the given specification.
protected
<V> V
conditionalSynchronizedExecutionOf(java.util.concurrent.Callable<V> operation)
           
protected
<V extends T>
java.lang.Long
countAllEntitiesWithoutSpecificationSharpening(Specification<V> specification)
          Special count method without sharpening original specifications to fit partition specifications.
protected  java.lang.Boolean entityExists_PossiblyInWrongPartition(T entity)
           
<V extends T>
java.util.Collection<V>
findAllEntitiesSpecifiedBy(Specification<V> specification)
          Finds and returns all entities approved by the given specification.
protected
<V extends T>
java.util.Collection<V>
findAllEntitiesSpecifiedBy(Specification<V> specification, java.lang.Boolean sharpenOriginalSpecificationsToFitPartitionSpecification)
           
<V extends T>
PartitionRepository
findPartitionFor(Specification<V> partitionSpecification)
           
<V extends T>
V
findSingleEntitySpecifiedBy(Specification<V> specification)
          Finds and returns one single entity approved by the given specification.
 java.util.Map<Specification<? extends T>,PartitionRepository> getAllPartitions()
          Optional: mainly added for testing purposes, may throw UnsupportedOperationException
protected  PartitionRepository getCorrectPartitionRepository(PartitionRepository originalPartitionRepository, PartitionRepository toBeReusedPartitionRepository)
           
 java.lang.String getFormat()
          This method applies to this partition only!
 EntityPersistenceMetaData getMetaDataFor(T entity)
           
 java.util.Set<T> getPartitionOnlyEntities()
          This method applies to this partition only!
 java.util.Map<Specification<? extends T>,PartitionRepository> getPartitions()
          Optional: mainly added for testing purposes, may throw UnsupportedOperationException

This method applies to this partition only!

 Specification<T> getPartitionSpecification()
          This method applies to this partition only!
 PersistenceDefinition getPersistenceDefinition()
          This method applies to this partition only!
 java.io.File getRepositoryDirectory()
          This method applies to this partition only!
 java.lang.String getRepositoryId()
          This method applies to this partition only!
 PartitionRepository<? super T> getRootRepository()
           
 PartitionRepository<? super T> getSuperPartitionRepository()
          This method applies to this partition only!
 Specification<? super T> getSuperPartitionSpecification()
           
 Repository<T> getTargetRepository()
          This method applies to this partition only!
 java.lang.Class<T> getType()
           
protected  void handlePartitionRepositoryException(java.lang.Exception e, java.lang.String errorMessage)
           
 java.lang.Object invoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)
           
 java.lang.Boolean isIndexingEntitiesRecursively()
           
 java.lang.Boolean isLeafPartition()
          This method applies to this partition only!
 java.lang.Boolean isPartitioningNatively()
          When true, the repository utilizes some sort of native partitioning mechanism, e.g.
 java.lang.Boolean isRootPartition()
          This method applies to this partition only!
<V extends T>
java.util.Iterator<V>
iterateAllEntitiesSpecifiedBy(Specification<V> specification)
          Finds and returns all entities approved by the given specification.
 void load()
          The semantics of this operation depends on the Repository's PersistenceDefinition.
 void persist()
          The semantics of this operation depends on the Repository's PersistenceDefinition.
<V extends T>
void
put(V entity)
          Puts the given entity into this repository.
<V extends T>
java.lang.Boolean
remove(V entity)
          Removes the given entity from this repository.
<V extends T>
java.lang.Long
removeAllEntitiesSpecifiedBy(Specification<V> specification)
          Removes all entities approved by the given specification.
protected
<V extends T>
void
removePartitionOnlyEntity(V entity)
           
 void repartition()
          Repartitions the entire repository.
<V extends T>
java.lang.Boolean
repartition(V entity)
          When an entity is altered, this method must be explicitely invoked to reassure proper placement in the repository partition hierarchy.
protected  boolean repositoryExemptFromSyncronization()
           
protected  boolean repositoryOperationsNeedToBeExecutedExclusively()
           
 void setPartitionSpecification(Specification<T> partitionSpecification)
          This method applies to this partition only!
 void setSuperPartitionRepository(PartitionRepository<? super T> partitionRepository)
          This method applies to this partition only!
 void setSuperPartitionSpecification(Specification<? super T> specification)
          This method applies to this partition only!
<V extends T>
void
update(V entity)
          Updates an entity.
protected  PartitionRepository wireUpPartition(Specification superPartitionSpecification, PartitionRepository superPartitionRepository, Specification partitionSpecification, PartitionRepository partitionRepository)
           
 
Methods inherited from class net.sourceforge.domian.repository.AbstractDomianCoreRepository
createUniqueSpecificationFor, getSynchronizer, makePartition, onMakePartition, setSynchronizer
 
Methods inherited from class net.sourceforge.domian.repository.AbstractRepository
contains, count, countAll, countAllEntitiesSpecifiedBy, find, findAll, findSingle, iterate, iterateAll, putAll, remove, removeAll, update
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface net.sourceforge.domian.repository.Repository
count, countAll, countAllEntitiesSpecifiedBy, find, findAll, findSingle, iterate, iterateAll, makePartition, putAll, remove, removeAll, update
 

Field Detail

synchronizer

protected final Synchronizer synchronizer
A synchronizer used to control concurrent and exclusive access to repository methods. E.g. all methods dealing with partitioning are run in an exclusive manner.


iteratorRegistry

protected final net.sourceforge.domian.repository.PartitionRepositoryInvocationHandler.IteratorRegistry iteratorRegistry
A internal registry keeping track of all active iterators.


executeOperationsExclusivelyOnly

protected boolean executeOperationsExclusivelyOnly

repositoryDelegateIsAlreadyPartitioned

protected boolean repositoryDelegateIsAlreadyPartitioned

superPartitionSpecification

protected Specification<? super T extends Entity> superPartitionSpecification

superPartitionRepository

protected PartitionRepository<? super T extends Entity> superPartitionRepository

partitionSpecification

protected Specification<T extends Entity> partitionSpecification

repositoryDelegate

protected Repository<T extends Entity> repositoryDelegate

subPartitions

protected final java.util.Map<Specification<? extends T extends Entity>,PartitionRepository> subPartitions
All sub-partition repositories, indexed by a unique specification.

Method Detail

invoke

public java.lang.Object invoke(java.lang.Object proxy,
                               java.lang.reflect.Method method,
                               java.lang.Object[] args)
                        throws java.lang.Throwable
Specified by:
invoke in interface java.lang.reflect.InvocationHandler
Throws:
java.lang.Throwable

getType

public java.lang.Class<T> getType()
Specified by:
getType in interface PartitionRepository<T extends Entity>
Returns:
the type of entities residing in this repository

getRootRepository

public PartitionRepository<? super T> getRootRepository()
Specified by:
getRootRepository in interface PartitionRepository<T extends Entity>
Returns:
the root repository of this partitioned repository graph

getSuperPartitionSpecification

public Specification<? super T> getSuperPartitionSpecification()

setSuperPartitionSpecification

public void setSuperPartitionSpecification(Specification<? super T> specification)
Description copied from interface: PartitionRepository
This method applies to this partition only!

Specified by:
setSuperPartitionSpecification in interface PartitionRepository<T extends Entity>

getSuperPartitionRepository

public PartitionRepository<? super T> getSuperPartitionRepository()
Description copied from interface: PartitionRepository
This method applies to this partition only!

Specified by:
getSuperPartitionRepository in interface PartitionRepository<T extends Entity>
Returns:
the repository in the super partition

setSuperPartitionRepository

public void setSuperPartitionRepository(PartitionRepository<? super T> partitionRepository)
Description copied from interface: PartitionRepository
This method applies to this partition only!

Specified by:
setSuperPartitionRepository in interface PartitionRepository<T extends Entity>

getPartitionSpecification

public Specification<T> getPartitionSpecification()
Description copied from interface: PartitionRepository
This method applies to this partition only!

Specified by:
getPartitionSpecification in interface PartitionRepository<T extends Entity>
Returns:
the specification defining this partition

setPartitionSpecification

public void setPartitionSpecification(Specification<T> partitionSpecification)
Description copied from interface: PartitionRepository
This method applies to this partition only!

Specified by:
setPartitionSpecification in interface PartitionRepository<T extends Entity>

getTargetRepository

public Repository<T> getTargetRepository()
Description copied from interface: PartitionRepository
This method applies to this partition only!

This method is a ( at the moment) necessary workaround for missing functionality regarding runtime modification of the Repository instance. Not only the Repository instance in action must be modified, but all super partitions leading up to the root repository as well. Is it possible anyway...?

Specified by:
getTargetRepository in interface PartitionRepository<T extends Entity>
Returns:
the delegated repository instance for this partition if any; otherwise null

isRootPartition

public java.lang.Boolean isRootPartition()
Description copied from interface: PartitionRepository
This method applies to this partition only!

Specified by:
isRootPartition in interface PartitionRepository<T extends Entity>
Returns:
true if this repository partition is the root partition (does not have a super partitions)

isLeafPartition

public java.lang.Boolean isLeafPartition()
Description copied from interface: PartitionRepository
This method applies to this partition only!

Specified by:
isLeafPartition in interface PartitionRepository<T extends Entity>
Returns:
true if this repository partition is a leaf partition (does not have any sub partitions)

repositoryOperationsNeedToBeExecutedExclusively

protected boolean repositoryOperationsNeedToBeExecutedExclusively()

repositoryExemptFromSyncronization

protected boolean repositoryExemptFromSyncronization()

wireUpPartition

protected PartitionRepository wireUpPartition(Specification superPartitionSpecification,
                                              PartitionRepository superPartitionRepository,
                                              Specification partitionSpecification,
                                              PartitionRepository partitionRepository)

conditionalSynchronizedExecutionOf

protected <V> V conditionalSynchronizedExecutionOf(java.util.concurrent.Callable<V> operation)
                                        throws java.lang.Exception
Throws:
java.lang.Exception

handlePartitionRepositoryException

protected void handlePartitionRepositoryException(java.lang.Exception e,
                                                  java.lang.String errorMessage)

iterateAllEntitiesSpecifiedBy

public <V extends T> java.util.Iterator<V> iterateAllEntitiesSpecifiedBy(Specification<V> specification)
Description copied from interface: Repository
Finds and returns all entities approved by the given specification. An iterator is returned for lazy retrieval of the entities.

Specified by:
iterateAllEntitiesSpecifiedBy in interface Repository<T extends Entity>
Parameters:
specification - the entity specification
Returns:
an iterator with all entities approved by the specification argument

countAllEntitiesWithoutSpecificationSharpening

protected <V extends T> java.lang.Long countAllEntitiesWithoutSpecificationSharpening(Specification<V> specification)
Special count method without sharpening original specifications to fit partition specifications.


findSingleEntitySpecifiedBy

public <V extends T> V findSingleEntitySpecifiedBy(Specification<V> specification)
Description copied from interface: Repository
Finds and returns one single entity approved by the given specification.

Specified by:
findSingleEntitySpecifiedBy in interface Repository<T extends Entity>
Overrides:
findSingleEntitySpecifiedBy in class AbstractRepository<T extends Entity>
Parameters:
specification - the entity specification
Returns:
the sole entity specified by the specification argument, or null if no entity is found

findAllEntitiesSpecifiedBy

public <V extends T> java.util.Collection<V> findAllEntitiesSpecifiedBy(Specification<V> specification)
Description copied from interface: Repository
Finds and returns all entities approved by the given specification.

Specified by:
findAllEntitiesSpecifiedBy in interface Repository<T extends Entity>
Parameters:
specification - the entity specification
Returns:
a collection of all entities approved by the specification argument, or an empty set if no entities are found and approved

findAllEntitiesSpecifiedBy

protected <V extends T> java.util.Collection<V> findAllEntitiesSpecifiedBy(Specification<V> specification,
                                                                           java.lang.Boolean sharpenOriginalSpecificationsToFitPartitionSpecification)

put

public <V extends T> void put(V entity)
Description copied from interface: Repository
Puts the given entity into this repository.

Specified by:
put in interface Repository<T extends Entity>
Parameters:
entity - the entity to be stored

update

public <V extends T> void update(V entity)
Description copied from interface: Repository
Updates an entity. When an entity is (re)-retrieved, all state changes will be visible, no matter what kinds of repository implementations being involved. Do notice that for all memory-based repositories (and possibly some delegated ones), entity updates are of course immediately visible.

(Only applicable for persistent repositories doing explicit data writes, like PersistenceDefinition.FILE, and possibly PersistenceDefinition.DELEGATED... For all other persistence definitions, this method is redundant/not applicable. Anyway, by applying this method in a consistent manner, an eventual repository migration will not need any code changes.

Specified by:
update in interface Repository<T extends Entity>

removeAllEntitiesSpecifiedBy

public <V extends T> java.lang.Long removeAllEntitiesSpecifiedBy(Specification<V> specification)
Description copied from interface: Repository
Removes all entities approved by the given specification.

Specified by:
removeAllEntitiesSpecifiedBy in interface Repository<T extends Entity>
Parameters:
specification - the entity specification
Returns:
the number of removed entities

remove

public <V extends T> java.lang.Boolean remove(V entity)
Description copied from interface: Repository
Removes the given entity from this repository.

Specified by:
remove in interface Repository<T extends Entity>
Parameters:
entity - the entity to be removed
Returns:
true if the entity is found and removed, otherwise false

isPartitioningNatively

public java.lang.Boolean isPartitioningNatively()
Description copied from interface: Repository
When true, the repository utilizes some sort of native partitioning mechanism, e.g. RDBMS indices. If true, partition repositories may be reusing the same repository instance.

Specified by:
isPartitioningNatively in interface Repository<T extends Entity>
Overrides:
isPartitioningNatively in class AbstractRepository<T extends Entity>
Returns:
true if repository utilizes some sort of native partitioning mechanism, e.g. RDBMS indices.

isIndexingEntitiesRecursively

public java.lang.Boolean isIndexingEntitiesRecursively()
Specified by:
isIndexingEntitiesRecursively in interface Repository<T extends Entity>
Overrides:
isIndexingEntitiesRecursively in class AbstractRepository<T extends Entity>
Returns:
true if member entities are stored recursively, and made searchable - false if only aggregate root is stored.

getRepositoryDirectory

public java.io.File getRepositoryDirectory()
This method applies to this partition only!

Specified by:
getRepositoryDirectory in interface PersistentRepository<T extends Entity>
Returns:
the repository directory, if applicable

getRepositoryId

public java.lang.String getRepositoryId()
This method applies to this partition only!

Specified by:
getRepositoryId in interface PersistentRepository<T extends Entity>
Returns:
the repository id

getPersistenceDefinition

public PersistenceDefinition getPersistenceDefinition()
This method applies to this partition only!

Specified by:
getPersistenceDefinition in interface PersistentRepository<T extends Entity>

getFormat

public java.lang.String getFormat()
This method applies to this partition only!

Specified by:
getFormat in interface PersistentRepository<T extends Entity>

load

public void load()
Description copied from interface: PersistentRepository
The semantics of this operation depends on the Repository's PersistenceDefinition.

Specified by:
load in interface PersistentRepository<T extends Entity>

persist

public void persist()
Description copied from interface: PersistentRepository
The semantics of this operation depends on the Repository's PersistenceDefinition.

Specified by:
persist in interface PersistentRepository<T extends Entity>

getMetaDataFor

public EntityPersistenceMetaData getMetaDataFor(T entity)
Specified by:
getMetaDataFor in interface PersistentRepository<T extends Entity>
Returns:
persistence meta data for given entity

close

public void close()
Description copied from interface: PersistentRepository
Closes the repository and releases all resources.

This method is redundant for certain repository implementations, but by using this method in a consistent manner, an eventual repository migration will not need any code changes.

Specified by:
close in interface PersistentRepository<T extends Entity>

getCorrectPartitionRepository

protected PartitionRepository getCorrectPartitionRepository(PartitionRepository originalPartitionRepository,
                                                            PartitionRepository toBeReusedPartitionRepository)

addPartition

protected <V extends T> PartitionRepository<V> addPartition(Specification<V> partitionSpecification,
                                                            PartitionRepository<? extends V> partitionRepository)

addPartitionFor

public <V extends T> PartitionRepository<V> addPartitionFor(Specification<V> partitionSpecification)
Description copied from interface: PartitionRepository
Adds a new partition to this repository. The partition is specified (indexed) by a Specification. The new partition repository will be of the same type as this repository.

Specified by:
addPartitionFor in interface PartitionRepository<T extends Entity>
Parameters:
partitionSpecification - the specification specifying the partition
Returns:
the newly created partition

addPartitionFor

public <V extends T> PartitionRepository<V> addPartitionFor(Specification<V> partitionSpecification,
                                                            java.lang.String repositoryId)
Description copied from interface: PartitionRepository
Adds a new partition to this repository. The partition is specified (indexed) by a Specification. The new partition repository will be of the same type as this repository.

Specified by:
addPartitionFor in interface PartitionRepository<T extends Entity>
Parameters:
partitionSpecification - the specification specifying the partition
repositoryId - the repository id; a non-blank value is mandatory for all persistent repositories
Returns:
the newly created partition

addPartitionFor

public <V extends T> PartitionRepository<V> addPartitionFor(Specification<V> partitionSpecification,
                                                            Repository<? super V> partitionRepository)
Description copied from interface: PartitionRepository
Adds a new partition to this repository. The partition is specified (indexed) by a Specification.

Specified by:
addPartitionFor in interface PartitionRepository<T extends Entity>
Parameters:
partitionSpecification - the specification specifying the partition
partitionRepository - the partition repository
Returns:
the newly created partition

repartition

public <V extends T> java.lang.Boolean repartition(V entity)
Description copied from interface: PartitionRepository
When an entity is altered, this method must be explicitely invoked to reassure proper placement in the repository partition hierarchy.

If any repository implementation does this implicitly in some fashion, this should be documented, e.g. in the Javadoc.

Specified by:
repartition in interface PartitionRepository<T extends Entity>
Parameters:
entity - the entity to be repartitioned
Returns:
true if entity was repartitioned

repartition

public void repartition()
Description copied from interface: PartitionRepository
Repartitions the entire repository.

Specified by:
repartition in interface PartitionRepository<T extends Entity>

entityExists_PossiblyInWrongPartition

protected java.lang.Boolean entityExists_PossiblyInWrongPartition(T entity)

getPartitions

public java.util.Map<Specification<? extends T>,PartitionRepository> getPartitions()
Description copied from interface: PartitionRepository
Optional: mainly added for testing purposes, may throw UnsupportedOperationException

This method applies to this partition only!

Specified by:
getPartitions in interface PartitionRepository<T extends Entity>
Returns:
the direct sub-partitions of this PartitionRepository only

getAllPartitions

public java.util.Map<Specification<? extends T>,PartitionRepository> getAllPartitions()
Description copied from interface: PartitionRepository
Optional: mainly added for testing purposes, may throw UnsupportedOperationException

Specified by:
getAllPartitions in interface PartitionRepository<T extends Entity>
Returns:
a flattened view of all sub-partitions from this PartitionRepository and below

collectAllPartitions

public void collectAllPartitions(java.util.Map<Specification<? extends T>,PartitionRepository> partitionMap)
Description copied from interface: PartitionRepository
Populates the given map with a flattened and recursive view of all partitions in this repository.

Optional: mainly added for testing purposes, may throw UnsupportedOperationException

Specified by:
collectAllPartitions in interface PartitionRepository<T extends Entity>
Parameters:
partitionMap - the index->partition map

collectAllPartitionsWithRepositorySatisfying

public <R extends Repository> void collectAllPartitionsWithRepositorySatisfying(Specification<R> specification,
                                                                                java.util.Map<Specification<? extends T>,PartitionRepository> partitionMap)
Description copied from interface: PartitionRepository
Populates the given map with a flattened and recursive view of all partitions in this repository, satisfying the given specification.

Optional: mainly added for testing purposes, may throw UnsupportedOperationException

Specified by:
collectAllPartitionsWithRepositorySatisfying in interface PartitionRepository<T extends Entity>
Parameters:
specification - the specification to satisfy to be included in result map
partitionMap - the index->partition map

findPartitionFor

public <V extends T> PartitionRepository findPartitionFor(Specification<V> partitionSpecification)
Specified by:
findPartitionFor in interface PartitionRepository<T extends Entity>
Parameters:
partitionSpecification - sub-partition specification
Returns:
the most specialized generalization of the specified partition (possibly this partition

getPartitionOnlyEntities

public java.util.Set<T> getPartitionOnlyEntities()
Description copied from interface: PartitionRepository
This method applies to this partition only!

Specified by:
getPartitionOnlyEntities in interface PartitionRepository<T extends Entity>
Returns:
a set of all entities residing in this particular partition

removePartitionOnlyEntity

protected <V extends T> void removePartitionOnlyEntity(V entity)


Copyright © 2006-2010. All Rights Reserved.