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
.jcr
.ArgeoJcrException
;
40 import org
.springframework
.core
.io
.ClassPathResource
;
41 import org
.springframework
.core
.io
.Resource
;
42 import org
.xml
.sax
.InputSource
;
45 * Repository factory which can create new repositories and access remote
46 * Jackrabbit repositories
49 public class JackrabbitRepositoryFactory
implements RepositoryFactory
{
50 // FIXME factorize with node
51 /** Key for a JCR repository alias */
52 public final static String JCR_REPOSITORY_ALIAS
= "argeo.jcr.repository.alias";
53 /** Key for a JCR repository URI */
54 public final static String JCR_REPOSITORY_URI
= "argeo.jcr.repository.uri";
56 private final static Log log
= LogFactory
57 .getLog(JackrabbitRepositoryFactory
.class);
59 private Resource fileRepositoryConfiguration
= new ClassPathResource(
60 "/org/argeo/jackrabbit/repository-h2.xml");
62 @SuppressWarnings({ "rawtypes" })
63 public Repository
getRepository(Map parameters
) throws RepositoryException
{
64 // // check if can be found by alias
65 // Repository repository = super.getRepository(parameters);
66 // if (repository != null)
70 Repository repository
;
72 if (parameters
.containsKey(JCR_REPOSITORY_URI
))
73 uri
= parameters
.get(JCR_REPOSITORY_URI
).toString();
74 else if (parameters
.containsKey(JcrUtils
.REPOSITORY_URI
))
75 uri
= parameters
.get(JcrUtils
.REPOSITORY_URI
).toString();
78 if (uri
.startsWith("http"))// http, https
79 repository
= createRemoteRepository(uri
);
80 else if (uri
.startsWith("file"))// http, https
81 repository
= createFileRepository(uri
, parameters
);
82 else if (uri
.startsWith("vm")) {
85 + " should have been managed by generic JCR repository factory");
86 repository
= getRepositoryByAlias(getAliasFromURI(uri
));
88 throw new ArgeoJcrException("Unrecognized URI format " + uri
);
92 else if (parameters
.containsKey(JCR_REPOSITORY_ALIAS
)) {
93 // Properties properties = new Properties();
94 // properties.putAll(parameters);
95 String alias
= parameters
.get(JCR_REPOSITORY_ALIAS
).toString();
96 // publish(alias, repository, properties);
97 // log.info("Registered JCR repository under alias '" + alias + "'
98 // with properties " + properties);
99 repository
= getRepositoryByAlias(alias
);
101 throw new ArgeoJcrException("Not enough information in "
104 if (repository
== null)
105 throw new ArgeoJcrException("Repository not found " + parameters
);
110 protected Repository
getRepositoryByAlias(String alias
) {
114 protected Repository
createRemoteRepository(String uri
)
115 throws RepositoryException
{
116 Map
<String
, String
> params
= new HashMap
<String
, String
>();
117 params
.put(JcrUtils
.REPOSITORY_URI
, uri
);
118 Repository repository
= new Jcr2davRepositoryFactory()
119 .getRepository(params
);
120 if (repository
== null)
121 throw new ArgeoJcrException("Remote Davex repository " + uri
123 log
.info("Initialized remote Jackrabbit repository from uri " + uri
);
127 @SuppressWarnings({ "rawtypes", "unchecked" })
128 protected Repository
createFileRepository(final String uri
, Map parameters
)
129 throws RepositoryException
{
130 InputStream configurationIn
= null;
132 Properties vars
= new Properties();
133 vars
.putAll(parameters
);
134 String dirPath
= uri
.substring("file:".length());
135 File homeDir
= new File(dirPath
);
136 if (homeDir
.exists() && !homeDir
.isDirectory())
137 throw new ArgeoJcrException("Repository home " + dirPath
138 + " is not a directory");
139 if (!homeDir
.exists())
141 configurationIn
= fileRepositoryConfiguration
.getInputStream();
142 vars
.put(RepositoryConfigurationParser
.REPOSITORY_HOME_VARIABLE
,
143 homeDir
.getCanonicalPath());
144 RepositoryConfig repositoryConfig
= RepositoryConfig
.create(
145 new InputSource(configurationIn
), vars
);
147 // TransientRepository repository = new
148 // TransientRepository(repositoryConfig);
149 final RepositoryImpl repository
= RepositoryImpl
150 .create(repositoryConfig
);
151 Session session
= repository
.login();
152 // FIXME make it generic
153 org
.argeo
.jcr
.JcrUtils
.addPrivilege(session
, "/", "ROLE_ADMIN",
155 org
.argeo
.jcr
.JcrUtils
.logoutQuietly(session
);
156 Runtime
.getRuntime().addShutdownHook(
157 new Thread("Clean JCR repository " + uri
) {
159 repository
.shutdown();
160 log
.info("Destroyed repository " + uri
);
163 log
.info("Initialized file Jackrabbit repository from uri " + uri
);
165 } catch (Exception e
) {
166 throw new ArgeoJcrException("Cannot create repository " + uri
, e
);
168 IOUtils
.closeQuietly(configurationIn
);
172 protected String
getAliasFromURI(String uri
) {
174 URI uriObj
= new URI(uri
);
175 String alias
= uriObj
.getPath();
176 if (alias
.charAt(0) == '/')
177 alias
= alias
.substring(1);
178 if (alias
.charAt(alias
.length() - 1) == '/')
179 alias
= alias
.substring(0, alias
.length() - 1);
181 } catch (URISyntaxException e
) {
182 throw new ArgeoJcrException("Cannot interpret URI " + uri
, e
);
187 * Called after the repository has been initialised. Does nothing by
190 @SuppressWarnings("rawtypes")
191 protected void postInitialization(Repository repository
, Map parameters
) {
195 public void setFileRepositoryConfiguration(
196 Resource fileRepositoryConfiguration
) {
197 this.fileRepositoryConfiguration
= fileRepositoryConfiguration
;