<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="https://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    Advanced CLI scripting with Groovy, Rhino, Jython, etc.
</h3>
<span style="margin-bottom: 10px;">
    modified by <a href="https://community.jboss.org/people/ssilvert">Stan Silvert</a> in <i>JBoss AS 7 Development</i> - <a href="https://community.jboss.org/docs/DOC-18796">View the full document</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>The JBoss AS7 <a class="jive-link-wiki-small" href="https://community.jboss.org/docs/DOC-16581">Command Line Interface</a> has support for<a class="jive-link-wiki-small" href="https://community.jboss.org/docs/DOC-17041"> writing CLI scripts</a> that can be executed against a running AS7 instance.&#160; You can also use the <a class="jive-link-wiki-small" href="https://community.jboss.org/docs/DOC-17597">CLI public API</a> to write more sophisticated programs that accomplish the same goals.&#160; This article shows how to do something in between.&#160; You can use the full power of any JVM-based scripting language using a simplified wrapper class for the CLI API.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>AS7.2 introduces the scriptsupport.CLI class.&#160; This class acts as a facade for the <a class="jive-link-wiki-small" href="https://community.jboss.org/docs/DOC-17597">CLI public API</a>.&#160; AS7.2 also introduces the <a class="jive-link-wiki-small" href="https://community.jboss.org/docs/DOC-18795">CLI remote client jar</a> which provides everything you need to connect to AS7 and execute CLI commands.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h3>Example</h3><p>As an example, we will implement a simple script in several different JVM-based scripting languages.&#160; The script will do the following:</p><ol><li style="text-align: start;">Connect to a local AS7 instance</li><li style="text-align: start;">Determine if the instance is standalone or domain</li><li style="text-align: start;">"cd" to the MBean containing runtime info</li><li style="text-align: start;">Read the attributes for "server start time" and "server up time".</li><li style="text-align: start;">Output those values.</li><li style="text-align: start;">Disconnect from the AS7 instance.</li></ol><h3></h3><h3>Groovy CLI scripts</h3><p>To run the groovy script below, just add the <a class="jive-link-wiki-small" href="https://community.jboss.org/docs/DOC-18795">CLI remote client jar</a> to the classpath when executing groovy:</p><pre class="jive-pre"><code class="jive-code">groovy -cp jboss-cli-client.jar uptime.groovy
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><strong>uptime.groovy</strong></p><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>import</b></font> org.jboss.as.cli.scriptsupport.*
&#160;
cli = CLI.newInstance()
cli.connect()
&#160;
<font color="navy"><b>if</b></font> (cli.getCommandContext().isDomainMode()) <font color="navy">{</font>
&#160; cli.cmd(<font color="red">"cd /host=master/core-service=platform-mbean/type=runtime"</font>)
<font color="navy">}</font> <font color="navy"><b>else</b></font> <font color="navy">{</font>
&#160; cli.cmd(<font color="red">"cd /core-service=platform-mbean/type=runtime"</font>)
<font color="navy">}</font>
&#160;
result = cli.cmd(<font color="red">":read-attribute(name=start-time)"</font>)
response = result.getResponse()
startTime = response.get(<font color="red">"result"</font>).asLong()
&#160;
result = cli.cmd(<font color="red">":read-attribute(name=uptime)"</font>)
response = result.getResponse()
serveruptime = response.get(<font color="red">"result"</font>).asString()
&#160;
println()
println(<font color="red">"The server was started on "</font> + <font color="navy"><b>new</b></font> Date(startTime))
println(<font color="red">"It has been running for "</font> + serveruptime + <font color="red">"ms"</font>)
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h3>Rhino (javascript) CLI scripts</h3><p>To run the Rhino script below, add the Rhino jars and the jboss-cli-client.jar to the classpath.&#160; You will also need to provide the Rhino main class.</p><pre class="jive-pre"><code class="jive-code">java -cp js-14.jar;js.jar;jboss-cli-client.jar org.mozilla.javascript.tools.shell.Main -f uptime.js
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><strong>uptime.js</strong></p><pre class="jive-pre"><code class="jive-code">importPackage(org.jboss.as.cli.scriptsupport)

cli = CLI.newInstance()
cli.connect()

if (cli.getCommandContext().isDomainMode()) {
&#160; cli.cmd("cd /host=master/core-service=platform-mbean/type=runtime")
} else {
&#160; cli.cmd("cd /core-service=platform-mbean/type=runtime")
}

result = cli.cmd(":read-attribute(name=start-time)")
response = result.getResponse()
startTime = response.get("result").asLong()

result = cli.cmd(":read-attribute(name=uptime)")
response = result.getResponse()
serveruptime = response.get("result").asString()

print()
print("The server was started on " + new Date(startTime))
print("It has been running for " + serveruptime + "ms")

