Archive

Archive for the ‘code’ Category

Ruby Timeout::Error

March 6th, 2008 osterday 4 comments

I wanted to post this before, but got busy and now I can’t find the original post about this, but anyway…

When doing network stuff in Ruby – using any Net:: libs – there’s a little trick to trap for timeouts. You can’t simply use “rescue” do handle it. The timeout error is not a subclass of the standard error, so you have to explicitly trap for it:

begin
# Do network stuff
rescue Timeout::Error
# Timeout error
rescue
# Other error
end

Categories: code, ruby Tags:

EJB3 and MyEclipse

March 31st, 2006 osterday Comments off

Since MyEclipse doesn’t natively support EJB3 (yet), you need to include the EJB3 libs in your project – I’m using JBoss, so I’ve got the JBoss EJB libs. There’s great post in the MyEclipse forums on getting it set up. I added a few posts over there on which libs I needed – but here’s my screenshot for reference. (Technically you only need Eclipse, but I use MyEclipse and think it’s worth the little bit of cash.)

And to use MySQL for the tutorial:

@GeneratedValue(strategy = GenerationType.AUTO)
and remove the @SequenceGenerator annotation – that worked for me.

Here’s another great EJB3 tutorial.

Categories: code Tags:

EJB3

March 24th, 2006 osterday 1 comment

I think I’ve mentioned before that over the past two years or so I’ve really been getting into Java and J2EE development. I’ve got a cool app running on JBoss using EJBs, Quartz for scheduling and MySQL for the database. Pretty stable too in the year or so it’s been in production. Most of the “good” stuff in our TF platform is running on JBoss now too – not really using the J2EE features yet since there’s so much old code still there, but making progress. Anyway…

I just started playing around with JBoss’ EJB3 stuff and I’m lovin’ it! I didn’t mind EJB 2.1 since I have MyEclipse and XDoclet to easy the amount of code I had to write, but this is much better! I’ve only just done a few small demo apps, but so far it’s really great.

Want to listen to a great podcast with Gavin King (of hibernate fame) discussing EJB3 and JBoss’ new Seam product, check out the Java Posse’s interview with him!

Categories: code Tags:

Going down the wrong PATH

October 19th, 2005 osterday 1 comment

For the most part, I love my development environment. I really like the J2EE platform and using MyEclipse/Eclipse/Subclipse/JBoss/Subversion is a great combination and works really well most of the time.

Today I had one of those user errors that is a pain to track down. I redeployed a custom J2EE app to our production JBoss server that I just updated and suddenly JDom breaks. Worked fine on my local box. Scratch head…

So I start by “cleaning” my local projects… Now my JSPs are coming up saying “bad class file” when referring to my classes included from my other project. Scratch head…

Tried a bunch of different things… Bang head against wall…

Finally after hours of searching the web and recreating the projects, etc… I come across a post that mentions that the JSPs use javac from the path…

Crap! I had put the old 1.4 jdk/bin directory in my path to make it easy to run javac before I was using Eclipse. So now that I’m using 1.5 (aka Java 5) the JSPs were still being compiled with the 1.4 javac causing my problem.

Once I removed the 1.4 reference in my XP environment vars the JSPs compiled fine.

Of course I’m still getting the originally JDom error on production, so I still have to track down what’s causing that! Doh!

References: Bad class file, Error running javac

My JDom error is this:

org.jdom.Element.addContent(Lorg/jdom/Content;)Lorg/jdom/Element;; nested exception is: java.lang.NoSuchMethodError: org.jdom.Element.addContent(Lorg/jdom/Content;)Lorg/jdom/Element;

I’m including JDom 1.0 jar in the EAR file and I don’t see any other jdom.jar files in the classpath. Ideas? Oh, production is using JBoss 4.0.1RC2 and my laptop is using 4.0.3. I found this post, but I’m pretty sure I’m using JDom 1.0 for compiling and running!

UPDATE: After upgrading our production box to JBoss 4.0.3 and JDK 1.5.0 (update 5) the JDom errors went away.

Categories: code Tags:

Cool regex stuff…

June 28th, 2005 osterday 1 comment

I was playing around with some regex (regular expression) stuff today and learned about “lookahead” and “lookbehind”.

This expression (?<=\{\{).+(?=\}\}) will return all the characters, numbers and spaces between two curly brackets, but not the brackets themselves.

For example, the string This is {{a test}} of regex returns just a test when the regex is applied.

I also found the Regex Coach which was a great help in debugging the regular expressions.

Categories: code Tags:

So Ajax it is…

March 18th, 2005 osterday Comments off

I guess I’ll have to accept “Ajax” as the official term of the new breed of webapps. In Monday’s Wall Street Journal, Lee Gromes posted an article in the Marketplace/Portals section called “Google Pioneers Use Of Old Microsoft Tools In New Web Programs” and the first two word are “Meet Ajax”. So thank you very much Mr. Jesse James… Garrett, that is.

