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
;
29 public class DefaultRepositoryRegister
extends Observable
implements
30 RepositoryRegister
, ArgeoJcrConstants
{
31 private final static Log log
= LogFactory
32 .getLog(DefaultRepositoryRegister
.class);
34 /** Read only map which will be directly exposed. */
35 private Map
<String
, Repository
> repositories
= Collections
36 .unmodifiableMap(new TreeMap
<String
, Repository
>());
38 @SuppressWarnings("rawtypes")
39 public synchronized Repository
getRepository(Map parameters
)
40 throws RepositoryException
{
41 if (!parameters
.containsKey(JCR_REPOSITORY_ALIAS
))
42 throw new RepositoryException("Parameter " + JCR_REPOSITORY_ALIAS
43 + " has to be defined.");
44 String alias
= parameters
.get(JCR_REPOSITORY_ALIAS
).toString();
45 if (!repositories
.containsKey(alias
))
46 throw new RepositoryException(
47 "No repository registered with alias " + alias
);
49 return repositories
.get(alias
);
52 /** Access to the read-only map */
53 public synchronized Map
<String
, Repository
> getRepositories() {
57 /** Registers a service, typically called when OSGi services are bound. */
58 @SuppressWarnings("rawtypes")
59 public synchronized void register(Repository repository
, Map properties
) {
61 if (properties
== null || !properties
.containsKey(JCR_REPOSITORY_ALIAS
)) {
62 log
.warn("Cannot register a repository if no "
63 + JCR_REPOSITORY_ALIAS
+ " property is speecified.");
66 alias
= properties
.get(JCR_REPOSITORY_ALIAS
).toString();
67 Map
<String
, Repository
> map
= new TreeMap
<String
, Repository
>(
69 map
.put(alias
, repository
);
70 repositories
= Collections
.unmodifiableMap(map
);
72 notifyObservers(alias
);
75 /** Unregisters a service, typically called when OSGi services are unbound. */
76 @SuppressWarnings("rawtypes")
77 public synchronized void unregister(Repository repository
, Map properties
) {
78 // TODO: also check bean name?
79 if (properties
== null || !properties
.containsKey(JCR_REPOSITORY_ALIAS
)) {
80 log
.warn("Cannot unregister a repository without property "
81 + JCR_REPOSITORY_ALIAS
);
85 String alias
= properties
.get(JCR_REPOSITORY_ALIAS
).toString();
86 Map
<String
, Repository
> map
= new TreeMap
<String
, Repository
>(
88 if (map
.remove(alias
) == null) {
89 log
.warn("No repository was registered with alias " + alias
);
92 repositories
= Collections
.unmodifiableMap(map
);
94 notifyObservers(alias
);