Skip to content
This repository has been archived by the owner on Apr 21, 2023. It is now read-only.

Commit

Permalink
Test connection and test cli path use now ProgressManager
Browse files Browse the repository at this point in the history
  • Loading branch information
dboissier committed Nov 24, 2015
1 parent 68b1735 commit ea269cf
Show file tree
Hide file tree
Showing 116 changed files with 809 additions and 335 deletions.
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
*.iml
*.ipr
*.iws
.classpath
.project
.settings/
target/
META-INF/
6 changes: 4 additions & 2 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
0.1.0
* initial version
0.1.0 - Initial version
* RedisDB: view database content with filter (KEYS instruction is temporary used) and group with separator pattern
* Couchbase: view bucket content with row limit options
* MongoDB: all features from the mongo4idea-0.7.2 ;)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013 David Boissier
* Copyright (c) 2015 David Boissier
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/org/codinjutsu/tools/nosql/DatabaseVendor.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright (c) 2015 David Boissier
*
* 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.codinjutsu.tools.nosql;

import org.codinjutsu.tools.nosql.commons.utils.GuiUtils;
Expand All @@ -6,7 +22,7 @@

public enum DatabaseVendor {

MONGO("MongoDB","mongo.png", "localhost:27017", "format: host:port. If replicat set: host:port1,host:port2,..."),
MONGO("MongoDB","mongodb.png", "localhost:27017", "format: host:port. If replicat set: host:port1,host:port2,..."),
REDIS("RedisDB", "redis.png", "localhost:6379", "format: host:port. If cluster: host:port1,host:port2,..."),
COUCHBASE("Couchbase", "couchbase.png", "localhost:23232", "format: host:port. If cluster: host:port1,host:port2,...");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013 David Boissier
* Copyright (c) 2015 David Boissier
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
115 changes: 75 additions & 40 deletions src/main/java/org/codinjutsu/tools/nosql/NoSqlConfigurable.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013 David Boissier
* Copyright (c) 2015 David Boissier
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,10 +24,14 @@
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.options.BaseConfigurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.LabeledComponent;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.util.ThrowableComputable;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.ui.*;
import com.intellij.ui.table.JBTable;
Expand All @@ -43,6 +47,7 @@
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeoutException;

import static org.apache.commons.lang.StringUtils.isBlank;

Expand All @@ -67,7 +72,7 @@ public NoSqlConfigurable(Project project) {
this.project = project;
this.configuration = NoSqlConfiguration.getInstance(project);
this.databaseVendorManager = DatabaseVendorManager.getInstance(project);
configurations = new LinkedList<ServerConfiguration>(this.configuration.getServerConfigurations());
configurations = new LinkedList<>(this.configuration.getServerConfigurations());
tableModel = new NoSqlServerTableModel(configurations);
mainPanel = new JPanel(new BorderLayout());
}
Expand All @@ -89,9 +94,9 @@ public String getHelpTopic() {
public JComponent createComponent() {
JPanel databaseVendorShellOptionsPanel = new JPanel();
databaseVendorShellOptionsPanel.setLayout(new BoxLayout(databaseVendorShellOptionsPanel, BoxLayout.Y_AXIS));
mongoShellPanel = new ShellPathPanel(DatabaseVendor.MONGO);
mongoShellPanel = new ShellPathPanel(DatabaseVendor.MONGO, "--version");
databaseVendorShellOptionsPanel.add(mongoShellPanel);
redisShellPanel = new ShellPathPanel(DatabaseVendor.REDIS);
redisShellPanel = new ShellPathPanel(DatabaseVendor.REDIS, "--version");
databaseVendorShellOptionsPanel.add(redisShellPanel);

mainPanel.add(databaseVendorShellOptionsPanel, BorderLayout.NORTH);
Expand Down Expand Up @@ -119,12 +124,20 @@ protected JComponent createMainComponent() {
table = new JBTable(tableModel);
table.getEmptyText().setText("No server configuration set");
table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

TableColumn autoConnectColumn = table.getColumnModel().getColumn(2);
int width = table.getFontMetrics(table.getFont()).stringWidth(table.getColumnName(2)) + 10;
autoConnectColumn.setPreferredWidth(width);
autoConnectColumn.setMaxWidth(width);
autoConnectColumn.setMinWidth(width);
table.setDefaultRenderer(DatabaseVendor.class, new ColoredTableCellRenderer() {
@Override
protected void customizeCellRenderer(JTable jTable, Object value, boolean b, boolean b1, int i, int i1) {
DatabaseVendor databaseVendor = (DatabaseVendor) value;
this.setIcon(databaseVendor.icon);
this.append(databaseVendor.name);
}
});

TableColumn autoConnectColumn = table.getColumnModel().getColumn(3);
int autoConnectColumnWidth = table.getFontMetrics(table.getFont()).stringWidth(table.getColumnName(3)) + 10;
autoConnectColumn.setPreferredWidth(autoConnectColumnWidth);
autoConnectColumn.setMaxWidth(autoConnectColumnWidth);
autoConnectColumn.setMinWidth(autoConnectColumnWidth);

return ToolbarDecorator.createDecorator(table)
.setAddAction(new AnActionButtonRunnable() {
Expand Down Expand Up @@ -162,7 +175,7 @@ public void run(AnActionButton button) {
ServerConfiguration copiedConfiguration = sourceConfiguration.clone();


ConfigurationDialog dialog = new ConfigurationDialog(mainPanel, project , databaseVendorManager, copiedConfiguration);
ConfigurationDialog dialog = new ConfigurationDialog(mainPanel, project, databaseVendorManager, copiedConfiguration);
dialog.setTitle("Edit a NoSql Server");
dialog.show();
if (!dialog.isOK()) {
Expand Down Expand Up @@ -266,37 +279,23 @@ private void stopEditing() {
}


public static boolean checkShellPath(String shellPath) throws ExecutionException {
if (isBlank(shellPath)) {
return false;
}

GeneralCommandLine commandLine = new GeneralCommandLine();
commandLine.setExePath(shellPath);
commandLine.addParameter("--version");
CapturingProcessHandler handler = new CapturingProcessHandler(commandLine.createProcess(), CharsetToolkit.getDefaultSystemCharset());
ProcessOutput result = handler.runProcess(15 * 1000);
return result.getExitCode() == 0;
}


private class ShellPathPanel extends JPanel implements Disposable {

private static final int TIMEOUT_MS = 60 * 1000;
private final String testParameter;
private LabeledComponent<TextFieldWithBrowseButton> shellPathField;
private JLabel testPathFeedbackLabel;

private ShellPathPanel(DatabaseVendor databaseVendor) {
private ShellPathPanel(DatabaseVendor databaseVendor, String testParameter) {
this.testParameter = testParameter;
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add(createLabel(databaseVendor.name));
shellPathField = createShellPathField(databaseVendor);
add(shellPathField);
add(createTestButton(databaseVendor));
testPathFeedbackLabel = new JLabel();
add(testPathFeedbackLabel);
}

private JLabel createLabel(String databaseVendorName) {
return new JLabel(String.format("Path to %s CLI:", databaseVendorName));
return new JLabel(String.format("Path to %s CLI:\t\t", databaseVendorName));
}

private JButton createTestButton(final DatabaseVendor databaseVendorName) {
Expand All @@ -311,19 +310,55 @@ public void actionPerformed(ActionEvent actionEvent) {
}


private void testPath(DatabaseVendor databaseVendor) {
private void testPath(final DatabaseVendor databaseVendor) {
ProcessOutput processOutput;
try {
testPathFeedbackLabel.setIcon(null);
if (checkShellPath(getShellPath())) {
testPathFeedbackLabel.setIcon(ServerConfigurationPanel.SUCCESS);
} else {
testPathFeedbackLabel.setIcon(ServerConfigurationPanel.FAIL);
}
} catch (ExecutionException e) {
Messages.showErrorDialog(mainPanel, e.getMessage(), String.format("Error During %s Shell Path Checking...", databaseVendor));
processOutput = ProgressManager.getInstance().runProcessWithProgressSynchronously(new ThrowableComputable<ProcessOutput, Exception>() {
@Override
public ProcessOutput compute() throws Exception {
return checkShellPath(databaseVendor, getShellPath());
}
}, "Testing " + databaseVendor.name + " CLI Executable...", true, NoSqlConfigurable.this.project);
} catch (ProcessCanceledException pce) {
return;
} catch (Exception e) {
Messages.showErrorDialog(mainPanel, e.getMessage(), "Something wrong happened");
return;
}
if (processOutput != null && processOutput.getExitCode() == 0) {
Messages.showInfoMessage(mainPanel, processOutput.getStdout(), databaseVendor.name + " CLI Path Checked");
}
}

public ProcessOutput checkShellPath(DatabaseVendor databaseVendor, String shellPath) throws ExecutionException, TimeoutException {
if (isBlank(shellPath)) {
return null;
}

GeneralCommandLine commandLine = new GeneralCommandLine();
commandLine.setExePath(shellPath);
if (testParameter != null) {
commandLine.addParameter(testParameter);
}
CapturingProcessHandler handler = new CapturingProcessHandler(commandLine.createProcess(), CharsetToolkit.getDefaultSystemCharset());
ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
ProcessOutput result = indicator == null ?
handler.runProcess(TIMEOUT_MS) :
handler.runProcessWithProgressIndicator(indicator);
if (result.isTimeout()) {
throw new TimeoutException("Couldn't check " + databaseVendor.name + " CLI executable - stopped by timeout.");
} else if (result.isCancelled()) {
throw new ProcessCanceledException();
} else if (result.getExitCode() != 0 || !result.getStderr().isEmpty()) {
throw new ExecutionException(String.format("Errors while executing %s. exitCode=%s errors: %s",
commandLine.toString(),
result.getExitCode(),
result.getStderr()));
}
return result;
}


public String getShellPath() {
String shellPath = shellPathField.getComponent().getText();
if (StringUtils.isNotBlank(shellPath)) {
Expand All @@ -338,7 +373,7 @@ public boolean isShellPathModified(String shellPath) {
}

private LabeledComponent<TextFieldWithBrowseButton> createShellPathField(DatabaseVendor databaseVendor) {
LabeledComponent<TextFieldWithBrowseButton> shellPathField = new LabeledComponent<TextFieldWithBrowseButton>();
LabeledComponent<TextFieldWithBrowseButton> shellPathField = new LabeledComponent<>();
TextFieldWithBrowseButton component = new TextFieldWithBrowseButton();
component.getChildComponent().setName("shellPathField");
shellPathField.setComponent(component);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013 David Boissier
* Copyright (c) 2015 David Boissier
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,11 +25,12 @@
import java.util.List;
import java.util.Map;

@SuppressWarnings("unused")
@State(
name = "NoSqlConfiguration",
storages = {
@Storage(file = "$PROJECT_FILE$"),
@Storage(file = "$PROJECT_CONFIG_DIR$/mongoSettings.xml", scheme = StorageScheme.DIRECTORY_BASED)
@Storage(file = "$PROJECT_CONFIG_DIR$/noSqlSettings.xml", scheme = StorageScheme.DIRECTORY_BASED)
}
)
public class NoSqlConfiguration implements PersistentStateComponent<NoSqlConfiguration> {
Expand Down Expand Up @@ -66,6 +67,10 @@ public void setShellPathByDatabaseVendor(Map<DatabaseVendor, String> shellPathBy
this.shellPathByDatabaseVendor = shellPathByDatabaseVendor;
}

public Map<DatabaseVendor, String> getShellPathByDatabaseVendor() {
return shellPathByDatabaseVendor;
}

public void setShellPath(DatabaseVendor databaseVendor, String shellPath) {
shellPathByDatabaseVendor.put(databaseVendor,shellPath);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013 David Boissier
* Copyright (c) 2015 David Boissier
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -44,7 +44,7 @@
import org.codinjutsu.tools.nosql.mongo.model.MongoDatabase;
import org.codinjutsu.tools.nosql.mongo.view.action.DropCollectionAction;
import org.codinjutsu.tools.nosql.mongo.view.action.DropDatabaseAction;
import org.codinjutsu.tools.nosql.mongo.view.action.MongoConsoleAction;
import org.codinjutsu.tools.nosql.commons.view.action.NoSqlDatabaseConsoleAction;
import org.codinjutsu.tools.nosql.commons.view.action.ViewCollectionValuesAction;
import org.codinjutsu.tools.nosql.commons.view.editor.NoSqlDatabaseFileSystem;
import org.codinjutsu.tools.nosql.mongo.view.editor.MongoObjectFile;
Expand Down Expand Up @@ -235,7 +235,7 @@ public void dispose() {
RefreshServerAction refreshServerAction = new RefreshServerAction(this);
if (ApplicationManager.getApplication() != null) {
actionGroup.add(refreshServerAction);
actionGroup.add(new MongoConsoleAction(this));
actionGroup.add(new NoSqlDatabaseConsoleAction(this));
actionGroup.add(viewCollectionValuesAction);
actionGroup.add(expandAllAction);
actionGroup.add(collapseAllAction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
class NoSqlServerTableModel extends AbstractTableModel implements ItemRemovable {
private final String[] columnNames = new String[]{
"Label",
"Vendor",
"URL",
"Autoconnect"
};
private final Class[] columnClasses = new Class[]{String.class, String.class, Boolean.class};
private final Class[] columnClasses = new Class[]{String.class, DatabaseVendor.class, String.class, Boolean.class};

private final List<ServerConfiguration> mongoServerConfigurations;

Expand All @@ -45,7 +46,7 @@ public Class getColumnClass(int column) {
}

public int getColumnCount() {
return 3;
return columnNames.length;
}

public int getRowCount() {
Expand All @@ -63,10 +64,13 @@ public Object getValueAt(int row, int column) {
case 0: { // "Label" column
return configuration.getLabel();
}
case 1: { // "URL" column
case 1: { // "Vendor" column
return configuration.getDatabaseVendor();
}
case 2: { // "URL" column
return configuration.getServerUrl();
}
case 2: { // "Autoconnect" column
case 3: { // "Autoconnect" column
return configuration.isConnectOnIdeStartup();
}
default: {
Expand All @@ -83,10 +87,14 @@ public void setValueAt(Object value, int row, int column) {
break;
}
case 1: {
//do nothing url = serverHosts
//do nothing ??
break;
}
case 2: {
//do nothing url = serverHosts
break;
}
case 3: {
configuration.setConnectOnIdeStartup((Boolean) value);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.codinjutsu.tools.nosql;

import com.intellij.icons.AllIcons;
import com.intellij.ui.ColoredTreeCellRenderer;
import com.intellij.ui.JBColor;
import org.apache.commons.lang.StringUtils;
Expand All @@ -31,8 +32,7 @@
public class NoSqlTreeRenderer extends ColoredTreeCellRenderer {

private static final Icon DATABASE = GuiUtils.loadIcon("database.png");
private static final Icon MONGO_COLLECTION = GuiUtils.loadIcon("folder.png");
private static final Icon MONGO_SERVER_ERROR = GuiUtils.loadIcon("mongo_warning.png");
private static final Icon MONGO_COLLECTION = AllIcons.Nodes.Folder;

@Override
public void customizeCellRenderer(@NotNull JTree mongoTree, Object value, boolean isSelected, boolean isExpanded, boolean isLeaf, int row, boolean focus) {
Expand All @@ -51,7 +51,6 @@ public void customizeCellRenderer(@NotNull JTree mongoTree, Object value, boolea
setIcon(mongoServer.getConfiguration().getDatabaseVendor().icon);
} else {
setForeground(JBColor.RED);
setIcon(MONGO_SERVER_ERROR);
setToolTipText("Unable to connect");
}
} else if (userObject instanceof Database) {
Expand Down
Loading

0 comments on commit ea269cf

Please sign in to comment.