Skip to content

Commit

Permalink
STS-2948: report to the user when a connection has failed
Browse files Browse the repository at this point in the history
  • Loading branch information
leods committed Oct 3, 2012
1 parent 9fdef36 commit 9753435
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@
*******************************************************************************/
package org.springframework.ide.eclipse.beans.ui.livegraph.actions;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.springframework.ide.eclipse.beans.ui.livegraph.LiveGraphUiPlugin;
import org.springframework.ide.eclipse.beans.ui.livegraph.model.LiveBeansModel;
import org.springframework.ide.eclipse.beans.ui.livegraph.model.LiveBeansModelGenerator;
import org.springframework.ide.eclipse.beans.ui.livegraph.views.LiveBeansGraphView;
import org.springsource.ide.eclipse.commons.core.StatusHandler;

/**
* @author Leo Dos Santos
Expand All @@ -34,9 +40,24 @@ public ConnectToApplicationAction(LiveBeansGraphView view) {
@Override
public void run() {
if (dialog.open() == IDialogConstants.OK_ID) {
LiveBeansModel model = LiveBeansModelGenerator.connectToModel(dialog.getServiceUrl(), dialog.getUsername(),
dialog.getPassword(), dialog.getApplicationName());
view.setInput(model);
try {
LiveBeansModel model = LiveBeansModelGenerator.connectToModel(dialog.getServiceUrl(),
dialog.getUsername(), dialog.getPassword(), dialog.getApplicationName());
view.setInput(model);
}
catch (CoreException e) {
Status status = new Status(IStatus.INFO, LiveGraphUiPlugin.PLUGIN_ID, e.getMessage(), e);
ErrorDialog
.openError(
dialog.getShell(),
"Connection Failed",
"Could not connect to the given server or application.\n\n"
+ "Please ensure that the server is configured for JMX access and that the host name and port are correct. "
+ "If the server requires authentication, please provide a username and password. "
+ "This feature is only supported for applications on Spring Framework 3.2 or greater.\n\n"
+ "See the Error Log for more details.", status);
StatusHandler.log(status);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.HashMap;
import java.util.Map;

import javax.management.InstanceNotFoundException;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.MalformedObjectNameException;
Expand All @@ -24,6 +25,7 @@
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.json.JSONException;
Expand All @@ -39,29 +41,65 @@
*/
public class LiveBeansModelGenerator {

public static LiveBeansModel connectToModel(JMXConnector connector, String appName) {
/**
* This method will not attempt to close the given {@link JMXConnector}. If
* the connection has failed, clients may capture the thrown
* {@link CoreException} and inform the user. This method will never return
* a <code>null</code> {@link LiveBeansModel}
*
* @param connector
* @param appName
* @return A valid {@link LiveBeansModel} model, empty if connection has
* failed
* @throws CoreException
*/
public static LiveBeansModel connectToModel(JMXConnector connector, String appName) throws CoreException {
try {
if (connector != null && appName != null && appName.length() > 0) {
ObjectName name = ObjectName.getInstance("", "application", "/".concat(appName));
MBeanServerConnection connection = connector.getMBeanServerConnection();
// Test the MBean's existence before proceeding. Will throw
// InstanceNotFoundException
connection.getObjectInstance(name);
LiveBeansViewMBean mbean = MBeanServerInvocationHandler.newProxyInstance(connection, name,
LiveBeansViewMBean.class, false);
return generateModel(mbean, appName);
}
}
catch (MalformedObjectNameException e) {
StatusHandler.log(new Status(IStatus.ERROR, LiveGraphUiPlugin.PLUGIN_ID,
throw new CoreException(new Status(IStatus.ERROR, LiveGraphUiPlugin.PLUGIN_ID,
"An error occurred while connecting to server. Please check that the application name is correct.",
e));
}
catch (InstanceNotFoundException e) {
throw new CoreException(new Status(IStatus.ERROR, LiveGraphUiPlugin.PLUGIN_ID,
"An error occurred while connecting to server. Please check that the application name is correct.",
e));
}
catch (IOException e) {
StatusHandler.log(new Status(IStatus.ERROR, LiveGraphUiPlugin.PLUGIN_ID,
throw new CoreException(new Status(IStatus.ERROR, LiveGraphUiPlugin.PLUGIN_ID,
"An error occurred while connecting to server.", e));
}
return new LiveBeansModel();
}

public static LiveBeansModel connectToModel(String serviceUrl, String username, String password, String appName) {
/**
* This method will attempt to create a {@link JMXConnector} from the given
* parameters and will close it when it is finished. If the connection has
* failed, clients may capture the thrown {@link CoreException} and inform
* the user. This method will never return a <code>null</code>
* {@link LiveBeansModel}
*
* @param serviceUrl
* @param username
* @param password
* @param appName
* @return A valid {@link LiveBeansModel} model, empty if connection has
* failed
* @throws CoreException
*/
public static LiveBeansModel connectToModel(String serviceUrl, String username, String password, String appName)
throws CoreException {
JMXConnector connector = null;
try {
connector = setupConnector(serviceUrl, username, password);
Expand All @@ -80,7 +118,7 @@ public static LiveBeansModel connectToModel(String serviceUrl, String username,
}
}

private static LiveBeansModel generateModel(LiveBeansViewMBean mbean, String appName) {
private static LiveBeansModel generateModel(LiveBeansViewMBean mbean, String appName) throws CoreException {
LiveBeansModel model = new LiveBeansModel();
try {
if (mbean != null) {
Expand All @@ -90,13 +128,14 @@ private static LiveBeansModel generateModel(LiveBeansViewMBean mbean, String app
}
}
catch (JSONException e) {
StatusHandler.log(new Status(IStatus.ERROR, LiveGraphUiPlugin.PLUGIN_ID,
throw new CoreException(new Status(IStatus.ERROR, LiveGraphUiPlugin.PLUGIN_ID,
"An error occurred while generating graph model.", e));
}
return model;
}

private static JMXConnector setupConnector(String serviceUrl, String username, String password) {
private static JMXConnector setupConnector(String serviceUrl, String username, String password)
throws CoreException {
JMXConnector connector = null;
try {
if (serviceUrl != null && serviceUrl.length() > 0) {
Expand All @@ -109,11 +148,11 @@ private static JMXConnector setupConnector(String serviceUrl, String username, S
}
}
catch (MalformedURLException e) {
StatusHandler.log(new Status(IStatus.ERROR, LiveGraphUiPlugin.PLUGIN_ID,
throw new CoreException(new Status(IStatus.ERROR, LiveGraphUiPlugin.PLUGIN_ID,
"An error occurred while connecting to server. Please check that the service URL is correct.", e));
}
catch (IOException e) {
StatusHandler.log(new Status(IStatus.ERROR, LiveGraphUiPlugin.PLUGIN_ID,
throw new CoreException(new Status(IStatus.ERROR, LiveGraphUiPlugin.PLUGIN_ID,
"An error occurred while connecting to server.", e));
}
return connector;
Expand Down

0 comments on commit 9753435

Please sign in to comment.