View Javadoc

1   /*
2    * Copyright 2006-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sourceforge.domian.repository;
17  
18  
19  import static java.lang.Boolean.FALSE;
20  import static java.lang.Boolean.TRUE;
21  
22  import org.apache.commons.lang.Validate;
23  
24  import net.sourceforge.domian.entity.Entity;
25  import net.sourceforge.domian.specification.Specification;
26  import net.sourceforge.domian.util.concurrent.locks.Synchronizer;
27  
28  
29  /**
30   * ...
31   *
32   * @author Eirik Torske
33   * @since 0.4.2
34   */
35  public final class PartitionRepositoryReuseInvocationHandler<T extends Entity> extends PartitionRepositoryInvocationHandler<T> {
36  
37      PartitionRepositoryReuseInvocationHandler(final Repository<T> repositoryDelegate) {
38          this(repositoryDelegate, null, FALSE);
39      }
40  
41  
42      PartitionRepositoryReuseInvocationHandler(final Repository<T> repositoryDelegate,
43                                                final Synchronizer synchronizer,
44                                                final Boolean executeOperationsExclusivelyOnly) {
45          super(repositoryDelegate, synchronizer, executeOperationsExclusivelyOnly);
46      }
47  
48  
49      /*
50      @Override
51      protected PartitionRepository wireUpPartition(Specification superPartitionSpecification,
52                                                    final PartitionRepository superPartitionRepository,
53                                                    final Specification partitionSpecification,
54                                                    final PartitionRepository partitionRepository) {
55          if (superPartitionSpecification == null) {
56              // Default super spec, just to make isRootPartition to work...
57              superPartitionSpecification = allObjects();
58          }
59          partitionRepository.setSuperPartitionSpecification(superPartitionSpecification);
60          partitionRepository.setSuperPartitionRepository(superPartitionRepository);
61          partitionRepository.setPartitionSpecification(partitionSpecification);
62          return partitionRepository;
63      }
64      */
65  
66  
67      /*
68      @Override
69      public <V extends T> PartitionRepository<V> addPartitionFor(final Specification<V> partitionSpecification) {
70          return addPartitionFor(partitionSpecification, (String) null);
71      }
72      */
73  
74  
75      @Override
76      public <V extends T> PartitionRepository<V> addPartitionFor(final Specification<V> partitionSpecification,
77                                                                  final String repositoryId) {
78          //Validate.notNull(partitionSpecification, "Partition specification parameter cannot be null");
79          //final Object newRepositoryInstance;
80          //final Repository targetRepository = this.getTargetRepository();
81          //final Class<? extends Repository> targetRepositoryType = targetRepository.getClass();
82          /*
83          try {
84              if (canCastFrom_To(targetRepositoryType, PersistentRepository.class)) {
85                  Validate.isTrue(isNotBlank(repositoryId), "Repository ID cannot be blank for persistent repositories");
86                  final Constructor repositoryConstructor = targetRepositoryType.getConstructor(String.class, Synchronizer.class);
87                  synchronized (this.synchronizer) {
88                      newRepositoryInstance = repositoryConstructor.newInstance(repositoryId, this.synchronizer);
89                  }
90              } else {
91                  newRepositoryInstance = targetRepositoryType.newInstance();
92              }
93  
94          } catch (NoSuchMethodException e1) {
95              throw new IllegalArgumentException("Repository implementation " + targetRepositoryType +
96                                                 " lacks a public Constructor(java.lang.String repositoryId, net.sourceforge.domian.util.concurrent.locks.Synchronizer repositorySynchronizer)");
97          } catch (Exception e2) {
98              handlePartitionRepositoryException(e2, "addPartitionFor(Specification, String)");
99              return new NullRepository<V>().makePartition();
100         }
101         */
102         Validate.notNull(partitionSpecification, "Partition specification parameter cannot be null");
103         //Validate.notNull(partitionRepository, "Partition repository parameter cannot be null");
104         //log.info("The given repository partition-ID '" + repositoryId + "' will be disregarded as this partition repository instance is the same as the parent partition repository");
105         log.info("The given repository partition '" + repositoryId + "' will be disregarded as this partition repository instance will be the same as the parent partition repository");
106 
107         //final Repository partitionRepository = this;
108         //if (partitionRepository instanceof PartitionRepository) {
109         //return addPartition(partitionSpecification, (PartitionRepository) this);
110         return addPartition(partitionSpecification, null);
111         //}
112         //return addPartition(partitionSpecification, ((AbstractRepository<V>) partitionRepository).makePartition());
113     }
114 
115 
116     @Override
117     public <V extends T> PartitionRepository<V> addPartitionFor(final Specification<V> partitionSpecification,
118                                                                 final Repository<? super V> partitionRepository) {
119         Validate.notNull(partitionRepository, "Partition repository parameter cannot be null");
120         final String repositoryDenomination;
121         if (partitionRepository instanceof PersistentRepository) {
122             repositoryDenomination = ((PersistentRepository) partitionRepository).getRepositoryId();
123         } else {
124             repositoryDenomination = "[repository.hashCode=" + this.hashCode() + "]";
125         }
126         if (partitionRepository instanceof PartitionRepository) {
127             if (((PartitionRepository) partitionRepository).getTargetRepository().getClass().getSimpleName().equals(getTargetRepository().getClass().getSimpleName())) {
128                 return addPartitionFor(partitionSpecification, repositoryDenomination);
129             }
130         }
131         return super.addPartitionFor(partitionSpecification, partitionRepository);
132     }
133 
134 
135     /** Since partition repositories are common, no moving entities around between different repository implementations are necessary. */
136     @Override
137     public <V extends T> Boolean repartition(final V entity) {
138         Validate.notNull(entity, "Entity parameter cannot be null");
139         if (this.contains(entity)) {
140             return TRUE;
141         } else {
142             return FALSE;
143         }
144     }
145 }