Improve CMS
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 29 Sep 2016 11:15:53 +0000 (11:15 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 29 Sep 2016 11:15:53 +0000 (11:15 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@9211 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.cms/src/org/argeo/cms/internal/kernel/KernelConstants.java
org.argeo.cms/src/org/argeo/cms/internal/kernel/NodeRepositoryFactory.java

index 2d9d31d21d8cc4d483dbc4ab3d46d5769889d3fc..b0dd2c6b2300013af2d8be652039288cea5d7bcf 100644 (file)
@@ -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";
 }
index 74a9208c1d5042b30576611ddf9f0f4fdd16c9b3..b59b5e29c29ef23f8307b8b95e2719ff08717ee5 100644 (file)
  */
 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<ServiceReference<Repository>> 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<String, String> props = new Hashtable<String, String>();
-//                     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<String, String> props = new Hashtable<String, String>();
+       // 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<String, String> params = new HashMap<String, String>();
+               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;
+       }
+
 }