1 package com.trendmicro.grid.acl.ds;
2
3 import static net.sf.tinyjee.util.Assert.assertNotNull;
4
5 import javax.xml.bind.annotation.*;
6 import java.util.*;
7
8
9
10
11
12
13
14 @XmlType
15 @XmlRootElement(name = "repository-selection")
16 @XmlAccessorType(XmlAccessType.FIELD)
17 public class RepositorySelectors {
18
19 private static final RepositorySelector EMPTY_SELECTOR = new RepositorySelector() {
20 @Override
21 public Repository getRepository() {
22 return null;
23 }
24
25 @Override
26 public String getSelectedKey() {
27 return null;
28 }
29
30 @Override
31 public List getAvailableKeys() {
32 return Collections.emptyList();
33 }
34 };
35
36 @XmlElement(name = "selector")
37 private List<RepositorySelector<?>> allSelectors = new ArrayList<RepositorySelector<?>>();
38
39 private transient Map<String, RepositorySelector<?>> selectorsMap;
40
41 public List<RepositorySelector<?>> getAllSelectors() {
42 return Collections.unmodifiableList(allSelectors);
43 }
44
45 public long getLastModified() {
46 long lastModified = 0;
47 for (RepositorySelector<?> selector : allSelectors)
48 lastModified = Math.max(lastModified, selector.getLastModified());
49 return lastModified;
50 }
51
52 public void setLastModified(long time) {
53 for (RepositorySelector<?> selector : allSelectors)
54 selector.setLastModified(time);
55 }
56
57 @SuppressWarnings("unchecked")
58 public void addSelector(RepositorySelector selector) {
59 assertNotNull("selector", selector);
60
61 RepositorySelector<?> existing = getSelector(selector.getRepositoryClass());
62 if (existing != null && existing != EMPTY_SELECTOR)
63 existing.initFrom(selector);
64 else {
65 allSelectors.add(selector);
66 selectorsMap = null;
67 }
68 }
69
70
71
72
73
74
75
76
77
78
79 public <R extends Repository> RepositorySelector<R> getSelector(Class<R> repositoryClass) {
80 return getSelector(repositoryClass.getName());
81 }
82
83
84
85
86
87
88
89
90
91 @SuppressWarnings("unchecked")
92 public <R extends Repository> RepositorySelector<R> getSelector(String repositoryClass) {
93 Map<String, RepositorySelector<?>> map = selectorsMap;
94 if (map == null) {
95 map = new HashMap<String, RepositorySelector<?>>();
96 for (RepositorySelector<?> selector : allSelectors)
97 map.put(selector.getRepositoryClass(), selector);
98 selectorsMap = map;
99 }
100
101 RepositorySelector<?> selector = map.get(repositoryClass);
102 return selector == null ? EMPTY_SELECTOR : selector;
103 }
104 }