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
.ArgeoJcrConstants
;
40 import org
.argeo
.jcr
.ArgeoJcrException
;
41 import org
.springframework
.core
.io
.ClassPathResource
;
42 import org
.springframework
.core
.io
.Resource
;
43 import org
.xml
.sax
.InputSource
;
46 * Repository factory which can create new repositories and access remote
47 * Jackrabbit repositories
49 public class JackrabbitRepositoryFactory
implements RepositoryFactory
, ArgeoJcrConstants
{
50 private final static Log log
= LogFactory
.getLog(JackrabbitRepositoryFactory
.class);
52 private Resource fileRepositoryConfiguration
= new ClassPathResource("/org/argeo/jackrabbit/repository-h2.xml");
54 @SuppressWarnings({ "rawtypes" })
55 public Repository
getRepository(Map parameters
) throws RepositoryException
{
56 // // check if can be found by alias
57 // Repository repository = super.getRepository(parameters);
58 // if (repository != null)
62 Repository repository
;
64 if (parameters
.containsKey(JCR_REPOSITORY_URI
))
65 uri
= parameters
.get(JCR_REPOSITORY_URI
).toString();
66 else if (parameters
.containsKey(JcrUtils
.REPOSITORY_URI
))
67 uri
= parameters
.get(JcrUtils
.REPOSITORY_URI
).toString();
70 if (uri
.startsWith("http"))// http, https
71 repository
= createRemoteRepository(uri
);
72 else if (uri
.startsWith("file"))// http, https
73 repository
= createFileRepository(uri
, parameters
);
74 else if (uri
.startsWith("vm")) {
75 log
.warn("URI " + uri
+ " should have been managed by generic JCR repository factory");
76 repository
= getRepositoryByAlias(getAliasFromURI(uri
));
78 throw new ArgeoJcrException("Unrecognized URI format " + uri
);
82 else if (parameters
.containsKey(JCR_REPOSITORY_ALIAS
)) {
83 // Properties properties = new Properties();
84 // properties.putAll(parameters);
85 String alias
= parameters
.get(JCR_REPOSITORY_ALIAS
).toString();
86 // publish(alias, repository, properties);
87 // log.info("Registered JCR repository under alias '" + alias + "'
88 // with properties " + properties);
89 repository
= getRepositoryByAlias(alias
);
91 throw new ArgeoJcrException("Not enough information in " + parameters
);
93 if (repository
== null)
94 throw new ArgeoJcrException("Repository not found " + parameters
);
99 protected Repository
getRepositoryByAlias(String alias
) {
103 protected Repository
createRemoteRepository(String uri
) throws RepositoryException
{
104 Map
<String
, String
> params
= new HashMap
<String
, String
>();
105 params
.put(JcrUtils
.REPOSITORY_URI
, uri
);
106 Repository repository
= new Jcr2davRepositoryFactory().getRepository(params
);
107 if (repository
== null)
108 throw new ArgeoJcrException("Remote Davex repository " + uri
+ " not found");
109 log
.info("Initialized remote Jackrabbit repository from uri " + uri
);
113 @SuppressWarnings({ "rawtypes", "unchecked" })
114 protected Repository
createFileRepository(final String uri
, Map parameters
) throws RepositoryException
{
115 InputStream configurationIn
= null;
117 Properties vars
= new Properties();
118 vars
.putAll(parameters
);
119 String dirPath
= uri
.substring("file:".length());
120 File homeDir
= new File(dirPath
);
121 if (homeDir
.exists() && !homeDir
.isDirectory())
122 throw new ArgeoJcrException("Repository home " + dirPath
+ " is not a directory");
123 if (!homeDir
.exists())
125 configurationIn
= fileRepositoryConfiguration
.getInputStream();
126 vars
.put(RepositoryConfigurationParser
.REPOSITORY_HOME_VARIABLE
, homeDir
.getCanonicalPath());
127 RepositoryConfig repositoryConfig
= RepositoryConfig
.create(new InputSource(configurationIn
), vars
);
129 // TransientRepository repository = new
130 // TransientRepository(repositoryConfig);
131 final RepositoryImpl repository
= RepositoryImpl
.create(repositoryConfig
);
132 Session session
= repository
.login();
133 // FIXME make it generic
134 org
.argeo
.jcr
.JcrUtils
.addPrivilege(session
, "/", "ROLE_ADMIN", "jcr:all");
135 org
.argeo
.jcr
.JcrUtils
.logoutQuietly(session
);
136 Runtime
.getRuntime().addShutdownHook(new Thread("Clean JCR repository " + uri
) {
138 repository
.shutdown();
139 log
.info("Destroyed repository " + uri
);
142 log
.info("Initialized file Jackrabbit repository from uri " + uri
);
144 } catch (Exception e
) {
145 throw new ArgeoJcrException("Cannot create repository " + uri
, e
);
147 IOUtils
.closeQuietly(configurationIn
);
151 protected String
getAliasFromURI(String uri
) {
153 URI uriObj
= new URI(uri
);
154 String alias
= uriObj
.getPath();
155 if (alias
.charAt(0) == '/')
156 alias
= alias
.substring(1);
157 if (alias
.charAt(alias
.length() - 1) == '/')
158 alias
= alias
.substring(0, alias
.length() - 1);
160 } catch (URISyntaxException e
) {
161 throw new ArgeoJcrException("Cannot interpret URI " + uri
, e
);
166 * Called after the repository has been initialised. Does nothing by
169 @SuppressWarnings("rawtypes")
170 protected void postInitialization(Repository repository
, Map parameters
) {
174 public void setFileRepositoryConfiguration(Resource fileRepositoryConfiguration
) {
175 this.fileRepositoryConfiguration
= fileRepositoryConfiguration
;