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
.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
;
30 public class DefaultRepositoryRegister
extends Observable
implements
32 // FIXME factorize with node
33 /** Key for a JCR repository alias */
34 public final static String JCR_REPOSITORY_ALIAS
= "argeo.jcr.repository.alias";
35 /** Key for a JCR repository URI */
36 public final static String JCR_REPOSITORY_URI
= "argeo.jcr.repository.uri";
37 private final static Log log
= LogFactory
38 .getLog(DefaultRepositoryRegister
.class);
40 /** Read only map which will be directly exposed. */
41 private Map
<String
, Repository
> repositories
= Collections
42 .unmodifiableMap(new TreeMap
<String
, Repository
>());
44 @SuppressWarnings("rawtypes")
45 public synchronized Repository
getRepository(Map parameters
)
46 throws RepositoryException
{
47 if (!parameters
.containsKey(JCR_REPOSITORY_ALIAS
))
48 throw new RepositoryException("Parameter " + JCR_REPOSITORY_ALIAS
49 + " has to be defined.");
50 String alias
= parameters
.get(JCR_REPOSITORY_ALIAS
).toString();
51 if (!repositories
.containsKey(alias
))
52 throw new RepositoryException(
53 "No repository registered with alias " + alias
);
55 return repositories
.get(alias
);
58 /** Access to the read-only map */
59 public synchronized Map
<String
, Repository
> getRepositories() {
63 /** Registers a service, typically called when OSGi services are bound. */
64 @SuppressWarnings("rawtypes")
65 public synchronized void register(Repository repository
, Map properties
) {
67 if (properties
== null || !properties
.containsKey(JCR_REPOSITORY_ALIAS
)) {
68 log
.warn("Cannot register a repository if no "
69 + JCR_REPOSITORY_ALIAS
+ " property is specified.");
72 alias
= properties
.get(JCR_REPOSITORY_ALIAS
).toString();
73 Map
<String
, Repository
> map
= new TreeMap
<String
, Repository
>(
75 map
.put(alias
, repository
);
76 repositories
= Collections
.unmodifiableMap(map
);
78 notifyObservers(alias
);
81 /** Unregisters a service, typically called when OSGi services are unbound. */
82 @SuppressWarnings("rawtypes")
83 public synchronized void unregister(Repository repository
, Map properties
) {
84 // TODO: also check bean name?
85 if (properties
== null || !properties
.containsKey(JCR_REPOSITORY_ALIAS
)) {
86 log
.warn("Cannot unregister a repository without property "
87 + JCR_REPOSITORY_ALIAS
);
91 String alias
= properties
.get(JCR_REPOSITORY_ALIAS
).toString();
92 Map
<String
, Repository
> map
= new TreeMap
<String
, Repository
>(
94 if (map
.remove(alias
) == null) {
95 log
.warn("No repository was registered with alias " + alias
);
98 repositories
= Collections
.unmodifiableMap(map
);
100 notifyObservers(alias
);