View Javadoc
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.cms.ui.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  import org.argeo.api.NodeConstants;
29  
30  public class DefaultRepositoryRegister extends Observable implements RepositoryRegister {
31  	/** Key for a JCR repository alias */
32  	private final static String CN = NodeConstants.CN;
33  	/** Key for a JCR repository URI */
34  	// public final static String JCR_REPOSITORY_URI = "argeo.jcr.repository.uri";
35  	private final static Log log = LogFactory.getLog(DefaultRepositoryRegister.class);
36  
37  	/** Read only map which will be directly exposed. */
38  	private Map<String, Repository> repositories = Collections.unmodifiableMap(new TreeMap<String, Repository>());
39  
40  	@SuppressWarnings("rawtypes")
41  	public synchronized Repository getRepository(Map parameters) throws RepositoryException {
42  		if (!parameters.containsKey(CN))
43  			throw new RepositoryException("Parameter " + CN + " has to be defined.");
44  		String alias = parameters.get(CN).toString();
45  		if (!repositories.containsKey(alias))
46  			throw new RepositoryException("No repository registered with alias " + alias);
47  
48  		return repositories.get(alias);
49  	}
50  
51  	/** Access to the read-only map */
52  	public synchronized Map<String, Repository> getRepositories() {
53  		return repositories;
54  	}
55  
56  	/** Registers a service, typically called when OSGi services are bound. */
57  	@SuppressWarnings("rawtypes")
58  	public synchronized void register(Repository repository, Map properties) {
59  		String alias;
60  		if (properties == null || !properties.containsKey(CN)) {
61  			log.warn("Cannot register a repository if no " + CN + " property is specified.");
62  			return;
63  		}
64  		alias = properties.get(CN).toString();
65  		Map<String, Repository> map = new TreeMap<String, Repository>(repositories);
66  		map.put(alias, repository);
67  		repositories = Collections.unmodifiableMap(map);
68  		setChanged();
69  		notifyObservers(alias);
70  	}
71  
72  	/** Unregisters a service, typically called when OSGi services are unbound. */
73  	@SuppressWarnings("rawtypes")
74  	public synchronized void unregister(Repository repository, Map properties) {
75  		// TODO: also check bean name?
76  		if (properties == null || !properties.containsKey(CN)) {
77  			log.warn("Cannot unregister a repository without property " + CN);
78  			return;
79  		}
80  
81  		String alias = properties.get(CN).toString();
82  		Map<String, Repository> map = new TreeMap<String, Repository>(repositories);
83  		if (map.remove(alias) == null) {
84  			log.warn("No repository was registered with alias " + alias);
85  			return;
86  		}
87  		repositories = Collections.unmodifiableMap(map);
88  		setChanged();
89  		notifyObservers(alias);
90  	}
91  }