Home - Write Plug Ins

Write Your Command Plugins.

How is plugin developed? I'll use the "Web Stats" plugin as an example.

1. Take a look at the Command Plugin Template. It's availabe at /ScriptLibrary/AjaxLabCommandPrompt/Engine/Plugins/Your_Commands_Template.js

  	
Type.registerNamespace("AjaxLab.Engine.Plugins");
AjaxLab.Engine.Plugins.YourCommandClassName= function()
{
var customCommands = "YourCommand1|YourCommand2";
var instance = this;
AjaxLab.Engine.Plugins.BaseCommands.initializeBase(this);
this.registerCommands(customCommands);

/* Implement the Plug In here */

this.processCommands = function(inputtext) {


}
}
AjaxLab.Engine.Plugins.YourCommandClassName.registerClass('AjaxLab.Engine.Plugins.YourCommandClassName', AjaxLab.Engine.Abstract.Commands);

All we have to do is to replace the three bolded "YourCommandClassName" with our own class name (note, our complete class name will be "AjaxLab.Engine.Plugins.YourCommandClassName" and the rest is taken cared by the command prompt itself. The bolded "YourCommand1|YourCommand2" will be our custom commands. More on this later.

2. Okay, Let's code our IIS Log plugin.

First, let's call our plugin "MSIISLogParser", change the template and save it as AjaxLabMSIISLogParser.js. So base on the command template above, it becomes

  	
Type.registerNamespace("AjaxLab.Engine.Plugins");
AjaxLab.Engine.Plugins.MSIISLogParser= function()
{
var customCommands = "YourCommand1|YourCommand2";
var instance = this;
AjaxLab.Engine.Plugins.BaseCommands.initializeBase(this);
this.registerCommands(customCommands);

/* Implement the Plug In here */

this.processCommands = function(inputtext) {


}
}
AjaxLab.Engine.Plugins.MSIISLogParser.registerClass('AjaxLab.Engine.Plugins.MSIISLogParser', AjaxLab.Engine.Abstract.Commands)


So again, our full class name is "AjaxLab.Engine.Plugins.MSIISLogParser"

3. Register our own commands.

Now, Let's add our own commands. I'm thinking of using "webstats" or "logparser", oh hell, let's use both. So let's replace the bolded "YourCommand1|YourCommand2"with our own then.

  	
Type.registerNamespace("AjaxLab.Engine.Plugins");
AjaxLab.Engine.Plugins.MSIISLogParser= function()
{
var customCommands = "webstats||logparser";
var instance = this;
AjaxLab.Engine.Plugins.BaseCommands.initializeBase(this);
this.registerCommands(customCommands);

/* Implement the Plug In here */

this.processCommands = function(inputtext) {


}
}
AjaxLab.Engine.Plugins.MSIISLogParser.registerClass('AjaxLab.Engine.Plugins.MSIISLogParser', AjaxLab.Engine.Abstract.Commands)

4. How do we know what commands that the user entered?

Notice this "this.processCommands" function? That's where we are going to implement our own logic. Command prompt by default, pass the commands that the user entered on each line before they hit the "ENTER" key or carraige return and pass them into the "inputtext" parameter. For example:

 
http://www.ajaxlab.com>webstats top visitors inputtext is "webstats top visitors"


5. Process Commands.

Okay. Now, let' s take "webstats top visitors" command as an example and see how we are going to process it.

For this plug-in, I decided to use MSLogParser to parse the IIS log files and because MSSLogParser works nicely with C#, I decided to go all .NET with this plugIn and use AjaxPro.NET as well. (I encourage other plug-ins in scriptaculous/php to be submitted )


this.processCommands = function(inputtext) {
instance.Log4J.setRootLevel(instance.Log4J.LevelTypes.Debug);
instance.Log4J.Debug("LogParser command detected and input string is :"+inputtext);
var command = inputtext.split(" ");
if (command == null)
{
instance.Log4J.Debug("LogParser Command null");
}
instance.Log4J.Debug("Logparser input string command[0]:"+command[0]);
if(command[0].toUpperCase() == "LOGPARSER" || "WEBSTATS")
{
if(command[1].toUpperCase() == "TOP")
{
if(command[2].toUpperCase() =="VISITORS")
{
instance.Log4J.Debug("Start retrieving Top IPCount");
AjaxLabCmd.CMDLogParserDemo.SelectTopVisits(command[3],this.IPCountCallBack);
}
else{
instance.Console.printToConsole(command[2]+" is not recognized as an internal or external command");
}
}

else{
instance.Log4J.Debug("LogParser command :"+command[1]+" not recognized");
instance.Console.printToConsole(inputtext+" is not recognized as an internal or external command");
}
}
else{
instance.Log4J.Debug("LogParser base command not found");
instance.Console.printToConsole(inputtext+" is not recognized as an internal or external command");
}
}

this.IPCountCallBack = function(res) {
instance.Log4J.Debug("Start Top Viists Print"); instance.Console.println(""); instance.Console.println("IP Count"); instance.Console.println("---------- ----------");
if(res.value.Tables[0]!=null) { for(var i=0; i<res.value.Tables[0].Rows.length; i++) { instance.Console.println(res.value.Tables[0].Rows[i].IP + " : " + res.value.Tables[0].Rows[i].Total); } } else{ instance.Console.println("No values"); } instance.Console.newPrompt(); instance.Log4J.Debug("Endclear Top Visits Print");
}

 

What's going on Here? If you look closely, it's simply doing simple javascript actions. First, it parses the incoming command "webstats top visitors" into an array sepearated via spaces ( inputtext.split(" ") ). Second, for the command "webstates top visitors", if all three words are found, then it uses AjaxPro.NET to do an AJAX asynchrounous call to the backend C# method:

AjaxLabCmd.CMDLogParserDemo.SelectTopVisits(command[3],this.IPCountCallBack
);


This is a syntax specific to AjaxPro.NET. AjaxLabCmd is the backend Namespace. CMDLogParserDemo is the backend class to parse the IIS Log file. SelectTopVisits is the class method that does the specfic query, command[3] is an optional "number of results" parameter passed to the backend and this.IPCountCallBack is the ajax call back method for us to handle the result of the Ajax Query which simply loops through the result and spit it back onto the command prompt screen. Now, for the demostration, I won't go into the backend implementation as how exactly I used MS LogParser 2.2 to query the IIS log files, if you are interested, then please let me know and I'll blog a post about it.

Again, exactly how and what you used to call your backend is up to you. You can use Dojo, scriptaculous, php or whatever is your favorite. At this point, the magic is in your hands.

Note, there are serveral default methods that are available to plug-in authors.

a). instance.Log4J.Debug() method is available to you if you want to output some debugging information.
b). instance.Console.printToConsole() is used to print anything you want onto the console screen.
c). instance.Console.newPrompt() simply output another prompt root like "c:/:> " or in this demo, it's "http://www.ajaxlab.com :>"

