2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.jackrabbit
;
19 import java
.io
.InputStream
;
21 import java
.net
.URISyntaxException
;
22 import java
.util
.HashMap
;
24 import java
.util
.Properties
;
26 import javax
.jcr
.Repository
;
27 import javax
.jcr
.RepositoryException
;
28 import javax
.jcr
.RepositoryFactory
;
29 import javax
.jcr
.Session
;
31 import org
.apache
.commons
.io
.IOUtils
;
32 import org
.apache
.commons
.logging
.Log
;
33 import org
.apache
.commons
.logging
.LogFactory
;
34 import org
.apache
.jackrabbit
.commons
.JcrUtils
;
35 import org
.apache
.jackrabbit
.core
.RepositoryImpl
;
36 import org
.apache
.jackrabbit
.core
.config
.RepositoryConfig
;
37 import org
.apache
.jackrabbit
.core
.config
.RepositoryConfigurationParser
;
38 import org
.apache
.jackrabbit
.jcr2dav
.Jcr2davRepositoryFactory
;
39 import org
.argeo
.ArgeoException
;
40 import org
.argeo
.jcr
.ArgeoJcrConstants
;
41 import org
.argeo
.jcr
.ArgeoJcrException
;
42 import org
.springframework
.core
.io
.ClassPathResource
;
43 import org
.springframework
.core
.io
.Resource
;
44 import org
.xml
.sax
.InputSource
;
47 * Repository factory which can create new repositories and access remote
48 * Jackrabbit repositories
50 public class JackrabbitRepositoryFactory
implements RepositoryFactory
, ArgeoJcrConstants
{
52 private final static Log log
= LogFactory
.getLog(JackrabbitRepositoryFactory
.class);
54 private Resource fileRepositoryConfiguration
= new ClassPathResource("/org/argeo/jackrabbit/repository-h2.xml");
56 @SuppressWarnings({ "rawtypes", "unchecked" })
57 public Repository
getRepository(Map parameters
) throws RepositoryException
{
58 // // check if can be found by alias
59 // Repository repository = super.getRepository(parameters);
60 // if (repository != null)
64 Repository repository
;
66 if (parameters
.containsKey(JCR_REPOSITORY_URI
))
67 uri
= parameters
.get(JCR_REPOSITORY_URI
).toString();
68 else if (parameters
.containsKey(JcrUtils
.REPOSITORY_URI
))
69 uri
= parameters
.get(JcrUtils
.REPOSITORY_URI
).toString();
72 if (uri
.startsWith("http"))// http, https
73 repository
= createRemoteRepository(uri
);
74 else if (uri
.startsWith("file"))// http, https
75 repository
= createFileRepository(uri
, parameters
);
76 else if (uri
.startsWith("vm")) {
79 + " should have been managed by generic JCR repository factory");
80 repository
= getRepositoryByAlias(getAliasFromURI(uri
));
83 throw new ArgeoJcrException("Unrecognized URI format " + uri
);
87 else if (parameters
.containsKey(JCR_REPOSITORY_ALIAS
)) {
88 // Properties properties = new Properties();
89 // properties.putAll(parameters);
90 String alias
= parameters
.get(JCR_REPOSITORY_ALIAS
).toString();
91 // publish(alias, repository, properties);
92 // log.info("Registered JCR repository under alias '" + alias + "'
93 // with properties " + properties);
94 repository
= getRepositoryByAlias(alias
);
96 throw new ArgeoJcrException("Not enough information in " + parameters
);
98 if (repository
== null)
99 throw new ArgeoJcrException("Repository not found " + parameters
);
104 protected Repository
getRepositoryByAlias(String alias
) {
108 protected Repository
createRemoteRepository(String uri
) throws RepositoryException
{
109 Map
<String
, String
> params
= new HashMap
<String
, String
>();
110 params
.put(JcrUtils
.REPOSITORY_URI
, uri
);
111 Repository repository
= new Jcr2davRepositoryFactory().getRepository(params
);
112 if (repository
== null)
113 throw new ArgeoException("Remote Davex repository " + uri
+ " not found");
114 log
.info("Initialized remote Jackrabbit repository from uri " + uri
);
118 @SuppressWarnings({ "rawtypes", "unchecked" })
119 protected Repository
createFileRepository(final String uri
, Map parameters
) throws RepositoryException
{
120 InputStream configurationIn
= null;
122 Properties vars
= new Properties();
123 vars
.putAll(parameters
);
124 String dirPath
= uri
.substring("file:".length());
125 File homeDir
= new File(dirPath
);
126 if (homeDir
.exists() && !homeDir
.isDirectory())
127 throw new ArgeoException("Repository home " + dirPath
+ " is not a directory");
128 if (!homeDir
.exists())
130 configurationIn
= fileRepositoryConfiguration
.getInputStream();
131 vars
.put(RepositoryConfigurationParser
.REPOSITORY_HOME_VARIABLE
, homeDir
.getCanonicalPath());
132 RepositoryConfig repositoryConfig
= RepositoryConfig
.create(new InputSource(configurationIn
), vars
);
134 // TransientRepository repository = new
135 // TransientRepository(repositoryConfig);
136 final RepositoryImpl repository
= RepositoryImpl
.create(repositoryConfig
);
137 Session session
= repository
.login();
138 // FIXME make it generic
139 org
.argeo
.jcr
.JcrUtils
.addPrivilege(session
, "/", "ROLE_ADMIN", "jcr:all");
140 org
.argeo
.jcr
.JcrUtils
.logoutQuietly(session
);
141 Runtime
.getRuntime().addShutdownHook(new Thread("Clean JCR repository " + uri
) {
143 repository
.shutdown();
144 log
.info("Destroyed repository " + uri
);
147 log
.info("Initialized file Jackrabbit repository from uri " + uri
);
149 } catch (Exception e
) {
150 throw new ArgeoException("Cannot create repository " + uri
, e
);
152 IOUtils
.closeQuietly(configurationIn
);
156 protected String
getAliasFromURI(String uri
) {
158 URI uriObj
= new URI(uri
);
159 String alias
= uriObj
.getPath();
160 if (alias
.charAt(0) == '/')
161 alias
= alias
.substring(1);
162 if (alias
.charAt(alias
.length() - 1) == '/')
163 alias
= alias
.substring(0, alias
.length() - 1);
165 } catch (URISyntaxException e
) {
166 throw new ArgeoException("Cannot interpret URI " + uri
, e
);
171 * Called after the repository has been initialised. Does nothing by
174 @SuppressWarnings("rawtypes")
175 protected void postInitialization(Repository repository
, Map parameters
) {
179 public void setFileRepositoryConfiguration(Resource fileRepositoryConfiguration
) {
180 this.fileRepositoryConfiguration
= fileRepositoryConfiguration
;