cli.disconnect()
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h3>Using the scriptsupport.CLI class</h3><p>As mentioned before, the <strong>org.jboss.as.cli.scriptsupport.CLI</strong> class acts as a facade for the <a class="jive-link-wiki-small" href="https://community.jboss.org/docs/DOC-17597">CLI public API</a>.&#160;&#160; For the most part, its methods are self-explanitory.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><table border="0" cellpadding="3" cellspacing="0" class="overviewSummary" summary="Method Summary table, listing methods, and an explanation"><tbody><tr class="altColor"><td class="colFirst" style="border:0px solid black;"><code><a class="jive-link-anchor-small">CLI.Result</a></code></td><td class="colLast" style="border:0px solid black;"><code><strong><a class="jive-link-anchor-small">cmd</a></strong>(<a class="jive-link-external-small" href="http://download.oracle.com/javase/6/docs/api/java/lang/String.html?is-external=true">String</a> cliCommand)</code> <p class="block">Execute a CLI command.</p></td></tr><tr class="rowColor"><td class="colFirst" style="border:0px solid black;"><code>void</code></td><td class="colLast" style="border:0px solid black;"><code><strong><a class="jive-link-anchor-small">connect</a></strong>()</code> <p class="block">Connect to the server using the default host and port.</p></td></tr><tr class="altColor"><td class="colFirst" style="border:0px solid black;"><code>void</code></td><td class="colLast" style="border:0px solid black;"><code><strong><a class="jive-link-anchor-small">connect</a></strong>(<a class="jive-link-external-small" href="http://download.oracle.com/javase/6/docs/api/java/lang/String.html?is-external=true">String</a> username, char[] password)</code> <p class="block">Connect to the server using the default host and port.</p></td></tr><tr class="rowColor"><td class="colFirst" style="border:0px solid black;"><code>void</code></td><td class="colLast" style="border:0px solid black;"><code><strong><a class="jive-link-anchor-small">connect</a></strong>(<a class="jive-link-external-small" href="http://download.oracle.com/javase/6/docs/api/java/lang/String.html?is-external=true">String</a> controllerHost, int controllerPort, <a class="jive-link-external-small" href="http://download.oracle.com/javase/6/docs/api/java/lang/String.html?is-external=true">String</a> username, char[] password)</code> <p class="block">Connect to the server using a specified host and port.</p></td></tr><tr class="altColor"><td class="colFirst" style="border:0px solid black;"><code>void</code></td><td class="colLast" style="border:0px solid black;"><code><strong><a class="jive-link-anchor-small">disconnect</a></strong>()</code> <p class="block">Disconnect from the server.</p></td></tr><tr class="rowColor"><td class="colFirst" style="border:0px solid black;"><code><a class="jive-link-anchor-small">CommandContext</a></code></td><td class="colLast" style="border:0px solid black;"><code><strong><a class="jive-link-anchor-small">getCommandContext</a></strong>()</code> <p class="block">Return the CLI CommandContext that was created when connected to the server.</p></td></tr><tr class="altColor"><td class="colFirst" style="border:0px solid black;"><code>static <a class="jive-link-anchor-small">CLI</a></code></td><td class="colLast" style="border:0px solid black;"><code><strong><a class="jive-link-anchor-small">newInstance</a></strong>()</code> <p class="block">Create a new CLI instance.</p></td></tr></tbody></table><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h3>CLI.cmd() and CLI.Result</h3><p>The <code><strong><a class="jive-link-anchor-small">cmd</a></strong>(<a class="jive-link-external-small" href="http://download.oracle.com/javase/6/docs/api/java/lang/String.html?is-external=true">String</a> cliCommand)</code> method takes a String that can be anything you would normally enter in CLI's regular interactive mode.&#160; It returns CLI.Result, which has the following methods:</p><table border="0" cellpadding="3" cellspacing="0" class="overviewSummary" summary="Method Summary table, listing methods, and an explanation"><tbody><tr class="altColor"><td class="colFirst" style="border:0px solid black;"><code><a class="jive-link-external-small" href="http://download.oracle.com/javase/6/docs/api/java/lang/String.html?is-external=true">String</a></code></td><td class="colLast" style="border:0px solid black;"><code><strong><a class="jive-link-anchor-small">getCliCommand</a></strong>()</code> <p class="block">Return the original command as a String.</p></td></tr><tr class="rowColor"><td class="colFirst" style="border:0px solid black;"><code>org.jboss.dmr.ModelNode</code></td><td class="colLast" style="border:0px solid black;"><code><strong><a class="jive-link-anchor-small">getRequest</a></strong>()</code> <p class="block">If the command resulted in a server-side operation, return the ModelNode representation of the operation.</p></td></tr><tr class="altColor"><td class="colFirst" style="border:0px solid black;"><code>org.jboss.dmr.ModelNode</code></td><td class="colLast" style="border:0px solid black;"><code><strong><a class="jive-link-anchor-small">getResponse</a></strong>()</code> <p class="block">If the command resulted in a server-side operation, return the ModelNode representation of the response.</p></td></tr><tr class="rowColor"><td class="colFirst" style="border:0px solid black;"><code>boolean</code></td><td class="colLast" style="border:0px solid black;"><code><strong><a class="jive-link-anchor-small">isLocalCommand</a></strong>()</code> <p class="block">Return true if the command was only executed locally and did not result in a server-side operation.</p></td></tr><tr class="altColor"><td class="colFirst" style="border:0px solid black;"><code>boolean</code></td><td class="colLast" style="border:0px solid black;"><code><strong><a class="jive-link-anchor-small">isSuccess</a></strong>()</code> <p class="block">Return true if the command was successful.</p></td></tr></tbody></table><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>CLI.Result tells you everything that happened when you invoked CLI.cmd(cliCommand).&#160; Most of the time, you just want to know your command was successful.&#160; So you can just call the isSuccess() method.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>But if you want more details on the request and response, that information is available.&#160; Unless it is a local command, the command String is converted to a ModelNode object that the AS7 server understands.&#160; See <a class="jive-link-wiki-small" href="https://community.jboss.org/docs/DOC-16336">Format of a Detyped Operation Request</a> for details.&#160; A response from the server will also be in the form of a ModelNode object.&#160; See <a class="jive-link-wiki-small" href="https://community.jboss.org/docs/DOC-16354">Format of a Detyped Operation Response</a> for details on the response.</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Comment by <a href="https://community.jboss.org/docs/DOC-18796">going to Community</a></p>

        <p style="margin: 0;">Create a new document in JBoss AS 7 Development at <a href="https://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2225">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>