]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.server.jcr/src/org/argeo/jcr/DefaultRepositoryRegister.java
Adapt to package names changes in Spring Security
[lgpl/argeo-commons.git] / org.argeo.server.jcr / src / org / argeo / jcr / DefaultRepositoryRegister.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.jcr;
17
18 import java.util.Collections;
19 import java.util.Map;
20 import java.util.Observable;
21 import java.util.TreeMap;
22
23 import javax.jcr.Repository;
24 import javax.jcr.RepositoryException;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 public class DefaultRepositoryRegister extends Observable implements
30 RepositoryRegister, ArgeoJcrConstants {
31 private final static Log log = LogFactory
32 .getLog(DefaultRepositoryRegister.class);
33
34 /** Read only map which will be directly exposed. */
35 private Map<String, Repository> repositories = Collections
36 .unmodifiableMap(new TreeMap<String, Repository>());
37
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);
48
49 return repositories.get(alias);
50 }
51
52 /** Access to the read-only map */
53 public synchronized Map<String, Repository> getRepositories() {
54 return repositories;
55 }
56
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?
61 String alias;
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.");
65 return;
66 }
67 alias = properties.get(JCR_REPOSITORY_ALIAS).toString();
68 Map<String, Repository> map = new TreeMap<String, Repository>(
69 repositories);
70 map.put(alias, repository);
71 repositories = Collections.unmodifiableMap(map);
72 setChanged();
73 notifyObservers(alias);
74 }
75
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);
83 return;
84 }
85
86 String alias = properties.get(JCR_REPOSITORY_ALIAS).toString();
87 Map<String, Repository> map = new TreeMap<String, Repository>(
88 repositories);
89 map.put(alias, repository);
90 repositories = Collections.unmodifiableMap(map);
91 setChanged();
92 notifyObservers(alias);
93 }
94 }