How about JNX, pronounced jinx for Javascript aNd Xml? Ok, not the greatest, but that’s what I came up with of the top of my head…

Anyway, in the WSJ article, Gromes says the losers are Sun and Microsoft. There is no need to compare Javascript and Java on the client – we all know applets are pretty much dead from a mass market standpoint – but Java on the server is still going strong and Ajax apps can push that market. Microsoft missed the boat on Ajax – why didn’t they push it more? Why did they push ASP.NET and server side rendering of GUI controls? I’m not a ASP.NET programmer, but when I first looked that some of that stuff, I was really confused about what Microsoft was doing.

Whatever it’s called, it’s good stuff! I don’t care about trying to support browsers that are ancient. With CSS, Ajax and Firefox or IE, you can do quite amazing things for the user. (I’m not sure about all the accessibility stuff though.) I’m interested in stuff like Dojo and other things to work with client side rendering – versus server side MVC stuff like Struts. I should mention that I don’t usually do “web sites”, but “web apps” – that can make a difference.

I’m working on making my iTunes now playing feature auto update! Not brain surgery, but still cool!

UPDATE: I wonder if a law suit could ensue? Colgate-Palmolive’s Ajax?

Categories: code Tags:

Google Suggest

January 12th, 2005 osterday Comments off

This post is just in order to clean out some bloglines links that I’ve “saved for later” – which is getting a little out of control!

From a post from the guy who came up with Google Suggest:

The project stemmed from an idea I had a few months ago, and since then I’ve been working on it in my 20% time, which is a program where Google allows their employees to devote 20% of their working hours to any project they choose. What’s really amazed me about this project is how in a matter of months, working on my own, I was able to go from a lunch table conversation to launching a new service. In my opinion, this is one of the things that really makes Google a great place; that the company’s systems, resources and, most important, people are all aligned to make it as easy as possible to take an idea and turn it into something cool.

Some info on how it’s doneXMLHttpRequest. More on auto complete.

UPDATE: More info on how it’s done: Google Suggest Dissected, Part II (Slashdot) and the direct link.

I’ve posted before about XMLHttpRequest and it’s possibly one of my favorite things in the coding world – so I guess I can’t hate Microsoft too much since they’re the ones that first implemented it! I’m just really glad that Mozilla choose to incorporate it too!

Categories: code Tags:

iTunes and a “Now Playing” feature

December 29th, 2004 osterday 3 comments

[UPDATED - see below!]

Here’s some cool code I hacked together from the iTunes Windows COM SDK docs and some other sites – Dan Crevier’s Blog and ajc.

I had already posted how I did something like this in WinAmp, but I’ve been using iTunes a lot now and really like it. There are a few plugins and such out there to post the current track info to a file or site, but the main thing other scripts didn’t do was to post the stream info if iTunes was playing a stream.

Basically it’s some JavaScript that runs in the background listening for events from the iTunes applications. Currently I’m just using OnPlayerPlayEvent and OnQuittingEvent, but some of the other events might be cool to use. I just create a little XML and post it to the web server – this one’s real time, where my WinAmp one was polled.

I don’t know how good or efficient this code is, but it hasn’t crashed my box yet! I also am posting a simple cgi I hacked up in Perl to snag the bits of XML and output an HTML file that is included in my main page via SSI.

I just start iTunes, then run my script. When I exit iTunes, the JavaScript code exits as well – and it tells you it’s closing.

iTunes JavaScript code: (I think I got the html escaped – I need a better way to post code!)

var iTunesApp = WScript.CreateObject("iTunes.Application");
var iTunesIsRunning = true;

function ITEventTest_OnPlayerPlayEvent(iTrack) {
   getCurrentTrackInfo();
}

function ITEventTest_OnPlayerPlayingTrackChangedEvent(iTrack) {
   getCurrentTrackInfo();
}

function ITEventTest_OnQuittingEvent() {
   iTunesIsRunning = false;
}

function getCurrentTrackInfo() {
   var current = iTunesApp.CurrentTrack;
   var stream = iTunesApp.CurrentStreamTitle;

   var trackType = "";
   var trackName = "";
   var trackArtist = "";
   var trackAlbum = "";

   if (stream != "") {
      streamURL = iTunesApp.CurrentStreamURL;
      trackType="stream";
      trackName=stream;
      trackArtist=current.Name;
      trackAlbum=streamURL;
   } else {
      trackType="file";
      trackName=current.Name;
      trackArtist=current.Artist;
      trackAlbum=current.Album;
   }

   var response = postTrack(trackType, trackName, trackArtist, trackAlbum);
}

function postTrack(trackType, trackName, trackArtist, trackAlbum) {
   var xmlTrack = new ActiveXObject("MSXML2.DOMDocument.4.0");
   xmlTrack.loadXML(’<?xml version="1.0" ?><track/>’);
   var root = xmlTrack.documentElement;
   root.setAttribute("type", trackType);

   var textNode = xmlTrack.createElement("name");
   textNode.text = trackName;
   root.appendChild(textNode);
   var textNode = xmlTrack.createElement("artist");
   textNode.text = trackArtist;
   root.appendChild(textNode);
   var textNode = xmlTrack.createElement("album");
   textNode.text = trackAlbum;
   root.appendChild(textNode);

   var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP.4.0");
   xmlhttp.open("POST", "http://[cgi url]",false);
   xmlhttp.send(xmlTrack);
   if (xmlhttp.status != "200") {
      WScript.Echo("Error posting to server. Code is: " + xmlhttp.status);
   }
}

WScript.ConnectObject(iTunesApp, "ITEventTest_");
WScript.Echo("Waiting for changes…");
getCurrentTrackInfo();

while (iTunesIsRunning) {
   WScript.Sleep(1000);
}

var response = postTrack("OFFLINE", "offline.", "", "");

WScript.DisconnectObject(iTunesApp);
WScript.Echo("iTunes was closed, iTunesListener exiting.");

Perl CGI on the server: (Beware: I’m not a great Perl coder!)

#!/usr/bin/perl

use XML::LibXML;

local $/;

my $parser = XML::LibXML->new();

my $request = <STDIN>;
my $xmlRequest = $parser->parse_string($request);

my $root = $xmlRequest->getDocumentElement;
my $type = $root->getAttribute("type");

my @nodes = $root->findnodes("name");
my $name = $nodes[0]->string_value();

@nodes = $root->findnodes("artist");
my $artist = $nodes[0]->string_value();

my @nodes = $root->findnodes("album");
my $album = $nodes[0]->string_value();

open(file,">[path writable by web server]/current_song.html");
print file $name;
print file "<br />";
print file $artist;
print file "<br />";
print file $album;
print file "\n";
close(file);

print "Content-type: text/html\n\n";
print "ok";

And the simple Server Side Include in my html:

<!–#include virtual=”../includes/iTunes/current_song.html” –>

Works for me and it’s pretty cool hack I think! I’m thinking about some other possiblities… If I can use iTunes to publish a stream, then I could have some playlist posting and even playlist manipulation – that could be cool.

Also I found a somewhat cool plugin for iTunes on Windows called “multi plugin”. I only really use the plugin for controlling iTunes with my laptop’s multimedia buttons when iTunes is minimized – iTunes should automatically do this like WinAmp did. I can’t figure out how to uninstall it though!

UPDATE:

Seems I deleted some code that is needed so that the getCurrentTrackInfo() function is called when listening to a stream. The “OnPlayerPlayEvent” event is not called when the track a stream is playing changes. You need to trap for “OnPlayerPlayingTrackChangedEvent”, so just add this:

function ITEventTest_OnPlayerPlayingTrackChangedEvent(iTrack) {
   getCurrentTrackInfo();
}

I’ve updated my code above to reflect this. The full title of the stream sometimes isn’t present when you first connect – just reconnect if that bothers you.

Also, I need to add some more error trapping too. The script crashes if you start the script and iTunes isn’t playing anything – at least this happens to me!

Enjoy!

Categories: code Tags:

When you have to use Windows – GnuWin32

April 15th, 2004 osterday Comments off

I hate to admit it, but I still use Windows as my desktop OS. XP isn’t too bad for me. Sure it has its problems, but it’s what gets the job done. Linux still needs a few more refinements and apps for me to really switch. But I do love the command line! So if you need to use Windows, but want those great utils, head over to the GnuWin32 packages page at SourceForge. These utils make things better, along with WinSCP and PuTTY.

Categories: code Tags:

Java and TextPad

March 19th, 2004 osterday Comments off

It seems that no one’s documented using TextPad’s Workspaces and Java. There’s a lot about using TextPad and Java, but we’ve been using the Workspace feature of TextPad for several years at work and I figured I’d document it (complete with screen shots!) so we don’t have ask each other after a re-install…

Note: Install TextPad after you install the JDK so you don’t have to manually enter the default Java “Tools” commands in TextPad.

Compile Java:

Command: javac.exe
Parameters: -g:none -deprecation -classpath $WspDir $File -d $WspDir
Initial Folder: $FileDir

Run Java Application:

Command: java.exe
Parameters: -classpath $WspDir $Basename
Initial Folder: $FileDir

Run Java Applet: (We don’t really use this too much.)

Command: appletviewer.exe
Parameters: $Basename
Initial Folder: $FileDir

Categories: code Tags: