1   package com.trendmicro.grid.acl.ds.dummy;
2   
3   import com.trendmicro.grid.acl.ds.*;
4   import com.trendmicro.grid.acl.ds.datatypes.SharedSource;
5   import com.trendmicro.grid.acl.ds.datatypes.SharedSourceDomain;
6   import com.trendmicro.grid.acl.ds.datatypes.SharedSourceInformation;
7   import com.trendmicro.grid.acl.l0.datatypes.*;
8   import com.trendmicro.grid.acl.metadata.Metadata;
9   import net.sf.tinyjee.concurrent.LockingMap;
10  import org.springframework.stereotype.Repository;
11  
12  import java.net.URI;
13  import java.util.ArrayList;
14  import java.util.Collection;
15  import java.util.Date;
16  import java.util.Map;
17  
18  /**
19   * Implements a dummy in-memory repository for sources.
20   *
21   * @author juergen_kellerer, 2010-05-07
22   * @version 1.0
23   */
24  @Repository
25  public class DummySourceRepository implements SourceRepository, SelectorAware {
26  
27  	Map<SourceIdentifier, SharedSource> sources = new LockingMap<SourceIdentifier, SharedSource>();
28  
29  	RepositorySelector<SourceDomainRepository> domainSelector;
30  
31  	@Override
32  	public void refreshSelectors(RepositorySelectorsRepository selectorsRepository) {
33  		domainSelector = selectorsRepository.getSelector(SourceDomainRepository.class);
34  	}
35  
36  	@Override
37  	public SourceIdentifier createIdentifier(URI remoteURI, URI internalURI) {
38  		return new SourceIdentifier(remoteURI, internalURI);
39  	}
40  
41  	@Override
42  	public SourceIdentiferListPage getReferencingSources(FileIdentifier file, int pageNumber) {
43  		return pageNumber > 0 ? null : new SourceIdentiferListPage(0, true, sources.keySet());
44  	}
45  
46  	@Override
47  	public FileIdentiferListPage getReferencedFiles(SourceIdentifier sourceIdentifier, int pageNumber) {
48  		return new FileIdentiferListPage(0, true);
49  	}
50  
51  	@Override
52  	public NameListPage getReferencedPackages(SourceIdentifier sourceIdentifier, int pageNumber) {
53  		return new NameListPage(0, true);
54  	}
55  
56  	@Override
57  	public SourceIdentiferListPage getSourcesOfDomainInRange(
58  			String domainName, Date modifiedFromDate, Date modifiedToDate, int pageNumber) {
59  
60  		if (pageNumber > 0)
61  			return null;
62  
63  		ArrayList<SourceIdentifier> identifiers = new ArrayList<SourceIdentifier>();
64  
65  		for (Source source : sources.values()) {
66  			if (!domainName.equals(source.getSourceDomain().getName())) continue;
67  
68  			Date lm = source.getSourceInformation().getLastModified();
69  			if (lm == null) continue;
70  			if (modifiedFromDate != null && lm.after(modifiedFromDate)) continue;
71  			if (modifiedToDate != null && lm.before(modifiedToDate)) continue;
72  
73  			identifiers.add(source.getSourceInformation().getIdentifier());
74  		}
75  
76  		return identifiers.isEmpty() ? null : new SourceIdentiferListPage(0, true, identifiers);
77  	}
78  
79  	@Override
80  	public Collection<SharedSourceInformation> getSourceInformationList(Collection<SourceIdentifier> identifiers) {
81  		Collection<SharedSourceInformation> result = new ArrayList<SharedSourceInformation>(identifiers.size());
82  		for (SourceIdentifier identifier : identifiers) {
83  			SharedSource source = sources.get(identifier);
84  			result.add(source == null ? null : (SharedSourceInformation) source.getSourceInformation());
85  		}
86  		return result;
87  	}
88  
89  	@Override
90  	public Collection<SharedSource> getSources(Collection<SourceIdentifier> identifiers) {
91  		Collection<SharedSource> result = new ArrayList<SharedSource>(identifiers.size());
92  		for (SourceIdentifier identifier : identifiers)
93  			result.add(sources.get(identifier));
94  		return result;
95  	}
96  
97  	@Override
98  	public SharedSource createSource(URI remoteURI, URI internalURI, SourceInformation info, Metadata metadata) {
99  		SourceIdentifier id = createIdentifier(remoteURI, internalURI);
100 		if (sources.containsKey(id))
101 			return null;
102 
103 		SharedSourceDomain domain = domainSelector.getRepository().getOrCreate(SourceDomain.getDomainNameFrom(remoteURI));
104 		SharedSourceInformation si = new SharedSourceInformation(id, info.getLastModified(), info.getContentTag(), info.isTemporary());
105 		SharedSource s = new SharedSource(remoteURI, internalURI, si, domain, metadata);
106 		sources.put(id, s);
107 		return s;
108 	}
109 
110 	@Override
111 	public SharedSource updateSource(SourceInformation sourceInformation, Metadata metadata) {
112 		SharedSource source = sources.get(sourceInformation.getIdentifier());
113 		if (source == null)
114 			return null;
115 
116 		source.getSourceInformation().setLastModified(sourceInformation.getLastModified());
117 		source.getSourceInformation().setContentTag(sourceInformation.getContentTag());
118 		source.setMetadata(metadata);
119 
120 		return source;
121 	}
122 
123 	@Override
124 	public void setInternalURI(SourceIdentifier identifier, URI internalURI) throws IllegalStateException {
125 		Source source = sources.get(identifier);
126 		if (source != null)
127 			source.setInternalURI(internalURI);
128 	}
129 }