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