]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.jcr/src/org/argeo/jcr/DefaultRepositoryRegister.java
[maven-release-plugin] prepare release argeo-commons-2.1.47
[lgpl/argeo-commons.git] / org.argeo.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 @Deprecated
30 public class DefaultRepositoryRegister extends Observable implements
31 RepositoryRegister {
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);
39
40 /** Read only map which will be directly exposed. */
41 private Map<String, Repository> repositories = Collections
42 .unmodifiableMap(new TreeMap<String, Repository>());
43
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);
54
55 return repositories.get(alias);
56 }
57
58 /** Access to the read-only map */
59 public synchronized Map<String, Repository> getRepositories() {
60 return repositories;
61 }
62
63 /** Registers a service, typically called when OSGi services are bound. */
64 @SuppressWarnings("rawtypes")
65 public synchronized void register(Repository repository, Map properties) {
66 String alias;
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.");
70 return;
71 }
72 alias = properties.get(JCR_REPOSITORY_ALIAS).toString();
73 Map<String, Repository> map = new TreeMap<String, Repository>(
74 repositories);
75 map.put(alias, repository);
76 repositories = Collections.unmodifiableMap(map);
77 setChanged();
78 notifyObservers(alias);
79 }
80
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);
88 return;
89 }
90
91 String alias = properties.get(JCR_REPOSITORY_ALIAS).toString();
92 Map<String, Repository> map = new TreeMap<String, Repository>(
93 repositories);
94 if (map.remove(alias) == null) {
95 log.warn("No repository was registered with alias " + alias);
96 return;
97 }
98 repositories = Collections.unmodifiableMap(map);
99 setChanged();
100 notifyObservers(alias);
101 }
102 }