Skip to content

Prep for clustering functionality #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,29 @@
* limitations under the License.
*/

package org.neo4j.driver.internal.util;
package org.neo4j.driver.internal;

import java.net.InetAddress;
import java.net.UnknownHostException;
import org.neo4j.driver.internal.security.SecurityPlan;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.Logger;
import org.neo4j.driver.v1.Logging;
import org.neo4j.driver.v1.Session;

public class AddressUtil
abstract class BaseDriver implements Driver
{
/**
* Return true if the host provided matches "localhost" or "127.x.x.x".
*
* @param host the host name to test
* @return true if localhost, false otherwise
*/
public static boolean isLocalHost( String host )
private final SecurityPlan securityPlan;
protected final Logger log;

BaseDriver( SecurityPlan securityPlan, Logging logging )
{
this.securityPlan = securityPlan;
this.log = logging.getLog( Session.LOG_NAME );
}

@Override
public boolean isEncrypted()
{
try
{
// confirmed to work as desired with both "localhost" and "127.x.x.x"
return InetAddress.getByName( host ).isLoopbackAddress();
}
catch ( UnknownHostException e )
{
// if it's unknown, it's not local so we can safely return false
return false;
}
return securityPlan.requiresEncryption();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,32 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.neo4j.driver.internal;

import org.neo4j.driver.v1.AuthToken;
import org.neo4j.driver.v1.Session;

public class Version
import static java.lang.String.format;

/**
* The connection settings are used whenever a new connection is
* established to a server, specifically as part of the INIT request.
*/
public class ConnectionSettings
{
public static final String DEFAULT_USER_AGENT = format( "neo4j-java/%s", driverVersion() );

/**
* Extracts the driver version from the driver jar MANIFEST.MF file.
*/
public static String driverVersion()
private static String driverVersion()
{
// "Session" is arbitrary - the only thing that matters is that the class we use here is in the
// 'org.neo4j.driver' package, because that is where the jar manifest specifies the version.
// This is done as part of the build, adding a MANIFEST.MF file to the generated jarfile.
Package pkg = Session.class.getPackage();
if(pkg != null && pkg.getImplementationVersion() != null)
if ( pkg != null && pkg.getImplementationVersion() != null )
{
return pkg.getImplementationVersion();
}
Expand All @@ -40,4 +50,29 @@ public static String driverVersion()
// This should only happen during development, so call the version 'dev'.
return "dev";
}

private final AuthToken authToken;
private final String userAgent;

public ConnectionSettings( AuthToken authToken, String userAgent )
{
this.authToken = authToken;
this.userAgent = userAgent;
}

public ConnectionSettings( AuthToken authToken )
{
this( authToken, DEFAULT_USER_AGENT );
}

public AuthToken authToken()
{
return authToken;
}

public String userAgent()
{
return userAgent;
}

}
64 changes: 64 additions & 0 deletions driver/src/main/java/org/neo4j/driver/internal/DirectDriver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.neo4j.driver.internal;

import org.neo4j.driver.internal.net.BoltServerAddress;
import org.neo4j.driver.internal.net.pooling.PoolSettings;
import org.neo4j.driver.internal.net.pooling.SocketConnectionPool;
import org.neo4j.driver.internal.security.SecurityPlan;
import org.neo4j.driver.internal.spi.ConnectionPool;
import org.neo4j.driver.v1.Logging;
import org.neo4j.driver.v1.Session;

import static java.lang.String.format;

public class DirectDriver extends BaseDriver
{
private final BoltServerAddress address;
private final ConnectionPool connections;

public DirectDriver( BoltServerAddress address, ConnectionSettings connectionSettings, SecurityPlan securityPlan,
PoolSettings poolSettings, Logging logging )
{
super( securityPlan, logging );
this.address = address;
this.connections = new SocketConnectionPool( connectionSettings, securityPlan, poolSettings, logging );
}

@Override
public Session session()
{
return new InternalSession( connections.acquire( address ), log );
}

@Override
public void close()
{
try
{
connections.close();
}
catch ( Exception ex )
{
log.error( format( "~~ [ERROR] %s", ex.getMessage() ), ex );
}
}

}
84 changes: 0 additions & 84 deletions driver/src/main/java/org/neo4j/driver/internal/InternalDriver.java

This file was deleted.

This file was deleted.

Loading