]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/ui/jcr/DefaultRepositoryRegister.java
Move MVC provider.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / jcr / DefaultRepositoryRegister.java
1 package org.argeo.cms.ui.jcr;
2
3 import java.util.Collections;
4 import java.util.Map;
5 import java.util.Observable;
6 import java.util.TreeMap;
7
8 import javax.jcr.Repository;
9 import javax.jcr.RepositoryException;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.argeo.api.NodeConstants;
14
15 public class DefaultRepositoryRegister extends Observable implements RepositoryRegister {
16 /** Key for a JCR repository alias */
17 private final static String CN = NodeConstants.CN;
18 /** Key for a JCR repository URI */
19 // public final static String JCR_REPOSITORY_URI = "argeo.jcr.repository.uri";
20 private final static Log log = LogFactory.getLog(DefaultRepositoryRegister.class);
21
22 /** Read only map which will be directly exposed. */
23 private Map<String, Repository> repositories = Collections.unmodifiableMap(new TreeMap<String, Repository>());
24
25 @SuppressWarnings("rawtypes")
26 public synchronized Repository getRepository(Map parameters) throws RepositoryException {
27 if (!parameters.containsKey(CN))
28 throw new RepositoryException("Parameter " + CN + " has to be defined.");
29 String alias = parameters.get(CN).toString();
30 if (!repositories.containsKey(alias))
31 throw new RepositoryException("No repository registered with alias " + alias);
32
33 return repositories.get(alias);
34 }
35
36 /** Access to the read-only map */
37 public synchronized Map<String, Repository> getRepositories() {
38 return repositories;
39 }
40
41 /** Registers a service, typically called when OSGi services are bound. */
42 @SuppressWarnings("rawtypes")
43 public synchronized void register(Repository repository, Map properties) {
44 String alias;
45 if (properties == null || !properties.containsKey(CN)) {
46 log.warn("Cannot register a repository if no " + CN + " property is specified.");
47 return;
48 }
49 alias = properties.get(CN).toString();
50 Map<String, Repository> map = new TreeMap<String, Repository>(repositories);
51 map.put(alias, repository);
52 repositories = Collections.unmodifiableMap(map);
53 setChanged();
54 notifyObservers(alias);
55 }
56
57 /** Unregisters a service, typically called when OSGi services are unbound. */
58 @SuppressWarnings("rawtypes")
59 public synchronized void unregister(Repository repository, Map properties) {
60 // TODO: also check bean name?
61 if (properties == null || !properties.containsKey(CN)) {
62 log.warn("Cannot unregister a repository without property " + CN);
63 return;
64 }
65
66 String alias = properties.get(CN).toString();
67 Map<String, Repository> map = new TreeMap<String, Repository>(repositories);
68 if (map.remove(alias) == null) {
69 log.warn("No repository was registered with alias " + alias);
70 return;
71 }
72 repositories = Collections.unmodifiableMap(map);
73 setChanged();
74 notifyObservers(alias);
75 }
76 }