<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Recent changes to Writing an ExCon command</title><link>https://sourceforge.net/p/excon/wiki/Writing%2520an%2520ExCon%2520command/</link><description>Recent changes to Writing an ExCon command</description><atom:link href="https://sourceforge.net/p/excon/wiki/Writing%20an%20ExCon%20command/feed" rel="self"/><language>en</language><lastBuildDate>Thu, 05 Dec 2013 16:59:29 -0000</lastBuildDate><atom:link href="https://sourceforge.net/p/excon/wiki/Writing%20an%20ExCon%20command/feed" rel="self" type="application/rss+xml"/><item><title>Writing an ExCon command modified by Chris DeGreef</title><link>https://sourceforge.net/p/excon/wiki/Writing%2520an%2520ExCon%2520command/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v2
+++ v3
@@ -36,12 +36,12 @@

 The purpose of this command is the echo whatever you type in at the console back to the console.  And also to write the same message to the log of your application.  The execute() method returns a String value.  It is the response given to the Sender and ultimately displayed on the console.  That is true for all commands.

-You may have wondered how the message to be echoed makes it from the command-line in the first place and into this Echo command.  &lt;a href="../index.php?page_id=29"&gt;Argument (parser)&lt;/a&gt; does that part for us.
+You may have wondered how the message to be echoed makes it from the command-line in the first place and into this Echo command.  Argument does that part for us.

 &lt;h2&gt;Writing Commands as Anonymous Inner Classes&lt;/h2&gt;
 Since commands have such a small "footprint" in your application you may not want to write classes for each of them.  You can in-stream them by using inner classes.

-Since this is an anonymous inner class it has to be used within the context of actually registering it.  So this example shows the necessary surround code for the [registration of the command] with the ExCon receiver.  The [parser definition] is also shown as part of the registration process.  This echo command requires a parser to get the "message" from the command-line into the command.
+Since this is an anonymous inner class it has to be used within the context of actually registering it.  So this example shows the necessary surround code for the [Registering your Commands in Java] with the ExCon receiver.  The parser definition is also shown as part of the registration process.  This echo command requires a parser to get the "message" from the command-line into the command.

 ~~~~~
 public class MyApplicationClass {
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Chris DeGreef</dc:creator><pubDate>Thu, 05 Dec 2013 16:59:29 -0000</pubDate><guid>https://sourceforge.net40c0c2e022d92ece1f04e5a2229ca409f1494882</guid></item><item><title>Writing an ExCon command modified by Chris DeGreef</title><link>https://sourceforge.net/p/excon/wiki/Writing%2520an%2520ExCon%2520command/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Chris DeGreef</dc:creator><pubDate>Thu, 05 Dec 2013 16:48:58 -0000</pubDate><guid>https://sourceforge.net9e03112ae9845cab286247b7285411a0989fa1cf</guid></item><item><title>Writing an ExCon command modified by Chris DeGreef</title><link>https://sourceforge.net/p/excon/wiki/Writing%2520an%2520ExCon%2520command/</link><description>&lt;div class="markdown_content"&gt;&lt;p&gt;The ExCon Receiver is a broker of commands that are request to be run by the Sender.  And Sender is a command-line utility that you would interact with to run the commands.&lt;/p&gt;
&lt;p&gt;In these examples that follow it will be assumed that your application also has the code to create the Receiver before registering these commands and to send it the go() message afterwards.  This won't be repeated in each of the examples.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Standard Commands&lt;/b&gt;&lt;br /&gt;
ExCon comes prepackaged with several "&lt;strong&gt;standard commands&lt;/strong&gt;".&lt;br /&gt;
&lt;pre class="java:html:nocontrols" name="code"&gt;rcvr.registerStandard();&lt;/pre&gt;&lt;br /&gt;
This will add several commands to the "Sender menu" that perform functions like garbage collection, stack dumps, JVM termination and so on.  All of the functions are pre-written as commands.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Writing your own Commands&lt;/b&gt;&lt;br /&gt;
Every command class must implement the ExCon interface called &lt;code&gt;IExternalRequest&lt;/code&gt;.  It requires a method called execute() that you must implement.  It is the method that is called when the command is requested to be run.  This is what the "Echo" command looks like.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;package&lt;/span&gt; &lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;obdobion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ExCon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;standard&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;import&lt;/span&gt; &lt;span class="n"&gt;org&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;apache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log4j&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;import&lt;/span&gt; &lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;obdobion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ExCon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ClientCommand&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;import&lt;/span&gt; &lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;obdobion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ExCon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IExternalRequest&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;class&lt;/span&gt; &lt;span class="n"&gt;Echo&lt;/span&gt; &lt;span class="n"&gt;implements&lt;/span&gt; &lt;span class="n"&gt;IExternalRequest&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;final&lt;/span&gt; &lt;span class="n"&gt;private&lt;/span&gt; &lt;span class="n"&gt;Logger&lt;/span&gt; &lt;span class="n"&gt;logger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getLogger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Echo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

    &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Echo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;final&lt;/span&gt; &lt;span class="n"&gt;ClientCommand&lt;/span&gt; &lt;span class="n"&gt;cc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;throws&lt;/span&gt; &lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Echo is more of an interesting test class that a command that implements a real-world feature.  But it serves the purpose here of showing off the framework of a command within ExCon.&lt;/p&gt;
&lt;p&gt;The purpose of this command is the echo whatever you type in at the console back to the console.  And also to write the same message to the log of your application.  The execute() method returns a String value.  It is the response given to the Sender and ultimately displayed on the console.  That is true for all commands.&lt;/p&gt;
&lt;p&gt;You may have wondered how the message to be echoed makes it from the command-line in the first place and into this Echo command.  &lt;a href="../index.php?page_id=29"&gt;Argument (parser)&lt;/a&gt; does that part for us.&lt;/p&gt;
&lt;h2&gt;Writing Commands as Anonymous Inner Classes&lt;/h2&gt;
&lt;p&gt;Since commands have such a small "footprint" in your application you may not want to write classes for each of them.  You can in-stream them by using inner classes.&lt;/p&gt;
&lt;p&gt;Since this is an anonymous inner class it has to be used within the context of actually registering it.  So this example shows the necessary surround code for the &lt;span&gt;[registration of the command]&lt;/span&gt; with the ExCon receiver.  The &lt;span&gt;[parser definition]&lt;/span&gt; is also shown as part of the registration process.  This echo command requires a parser to get the "message" from the command-line into the command.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;class&lt;/span&gt; &lt;span class="n"&gt;MyApplicationClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="n"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;myRegistration&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;rcvr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;echo&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="n"&gt;IExternalRequest&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;final&lt;/span&gt; &lt;span class="n"&gt;ClientCommand&lt;/span&gt; &lt;span class="n"&gt;cc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;throws&lt;/span&gt; &lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
              &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;-t String -k m --pos --var message --required&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is very nearly the same as creating the class like the first example and registering it like this.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;class&lt;/span&gt; &lt;span class="n"&gt;MyApplicationClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="n"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;myRegistration&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;rcvr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s"&gt;&amp;quot;echo&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Echo&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; 
        &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;-t String -k m --pos --var message --required&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This command would be accessed from the command-line by its name "echo".&lt;br /&gt;
&lt;pre class="html:nocontrols:nogutter" name="code"&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;xc echo 'Hello World'&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;lt; Hello World&lt;/p&gt;&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Chris DeGreef</dc:creator><pubDate>Thu, 05 Dec 2013 16:29:09 -0000</pubDate><guid>https://sourceforge.netb052ed9e6a06635512babf55f1734da4da213c6b</guid></item></channel></rss>