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