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

com.bigdata.rdf.sail.remote.BigdataSailFactory Maven / Gradle / Ivy

There is a newer version: 2.1.4
Show newest version
/**
Copyright (C) SYSTAP, LLC DBA Blazegraph 2006-2016.  All rights reserved.

Contact:
     SYSTAP, LLC DBA Blazegraph
     2501 Calvert ST NW #106
     Washington, DC 20008
     [email protected]

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
package com.bigdata.rdf.sail.remote;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import org.openrdf.repository.sail.SailRepository;
import org.openrdf.sail.Sail;

import com.bigdata.rdf.sail.webapp.client.RemoteRepositoryManager;
import com.bigdata.util.httpd.Config;

/**
 * Helper class to create a bigdata instance.
 * 
 * @author mikepersonick
 * 
 * @see  BigdataSailFactory must be
 *      moved to the client package 
 */
public class BigdataSailFactory {
	
	/**
	 * The default bigdata SAIL_PROVIDER.
	 */
	public static final String BIGDATA_SAIL_INSTANCE = "com.bigdata.rdf.sail.BigdataSail";
	
	/**
	 * The name of the property to set with the class that will provide the Sail.
	 * 
	 * It must have a constructor that takes a single properties file as the parameter.
	 */
	public static final String SAIL_PROVIDER = "com.bigdata.rdf.sail.remote.Provider";

    /**
     * A handy list of common Options you might want to specify when creating
     * your bigdata instance.
     * 
     * @author mikepersonick
     *
     */
    public static enum Option {
        
        /**
         * Inference on or off.  Off by default.
         */
        Inference,
        
        /**
         * Quads on or off.  Off by default.
         */
        Quads,
        
        /**
         * RDR (statement identifiers) on or off.  Off by default.
         */
        RDR,
        
        /**
         * Text index on or off.  Off by default.
         */
        TextIndex,
        
//        /**
//         * Create an in-memory instance.
//         */
//        InMemory,
//        
//        /**
//         * Create a persistent instance backed by a file.  You must specify
//         * the file.
//         */
//        Persistent
        
    }
    
    /**
    * Connect to a remote bigdata instance.
    * 
    * FIXME This does not parameterize the value of the ContextPath. See
    * {@link com.bigdata.BigdataStatics#getContextPath()}.
    */
   public static BigdataSailRemoteRepository connect(final String host,
         final int port) {
       return connect("http://" + host + ":" + port + "/" + Config.BLAZEGRAPH_PATH); 
        
    }
    
    /**
	 * Connect to a remote bigdata instance.
	 * 
     * @param sparqlEndpointURL
     *            The URL of the SPARQL end point.
	 * 
	 * FIXME This does not support the HA load balancer pattern. See #1148.
	 * 
	 * FIXME This does not parameterize the value of the ContextPath. See
	 * {@link com.bigdata.BigdataStatics#getContextPath()}.
	 * 
	 * FIXME This MIGHT leak HttpClient or Executor resources.
	 */
   public static BigdataSailRemoteRepository connect(
			final String sparqlEndpointURL) {

      return new RemoteRepositoryManager().getRepositoryForURL(
            sparqlEndpointURL).getBigdataSailRemoteRepository();

	}
	
   /**
    * Convenience method to allow the testing of the URL normalization
    * functionality.
    * 
    * @see 
    *      BigdataSailFactory.connect() 
    */
   @Deprecated // We are getting rid of this, right?
	public static String testServiceEndpointUrl(final String serviceEndpoint)
	{
		return normalizeEndpoint(serviceEndpoint);
	}

