Change RepositoryFactory for Argeo v2
authorMathieu Baudier <mbaudier@argeo.org>
Fri, 20 May 2016 10:34:10 +0000 (10:34 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Fri, 20 May 2016 10:34:10 +0000 (10:34 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@8929 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.cms/src/org/argeo/cms/internal/kernel/Kernel.java
org.argeo.server.jcr/src/org/argeo/jackrabbit/JackrabbitRepositoryFactory.java
org.argeo.server.jcr/src/org/argeo/jackrabbit/OsgiJackrabbitRepositoryFactory.java
org.argeo.server.jcr/src/org/argeo/jcr/DefaultRepositoryFactory.java
org.argeo.server.jcr/src/org/argeo/jcr/DefaultRepositoryRegister.java

index d3787a511db4d04088b8c8d0ed4468d09ab32b2b..c82c6c4516049c7354957999f52a96616bd6c78c 100644 (file)
@@ -214,8 +214,13 @@ final class Kernel implements KernelHeader, KernelConstants, ServiceListener {
                        Configuration nodeConf = conf.getConfiguration(ArgeoJcrConstants.REPO_PID_NODE);
                        if (nodeConf.getProperties() == null) {
                                Dictionary<String, ?> props = getNodeConfigFromFrameworkProperties();
-                               if (props == null)// TODO interactive configuration
+                               if (props == null) {
+                                       // TODO interactive configuration
+                                       if (log.isDebugEnabled())
+                                               log.debug("No argeo.node.repo.type=localfs|h2|postgresql|memory"
+                                                               + " property defined, entering interactive mode...");
                                        return;
+                               }
                                nodeConf.update(props);
                        }
                } catch (IOException e) {
@@ -238,8 +243,10 @@ final class Kernel implements KernelHeader, KernelConstants, ServiceListener {
 
                if (repository == null)
                        repository = new NodeRepository();
-               if (repositoryFactory == null)
+               if (repositoryFactory == null) {
                        repositoryFactory = new OsgiJackrabbitRepositoryFactory();
+                       repositoryFactory.setBundleContext(bc);
+               }
                userAdmin = new NodeUserAdmin(transactionManager, repository);
 
                // ADMIN UIs
@@ -545,13 +552,13 @@ final class Kernel implements KernelHeader, KernelConstants, ServiceListener {
                                        props.put(key, sr.getProperty(key));
                                if (ServiceEvent.REGISTERED == event.getType()) {
                                        try {
-                                               repositoryFactory.register(repository, props);
+                                               // repositoryFactory.register(repository, props);
                                                dataHttp.registerRepositoryServlets(alias, repository);
                                        } catch (Exception e) {
                                                throw new CmsException("Could not publish JCR repository " + alias, e);
                                        }
                                } else if (ServiceEvent.UNREGISTERING == event.getType()) {
-                                       repositoryFactory.unregister(repository, props);
+                                       // repositoryFactory.unregister(repository, props);
                                        dataHttp.unregisterRepositoryServlets(alias);
                                }
                        }
index d64bb5e688d137d66edef8ef509bf3641c4cf037..e5bb3bce0e3d00d2901febc697e4bd127989497e 100644 (file)
@@ -17,6 +17,8 @@ package org.argeo.jackrabbit;
 
 import java.io.File;
 import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
@@ -36,7 +38,7 @@ import org.apache.jackrabbit.core.config.RepositoryConfigurationParser;
 import org.apache.jackrabbit.jcr2dav.Jcr2davRepositoryFactory;
 import org.argeo.ArgeoException;
 import org.argeo.jcr.ArgeoJcrConstants;
-import org.argeo.jcr.DefaultRepositoryFactory;
+import org.argeo.jcr.ArgeoJcrException;
 import org.springframework.core.io.ClassPathResource;
 import org.springframework.core.io.Resource;
 import org.xml.sax.InputSource;
@@ -45,23 +47,21 @@ import org.xml.sax.InputSource;
  * Repository factory which can create new repositories and access remote
  * Jackrabbit repositories
  */
-public class JackrabbitRepositoryFactory extends DefaultRepositoryFactory
-               implements RepositoryFactory, ArgeoJcrConstants {
+public class JackrabbitRepositoryFactory implements RepositoryFactory, ArgeoJcrConstants {
 
-       private final static Log log = LogFactory
-                       .getLog(JackrabbitRepositoryFactory.class);
+       private final static Log log = LogFactory.getLog(JackrabbitRepositoryFactory.class);
 
-       private Resource fileRepositoryConfiguration = new ClassPathResource(
-                       "/org/argeo/jackrabbit/repository-h2.xml");
+       private Resource fileRepositoryConfiguration = new ClassPathResource("/org/argeo/jackrabbit/repository-h2.xml");
 
        @SuppressWarnings({ "rawtypes", "unchecked" })
        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 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(JCR_REPOSITORY_URI))
                        uri = parameters.get(JCR_REPOSITORY_URI).toString();
@@ -73,43 +73,50 @@ public class JackrabbitRepositoryFactory extends DefaultRepositoryFactory
                                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 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);
+
                }
 
-               // publish under alias
-               if (parameters.containsKey(JCR_REPOSITORY_ALIAS)) {
-                       Properties properties = new Properties();
-                       properties.putAll(parameters);
+               else if (parameters.containsKey(JCR_REPOSITORY_ALIAS)) {
+                       // Properties properties = new Properties();
+                       // properties.putAll(parameters);
                        String alias = parameters.get(JCR_REPOSITORY_ALIAS).toString();
-                       publish(alias, repository, properties);
-                       log.info("Registered JCR repository under alias '" + alias
-                                       + "' with properties " + properties);
-               }
+                       // 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 {
+       protected Repository getRepositoryByAlias(String alias) {
+               return null;
+       }
+
+       protected Repository createRemoteRepository(String uri) throws RepositoryException {
                Map<String, String> params = new HashMap<String, String>();
                params.put(JcrUtils.REPOSITORY_URI, uri);
-               Repository repository = new Jcr2davRepositoryFactory()
-                               .getRepository(params);
+               Repository repository = new Jcr2davRepositoryFactory().getRepository(params);
                if (repository == null)
-                       throw new ArgeoException("Remote Davex repository " + uri
-                                       + " not found");
+                       throw new ArgeoException("Remote Davex repository " + uri + " not found");
                log.info("Initialized remote Jackrabbit repository from uri " + uri);
                return repository;
        }
 
        @SuppressWarnings({ "rawtypes", "unchecked" })
-       protected Repository createFileRepository(final String uri, Map parameters)
-                       throws RepositoryException {
+       protected Repository createFileRepository(final String uri, Map parameters) throws RepositoryException {
                InputStream configurationIn = null;
                try {
                        Properties vars = new Properties();
@@ -117,32 +124,26 @@ public class JackrabbitRepositoryFactory extends DefaultRepositoryFactory
                        String dirPath = uri.substring("file:".length());
                        File homeDir = new File(dirPath);
                        if (homeDir.exists() && !homeDir.isDirectory())
-                               throw new ArgeoException("Repository home " + dirPath
-                                               + " is not a directory");
+                               throw new ArgeoException("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);
+                       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);
+                       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.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);
-                                               }
-                                       });
+                       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) {
@@ -152,6 +153,20 @@ public class JackrabbitRepositoryFactory extends DefaultRepositoryFactory
                }
        }
 
+       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 ArgeoException("Cannot interpret URI " + uri, e);
+               }
+       }
+
        /**
         * Called after the repository has been initialised. Does nothing by
         * default.
@@ -161,8 +176,7 @@ public class JackrabbitRepositoryFactory extends DefaultRepositoryFactory
 
        }
 
-       public void setFileRepositoryConfiguration(
-                       Resource fileRepositoryConfiguration) {
+       public void setFileRepositoryConfiguration(Resource fileRepositoryConfiguration) {
                this.fileRepositoryConfiguration = fileRepositoryConfiguration;
        }
 
index b28699e60c029fdf35e2cc81e4d421627789e2dd..a617ec6e338402a91704d843f1c814ddd6914419 100644 (file)
  */
 package org.argeo.jackrabbit;
 
