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
) {
60 // TODO: also check bean name?
62 if (properties
== null || !properties
.containsKey(JCR_REPOSITORY_ALIAS
)) {
63 log
.warn("Cannot register a repository if no "
64 + JCR_REPOSITORY_ALIAS
+ " property is speecified.");
67 alias
= properties
.get(JCR_REPOSITORY_ALIAS
).toString();
68 Map
<String
, Repository
> map
= new TreeMap
<String
, Repository
>(
70 map
.put(alias
, repository
);
71 repositories
= Collections
.unmodifiableMap(map
);
73 notifyObservers(alias
);
76 /** Unregisters a service, typically called when OSGi services are unbound. */
77 @SuppressWarnings("rawtypes")
78 public synchronized void unregister(Repository repository
, Map properties
) {
79 // TODO: also check bean name?
80 if (properties
== null || !properties
.containsKey(JCR_REPOSITORY_ALIAS
)) {
81 log
.warn("Cannot unregister a repository without property "
82 + JCR_REPOSITORY_ALIAS
);
86 String alias
= properties
.get(JCR_REPOSITORY_ALIAS
).toString();
87 Map
<String
, Repository
> map
= new TreeMap
<String
, Repository
>(
89 map
.put(alias
, repository
);
90 repositories
= Collections
.unmodifiableMap(map
);
92 notifyObservers(alias
);