]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.server.jcr/src/org/argeo/jcr/DefaultRepositoryRegister.java
Add DB drivers.
[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 String alias;
61 if (properties == null || !properties.containsKey(JCR_REPOSITORY_ALIAS)) {
62 log.warn("Cannot register a repository if no "
63 + JCR_REPOSITORY_ALIAS + " property is specified.");
64 return;
65 }
66 alias = properties.get(JCR_REPOSITORY_ALIAS).toString();
67 Map<String, Repository> map = new TreeMap<String, Repository>(
68 repositories);
69 map.put(alias, repository);
70 repositories = Collections.unmodifiableMap(map);
71 setChanged();
72 notifyObservers(alias);
73 }
74
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);
82 return;
83 }
84
85 String alias = properties.get(JCR_REPOSITORY_ALIAS).toString();
86 Map<String, Repository> map = new TreeMap<String, Repository>(
87 repositories);
88 if (map.remove(alias) == null) {
89 log.warn("No repository was registered with alias " + alias);
90 return;
91 }
92 repositories = Collections.unmodifiableMap(map);
93 setChanged();
94 notifyObservers(alias);
95 }
96 }