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
;
20 import java
.util
.HashMap
;
22 import java
.util
.Properties
;
24 import javax
.jcr
.Repository
;
25 import javax
.jcr
.RepositoryException
;
26 import javax
.jcr
.RepositoryFactory
;
27 import javax
.jcr
.Session
;
29 import org
.apache
.commons
.io
.IOUtils
;
30 import org
.apache
.commons
.logging
.Log
;
31 import org
.apache
.commons
.logging
.LogFactory
;
32 import org
.apache
.jackrabbit
.commons
.JcrUtils
;
33 import org
.apache
.jackrabbit
.core
.RepositoryImpl
;
34 import org
.apache
.jackrabbit
.core
.config
.RepositoryConfig
;
35 import org
.apache
.jackrabbit
.core
.config
.RepositoryConfigurationParser
;
36 import org
.apache
.jackrabbit
.jcr2dav
.Jcr2davRepositoryFactory
;
37 import org
.argeo
.ArgeoException
;
38 import org
.argeo
.jcr
.ArgeoJcrConstants
;
39 import org
.argeo
.jcr
.DefaultRepositoryFactory
;
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
48 public class JackrabbitRepositoryFactory
extends DefaultRepositoryFactory
49 implements RepositoryFactory
, ArgeoJcrConstants
{
51 private final static Log log
= LogFactory
52 .getLog(JackrabbitRepositoryFactory
.class);
54 private Resource fileRepositoryConfiguration
= new ClassPathResource(
55 "/org/argeo/jackrabbit/repository-h2.xml");
57 @SuppressWarnings({ "rawtypes", "unchecked" })
58 public Repository
getRepository(Map parameters
) throws RepositoryException
{
59 // check if can be found by alias
60 Repository repository
= super.getRepository(parameters
);
61 if (repository
!= null)
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
));
84 // publish under alias
85 if (parameters
.containsKey(JCR_REPOSITORY_ALIAS
)) {
86 Properties properties
= new Properties();
87 properties
.putAll(parameters
);
88 String alias
= parameters
.get(JCR_REPOSITORY_ALIAS
).toString();
89 publish(alias
, repository
, properties
);
90 log
.info("Registered JCR repository under alias '" + alias
91 + "' with properties " + properties
);
97 protected Repository
createRemoteRepository(String uri
)
98 throws RepositoryException
{
99 Map
<String
, String
> params
= new HashMap
<String
, String
>();
100 params
.put(JcrUtils
.REPOSITORY_URI
, uri
);
101 Repository repository
= new Jcr2davRepositoryFactory()
102 .getRepository(params
);
103 if (repository
== null)
104 throw new ArgeoException("Remote Davex repository " + uri
106 log
.info("Initialized remote Jackrabbit repository from uri " + uri
);
110 @SuppressWarnings({ "rawtypes", "unchecked" })
111 protected Repository
createFileRepository(final String uri
, Map parameters
)
112 throws RepositoryException
{
113 InputStream configurationIn
= null;
115 Properties vars
= new Properties();
116 vars
.putAll(parameters
);
117 String dirPath
= uri
.substring("file:".length());
118 File homeDir
= new File(dirPath
);
119 if (homeDir
.exists() && !homeDir
.isDirectory())
120 throw new ArgeoException("Repository home " + dirPath
121 + " is not a directory");
122 if (!homeDir
.exists())
124 configurationIn
= fileRepositoryConfiguration
.getInputStream();
125 vars
.put(RepositoryConfigurationParser
.REPOSITORY_HOME_VARIABLE
,
126 homeDir
.getCanonicalPath());
127 RepositoryConfig repositoryConfig
= RepositoryConfig
.create(
128 new InputSource(configurationIn
), vars
);
130 // TransientRepository repository = new
131 // TransientRepository(repositoryConfig);
132 final RepositoryImpl repository
= RepositoryImpl
133 .create(repositoryConfig
);
134 Session session
= repository
.login();
135 // FIXME make it generic
136 org
.argeo
.jcr
.JcrUtils
.addPrivilege(session
, "/", "ROLE_ADMIN",
138 org
.argeo
.jcr
.JcrUtils
.logoutQuietly(session
);
139 Runtime
.getRuntime().addShutdownHook(
140 new Thread("Clean JCR repository " + uri
) {
142 repository
.shutdown();
143 log
.info("Destroyed repository " + uri
);
146 log
.info("Initialized file Jackrabbit repository from uri " + uri
);
148 } catch (Exception e
) {
149 throw new ArgeoException("Cannot create repository " + uri
, e
);
151 IOUtils
.closeQuietly(configurationIn
);
156 * Called after the repository has been initialised. Does nothing by
159 @SuppressWarnings("rawtypes")
160 protected void postInitialization(Repository repository
, Map parameters
) {
164 public void setFileRepositoryConfiguration(
165 Resource fileRepositoryConfiguration
) {
166 this.fileRepositoryConfiguration
= fileRepositoryConfiguration
;