1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.cms.internal.kernel;
17
18 import java.net.URI;
19 import java.net.URISyntaxException;
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import javax.jcr.Repository;
25 import javax.jcr.RepositoryException;
26 import javax.jcr.RepositoryFactory;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.jackrabbit.jcr2dav.Jcr2davRepositoryFactory;
31 import org.argeo.api.NodeConstants;
32 import org.argeo.cms.internal.jcr.RepoConf;
33 import org.argeo.jcr.ArgeoJcrException;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.FrameworkUtil;
36 import org.osgi.framework.InvalidSyntaxException;
37 import org.osgi.framework.ServiceReference;
38
39
40
41
42
43 class NodeRepositoryFactory implements RepositoryFactory {
44 private final Log log = LogFactory.getLog(getClass());
45 private final BundleContext bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext();
46
47
48
49
50 protected Repository getRepositoryByAlias(String alias) {
51 try {
52 Collection<ServiceReference<Repository>> srs = bundleContext.getServiceReferences(Repository.class,
53 "(" + NodeConstants.CN + "=" + alias + ")");
54 if (srs.size() == 0)
55 throw new ArgeoJcrException("No repository with alias " + alias + " found in OSGi registry");
56 else if (srs.size() > 1)
57 throw new ArgeoJcrException(
58 srs.size() + " repositories with alias " + alias + " found in OSGi registry");
59 return bundleContext.getService(srs.iterator().next());
60 } catch (InvalidSyntaxException e) {
61 throw new ArgeoJcrException("Cannot find repository with alias " + alias, e);
62 }
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76
77 @SuppressWarnings({ "rawtypes" })
78 public Repository getRepository(Map parameters) throws RepositoryException {
79
80
81
82
83
84
85 Repository repository;
86 String uri = null;
87 if (parameters.containsKey(RepoConf.labeledUri.name()))
88 uri = parameters.get(NodeConstants.LABELED_URI).toString();
89 else if (parameters.containsKey(KernelConstants.JACKRABBIT_REPOSITORY_URI))
90 uri = parameters.get(KernelConstants.JACKRABBIT_REPOSITORY_URI).toString();
91
92 if (uri != null) {
93 if (uri.startsWith("http")) {
94 Object defaultWorkspace = parameters.get(RepoConf.defaultWorkspace.name());
95 repository = createRemoteRepository(uri, defaultWorkspace != null ? defaultWorkspace.toString() : null);
96 } else if (uri.startsWith("file"))
97 repository = createFileRepository(uri, parameters);
98 else if (uri.startsWith("vm")) {
99
100
101 repository = getRepositoryByAlias(getAliasFromURI(uri));
102 } else
103 throw new ArgeoJcrException("Unrecognized URI format " + uri);
104
105 }
106
107 else if (parameters.containsKey(NodeConstants.CN)) {
108
109
110 String alias = parameters.get(NodeConstants.CN).toString();
111
112
113
114 repository = getRepositoryByAlias(alias);
115 } else
116 throw new ArgeoJcrException("Not enough information in " + parameters);
117
118 if (repository == null)
119 throw new ArgeoJcrException("Repository not found " + parameters);
120
121 return repository;
122 }
123
124 protected Repository createRemoteRepository(String uri, String defaultWorkspace) throws RepositoryException {
125 Map<String, String> params = new HashMap<String, String>();
126 params.put(KernelConstants.JACKRABBIT_REPOSITORY_URI, uri);
127 if (defaultWorkspace != null)
128 params.put(KernelConstants.JACKRABBIT_REMOTE_DEFAULT_WORKSPACE, defaultWorkspace);
129 Repository repository = new Jcr2davRepositoryFactory().getRepository(params);
130 if (repository == null)
131 throw new ArgeoJcrException("Remote Davex repository " + uri + " not found");
132 log.info("Initialized remote Jackrabbit repository from uri " + uri);
133 return repository;
134 }
135
136 @SuppressWarnings({ "rawtypes" })
137 protected Repository createFileRepository(final String uri, Map parameters) throws RepositoryException {
138 throw new UnsupportedOperationException();
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179 }
180
181 protected String getAliasFromURI(String uri) {
182 try {
183 URI uriObj = new URI(uri);
184 String alias = uriObj.getPath();
185 if (alias.charAt(0) == '/')
186 alias = alias.substring(1);
187 if (alias.charAt(alias.length() - 1) == '/')
188 alias = alias.substring(0, alias.length() - 1);
189 return alias;
190 } catch (URISyntaxException e) {
191 throw new ArgeoJcrException("Cannot interpret URI " + uri, e);
192 }
193 }
194
195
196
197
198 @SuppressWarnings("rawtypes")
199 protected void postInitialization(Repository repository, Map parameters) {
200
201 }
202 }