001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.platform.relations.jena;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.ecm.core.api.CoreSession;
024import org.nuxeo.ecm.platform.relations.CoreGraphFactory;
025import org.nuxeo.ecm.platform.relations.api.Graph;
026import org.nuxeo.ecm.platform.relations.api.GraphDescription;
027import org.nuxeo.ecm.platform.relations.api.GraphFactory;
028import org.nuxeo.ecm.platform.relations.api.RelationManager;
029import org.nuxeo.ecm.platform.relations.services.RelationService;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * A factory that detects if a Jena graph or a core graph should be used, and remembers it for future invocations.
034 * <p>
035 * A Jena graph is used if it contains at least one relation for the given graph.
036 */
037public class JenaOrCoreGraphFactory implements GraphFactory {
038
039    private static final Log log = LogFactory.getLog(JenaOrCoreGraphFactory.class);
040
041    // used for tests
042    protected static JenaGraph testJenaGraph;
043
044    @Override
045    public Graph createGraph(GraphDescription graphDescription, CoreSession session) {
046        RelationService service = (RelationService) Framework.getService(RelationManager.class);
047
048        String name = graphDescription.getName();
049
050        Graph graph;
051        if (testJenaGraph == null) {
052            graph = new JenaGraph();
053        } else {
054            // test mode, allows reuse of in-memory graph
055            graph = testJenaGraph;
056        }
057        graph.setDescription(graphDescription);
058        if (graph.size().longValue() > 0) {
059            // Jena graph already contains data, use it
060            service.graphFactories.remove(name);
061            service.graphRegistry.put(name, graph);
062            log.info("Graph " + name + " using Jena");
063        } else {
064            // use a core graph and remember this factory
065            GraphFactory factory = new CoreGraphFactory();
066            service.graphFactories.put(name, factory);
067            graph = factory.createGraph(graphDescription, session);
068            log.info("Graph " + name + " using Core");
069        }
070        return graph;
071    }
072
073}