1 package org
.argeo
.cms
.ui
.jcr
;
3 import java
.util
.Collections
;
5 import java
.util
.Observable
;
6 import java
.util
.TreeMap
;
8 import javax
.jcr
.Repository
;
9 import javax
.jcr
.RepositoryException
;
11 import org
.argeo
.api
.cms
.CmsConstants
;
12 import org
.argeo
.api
.cms
.CmsLog
;
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);
21 /** Read only map which will be directly exposed. */
22 private Map
<String
, Repository
> repositories
= Collections
.unmodifiableMap(new TreeMap
<String
, Repository
>());
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
);
32 return repositories
.get(alias
);
35 /** Access to the read-only map */
36 public synchronized Map
<String
, Repository
> getRepositories() {
40 /** Registers a service, typically called when OSGi services are bound. */
41 @SuppressWarnings("rawtypes")
42 public synchronized void register(Repository repository
, Map properties
) {
44 if (properties
== null || !properties
.containsKey(CN
)) {
45 log
.warn("Cannot register a repository if no " + CN
+ " property is specified.");
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
);
53 notifyObservers(alias
);
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
);
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
);
71 repositories
= Collections
.unmodifiableMap(map
);
73 notifyObservers(alias
);