From 8500cae34c6596f6f3532db4431170d4a636f78c Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Thu, 29 Sep 2016 11:15:53 +0000 Subject: [PATCH] Improve CMS git-svn-id: https://svn.argeo.org/commons/trunk@9211 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../cms/internal/kernel/KernelConstants.java | 3 +- .../kernel/NodeRepositoryFactory.java | 170 ++++++++++++++++-- 2 files changed, 159 insertions(+), 14 deletions(-) diff --git a/org.argeo.cms/src/org/argeo/cms/internal/kernel/KernelConstants.java b/org.argeo.cms/src/org/argeo/cms/internal/kernel/KernelConstants.java index 2d9d31d21..b0dd2c6b2 100644 --- a/org.argeo.cms/src/org/argeo/cms/internal/kernel/KernelConstants.java +++ b/org.argeo.cms/src/org/argeo/cms/internal/kernel/KernelConstants.java @@ -36,6 +36,7 @@ public interface KernelConstants { String JETTY_FACTORY_PID = "org.eclipse.equinox.http.jetty.config"; String WHITEBOARD_PATTERN_PROP = "osgi.http.whiteboard.servlet.pattern"; - // avoid dependency to RWT OSGi + // avoid dependencies String CONTEXT_NAME_PROP = "contextName"; + String JACKRABBIT_REPOSITORY_URI = "org.apache.jackrabbit.repository.uri"; } diff --git a/org.argeo.cms/src/org/argeo/cms/internal/kernel/NodeRepositoryFactory.java b/org.argeo.cms/src/org/argeo/cms/internal/kernel/NodeRepositoryFactory.java index 74a9208c1..b59b5e29c 100644 --- a/org.argeo.cms/src/org/argeo/cms/internal/kernel/NodeRepositoryFactory.java +++ b/org.argeo.cms/src/org/argeo/cms/internal/kernel/NodeRepositoryFactory.java @@ -15,29 +15,42 @@ */ package org.argeo.cms.internal.kernel; +import java.net.URI; +import java.net.URISyntaxException; import java.util.Collection; +import java.util.HashMap; +import java.util.Map; import javax.jcr.Repository; +import javax.jcr.RepositoryException; +import javax.jcr.RepositoryFactory; -import org.argeo.jackrabbit.JackrabbitRepositoryFactory; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.jackrabbit.jcr2dav.Jcr2davRepositoryFactory; import org.argeo.jcr.ArgeoJcrException; +import org.argeo.node.NodeConstants; import org.osgi.framework.BundleContext; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceReference; +import org.springframework.core.io.Resource; /** * OSGi-aware Jackrabbit repository factory which can retrieve/publish * {@link Repository} as OSGi services. */ -class NodeRepositoryFactory extends JackrabbitRepositoryFactory { +class NodeRepositoryFactory implements RepositoryFactory { + private final Log log = LogFactory.getLog(getClass()); private final BundleContext bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext(); - @Override + // private Resource fileRepositoryConfiguration = new ClassPathResource( + // "/org/argeo/cms/internal/kernel/repository-localfs.xml"); + protected Repository getRepositoryByAlias(String alias) { try { Collection> srs = bundleContext.getServiceReferences(Repository.class, - "(" + JCR_REPOSITORY_ALIAS + "=" + alias + ")"); + "(" + NodeConstants.JCR_REPOSITORY_ALIAS + "=" + alias + ")"); if (srs.size() == 0) throw new ArgeoJcrException("No repository with alias " + alias + " found in OSGi registry"); else if (srs.size() > 1) @@ -49,13 +62,144 @@ class NodeRepositoryFactory extends JackrabbitRepositoryFactory { } } -// private void publish(String alias, Repository repository, Properties properties) { -// if (bundleContext != null) { -// // do not modify reference -// Hashtable props = new Hashtable(); -// props.putAll(props); -// props.put(JCR_REPOSITORY_ALIAS, alias); -// bundleContext.registerService(Repository.class.getName(), repository, props); -// } -// } + // private void publish(String alias, Repository repository, Properties + // properties) { + // if (bundleContext != null) { + // // do not modify reference + // Hashtable props = new Hashtable(); + // props.putAll(props); + // props.put(JCR_REPOSITORY_ALIAS, alias); + // bundleContext.registerService(Repository.class.getName(), repository, + // props); + // } + // } + + @SuppressWarnings({ "rawtypes" }) + public Repository getRepository(Map parameters) throws RepositoryException { + // // check if can be found by alias + // Repository repository = super.getRepository(parameters); + // if (repository != null) + // return repository; + + // check if remote + Repository repository; + String uri = null; + if (parameters.containsKey(NodeConstants.JCR_REPOSITORY_URI)) + uri = parameters.get(NodeConstants.JCR_REPOSITORY_URI).toString(); + else if (parameters.containsKey(KernelConstants.JACKRABBIT_REPOSITORY_URI)) + uri = parameters.get(KernelConstants.JACKRABBIT_REPOSITORY_URI).toString(); + + if (uri != null) { + if (uri.startsWith("http"))// http, https + repository = createRemoteRepository(uri); + else if (uri.startsWith("file"))// http, https + repository = createFileRepository(uri, parameters); + else if (uri.startsWith("vm")) { + // log.warn("URI " + uri + " should have been managed by generic + // JCR repository factory"); + repository = getRepositoryByAlias(getAliasFromURI(uri)); + } else + throw new ArgeoJcrException("Unrecognized URI format " + uri); + + } + + else if (parameters.containsKey(NodeConstants.JCR_REPOSITORY_ALIAS)) { + // Properties properties = new Properties(); + // properties.putAll(parameters); + String alias = parameters.get(NodeConstants.JCR_REPOSITORY_ALIAS).toString(); + // publish(alias, repository, properties); + // log.info("Registered JCR repository under alias '" + alias + "' + // with properties " + properties); + repository = getRepositoryByAlias(alias); + } else + throw new ArgeoJcrException("Not enough information in " + parameters); + + if (repository == null) + throw new ArgeoJcrException("Repository not found " + parameters); + + return repository; + } + + protected Repository createRemoteRepository(String uri) throws RepositoryException { + Map params = new HashMap(); + params.put(KernelConstants.JACKRABBIT_REPOSITORY_URI, uri); + Repository repository = new Jcr2davRepositoryFactory().getRepository(params); + if (repository == null) + throw new ArgeoJcrException("Remote Davex repository " + uri + " not found"); + log.info("Initialized remote Jackrabbit repository from uri " + uri); + return repository; + } + + @SuppressWarnings({ "rawtypes" }) + protected Repository createFileRepository(final String uri, Map parameters) throws RepositoryException { + throw new UnsupportedOperationException(); + // InputStream configurationIn = null; + // try { + // Properties vars = new Properties(); + // vars.putAll(parameters); + // String dirPath = uri.substring("file:".length()); + // File homeDir = new File(dirPath); + // if (homeDir.exists() && !homeDir.isDirectory()) + // throw new ArgeoJcrException("Repository home " + dirPath + " is not a + // directory"); + // if (!homeDir.exists()) + // homeDir.mkdirs(); + // configurationIn = fileRepositoryConfiguration.getInputStream(); + // vars.put(RepositoryConfigurationParser.REPOSITORY_HOME_VARIABLE, + // homeDir.getCanonicalPath()); + // RepositoryConfig repositoryConfig = RepositoryConfig.create(new + // InputSource(configurationIn), vars); + // + // // TransientRepository repository = new + // // TransientRepository(repositoryConfig); + // final RepositoryImpl repository = + // RepositoryImpl.create(repositoryConfig); + // Session session = repository.login(); + // // FIXME make it generic + // org.argeo.jcr.JcrUtils.addPrivilege(session, "/", "ROLE_ADMIN", + // "jcr:all"); + // org.argeo.jcr.JcrUtils.logoutQuietly(session); + // Runtime.getRuntime().addShutdownHook(new Thread("Clean JCR repository + // " + uri) { + // public void run() { + // repository.shutdown(); + // log.info("Destroyed repository " + uri); + // } + // }); + // log.info("Initialized file Jackrabbit repository from uri " + uri); + // return repository; + // } catch (Exception e) { + // throw new ArgeoJcrException("Cannot create repository " + uri, e); + // } finally { + // IOUtils.closeQuietly(configurationIn); + // } + } + + protected String getAliasFromURI(String uri) { + try { + URI uriObj = new URI(uri); + String alias = uriObj.getPath(); + if (alias.charAt(0) == '/') + alias = alias.substring(1); + if (alias.charAt(alias.length() - 1) == '/') + alias = alias.substring(0, alias.length() - 1); + return alias; + } catch (URISyntaxException e) { + throw new ArgeoJcrException("Cannot interpret URI " + uri, e); + } + } + + /** + * Called after the repository has been initialised. Does nothing by + * default. + */ + @SuppressWarnings("rawtypes") + protected void postInitialization(Repository repository, Map parameters) { + + } + + public void setFileRepositoryConfiguration(Resource fileRepositoryConfiguration) { +// this.fileRepositoryConfiguration = fileRepositoryConfiguration; + } + } -- 2.30.2