All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.day.jcr.vault.util.RepositoryProvider Maven / Gradle / Ivy

/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2011 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 *
 **************************************************************************/

package com.day.jcr.vault.util;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.imageio.spi.ServiceRegistry;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.day.jcr.vault.fs.api.RepositoryAddress;
import com.day.jcr.vault.fs.api.RepositoryFactory;

/**
 * RepositoryProvider...
 *
 */
public class RepositoryProvider {

    protected static Logger log = LoggerFactory.getLogger(RepositoryProvider.class);

    private Map repos
            = new HashMap();

    public Repository getRepository(RepositoryAddress address)
            throws RepositoryException {
        Repository rep = repos.get(address);
        if (rep == null) {
            rep = createRepository(address);
            repos.put(address, rep);
        }
        return rep;
    }

    private Repository createRepository(RepositoryAddress address)
            throws RepositoryException {
        Iterator iter = ServiceRegistry.lookupProviders(RepositoryFactory.class);
        Set supported = new HashSet();
        while (iter.hasNext()) {
            RepositoryFactory fac = iter.next();
            supported.addAll(fac.getSupportedSchemes());
            Repository rep = fac.createRepository(address);
            if (rep != null) {
                // wrap JCR logger
                if (Boolean.getBoolean("jcrlog.sysout") || System.getProperty("jcrlog.file") != null) {
                    Repository wrapped = wrapLogger(rep, address);
                    if (wrapped != null) {
                        log.info("Enabling JCR Logger.");
                        rep = wrapped;
                    }
                }
                return rep;
            }
        }
        StringBuffer msg = new StringBuffer("URL scheme ");
        msg.append(address.getURI().getScheme());
        msg.append(" not supported. only");
        for (String s: supported) {
            msg.append(" ").append(s);
        }
        throw new RepositoryException(msg.toString());
    }

    private Repository wrapLogger(Repository base,RepositoryAddress address) {
        try {
            Class clazz = getClass().getClassLoader().loadClass("org.apache.jackrabbit.jcrlog.RepositoryLogger");
            // just map all properties
            Properties props = new Properties();
            for (Object o: System.getProperties().keySet()) {
                String name = o.toString();
                if (name.startsWith("jcrlog.")) {
                    props.put(name.substring("jcrlog.".length()), System.getProperty(name));
                }
            }
            Constructor c = clazz.getConstructor(Repository.class, Properties.class, String.class);
            return (Repository) c.newInstance(base, props, address.toString());
        } catch (Exception e) {
            log.error("Unable to initialize JCR logger: {}", e.toString());
            return null;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy