Archive | Tech
MacFuse
As I’ve said before, [TextMate](http://macromates.com/) is becoming my killer app on OSX. One thing I have been missing is a way to edit files over SSH. Emacs has [Tramp](http://www.gnu.org/software/tramp/), which works very well. I googled around and noted that the TextMate list had mentioned [MacFuse](http://code.google.com/p/macfuse/) as a way to accomplish this.
I remembered hearing about MacFuse in the past and have played with the original [Fuse](http://fuse.sourceforge.net/) before. Fuse is a way for a normal use to mount a filesystem. And “filesystem” is a relative term — with the Linux-based Fuse, you could mount your GMail account and use that like a file system, or even an SSH connection, which was my hope. I looked at MacFuse and they are getting some good stuff around. MacFuse not only has a pre-built version for SSH (called, wittingly, “[sshfs](http://code.google.com/p/macfuse/wiki/MACFUSE_FS_SSHFS)”) but also [one for making SpotlightFS](http://code.google.com/p/macfuse/wiki/MACFUSE_FS_SPOTLIGHTFS). Sorta like Smart Folders. Only different.
I installed MacFuse and it required a restart. I did that and installed sshfs. I configured a connection (I use private keys with my SSH — I think it’s required for this operation. At least it should be). When I used Finder or the open file dialog, it was slow at first. But as MacFuse was able to do it’s caching, it turned out to be only a little slower than a normal file system. And reading the file in TextMate and then saving it were also so. But, really, the same is true in Tramp as well.
I think I like the Emacs/Tramp combination better, but I don’t see TextMate getting anything like that. So MacFuse and sshfs will have to do. And I think they will do nicely.
Array Functions for JavaScript
I was going to go through the exercise of doing this myself, but it seems that [someone already beat me to it](http://erik.eae.net/archives/2005/06/05/17.53.19/). That’s okay — I’ll stand on the shoulders of giants.
These methods first check to make sure that they do not already exist on the Array object. If they do, they don’t overwrite it. If not, they create it.
So that allows you to do this in Firefox and IE:
myarray.forEach( function(item){
// item is in myarray
// so do something with it.
});
You also have `filter`, which is quite handy, too:
function isHidden(divname, index, array) {
var div = document.getElementsByTagName(divname);
return (div.style.display==’none’);
}
hidden = mydivs.filter( isHidden);
This works exactly like you would expect it to if you have arrays generated by JSON — in other words, like a charm.
JSON — not just for AJAX
I recently blogged on the evils of mixing Java and JavaScript in JSP pages but didn’t give any good ways around it. I mean, you have a list or array in Java and you want JavaScript in the JSP page to access this data — how do you do that?
Let me introduce you to to Json-lib.
You’ve probably heard of JSON before and people rant and rave about it on using JSON with AJAX instead of XML[1]. And I don’t think there is anything wrong it that, but I didn’t want or need AJAX — I just need to move data from Java to JavaScript in a JSP page. And JSON makes it oh so easy.
- In your Servlet, get and arrange your data the way you like it.
- Then using Json-lib make a JSONObject like so:
JSONObject jsMap = JSONObject.fromObject(jobsMap);. - Put the string of that JSON object into your
request. You know what I mean:request.setAttribute("JSON_MAP",jsMap.toString());. - In your JSP page, just eval that string:
var jobMap = eval(<%= request.getAttribute("JSON_MAP");%>.
And that’s it — your structure has been transfered from Java to JavaScript with no fuss nor muss.
The Json-lib jar is actually quite small. But the bad thing is that it has quite a few dependencies that their page doesn’t explain well and that doesn’t come with the download. I needed to grab commons-beanutils, commons-logging, and ezmorph to get things rolling with Json-lib. But it has sure been worth it.
[1] Then should it be called AJAJ?
Mixing inline JSP code with JavaScript
Before I get started, let us all repeat three times: “Inline JSP is evil.” Now that we are all on the same page . . .
I’ve inherited a project that has a ton of JSP’s with inline code. This code specifies a lot of logic and decisions that, frankly, should be back at the Action. If that wasn’t bad enough, a lot of the JSP code is embedded in JavaScript functions. Huh? JSP helping generate JavaScript? Yessir. And it’s worse than you think.
In my ideal world, this would be the only way that JSP code would integrate into JavaScript:
foo(< %=varValue%>);
Then the function foo would with whatever the value of varValue would be.But, hey, that’s ideal. I could live with the following (think of this in the middle of a JavaScript function):
var newVar = < %=varValue%>;
That’s just copying the value of a Java object to a JavaScript object. Not great, but livable.The following isn’t as good as the above, but I can deal with it:
var newList = < %=varValue%>.split(",");
A cheap and dirty way to to make an array, huh? However, the next one should never be allowed:
< % for (int i=0; i<=sArray.length; i++) {
if (sArray[i]!=null) { %>
myArray[< %=i%>]=< %=sArray[i]%>;
< % } %>
< % } %>
I’m trying to figure out why anyone would think that mixing Java loops and if-statements in the middle of JavaScript function is a good idea. It’s not readable, it’s not changeable, and it will break when the next person edits that function. C’mon. Really.
This is why JavaScript is becoming my 2007 Language of the Year — because things like the above need to die.
JavaScript: my 2007 Language of the Year
I’m sure many of you have heard of the “Language of the Year”:http://www.pragmaticprogrammer.com/loty/ idea. I’ve done it in the past, with minor success. I usually get caught in other things by May or June and soon forget about it.
I didn’t plan on JavaScript being my LotY — it just sort of happened. At my new position, I’m surrounded by poorly written JavaScript that is interfacing with different web pages. Changed that JavaScript code has been a pain. I’ve searched around the web for different help and caught some ideas – some good and some bad. I then have spent some money on _JavaScript: The Definitive Guide_ and that has gotten me to see JavaScript in a whole new light. I’m beginning to see what Blaine told me once — it’s really just Lisp.
Anyway, here is a function that does my normal first task when learning a new language — the “The Sieve of Erastothenes”:http://mathforum.org/dr.math/faq/faq.prime.num.html . This was harder than it has been in other languages I’ve recently learned because JavaScript does not have a built-in method on the array that returns the index of an object in the array. So the code was a bit longer, but somewhat more interesting. This worked fine in “Spidermonkey”:http://www.mozilla.org/js/spidermonkey/ .
function sieve(upper) {
var primes = new Array();
var composites = new Array();
for (var num=2; num< =upper; num++) {
var found=false;
for( var i in composites){
if (composites[i]==num) {
found=true;
break;
}
}
if (!found) {
primes.push(num);
for (var k=num*2; k<=upper; k+=num) {
composites.push(k);
}
}
}
print(primes);
}
