Improper error handling in org.jboss.as.cli.scriptsupport.CLI

Solution Unverified - Updated -

Issue

  • Improper error handling in org.jboss.as.cli.scriptsupport.CLI.
Scenario:
1. Attempt to connect via CLI.connect(...)
2. Connection fails because EAP is not running yet. CLI throws "java.lang.IllegalStateException: Unable to connect to controller"
3. Attempt to connect again.
4. CLI throws exception "java.lang.IllegalStateException: Already connected to server."
** This message is incorrect. No connection has been established.
5. Any attempts after this to call CLI.cmd(...) result in NullPointerException at CLI.java line 146, because ctx.getModelControllerClient() returns null
  • The root of the problem is that all three of the CLI.connect(...) methods have incorrect error handling. See code below:

public void connect() { checkAlreadyConnected(); try { ctx = CommandContextFactory.getInstance().newCommandContext(); ctx.connectController(); } catch (CliInitializationException e) { throw new IllegalStateException("Unable to initialize command context.", e); } catch (CommandLineException e) { throw new IllegalStateException("Unable to connect to controller.", e); } }

checkAlreadyConnected() checks for an existing connection by seeing whether ctx is null. If it is not null, the class assumes that a connection exists. If the line ctx.connectController() [above] throws an exception, ctx is never set to null, so subsquent calls to checkAlreadyConnected() will report that a connect already exists.

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 6.4.0

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.