There are more methods available to you. Please take a look at the API section for more detail.

6. Register this library with /ScriptLibrary/AjaxLabCommandPrompt/Engine/Core/Cmd.js, so command prompt knows to instantiate the plugin.

Type.registerNamespace("AjaxLab.Engine.Core");
AjaxLab.Engine.Core.Cmd= function(CommandPromptContainer, CommandPromptTextArea, DebugArea, CommandPromptRoot)
{
AjaxLab.Engine.Core.Cmd.initializeBase(this, [CommandPromptContainer, CommandPromptTextArea, DebugArea, CommandPromptRoot]);
this.InitPlugIns = function()
{
//Base Command Plugin. Try not to mess with it.
this.registerCommandsPlugin("ScriptLibrary/AjaxLabCommandPrompt/Engine/Plugins/AjaxLabBaseCommands.js",
"AjaxLab.Engine.Plugins.BaseCommands"
);


//Register Your Plugins Here
this.registerCommandsPlugin("ScriptLibrary/AjaxLabCommandPrompt/Engine/Plugins/AjaxLabMSIISLogParser.js",
"AjaxLab.Engine.Plugins.MSIISLogParser"
);


}


}
AjaxLab.Engine.Core.Cmd.registerClass('AjaxLab.Engine.Core.Cmd', AjaxLab.Engine.Abstract.Cmd);a
The first Parameter is the path to our plugin library and the second parameter is our FULL class name

Credits

AjaxLab.com