Java Code Examples for org.apache.tinkerpop.gremlin.driver.Cluster#open()

The following examples show how to use org.apache.tinkerpop.gremlin.driver.Cluster#open() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DriverRemoteConnection.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
public DriverRemoteConnection(final Configuration conf) {
    final boolean hasClusterConf = IteratorUtils.anyMatch(conf.getKeys(), k -> k.startsWith("clusterConfiguration"));
    if (conf.containsKey(GREMLIN_REMOTE_DRIVER_CLUSTERFILE) && hasClusterConf)
        throw new IllegalStateException(String.format("A configuration should not contain both '%s' and 'clusterConfiguration'", GREMLIN_REMOTE_DRIVER_CLUSTERFILE));

    remoteTraversalSourceName = conf.getString(GREMLIN_REMOTE_DRIVER_SOURCENAME, DEFAULT_TRAVERSAL_SOURCE);

    try {
        final Cluster cluster;
        if (!conf.containsKey(GREMLIN_REMOTE_DRIVER_CLUSTERFILE) && !hasClusterConf)
            cluster = Cluster.open();
        else
            cluster = conf.containsKey(GREMLIN_REMOTE_DRIVER_CLUSTERFILE) ?
                    Cluster.open(conf.getString(GREMLIN_REMOTE_DRIVER_CLUSTERFILE)) : Cluster.open(conf.subset("clusterConfiguration"));

        client = cluster.connect(Client.Settings.build().create()).alias(remoteTraversalSourceName);
    } catch (Exception ex) {
        throw new IllegalStateException(ex);
    }

    attachElements = false;

    tryCloseCluster = true;
    tryCloseClient = true;
    this.conf = Optional.of(conf);
}
 
Example 2
Source File: DriverRemoteAcceptor.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public Object connect(final List<String> args) throws RemoteException {
    if (args.size() < 1) throw new RemoteException("Expects the location of a configuration file or variable name for a Cluster object as an argument");

    try {
        final String fileOrVar = args.get(0);
        if (new File(fileOrVar).isFile())
            this.currentCluster = Cluster.open(fileOrVar);
        else if (shellEnvironment.getVariable(fileOrVar) != null) {
            final Object o = shellEnvironment.getVariable(fileOrVar);
            if (o instanceof Cluster) {
                this.currentCluster = (Cluster) o;
            }
        }

        if (null == currentCluster)
            throw new RemoteException("Expects the location of a configuration file or variable name for a Cluster object as an argument");
        final boolean useSession = args.size() >= 2 && (args.get(1).equals(TOKEN_SESSION) || args.get(1).equals(TOKEN_SESSION_MANAGED));
        if (useSession) {
            final String sessionName = args.size() == 3 ? args.get(2) : UUID.randomUUID().toString();
            session = Optional.of(sessionName);

            final boolean managed = args.get(1).equals(TOKEN_SESSION_MANAGED);

            this.currentClient = this.currentCluster.connect(sessionName, managed);
        } else {
            this.currentClient = this.currentCluster.connect();
        }
        this.currentClient.init();
        return String.format("Configured %s", this.currentCluster) + getSessionStringSegment();
    } catch (final FileNotFoundException ignored) {
        throw new RemoteException("The 'connect' option must be accompanied by a valid configuration file");
    } catch (final Exception ex) {
        throw new RemoteException("Error during 'connect' - " + ex.getMessage(), ex);
    }
}
 
Example 3
Source File: DriverRemoteConnection.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link DriverRemoteConnection} using a new {@link Cluster} instance created from the supplied
 * configuration file. When {@link #close()} is called, this new {@link Cluster} is also closed.
 */
public static DriverRemoteConnection using(final String clusterConfFile, final String remoteTraversalSourceName) {
    try {
        return new DriverRemoteConnection(Cluster.open(clusterConfFile), true, remoteTraversalSourceName);
    } catch (Exception ex) {
        throw new IllegalStateException(ex);
    }
}
 
Example 4
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldClusterReadFileFromResources() throws Exception {
    final Cluster cluster = Cluster.open(TestClientFactory.RESOURCE_PATH);
    assertTrue(cluster != null);
}