001/* 002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Florent Guillaume 016 */ 017package org.nuxeo.ecm.platform.relations.jena; 018 019import org.apache.commons.logging.Log; 020import org.apache.commons.logging.LogFactory; 021import org.nuxeo.ecm.core.api.CoreSession; 022import org.nuxeo.ecm.platform.relations.CoreGraphFactory; 023import org.nuxeo.ecm.platform.relations.api.Graph; 024import org.nuxeo.ecm.platform.relations.api.GraphDescription; 025import org.nuxeo.ecm.platform.relations.api.GraphFactory; 026import org.nuxeo.ecm.platform.relations.services.RelationService; 027import org.nuxeo.runtime.api.Framework; 028 029/** 030 * A factory that detects if a Jena graph or a core graph should be used, and remembers it for future invocations. 031 * <p> 032 * A Jena graph is used if it contains at least one relation for the given graph. 033 */ 034public class JenaOrCoreGraphFactory implements GraphFactory { 035 036 private static final Log log = LogFactory.getLog(JenaOrCoreGraphFactory.class); 037 038 // used for tests 039 protected static JenaGraph testJenaGraph; 040 041 @Override 042 public Graph createGraph(GraphDescription graphDescription, CoreSession session) { 043 RelationService service = (RelationService) Framework.getRuntime().getComponent(RelationService.NAME); 044 String name = graphDescription.getName(); 045 046 Graph graph; 047 if (testJenaGraph == null) { 048 graph = new JenaGraph(); 049 } else { 050 // test mode, allows reuse of in-memory graph 051 graph = testJenaGraph; 052 } 053 graph.setDescription(graphDescription); 054 if (graph.size().longValue() > 0) { 055 // Jena graph already contains data, use it 056 service.graphFactories.remove(name); 057 service.graphRegistry.put(name, graph); 058 log.info("Graph " + name + " using Jena"); 059 } else { 060 // use a core graph and remember this factory 061 GraphFactory factory = new CoreGraphFactory(); 062 service.graphFactories.put(name, factory); 063 graph = factory.createGraph(graphDescription, session); 064 log.info("Graph " + name + " using Core"); 065 } 066 return graph; 067 } 068 069}