]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jackrabbit/JackrabbitRepositoryFactory.java
9cb846dfd83920c969ff723825779375bdc2de8d
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jackrabbit / src / main / java / org / argeo / jackrabbit / JackrabbitRepositoryFactory.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.jackrabbit;
17
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Properties;
21
22 import javax.jcr.Repository;
23 import javax.jcr.RepositoryException;
24 import javax.jcr.RepositoryFactory;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.jackrabbit.commons.JcrUtils;
29 import org.apache.jackrabbit.jcr2dav.Jcr2davRepositoryFactory;
30 import org.argeo.ArgeoException;
31 import org.argeo.jcr.ArgeoJcrConstants;
32 import org.argeo.jcr.DefaultRepositoryFactory;
33
34 /**
35 * Repository factory which can create new repositories and access remote
36 * Jackrabbit repositories
37 */
38 public class JackrabbitRepositoryFactory extends DefaultRepositoryFactory
39 implements RepositoryFactory, ArgeoJcrConstants {
40 private final static Log log = LogFactory
41 .getLog(JackrabbitRepositoryFactory.class);
42
43 @SuppressWarnings({ "rawtypes", "unchecked" })
44 public Repository getRepository(Map parameters) throws RepositoryException {
45 // check if can be found by alias
46 Repository repository = super.getRepository(parameters);
47 if (repository != null)
48 return repository;
49
50 // check if remote
51 String uri = null;
52 if (parameters.containsKey(JCR_REPOSITORY_URI))
53 uri = parameters.get(JCR_REPOSITORY_URI).toString();
54 else if (parameters.containsKey(JcrUtils.REPOSITORY_URI))
55 uri = parameters.get(JcrUtils.REPOSITORY_URI).toString();
56
57 if (uri != null) {
58 if (uri.startsWith("http"))// http, https
59 repository = createRemoteRepository(uri);
60 else if (uri.startsWith("vm")) {
61 log.warn("URI "
62 + uri
63 + " should have been managed by generic JCR repository factory");
64 repository = getRepositoryByAlias(getAliasFromURI(uri));
65 }
66 }
67
68 // publish under alias
69 if (parameters.containsKey(JCR_REPOSITORY_ALIAS)) {
70 Properties properties = new Properties();
71 properties.putAll(parameters);
72 String alias = parameters.get(JCR_REPOSITORY_ALIAS).toString();
73 publish(alias, repository, properties);
74 log.info("Registered JCR repository under alias '" + alias
75 + "' with properties " + properties);
76 }
77
78 return repository;
79 }
80
81 protected Repository createRemoteRepository(String uri)
82 throws RepositoryException {
83 Map<String, String> params = new HashMap<String, String>();
84 params.put(JcrUtils.REPOSITORY_URI, uri);
85 Repository repository = new Jcr2davRepositoryFactory()
86 .getRepository(params);
87 if (repository == null)
88 throw new ArgeoException("Remote Davex repository " + uri
89 + " not found");
90 log.info("Initialized remote Jackrabbit repository from uri " + uri);
91 return repository;
92 }
93
94 /**
95 * Called after the repository has been initialised. Does nothing by
96 * default.
97 */
98 @SuppressWarnings("rawtypes")
99 protected void postInitialization(Repository repository, Map parameters) {
100
101 }
102
103 }