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