1 package org
.argeo
.cms
.internal
.jcr
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
6 import java
.net
.URISyntaxException
;
7 import java
.nio
.file
.Files
;
8 import java
.nio
.file
.Path
;
9 import java
.nio
.file
.Paths
;
10 import java
.util
.Dictionary
;
11 import java
.util
.Enumeration
;
12 import java
.util
.Properties
;
13 import java
.util
.UUID
;
15 import javax
.jcr
.RepositoryException
;
17 import org
.apache
.commons
.logging
.Log
;
18 import org
.apache
.commons
.logging
.LogFactory
;
19 import org
.apache
.jackrabbit
.core
.RepositoryContext
;
20 import org
.apache
.jackrabbit
.core
.RepositoryImpl
;
21 import org
.apache
.jackrabbit
.core
.cache
.CacheManager
;
22 import org
.apache
.jackrabbit
.core
.config
.RepositoryConfig
;
23 import org
.apache
.jackrabbit
.core
.config
.RepositoryConfigurationParser
;
24 import org
.argeo
.cms
.CmsException
;
25 import org
.argeo
.cms
.internal
.kernel
.CmsPaths
;
26 import org
.argeo
.jcr
.ArgeoJcrException
;
27 import org
.argeo
.node
.NodeConstants
;
28 import org
.xml
.sax
.InputSource
;
30 /** Can interpret properties in order to create an actual JCR repository. */
31 public class RepositoryBuilder
{
32 private final static Log log
= LogFactory
.getLog(RepositoryBuilder
.class);
34 public RepositoryContext
createRepositoryContext(Dictionary
<String
, ?
> properties
)
35 throws RepositoryException
, IOException
{
36 RepositoryConfig repositoryConfig
= createRepositoryConfig(properties
);
37 RepositoryContext repositoryContext
= createJackrabbitRepository(repositoryConfig
);
38 RepositoryImpl repository
= repositoryContext
.getRepository();
41 Object maxCacheMbStr
= prop(properties
, RepoConf
.maxCacheMB
);
42 if (maxCacheMbStr
!= null) {
43 Integer maxCacheMB
= Integer
.parseInt(maxCacheMbStr
.toString());
44 CacheManager cacheManager
= repository
.getCacheManager();
45 cacheManager
.setMaxMemory(maxCacheMB
* 1024l * 1024l);
46 cacheManager
.setMaxMemoryPerCache((maxCacheMB
/ 4) * 1024l * 1024l);
49 return repositoryContext
;
52 RepositoryConfig
createRepositoryConfig(Dictionary
<String
, ?
> properties
) throws RepositoryException
, IOException
{
53 JackrabbitType type
= JackrabbitType
.valueOf(prop(properties
, RepoConf
.type
).toString());
54 ClassLoader cl
= getClass().getClassLoader();
55 final String base
= "/org/argeo/cms/internal/jcr";
56 try (InputStream in
= cl
.getResourceAsStream(base
+ "/repository-" + type
.name() + ".xml")) {
58 throw new ArgeoJcrException("Repository configuration not found");
59 InputSource config
= new InputSource(in
);
60 Properties jackrabbitVars
= getConfigurationProperties(type
, properties
);
61 RepositoryConfig repositoryConfig
= RepositoryConfig
.create(config
, jackrabbitVars
);
62 return repositoryConfig
;
66 private Properties
getConfigurationProperties(JackrabbitType type
, Dictionary
<String
, ?
> properties
) {
67 Properties props
= new Properties();
68 for (Enumeration
<String
> keys
= properties
.keys(); keys
.hasMoreElements();) {
69 String key
= keys
.nextElement();
70 props
.put(key
, properties
.get(key
));
74 // cf. https://wiki.apache.org/jackrabbit/Clustering
75 // TODO deal with multiple repos
76 String clusterId
= System
.getProperty("org.apache.jackrabbit.core.cluster.node_id");
77 String clusterIdProp
= props
.getProperty(RepoConf
.clusterId
.name());
78 if (clusterId
!= null) {
79 if (clusterIdProp
!= null)
80 throw new CmsException("Cluster id defined as System properties and in deploy config");
81 props
.put(RepoConf
.clusterId
.name(), clusterId
);
83 clusterId
= clusterIdProp
;
87 String homeUri
= props
.getProperty(RepoConf
.labeledUri
.name());
89 if (homeUri
== null) {
90 String cn
= props
.getProperty(NodeConstants
.CN
);
92 if (clusterId
!= null) {
93 homePath
= CmsPaths
.getRepoDirPath(cn
+ '/' + clusterId
);
95 homePath
= CmsPaths
.getRepoDirPath(cn
);
99 URI uri
= new URI(homeUri
);
100 String host
= uri
.getHost();
101 if (host
== null || host
.trim().equals("")) {
102 homePath
= Paths
.get(uri
).toAbsolutePath();
104 // TODO remote at this stage?
105 throw new IllegalArgumentException("Cannot manage repository path for host " + host
);
107 } catch (URISyntaxException e
) {
108 throw new CmsException("Invalid repository home URI", e
);
111 // TODO use Jackrabbit API (?)
112 Path rootUuidPath
= homePath
.resolve("repository/meta/rootUUID");
114 if (!Files
.exists(rootUuidPath
)) {
115 Files
.createDirectories(rootUuidPath
.getParent());
116 Files
.write(rootUuidPath
, UUID
.randomUUID().toString().getBytes());
118 // File homeDir = homePath.toFile();
120 } catch (IOException e
) {
121 throw new CmsException("Cannot set up repository home " + homePath
, e
);
123 // home cannot be overridden
124 props
.put(RepositoryConfigurationParser
.REPOSITORY_HOME_VARIABLE
, homePath
.toString());
126 setProp(props
, RepoConf
.indexesBase
, CmsPaths
.getRepoIndexesBase().toString());
128 setProp(props
, RepoConf
.defaultWorkspace
);
129 setProp(props
, RepoConf
.maxPoolSize
);
130 // Jackrabbit defaults
131 setProp(props
, RepoConf
.bundleCacheMB
);
132 // See http://wiki.apache.org/jackrabbit/Search
133 setProp(props
, RepoConf
.extractorPoolSize
);
134 setProp(props
, RepoConf
.searchCacheSize
);
135 setProp(props
, RepoConf
.maxVolatileIndexSize
);
141 dburl
= "jdbc:h2:" + homePath
.toAbsolutePath() + "/h2/repository";
142 setProp(props
, RepoConf
.dburl
, dburl
);
143 setProp(props
, RepoConf
.dbuser
, "sa");
144 setProp(props
, RepoConf
.dbpassword
, "");
148 case postgresql_cluster
:
149 case postgresql_cluster_ds
:
150 dburl
= "jdbc:postgresql://localhost/demo";
151 setProp(props
, RepoConf
.dburl
, dburl
);
152 setProp(props
, RepoConf
.dbuser
, "argeo");
153 setProp(props
, RepoConf
.dbpassword
, "argeo");
160 throw new ArgeoJcrException("Unsupported node type " + type
);
165 private void setProp(Properties props
, RepoConf key
, String def
) {
166 Object value
= props
.get(key
.name());
170 value
= key
.getDefault();
172 props
.put(key
.name(), value
.toString());
175 private void setProp(Properties props
, RepoConf key
) {
176 setProp(props
, key
, null);
179 private String
prop(Dictionary
<String
, ?
> properties
, RepoConf key
) {
180 Object value
= properties
.get(key
.name());
182 return key
.getDefault() != null ? key
.getDefault().toString() : null;
184 return value
.toString();
187 private RepositoryContext
createJackrabbitRepository(RepositoryConfig repositoryConfig
) throws RepositoryException
{
188 ClassLoader currentContextCl
= Thread
.currentThread().getContextClassLoader();
189 Thread
.currentThread().setContextClassLoader(RepositoryBuilder
.class.getClassLoader());
191 long begin
= System
.currentTimeMillis();
193 // Actual repository creation
195 RepositoryContext repositoryContext
= RepositoryContext
.create(repositoryConfig
);
197 double duration
= ((double) (System
.currentTimeMillis() - begin
)) / 1000;
198 if (log
.isDebugEnabled())
200 "Created Jackrabbit repository in " + duration
+ " s, home: " + repositoryConfig
.getHomeDir());
202 return repositoryContext
;
204 Thread
.currentThread().setContextClassLoader(currentContextCl
);