+import java.util.Collection;
 import java.util.Hashtable;
 import java.util.Properties;
 
 import javax.jcr.Repository;
 
+import org.argeo.jcr.ArgeoJcrException;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
 
 /**
  * OSGi-aware Jackrabbit repository factory which can retrieve/publish
  * {@link Repository} as OSGi services.
  */
-public class OsgiJackrabbitRepositoryFactory extends
-               JackrabbitRepositoryFactory {
+public class OsgiJackrabbitRepositoryFactory extends JackrabbitRepositoryFactory {
        private BundleContext bundleContext;
 
-       protected void publish(String alias, Repository repository,
-                       Properties properties) {
+       @Override
+       protected Repository getRepositoryByAlias(String alias) {
+               try {
+                       Collection<ServiceReference<Repository>> srs = bundleContext.getServiceReferences(Repository.class,
+                                       "(" + 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)
+                               throw new ArgeoJcrException(
+                                               srs.size() + " repositories with alias " + alias + " found in OSGi registry");
+                       return bundleContext.getService(srs.iterator().next());
+               } catch (InvalidSyntaxException e) {
+                       throw new ArgeoJcrException("Cannot find repository with alias " + alias, e);
+               }
+       }
+
+       protected 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);
+                       bundleContext.registerService(Repository.class.getName(), repository, props);
                }
        }
 
index a7b30e7be8418f5962a7afec347996d06826dc44..c60f023dde572fe6cc67752fa0006391d2fea2b9 100644 (file)
@@ -29,6 +29,7 @@ import org.argeo.ArgeoException;
 /**
  * Simple implementation of {@link RepositoryFactory}, supporting OSGi aliases.
  */
+@Deprecated
 public class DefaultRepositoryFactory extends DefaultRepositoryRegister
                implements RepositoryFactory, ArgeoJcrConstants {
        @SuppressWarnings("rawtypes")
index 690bc3c9ceded0c9650e28437ca6fab2bff7dc80..e2a5026d83ba899799950e5eb6e139025a55fff6 100644 (file)
@@ -26,6 +26,7 @@ import javax.jcr.RepositoryException;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+@Deprecated
 public class DefaultRepositoryRegister extends Observable implements
                RepositoryRegister, ArgeoJcrConstants {
        private final static Log log = LogFactory