1 package org
.argeo
.cms
.internal
.kernel
;
4 import java
.util
.Dictionary
;
5 import java
.util
.HashMap
;
8 import javax
.jcr
.Repository
;
9 import javax
.jcr
.RepositoryFactory
;
11 import org
.apache
.commons
.logging
.Log
;
12 import org
.apache
.commons
.logging
.LogFactory
;
13 import org
.apache
.jackrabbit
.core
.RepositoryContext
;
14 import org
.argeo
.api
.NodeConstants
;
15 import org
.argeo
.cms
.internal
.jcr
.RepoConf
;
16 import org
.argeo
.cms
.internal
.jcr
.RepositoryBuilder
;
17 import org
.argeo
.util
.LangUtils
;
18 import org
.osgi
.framework
.Constants
;
19 import org
.osgi
.service
.cm
.ConfigurationException
;
20 import org
.osgi
.service
.cm
.ManagedServiceFactory
;
22 /** A {@link ManagedServiceFactory} creating or referencing JCR repositories. */
23 class RepositoryServiceFactory
implements ManagedServiceFactory
{
24 private final static Log log
= LogFactory
.getLog(RepositoryServiceFactory
.class);
25 // private final BundleContext bc = FrameworkUtil.getBundle(RepositoryServiceFactory.class).getBundleContext();
27 private Map
<String
, RepositoryContext
> repositories
= new HashMap
<String
, RepositoryContext
>();
28 private Map
<String
, Object
> pidToCn
= new HashMap
<String
, Object
>();
31 public String
getName() {
32 return "Jackrabbit repository service factory";
36 public void updated(String pid
, Dictionary
<String
, ?
> properties
) throws ConfigurationException
{
37 if (repositories
.containsKey(pid
))
38 throw new IllegalArgumentException("Already a repository registered for " + pid
);
40 if (properties
== null)
43 if (repositories
.containsKey(pid
)) {
44 log
.warn("Ignore update of Jackrabbit repository " + pid
);
49 Object labeledUri
= properties
.get(RepoConf
.labeledUri
.name());
50 if (labeledUri
== null) {
51 RepositoryBuilder repositoryBuilder
= new RepositoryBuilder();
52 RepositoryContext repositoryContext
= repositoryBuilder
.createRepositoryContext(properties
);
53 repositories
.put(pid
, repositoryContext
);
54 Dictionary
<String
, Object
> props
= LangUtils
.dict(Constants
.SERVICE_PID
, pid
);
55 // props.put(ArgeoJcrConstants.JCR_REPOSITORY_URI,
56 // properties.get(RepoConf.labeledUri.name()));
57 Object cn
= properties
.get(NodeConstants
.CN
);
59 props
.put(NodeConstants
.CN
, cn
);
60 // props.put(NodeConstants.JCR_REPOSITORY_ALIAS, cn);
63 Activator
.registerService(RepositoryContext
.class, repositoryContext
, props
);
66 Object cn
= properties
.get(NodeConstants
.CN
);
67 Object defaultWorkspace
= properties
.get(RepoConf
.defaultWorkspace
.name());
68 if (defaultWorkspace
== null)
69 defaultWorkspace
= RepoConf
.defaultWorkspace
.getDefault();
70 URI uri
= new URI(labeledUri
.toString());
71 // RepositoryFactory repositoryFactory = bc
72 // .getService(bc.getServiceReference(RepositoryFactory.class));
73 RepositoryFactory repositoryFactory
= Activator
.getService(RepositoryFactory
.class);
74 Map
<String
, String
> parameters
= new HashMap
<String
, String
>();
75 parameters
.put(RepoConf
.labeledUri
.name(), uri
.toString());
76 parameters
.put(RepoConf
.defaultWorkspace
.name(), defaultWorkspace
.toString());
77 Repository repository
= repositoryFactory
.getRepository(parameters
);
78 // Repository repository = NodeUtils.getRepositoryByUri(repositoryFactory,
80 Dictionary
<String
, Object
> props
= LangUtils
.dict(Constants
.SERVICE_PID
, pid
);
81 props
.put(RepoConf
.labeledUri
.name(),
82 new URI(uri
.getScheme(), null, uri
.getHost(), uri
.getPort(), uri
.getPath(), null, null)
85 props
.put(NodeConstants
.CN
, cn
);
86 // props.put(NodeConstants.JCR_REPOSITORY_ALIAS, cn);
89 Activator
.registerService(Repository
.class, repository
, props
);
92 if (cn
.equals(NodeConstants
.NODE_REPOSITORY
)) {
93 Dictionary
<String
, Object
> homeProps
= LangUtils
.dict(NodeConstants
.CN
,
94 NodeConstants
.EGO_REPOSITORY
);
95 EgoRepository homeRepository
= new EgoRepository(repository
, true);
96 Activator
.registerService(Repository
.class, homeRepository
, homeProps
);
98 } catch (Exception e
) {
99 // TODO Auto-generated catch block
103 } catch (Exception e
) {
104 throw new IllegalStateException("Cannot create Jackrabbit repository " + pid
, e
);
110 public void deleted(String pid
) {
111 RepositoryContext repositoryContext
= repositories
.remove(pid
);
112 repositoryContext
.getRepository().shutdown();
113 if (log
.isDebugEnabled())
114 log
.debug("Deleted repository " + pid
);
117 public void shutdown() {
118 for (String pid
: repositories
.keySet()) {
120 repositories
.get(pid
).getRepository().shutdown();
121 if (log
.isDebugEnabled())
122 log
.debug("Shut down repository " + pid
123 + (pidToCn
.containsKey(pid
) ?
" (" + pidToCn
.get(pid
) + ")" : ""));
124 } catch (Exception e
) {
125 log
.error("Error when shutting down Jackrabbit repository " + pid
, e
);