1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sourceforge.domian.repository;
17
18
19 import java.io.File;
20 import static java.lang.Boolean.FALSE;
21 import java.util.Collection;
22 import java.util.Iterator;
23 import java.util.Set;
24
25 import static org.apache.commons.lang.SystemUtils.FILE_SEPARATOR;
26 import org.apache.commons.lang.Validate;
27
28 import net.sourceforge.domian.entity.Entity;
29 import net.sourceforge.domian.repository.EntityPersistenceMetaData;
30 import net.sourceforge.domian.repository.PersistenceDefinition;
31 import static net.sourceforge.domian.repository.PersistenceDefinition.FILE;
32 import net.sourceforge.domian.repository.PersistentEntity;
33 import net.sourceforge.domian.specification.Specification;
34 import net.sourceforge.domian.util.concurrent.locks.SemaphoreSynchronizer;
35 import net.sourceforge.domian.util.concurrent.locks.Synchronizer;
36
37 import com.thoughtworks.xstream.persistence.PersistenceStrategy;
38 import com.thoughtworks.xstream.persistence.XmlSet;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 public class XStreamXmlFilePerEntityRepository<T extends Entity> extends AbstractXStreamXmlFilePerEntityRepository<T> {
71
72 protected static final String DEFAULT_REPOSITORY_ROOT_DIR_NAME = ".xstream-xml-file-per-entity-repository";
73 protected static final String DEFAULT_REPOSITORY_ROOT_PATH = DEFAULT_DOMIAN_ROOT_PATH + FILE_SEPARATOR + DEFAULT_REPOSITORY_ROOT_DIR_NAME;
74
75 public XStreamXmlFilePerEntityRepository(final String repositoryId) {
76 this(DEFAULT_REPOSITORY_ROOT_PATH, repositoryId);
77 }
78
79 public XStreamXmlFilePerEntityRepository(final String repositoryId, final Synchronizer synchronizer) {
80 this(DEFAULT_REPOSITORY_ROOT_PATH, repositoryId, synchronizer);
81 }
82
83 public XStreamXmlFilePerEntityRepository(final String repositoryRootPath, final String repositoryId) {
84 this(repositoryRootPath, repositoryId, new SemaphoreSynchronizer());
85 }
86
87 public XStreamXmlFilePerEntityRepository(final String repositoryRootPath, final String repositoryId, final Synchronizer synchronizer) {
88 Validate.notEmpty(repositoryRootPath, "The repository root path cannot be empty");
89 Validate.notEmpty(repositoryId, "The repository ID cannot be empty");
90 Validate.notNull(synchronizer, "The synchronizer parameter cannot be null");
91 this.repositoryRootPath = repositoryRootPath;
92 this.repositoryId = repositoryId;
93 this.synchronizer = synchronizer;
94 createRepositoryFilesIfNotExist();
95 }
96
97
98 public Long countAllEntities() {
99 return (long) new File(this.repositoryRootPath).list().length;
100 }
101
102 @Override
103 public <V extends T> Iterator<V> iterateAllEntitiesSpecifiedBy(final Specification<V> specification) {
104 Validate.notNull(specification, "Specification parameter cannot be null");
105 return callExclusivelyWithRetry(new IterateAllEntitiesSpecifiedBy<V>(specification));
106 }
107
108 @Override
109 public <V extends T> Collection<V> findAllEntitiesSpecifiedBy(final Specification<V> specification) {
110 Validate.notNull(specification, "Specification parameter cannot be null");
111 return callExclusivelyWithRetry(new FindAllEntitiesSpecifiedBy<V>(specification));
112 }
113
114 @Override
115 public <V extends T> void put(final V entity) {
116 callExclusivelyWithRetry(new Put<V>(entity));
117 }
118
119 @Override
120 public <V extends T> Long removeAllEntitiesSpecifiedBy(final Specification<V> specification) {
121 Validate.notNull(specification, "Specification parameter cannot be null");
122 return callExclusivelyWithRetry(new RemoveAllEntitiesSpecifiedBy<V>(specification));
123 }
124
125 @Override
126 public <V extends T> Boolean remove(final V entity) {
127 if (entity == null) {
128 return FALSE;
129 }
130 return callExclusivelyWithRetry(new Remove<V>(entity));
131 }
132
133 @Override
134 public PersistenceDefinition getPersistenceDefinition() {
135 return FILE;
136 }
137
138 @Override
139 public String getFormat() {
140 return "XStream XML";
141 }
142
143 @Override
144 public void load() {
145 log.warn("load() is only applicable for repositories with asynchronous persistence [PersistenceDefinition.INMEMORY_AND_*]");
146 }
147
148 @Override
149 public void persist() {
150 log.warn("persist() is only applicable for repositories with asynchronous persistence [PersistenceDefinition.INMEMORY_AND_*]");
151 }
152
153 @Override
154 public <V extends T> void update(V entity) {
155 put(entity);
156 }
157
158 @Override
159 public EntityMetaData getMetaDataFor(final T entity) {
160 PersistentEntity<T> persistentEntity = null;
161 final PersistenceStrategy persistenceStrategy = new EntityId_NamedXStreamFilePersistenceStrategy(getRepositoryDirectory());
162 final Set<PersistentEntity<T>> persistedEntitySet = new XmlSet(persistenceStrategy);
163 for (final PersistentEntity<T> persistentEntityFound : persistedEntitySet) {
164 final Entity entityFound = persistentEntityFound.getEntity();
165 if (entity.equals(entityFound)) {
166 persistentEntity = persistentEntityFound;
167 break;
168 }
169 }
170 return persistentEntity == null ? null : persistentEntity.getMetaData();
171 }
172
173 @Override
174 public void close() {}
175 }