Skip to content

Commit

Permalink
Merge branch 'driver-upgrade'
Browse files Browse the repository at this point in the history
Conflicts:
	pom.xml
  • Loading branch information
swarris committed Jan 31, 2022
2 parents 05c013a + 0c49209 commit 3bd5700
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 35 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
<maven.test.skip>true</maven.test.skip>
<bundle.symbolicName>nl.corwur.cytoscape.neo4j</bundle.symbolicName>
<bundle.namespace>nl.corwur.cytoscape.neo4j</bundle.namespace>
<cytoscape.version>3.8.0</cytoscape.version>
<cytoscape.version>3.9.0</cytoscape.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.osgi.core.version>6.0.0</org.osgi.core.version>
<neo4j-java-driver.version>4.4.1</neo4j-java-driver.version>
<junit.version>4.13.1</junit.version>
<log4j.version>2.17.1</log4j.version>
<pax-logging.version>1.5.3</pax-logging.version>
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
<maven-bundle-plugin.version>4.2.1</maven-bundle-plugin.version>
<mockito-core.version>3.3.3</mockito-core.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package nl.corwur.cytoscape.neo4j.internal.configuration;

import org.apache.log4j.Logger;

import nl.corwur.cytoscape.neo4j.internal.neo4j.ConnectionParameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -16,9 +17,14 @@
*/
public class AppConfiguration {

private static final Logger LOG = Logger.getLogger(AppConfiguration.class);
private static final Logger LOGGER = LoggerFactory.getLogger(AppConfiguration.class);

private static final int BOLT_PORT = 7687;
private static final Logger LOG = LoggerFactory.getLogger(AppConfiguration.class);
private static final String TEMPLATEDIR = "templatedir";
private static final String NEO4J_PROTOCOL = "neo4j.protocol";
private static final String NEO4J_HOST = "neo4j.host";
private static final String NEO4J_PORT = "neo4j.port";
private static final String NEO4J_USERNAME = "neo4j.username";
private static final String NEO4J_DATABASE = "neo4j.database";
private Properties properties = new Properties();
Expand All @@ -31,6 +37,25 @@ public String getNeo4jHost() {
return properties.getProperty(NEO4J_HOST);
}

public ConnectionParameter.Protocol getNeo4jProtocol() {
try {
return ConnectionParameter.Protocol.valueOf(properties.getProperty(NEO4J_PROTOCOL));
} catch (IllegalArgumentException e) {
LOGGER.warn("Invalid protocol using bolt:// {}", e);
return ConnectionParameter.Protocol.BOLT;
}
}

public int getNeo4jPort() {
try {
return Integer.parseInt(properties.getProperty(NEO4J_PORT));
} catch (NumberFormatException e) {
LOGGER.warn("Invalid port number using default bolt port (7687) {}", e);
return BOLT_PORT;
}
}


public String getNeo4jUsername() {
return properties.getProperty(NEO4J_USERNAME);
}
Expand All @@ -56,6 +81,8 @@ public void load() {
}

private void setDefaultProperties() {
properties.setProperty(NEO4J_PROTOCOL, "BOLT");
properties.setProperty(NEO4J_PORT, "7687");
properties.setProperty(NEO4J_HOST, "localhost");
properties.setProperty(NEO4J_USERNAME, "neo4j");
properties.setProperty(NEO4J_DATABASE, "neo4j");
Expand All @@ -76,7 +103,8 @@ private Path getConfigurationFile() {
return Paths.get(tmpDir, "corwur-cyneo4j.properties");
}

public void setConnectionParameters(String hostname, String username, String database) {
public void setConnectionParameters(String protocol,String hostname, String username, String database) {
properties.setProperty(NEO4J_PROTOCOL, protocol);
properties.setProperty(NEO4J_HOST, hostname);
properties.setProperty(NEO4J_USERNAME, username);
properties.setProperty(NEO4J_DATABASE, database);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
package nl.corwur.cytoscape.neo4j.internal.neo4j;

public class ConnectionParameter {
private final String host;

public enum Protocol {
BOLT("bolt"),
NEO4J("neo4j");


private final String protocol;
Protocol(String protocol) {
this.protocol = protocol;
}

}

private final int port;
private final Protocol protocol;
private final String hostname;
private final String username;
private final char[] password;
private final String database;

public ConnectionParameter(String url, String username, char[] password, String database) {
this.host = url;
public ConnectionParameter(Protocol protocol, String hostname, int port, String username, char[] password, String database) {
this.protocol = protocol;
this.hostname = hostname;
this.username = username;
this.password = password;
this.database = database;
this.port = port;
}

String getUrl() {
return protocol.protocol + "://" + hostname + ":" + port;
}

String getBoltUrl() {
return "neo4j://" + host;
Protocol getProtocol() {
return protocol;
}

String getUsername() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package nl.corwur.cytoscape.neo4j.internal.neo4j;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import nl.corwur.cytoscape.neo4j.internal.graph.Graph;

import java.util.List;
Expand All @@ -9,7 +11,7 @@
import org.neo4j.driver.exceptions.ServiceUnavailableException;

public class Neo4jClient {
private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Neo4jClient.class);
private static Logger logger = LoggerFactory.getLogger(Neo4jClient.class);

private Driver driver;
private Neo4jGraphFactory neo4JGraphFactory = new Neo4jGraphFactory();
Expand All @@ -18,7 +20,7 @@ public class Neo4jClient {
public boolean connect(ConnectionParameter connectionParameter) {
try {
driver = GraphDatabase.driver(
connectionParameter.getBoltUrl(),
connectionParameter.getUrl(),
AuthTokens.basic(
connectionParameter.getUsername(),
connectionParameter.getPasswordAsString()
Expand All @@ -34,7 +36,11 @@ public boolean connect(ConnectionParameter connectionParameter) {
}

private Session getSession(){
return driver.session(SessionConfig.builder().withDatabase(database).build());
if(driver.supportsMultiDb()) {
return driver.session(SessionConfig.builder().withDatabase(database).build());
} else {
return driver.session(SessionConfig.builder().build());
}
}

public void executeQuery(CypherQuery cypherQuery) throws Neo4jClientException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package nl.corwur.cytoscape.neo4j.internal.tasks.querytemplate.mapping.values;

import org.apache.log4j.Logger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
Expand All @@ -14,7 +14,7 @@
*/
public abstract class ValueScriptExpression<T, V> implements ValueExpression<T, V> {

private Logger logger = Logger.getLogger(ValueScriptExpression.class);
private Logger logger = LoggerFactory.getLogger(ValueScriptExpression.class);

private final String script;
private final String varName;
Expand All @@ -33,7 +33,7 @@ public V eval(T val) {
try {
return type.cast(engine.eval(script));
} catch (ScriptException | ClassCastException e) {
logger.error(e);
logger.error("{}",e);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import nl.corwur.cytoscape.neo4j.internal.tasks.querytemplate.template.Reader;
import nl.corwur.cytoscape.neo4j.internal.tasks.querytemplate.template.ReaderException;
import nl.corwur.cytoscape.neo4j.internal.tasks.querytemplate.template.Writer;
import org.apache.log4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.bind.JAXBException;
import java.io.IOException;
Expand All @@ -21,7 +22,7 @@

public class CypherQueryTemplateDirectoryProvider {

private static Logger logger = Logger.getLogger(CypherQueryTemplateDirectoryProvider.class);
private static Logger logger = LoggerFactory.getLogger(CypherQueryTemplateDirectoryProvider.class);

private Map<Long, TemplateMapEntry> cypherQueryTemplateMap = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,28 @@ class ConnectDialog extends JDialog { //NOSONAR , hierarchy level > 5

private static final String CANCEL_CMD = "cancel";
private static final String OK_CMD = "ok";

private JComboBox protocolCombobox = new JComboBox(ConnectionParameter.Protocol.values());
private JTextField usernameField = new JTextField("neo4j");
private JTextField hostnameField = new JTextField("localhost");
private JTextField portField = new JTextField("7687");
private JTextField databaseField = new JTextField("database");
private JPasswordField password = new JPasswordField();
private boolean ok = false;
private final transient Predicate<ConnectionParameter> connectionCheck;

ConnectDialog(JFrame jFrame, Predicate<ConnectionParameter> connectionCheck, String hostname, String username, String database) {
ConnectDialog(JFrame jFrame, Predicate<ConnectionParameter> connectionCheck,
ConnectionParameter.Protocol protocol,
int port,
String hostname,
String username,
String database) {
super(jFrame);
this.connectionCheck = connectionCheck;
protocolCombobox.setSelectedItem(protocol);
usernameField.setText(username);
hostnameField.setText(hostname);
portField.setText(String.valueOf(port));
databaseField.setText(database);
}

Expand All @@ -33,7 +43,7 @@ void showConnectDialog() {
setModal(true);
setResizable(true);
pack();
setSize(380, 240);
setSize(380, 280);
setVisible(true);
}

Expand All @@ -54,7 +64,13 @@ void setDatabaseField(String database){
}

private ConnectionParameter getParameters() {
return new ConnectionParameter(hostnameField.getText(), usernameField.getText(), password.getPassword(), databaseField.getText());
return new ConnectionParameter(
(ConnectionParameter.Protocol) protocolCombobox.getSelectedItem(),
hostnameField.getText(),
Integer.parseInt(portField.getText()),
usernameField.getText(),
password.getPassword(),
databaseField.getText());
}

private void init() {
Expand All @@ -72,7 +88,9 @@ private void init() {
cancelButton.addActionListener(ae -> dispose());
cancelButton.setActionCommand(CANCEL_CMD);

JLabel protocolLabel = new JLabel("Protocol");
JLabel hostnameLabel = new JLabel("Hostname");
JLabel portLabel = new JLabel("Port");
JLabel databaseLabel = new JLabel("Database");
JLabel usernameLabel = new JLabel("Username");
JLabel passwordLabel = new JLabel("Password");
Expand All @@ -84,53 +102,83 @@ private void init() {
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);

int y = 0;
//Protocol
gbc.gridx = 0;
gbc.gridy = y;
gbc.weightx = 0;
topPanel.add(protocolLabel, gbc);

gbc.gridx = 1;
gbc.gridy = y;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
topPanel.add(protocolCombobox, gbc);

//Hostname
y++;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridy = y;
gbc.weightx = 0;
topPanel.add(hostnameLabel, gbc);

gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridy = y;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
topPanel.add(hostnameField, gbc);

//Port
y++;
gbc.gridx = 0;
gbc.gridy = y;
gbc.weightx = 0;
topPanel.add(portLabel, gbc);

gbc.gridx = 1;
gbc.gridy = y;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
topPanel.add(portField, gbc);

//Database name
y++;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridy = y;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 0;
topPanel.add(databaseLabel, gbc);

gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridy = y;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 0;
topPanel.add(databaseField, gbc);

//Username
y++;
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridy = y;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 0;
topPanel.add(usernameLabel, gbc);

gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridy = y;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 0;
topPanel.add(usernameField, gbc);

//Password
y++;
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridy = y;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 0;
topPanel.add(passwordLabel, gbc);

gbc.gridx = 1;
gbc.gridy = 3;
gbc.gridy = y;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 0;
topPanel.add(password, gbc);
Expand All @@ -151,7 +199,9 @@ private void ok() {
}

public static void main(String[] args) {
ConnectDialog connectDialog = new ConnectDialog(null, connectionParameter -> true, "localhost", "neo4j", "neo4j");
ConnectDialog connectDialog = new ConnectDialog(null, connectionParameter -> true,
ConnectionParameter.Protocol.BOLT, 7687,
"localhost", "neo4j", "neo4j");
connectDialog.showConnectDialog();

}
Expand All @@ -167,4 +217,8 @@ public String getUsername() {
public String getDatabase() {
return databaseField.getText();
}

public String getProtocol() {
return protocolCombobox.getSelectedItem().toString();
}
}
Loading

0 comments on commit 3bd5700

Please sign in to comment.