]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/kernel/NodeRepositoryFactory.java
Make remote default workspace configurable via System properties.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / kernel / NodeRepositoryFactory.java
1 package org.argeo.cms.internal.kernel;
2
3 import java.net.URI;
4 import java.net.URISyntaxException;
5 import java.util.Collection;
6 import java.util.HashMap;
7 import java.util.Map;
8
9 import javax.jcr.Repository;
10 import javax.jcr.RepositoryException;
11 import javax.jcr.RepositoryFactory;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.apache.jackrabbit.jcr2dav.Jcr2davRepositoryFactory;
16 import org.argeo.api.NodeConstants;
17 import org.argeo.cms.internal.jcr.RepoConf;
18 import org.osgi.framework.BundleContext;
19 import org.osgi.framework.FrameworkUtil;
20 import org.osgi.framework.InvalidSyntaxException;
21 import org.osgi.framework.ServiceReference;
22
23 /**
24 * OSGi-aware Jackrabbit repository factory which can retrieve/publish
25 * {@link Repository} as OSGi services.
26 */
27 class NodeRepositoryFactory implements RepositoryFactory {
28 private final Log log = LogFactory.getLog(getClass());
29 // private final BundleContext bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext();
30
31 // private Resource fileRepositoryConfiguration = new ClassPathResource(
32 // "/org/argeo/cms/internal/kernel/repository-localfs.xml");
33
34 protected Repository getRepositoryByAlias(String alias) {
35 BundleContext bundleContext = Activator.getBundleContext();
36 if (bundleContext != null) {
37 try {
38 Collection<ServiceReference<Repository>> srs = bundleContext.getServiceReferences(Repository.class,
39 "(" + NodeConstants.CN + "=" + alias + ")");
40 if (srs.size() == 0)
41 throw new IllegalArgumentException("No repository with alias " + alias + " found in OSGi registry");
42 else if (srs.size() > 1)
43 throw new IllegalArgumentException(
44 srs.size() + " repositories with alias " + alias + " found in OSGi registry");
45 return bundleContext.getService(srs.iterator().next());
46 } catch (InvalidSyntaxException e) {
47 throw new IllegalArgumentException("Cannot find repository with alias " + alias, e);
48 }
49 } else {
50 // TODO ability to filter static services
51 return null;
52 }
53 }
54
55 // private void publish(String alias, Repository repository, Properties
56 // properties) {
57 // if (bundleContext != null) {
58 // // do not modify reference
59 // Hashtable<String, String> props = new Hashtable<String, String>();
60 // props.putAll(props);
61 // props.put(JCR_REPOSITORY_ALIAS, alias);
62 // bundleContext.registerService(Repository.class.getName(), repository,
63 // props);
64 // }
65 // }
66
67 @SuppressWarnings({ "rawtypes" })
68 public Repository getRepository(Map parameters) throws RepositoryException {
69 // // check if can be found by alias
70 // Repository repository = super.getRepository(parameters);
71 // if (repository != null)
72 // return repository;
73
74 // check if remote
75 Repository repository;
76 String uri = null;
77 if (parameters.containsKey(RepoConf.labeledUri.name()))
78 uri = parameters.get(NodeConstants.LABELED_URI).toString();
79 else if (parameters.containsKey(KernelConstants.JACKRABBIT_REPOSITORY_URI))
80 uri = parameters.get(KernelConstants.JACKRABBIT_REPOSITORY_URI).toString();
81
82 if (uri != null) {
83 if (uri.startsWith("http")) {// http, https
84 Object defaultWorkspace = parameters.get(RepoConf.defaultWorkspace.name());
85 repository = createRemoteRepository(uri, defaultWorkspace != null ? defaultWorkspace.toString() : null);
86 } else if (uri.startsWith("file"))// http, https
87 repository = createFileRepository(uri, parameters);
88 else if (uri.startsWith("vm")) {
89 // log.warn("URI " + uri + " should have been managed by generic
90 // JCR repository factory");
91 repository = getRepositoryByAlias(getAliasFromURI(uri));
92 } else
93 throw new IllegalArgumentException("Unrecognized URI format " + uri);
94
95 }
96
97 else if (parameters.containsKey(NodeConstants.CN)) {
98 // Properties properties = new Properties();
99 // properties.putAll(parameters);
100 String alias = parameters.get(NodeConstants.CN).toString();
101 // publish(alias, repository, properties);
102 // log.info("Registered JCR repository under alias '" + alias + "'
103 // with properties " + properties);
104 repository = getRepositoryByAlias(alias);
105 } else
106 throw new IllegalArgumentException("Not enough information in " + parameters);
107
108 if (repository == null)
109 throw new IllegalArgumentException("Repository not found " + parameters);
110
111 return repository;
112 }
113
114 protected Repository createRemoteRepository(String uri, String defaultWorkspace) throws RepositoryException {
115 Map<String, String> params = new HashMap<String, String>();
116 params.put(KernelConstants.JACKRABBIT_REPOSITORY_URI, uri);
117 if (defaultWorkspace != null)
118 params.put(KernelConstants.JACKRABBIT_REMOTE_DEFAULT_WORKSPACE, defaultWorkspace);
119 Repository repository = new Jcr2davRepositoryFactory().getRepository(params);
120 if (repository == null)
121 throw new IllegalArgumentException("Remote Davex repository " + uri + " not found");
122 log.info("Initialized remote Jackrabbit repository from uri " + uri);
123 return repository;
124 }
125
126 @SuppressWarnings({ "rawtypes" })
127 protected Repository createFileRepository(final String uri, Map parameters) throws RepositoryException {
128 throw new UnsupportedOperationException();
129 // InputStream configurationIn = null;
130 // try {
131 // Properties vars = new Properties();
132 // vars.putAll(parameters);
133 // String dirPath = uri.substring("file:".length());
134 // File homeDir = new File(dirPath);
135 // if (homeDir.exists() && !homeDir.isDirectory())
136 // throw new ArgeoJcrException("Repository home " + dirPath + " is not a
137 // directory");
138 // if (!homeDir.exists())
139 // homeDir.mkdirs();
140 // configurationIn = fileRepositoryConfiguration.getInputStream();
141 // vars.put(RepositoryConfigurationParser.REPOSITORY_HOME_VARIABLE,
142 // homeDir.getCanonicalPath());
143 // RepositoryConfig repositoryConfig = RepositoryConfig.create(new
144 // InputSource(configurationIn), vars);
145 //
146 // // TransientRepository repository = new
147 // // TransientRepository(repositoryConfig);
148 // final RepositoryImpl repository =
149 // RepositoryImpl.create(repositoryConfig);
150 // Session session = repository.login();
151 // // FIXME make it generic
152 // org.argeo.jcr.JcrUtils.addPrivilege(session, "/", "ROLE_ADMIN",
153 // "jcr:all");
154 // org.argeo.jcr.JcrUtils.logoutQuietly(session);
155 // Runtime.getRuntime().addShutdownHook(new Thread("Clean JCR repository
156 // " + uri) {
157 // public void run() {
158 // repository.shutdown();
159 // log.info("Destroyed repository " + uri);
160 // }
161 // });
162 // log.info("Initialized file Jackrabbit repository from uri " + uri);
163 // return repository;
164 // } catch (Exception e) {
165 // throw new ArgeoJcrException("Cannot create repository " + uri, e);
166 // } finally {
167 // IOUtils.closeQuietly(configurationIn);
168 // }
169 }
170
171 protected String getAliasFromURI(String uri) {
172 try {
173 URI uriObj = new URI(uri);
174 String alias = uriObj.getPath();
175 if (alias.charAt(0) == '/')
176 alias = alias.substring(1);
177 if (alias.charAt(alias.length() - 1) == '/')
178 alias = alias.substring(0, alias.length() - 1);
179 return alias;
180 } catch (URISyntaxException e) {
181 throw new IllegalArgumentException("Cannot interpret URI " + uri, e);
182 }
183 }
184
185 /**
186 * Called after the repository has been initialised. Does nothing by default.
187 */
188 @SuppressWarnings("rawtypes")
189 protected void postInitialization(Repository repository, Map parameters) {
190
191 }
192 }