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