	/**
	 * Massage the service endpoint to ensure that it ends with
	 * /blazegraph
	 */
   @Deprecated // We are getting rid of this, right?
    static private String normalizeEndpoint(final String serviceEndpoint) {

        if (serviceEndpoint.endsWith("/sparql")) {
            
        	return serviceEndpoint.substring(0,
        				serviceEndpoint.length()-"/sparql".length());
        	
        } if (serviceEndpoint.endsWith("/sparql/")) {
            
        	return serviceEndpoint.substring(0,
        				serviceEndpoint.length()-"/sparql/".length());
        	
        } else if (serviceEndpoint.endsWith("/" + Config.BLAZEGRAPH_PATH + "/")) {
            
        	return serviceEndpoint.substring(0, 
        				serviceEndpoint.length()-1) ;
       
        } else if (serviceEndpoint.endsWith("/" + Config.BLAZEGRAPH_PATH)) {
            
        	return serviceEndpoint;
        	
		} else if (serviceEndpoint.contains("/" + Config.BLAZEGRAPH_PATH)
				&& serviceEndpoint.endsWith("/")) {
			// This is the case of /blazegraph/namespace/NAMESPACE/

			return serviceEndpoint.substring(0, serviceEndpoint.length() - 1);

		} else if (serviceEndpoint.contains("/" + Config.BLAZEGRAPH_PATH)) {
			// This is the case of /blazegraph/namespace/NAMESPACE

			return serviceEndpoint;

		} else if (serviceEndpoint.endsWith("/")) {

			return serviceEndpoint + Config.BLAZEGRAPH_PATH;

		} else {
            
        	return serviceEndpoint + "/" + Config.BLAZEGRAPH_PATH;
        	
        }
        
    }

    /**
     * Open an existing persistent bigdata instance. If a journal does
     * not exist at the specified location then an exception will be thrown.
     */
    public static SailRepository openRepository(final String file) {
        
        return new SailRepository(openSail(file, false));
        
    }
        
    /**
     * Open an existing persistent bigdata instance. If a journal does
     * not exist at the specified location and the boolean create flag is true
     * a journal will be created at that location with the default set of
     * options.
     */
    public static SailRepository openRepository(final String file, 
            final boolean create) {
        
        return new SailRepository(openSail(file, create));
        
    }
        
    /**
     * Open an existing persistent bigdata instance. If a journal does
     * not exist at the specified location then an exception will be thrown.
     */
    public static Sail openSail(final String file) {
        
        return openSail(file, false);
        
    }
        
    /**
     * Open an existing persistent bigdata instance. If a journal does
     * not exist at the specified location and the boolean create flag is true
     * a journal will be created at that location with the default set of
     * options.
     */
    public static Sail openSail(final String file, final boolean create) {
        
        if (!new File(file).exists()) {
            
            if (!create) {
                throw new IllegalArgumentException("journal does not exist at specified location");
            } else {
                return createSail(file);
            }
            
        } else {
        
            final Properties props = new Properties();
            props.setProperty("com.bigdata.journal.AbstractJournal.file", file);
            
            final Sail sail = getSailProviderInstance(props);
            
            return sail;
            
        }
        
    }
    
    /**
     * Create a new bigdata instance using the specified options.  Since no
     * journal file is specified this must be an in-memory instance.
     */
    public static SailRepository createRepository(final Option... args) {
        
        return createRepository(new Properties(), null, args);
        
    }
    
    /**
     * Create a new bigdata instance using the specified options.  Since no
     * journal file is specified this must be an in-memory instance.
     */
    public static SailRepository createRepository(final Properties props,
            final Option... args) {
        
        return createRepository(props, null, args);
        
    }
    
    /**
     * Create a new bigdata instance using the specified options.
     */
    public static SailRepository createRepository(final String file,
            final Option... args) {
        
        return createRepository(new Properties(), file, args);
        
    }
    
    /**
     * Create a new bigdata instance using the specified options.  Since no
     * journal file is specified this must be an in-memory instance.
     */
    public static SailRepository createRepository(final Properties props,
            final String file, final Option... args) {
        
        return new SailRepository(createSail(props, file, args));
        
    }
    
    /**
     * Create a new bigdata instance using the specified options.  Since no
     * journal file is specified this must be an in-memory instance.
     */
    public static Sail createSail(final Option... args) {
        
        return createSail(new Properties(), null, args);
        
    }
    
    /**
     * Create a new bigdata instance using the specified options and filename.  
     */
    public static Sail createSail(final String file, 
            final Option... args) {
    
    	//Ticket #1185: BigdataGraphFactory create not working. 
    	
        return createSail(new Properties(), file, args);
        
    }
    
    /**
     * Create a new bigdata instance using the specified options.
     */
    public static Sail createSail(final Properties props,
            final String file, final Option... args) {
        
        final List




© 2015 - 2025 Weber Informatics LLC | Privacy Policy