1 package org
.argeo
.cms
.internal
.kernel
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
7 import java
.net
.URISyntaxException
;
8 import java
.nio
.file
.Files
;
9 import java
.nio
.file
.Path
;
10 import java
.nio
.file
.Paths
;
11 import java
.util
.Dictionary
;
12 import java
.util
.Enumeration
;
13 import java
.util
.Properties
;
14 import java
.util
.UUID
;
16 import javax
.jcr
.RepositoryException
;
18 import org
.apache
.commons
.io
.IOUtils
;
19 import org
.apache
.commons
.logging
.Log
;
20 import org
.apache
.commons
.logging
.LogFactory
;
21 import org
.apache
.jackrabbit
.core
.RepositoryContext
;
22 import org
.apache
.jackrabbit
.core
.RepositoryImpl
;
23 import org
.apache
.jackrabbit
.core
.cache
.CacheManager
;
24 import org
.apache
.jackrabbit
.core
.config
.RepositoryConfig
;
25 import org
.apache
.jackrabbit
.core
.config
.RepositoryConfigurationParser
;
26 import org
.argeo
.cms
.CmsException
;
27 import org
.argeo
.jcr
.ArgeoJcrException
;
28 import org
.argeo
.node
.NodeConstants
;
29 import org
.xml
.sax
.InputSource
;
31 /** Can interpret properties in order to create an actual JCR repository. */
32 class RepositoryBuilder
{
33 private final static Log log
= LogFactory
.getLog(RepositoryBuilder
.class);
35 RepositoryContext
createRepositoryContext(Dictionary
<String
, ?
> properties
) throws RepositoryException
{
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
{
53 JackrabbitType type
= JackrabbitType
.valueOf(prop(properties
, RepoConf
.type
).toString());
54 ClassLoader cl
= getClass().getClassLoader();
55 InputStream in
= null;
57 final String base
= "/org/argeo/cms/internal/kernel";
60 in
= cl
.getResourceAsStream(base
+ "/repository-h2.xml");
63 in
= cl
.getResourceAsStream(base
+ "/repository-postgresql.xml");
66 in
= cl
.getResourceAsStream(base
+ "/repository-postgresql-ds.xml");
69 in
= cl
.getResourceAsStream(base
+ "/repository-memory.xml");
72 in
= cl
.getResourceAsStream(base
+ "/repository-localfs.xml");
75 throw new ArgeoJcrException("Unsupported node type " + type
);
79 throw new ArgeoJcrException("Repository configuration not found");
80 InputSource config
= new InputSource(in
);
81 Properties jackrabbitVars
= getConfigurationProperties(type
, properties
);
82 RepositoryConfig repositoryConfig
= RepositoryConfig
.create(config
, jackrabbitVars
);
83 return repositoryConfig
;
85 IOUtils
.closeQuietly(in
);
89 private Properties
getConfigurationProperties(JackrabbitType type
, Dictionary
<String
, ?
> properties
) {
90 Properties props
= new Properties();
91 for (Enumeration
<String
> keys
= properties
.keys(); keys
.hasMoreElements();) {
92 String key
= keys
.nextElement();
93 // if (key.equals(ConfigurationAdmin.SERVICE_FACTORYPID) ||
94 // key.equals(Constants.SERVICE_PID)
95 // || key.equals(ArgeoJcrConstants.JCR_REPOSITORY_ALIAS))
98 // String value = prop(properties, RepoConf.valueOf(key));
100 props
.put(key
, properties
.get(key
));
101 // } catch (IllegalArgumentException e) {
102 // // ignore non RepoConf
103 // // FIXME make it more flexible/extensible
108 String homeUri
= props
.getProperty(RepoConf
.labeledUri
.name());
110 if (homeUri
== null) {
111 String cn
= props
.getProperty(NodeConstants
.CN
);
113 homePath
= KernelUtils
.getOsgiInstancePath(KernelConstants
.DIR_REPOS
+ '/' + cn
);
116 homePath
= Paths
.get(new URI(homeUri
)).toAbsolutePath();
117 } catch (URISyntaxException e
) {
118 throw new CmsException("Invalid repository home URI", e
);
121 Path rootUuidPath
= homePath
.resolve("repository/meta/rootUUID");
122 if (!Files
.exists(rootUuidPath
)) {
124 Files
.createDirectories(rootUuidPath
.getParent());
125 Files
.write(rootUuidPath
, UUID
.randomUUID().toString().getBytes());
126 } catch (IOException e
) {
127 log
.error("Could not set rootUUID", e
);
130 File homeDir
= homePath
.toFile();
132 // home cannot be overridden
133 props
.put(RepositoryConfigurationParser
.REPOSITORY_HOME_VARIABLE
, homePath
.toString());
136 setProp(props
, RepoConf
.defaultWorkspace
);
137 setProp(props
, RepoConf
.maxPoolSize
);
138 // Jackrabbit defaults
139 setProp(props
, RepoConf
.bundleCacheMB
);
140 // See http://wiki.apache.org/jackrabbit/Search
141 setProp(props
, RepoConf
.extractorPoolSize
);
142 setProp(props
, RepoConf
.searchCacheSize
);
143 setProp(props
, RepoConf
.maxVolatileIndexSize
);
149 dburl
= "jdbc:h2:" + homeDir
.getPath() + "/h2/repository";
150 setProp(props
, RepoConf
.dburl
, dburl
);
151 setProp(props
, RepoConf
.dbuser
, "sa");
152 setProp(props
, RepoConf
.dbpassword
, "");
155 dburl
= "jdbc:postgresql://localhost/demo";
156 setProp(props
, RepoConf
.dburl
, dburl
);
157 setProp(props
, RepoConf
.dbuser
, "argeo");
158 setProp(props
, RepoConf
.dbpassword
, "argeo");
165 throw new ArgeoJcrException("Unsupported node type " + type
);
170 private void setProp(Properties props
, RepoConf key
, String def
) {
171 Object value
= props
.get(key
.name());
175 value
= key
.getDefault();
177 props
.put(key
.name(), value
.toString());
180 private void setProp(Properties props
, RepoConf key
) {
181 setProp(props
, key
, null);
184 private String
prop(Dictionary
<String
, ?
> properties
, RepoConf key
) {
185 Object value
= properties
.get(key
.name());
187 return key
.getDefault() != null ? key
.getDefault().toString() : null;
189 return value
.toString();
192 private RepositoryContext
createJackrabbitRepository(RepositoryConfig repositoryConfig
) throws RepositoryException
{
193 ClassLoader currentContextCl
= Thread
.currentThread().getContextClassLoader();
194 Thread
.currentThread().setContextClassLoader(RepositoryBuilder
.class.getClassLoader());
196 long begin
= System
.currentTimeMillis();
198 // Actual repository creation
200 RepositoryContext repositoryContext
= RepositoryContext
.create(repositoryConfig
);
202 double duration
= ((double) (System
.currentTimeMillis() - begin
)) / 1000;
203 if (log
.isTraceEnabled())
205 "Created Jackrabbit repository in " + duration
+ " s, home: " + repositoryConfig
.getHomeDir());
207 return repositoryContext
;
209 Thread
.currentThread().setContextClassLoader(currentContextCl
);