For my second little HTML Apollo test, I thought I'd build a simple log monitor. One of my favorite plugins for Eclipse is Mike Nimer's Log Viewer. It lets you watch a log file and updates in real time. Much more nicer than constantly reloading the log view in the ColdFusion Admin.
I wanted to know if this would be possible with Apollo, and of course, it is. Best of all, it isn't too difficult. I'm not going to describe every line of code (although I am including a zip), but instead will focus on the parts that pertain to Apollo.
var path = "/Applications/ColdFusionScorpio/logs/application.log"; var file = new runtime.flash.filesystem.File(); file.nativePath = path;
Note - there are actually multiple ways of pointing to a file. This is just one and obviously would not work on a PC. You get hooks to multiple default folders which is really handy for development. But in my case, I wanted a hard coded path.
On my "check" function, I look at the size of the file object. This is automatically updated by Apollo so I don't need to recreate my file. If the file's size doens't match my last recorded size, then I'm going to read in the difference. That is amazingly simple:
var fileStream = new runtime.flash.filesystem.FileStream(); fileStream.open(file, runtime.flash.filesystem.FileMode.READ); fileStream.position = oldsize; var diffsize = file.size - oldsize; var change = fileStream.readMultiByte(diffsize, "iso-8859-1");
I create an instance of the FileStream option and point it to my file. I can then read the file - but notice that I can begin at my last position. This means I'm not reading in a 2 gig file all at once. With the old position I can simply read in the rest of the file.
That's it. Seriously. How sweet is that? I did run into a bit of trouble though. Sometimes Apollo would run it's function midway through ColdFusion writing a line out to the browser. I took a "back up" approach and simply moved my change string to include only the text up to and including the first line break.
But once I did - I was able to add a very cool feature to our application - something I'm hoping I can do in Apollo. Basically, I made it so that you could drag a file onto the browser. JS would pick up the file and upload it. Even cooler - you could drag a folder, and JS/CF would recreate the folder structure in the virtual folder system and upload all the appropriate files. Cool stuff. I don't think Apollo supports "drag a file on to me" yet, but when/if it does, I'm thinking of building a simple demo of this.