X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.jcr%2Fsrc%2Forg%2Fargeo%2Fjackrabbit%2FJackrabbitRepositoryFactory.java;fp=org.argeo.jcr%2Fsrc%2Forg%2Fargeo%2Fjackrabbit%2FJackrabbitRepositoryFactory.java;h=e4134883c1ad6b9bb970919d41e9d0d1b75c0977;hb=e66b9893b0e511f8ab295e3cee42b7dc966f1597;hp=0000000000000000000000000000000000000000;hpb=8260f4470f514ea347ca53f5b4dfc632c4a4de66;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.jcr/src/org/argeo/jackrabbit/JackrabbitRepositoryFactory.java b/org.argeo.jcr/src/org/argeo/jackrabbit/JackrabbitRepositoryFactory.java new file mode 100644 index 000000000..e4134883c --- /dev/null +++ b/org.argeo.jcr/src/org/argeo/jackrabbit/JackrabbitRepositoryFactory.java @@ -0,0 +1,178 @@ +/* + * Copyright (C) 2007-2012 Argeo GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +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; + +import javax.jcr.Repository; +import javax.jcr.RepositoryException; +import javax.jcr.RepositoryFactory; +import javax.jcr.Session; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.jackrabbit.commons.JcrUtils; +import org.apache.jackrabbit.core.RepositoryImpl; +import org.apache.jackrabbit.core.config.RepositoryConfig; +import org.apache.jackrabbit.core.config.RepositoryConfigurationParser; +import org.apache.jackrabbit.jcr2dav.Jcr2davRepositoryFactory; +import org.argeo.jcr.ArgeoJcrConstants; +import org.argeo.jcr.ArgeoJcrException; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.xml.sax.InputSource; + +/** + * Repository factory which can create new repositories and access remote + * Jackrabbit repositories + */ +public class JackrabbitRepositoryFactory implements RepositoryFactory, ArgeoJcrConstants { + private final static Log log = LogFactory.getLog(JackrabbitRepositoryFactory.class); + + private Resource fileRepositoryConfiguration = new ClassPathResource("/org/argeo/jackrabbit/repository-h2.xml"); + + @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(JCR_REPOSITORY_URI)) + uri = parameters.get(JCR_REPOSITORY_URI).toString(); + else if (parameters.containsKey(JcrUtils.REPOSITORY_URI)) + uri = parameters.get(JcrUtils.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(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); + 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 getRepositoryByAlias(String alias) { + return null; + } + + protected Repository createRemoteRepository(String uri) throws RepositoryException { + Map params = new HashMap(); + params.put(JcrUtils.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", "unchecked" }) + protected Repository createFileRepository(final String uri, Map parameters) throws RepositoryException { + 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; + } + +}