2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.cms
.ui
.jcr
;
18 import java
.util
.Collections
;
20 import java
.util
.Observable
;
21 import java
.util
.TreeMap
;
23 import javax
.jcr
.Repository
;
24 import javax
.jcr
.RepositoryException
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.apache
.commons
.logging
.LogFactory
;
28 import org
.argeo
.node
.NodeConstants
;
30 public class DefaultRepositoryRegister
extends Observable
implements RepositoryRegister
{
31 /** Key for a JCR repository alias */
32 private final static String CN
= NodeConstants
.CN
;
33 /** Key for a JCR repository URI */
34 // public final static String JCR_REPOSITORY_URI = "argeo.jcr.repository.uri";
35 private final static Log log
= LogFactory
.getLog(DefaultRepositoryRegister
.class);
37 /** Read only map which will be directly exposed. */
38 private Map
<String
, Repository
> repositories
= Collections
.unmodifiableMap(new TreeMap
<String
, Repository
>());
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
);
48 return repositories
.get(alias
);
51 /** Access to the read-only map */
52 public synchronized Map
<String
, Repository
> getRepositories() {
56 /** Registers a service, typically called when OSGi services are bound. */
57 @SuppressWarnings("rawtypes")
58 public synchronized void register(Repository repository
, Map properties
) {
60 if (properties
== null || !properties
.containsKey(CN
)) {
61 log
.warn("Cannot register a repository if no " + CN
+ " property is specified.");
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
);
69 notifyObservers(alias
);
72 /** Unregisters a service, typically called when OSGi services are unbound. */
73 @SuppressWarnings("rawtypes")
74 public synchronized void unregister(Repository repository
, Map properties
) {
75 // TODO: also check bean name?
76 if (properties
== null || !properties
.containsKey(CN
)) {
77 log
.warn("Cannot unregister a repository without property " + CN
);
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
);
87 repositories
= Collections
.unmodifiableMap(map
);
89 notifyObservers(alias
);