1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.cms.ui.jcr;
17
18 import java.util.Collections;
19 import java.util.Map;
20 import java.util.Observable;
21 import java.util.TreeMap;
22
23 import javax.jcr.Repository;
24 import javax.jcr.RepositoryException;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.argeo.api.NodeConstants;
29
30 public class DefaultRepositoryRegister extends Observable implements RepositoryRegister {
31
32 private final static String CN = NodeConstants.CN;
33
34
35 private final static Log log = LogFactory.getLog(DefaultRepositoryRegister.class);
36
37
38 private Map<String, Repository> repositories = Collections.unmodifiableMap(new TreeMap<String, Repository>());
39
40 @SuppressWarnings("rawtypes")
41 public synchronized Repository getRepository(Map parameters) throws RepositoryException {
42 if (!parameters.containsKey(CN))
43 throw new RepositoryException("Parameter " + CN + " has to be defined.");
44 String alias = parameters.get(CN).toString();
45 if (!repositories.containsKey(alias))
46 throw new RepositoryException("No repository registered with alias " + alias);
47
48 return repositories.get(alias);
49 }
50
51
52 public synchronized Map<String, Repository> getRepositories() {
53 return repositories;
54 }
55
56
57 @SuppressWarnings("rawtypes")
58 public synchronized void register(Repository repository, Map properties) {
59 String alias;
60 if (properties == null || !properties.containsKey(CN)) {
61 log.warn("Cannot register a repository if no " + CN + " property is specified.");
62 return;
63 }
64 alias = properties.get(CN).toString();
65 Map<String, Repository> map = new TreeMap<String, Repository>(repositories);
66 map.put(alias, repository);
67 repositories = Collections.unmodifiableMap(map);
68 setChanged();
69 notifyObservers(alias);
70 }
71
72
73 @SuppressWarnings("rawtypes")
74 public synchronized void unregister(Repository repository, Map properties) {
75
76 if (properties == null || !properties.containsKey(CN)) {
77 log.warn("Cannot unregister a repository without property " + CN);
78 return;
79 }
80
81 String alias = properties.get(CN).toString();
82 Map<String, Repository> map = new TreeMap<String, Repository>(repositories);
83 if (map.remove(alias) == null) {
84 log.warn("No repository was registered with alias " + alias);
85 return;
86 }
87 repositories = Collections.unmodifiableMap(map);
88 setChanged();
89 notifyObservers(alias);
90 }
91 }