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
;
28 import org
.apache
.commons
.io
.IOUtils
;
29 import org
.apache
.commons
.logging
.Log
;
30 import org
.apache
.commons
.logging
.LogFactory
;
31 import org
.apache
.jackrabbit
.commons
.JcrUtils
;
32 import org
.apache
.jackrabbit
.core
.RepositoryImpl
;
33 import org
.apache
.jackrabbit
.core
.config
.RepositoryConfig
;
34 import org
.apache
.jackrabbit
.core
.config
.RepositoryConfigurationParser
;
35 import org
.apache
.jackrabbit
.jcr2dav
.Jcr2davRepositoryFactory
;
36 import org
.argeo
.ArgeoException
;
37 import org
.argeo
.jcr
.ArgeoJcrConstants
;
38 import org
.argeo
.jcr
.DefaultRepositoryFactory
;
39 import org
.springframework
.core
.io
.ClassPathResource
;
40 import org
.springframework
.core
.io
.Resource
;
41 import org
.xml
.sax
.InputSource
;
44 * Repository factory which can create new repositories and access remote
45 * Jackrabbit repositories
47 public class JackrabbitRepositoryFactory
extends DefaultRepositoryFactory
48 implements RepositoryFactory
, ArgeoJcrConstants
{
50 private final static Log log
= LogFactory
51 .getLog(JackrabbitRepositoryFactory
.class);
53 private Resource fileRepositoryConfiguration
= new ClassPathResource(
54 "/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)
65 if (parameters
.containsKey(JCR_REPOSITORY_URI
))
66 uri
= parameters
.get(JCR_REPOSITORY_URI
).toString();
67 else if (parameters
.containsKey(JcrUtils
.REPOSITORY_URI
))
68 uri
= parameters
.get(JcrUtils
.REPOSITORY_URI
).toString();
71 if (uri
.startsWith("http"))// http, https
72 repository
= createRemoteRepository(uri
);
73 else if (uri
.startsWith("file"))// http, https
74 repository
= createFileRepository(uri
, parameters
);
75 else if (uri
.startsWith("vm")) {
78 + " should have been managed by generic JCR repository factory");
79 repository
= getRepositoryByAlias(getAliasFromURI(uri
));
83 // publish under alias
84 if (parameters
.containsKey(JCR_REPOSITORY_ALIAS
)) {
85 Properties properties
= new Properties();
86 properties
.putAll(parameters
);
87 String alias
= parameters
.get(JCR_REPOSITORY_ALIAS
).toString();
88 publish(alias
, repository
, properties
);
89 log
.info("Registered JCR repository under alias '" + alias
90 + "' with properties " + properties
);
96 protected Repository
createRemoteRepository(String uri
)
97 throws RepositoryException
{
98 Map
<String
, String
> params
= new HashMap
<String
, String
>();
99 params
.put(JcrUtils
.REPOSITORY_URI
, uri
);
100 Repository repository
= new Jcr2davRepositoryFactory()
101 .getRepository(params
);
102 if (repository
== null)
103 throw new ArgeoException("Remote Davex repository " + uri
105 log
.info("Initialized remote Jackrabbit repository from uri " + uri
);
109 @SuppressWarnings({ "rawtypes", "unchecked" })
110 protected Repository
createFileRepository(final String uri
, Map parameters
)
111 throws RepositoryException
{
112 InputStream configurationIn
= null;
114 Properties vars
= new Properties();
115 vars
.putAll(parameters
);
116 String dirPath
= uri
.substring("file:".length());
117 File homeDir
= new File(dirPath
);
118 if (homeDir
.exists() && !homeDir
.isDirectory())
119 throw new ArgeoException("Repository home " + dirPath
120 + " is not a directory");
121 if (!homeDir
.exists())
123 configurationIn
= fileRepositoryConfiguration
.getInputStream();
124 vars
.put(RepositoryConfigurationParser
.REPOSITORY_HOME_VARIABLE
,
125 homeDir
.getCanonicalPath());
126 RepositoryConfig repositoryConfig
= RepositoryConfig
.create(
127 new InputSource(configurationIn
), vars
);
129 // TransientRepository repository = new
130 // TransientRepository(repositoryConfig);
131 final RepositoryImpl repository
= RepositoryImpl
132 .create(repositoryConfig
);
133 Runtime
.getRuntime().addShutdownHook(
134 new Thread("Clean JCR repository " + uri
) {
136 repository
.shutdown();
137 log
.info("Destroyed repository " + uri
);
140 log
.info("Initialized file Jackrabbit repository from uri " + uri
);
142 } catch (Exception e
) {
143 throw new ArgeoException("Cannot create repository " + uri
, e
);
145 IOUtils
.closeQuietly(configurationIn
);
150 * Called after the repository has been initialised. Does nothing by
153 @SuppressWarnings("rawtypes")
154 protected void postInitialization(Repository repository
, Map parameters
) {
158 public void setFileRepositoryConfiguration(
159 Resource fileRepositoryConfiguration
) {
160 this.fileRepositoryConfiguration
= fileRepositoryConfiguration
;