Archives

My Entire Ebook Library Online

Oh… you didn’t think that title meant I was providing my entire ebook library to the public, did you? Sorry. Thar be no pirates here, maties. This is more of a tutorial on how I make my ebook library available to me. Onward, hoe!

I love using Calibre to maintain my ebook collection. If you have not heard of it and would like more control over your ebooks, then you should take a look. Calibre also has a built in server that you can access via a web browser. This allows you to browse your ebook collection and download them if you like. This is a really neat feature, except that the software has to be running in order to access it.

What I really want is to be able to publish this library and be able to access it from anywhere without any dependence on my computer and the Calibre software running.

This is my solution.

Continue Reading

Java’s Assimilation Into Oracle

Whenever I’m working with Java code I have a habit of opening up a web browser and going to http://java.sun.com immediately. From there I select the Java API Reference and away I go writing Java code. I did the same thing today and was a bit shocked that the Oracle brand has completely infiltrated the Java site. I knew Oracle had acquired Sun, but it wasn’t until today that I really looked at that reality.

Not that it matters much, but I thought it was interesting.

Bash Script Naming – Say My Name, Say My Name!

I use Java as my main Linux server programming language these days and so I usually have a Bash script to go along with each program. The Bash script help encapsulate creating the proper CLASSPATH needed to run the Java program, calls the correct Java class and also passes through any command line parameters that I might need. Recently I had several Java programs that I wrote and bundled inside the same jar file which I needed to call separately. They had all of the same CLASSPATH and environment requirements, so why should I create separate Bash scripts for each one?

What’s In A Name?

Instead of a separate Bash script for each Java program I used a neat little feature of Linux called symbolic linking. I create in a “bin” subdirectory my main Bash script and then link to it using distinct names from the main program directory. The script then detects what names is being used to call it and executes the appropriate Java class. Some of the Bash code looks like this:

Notice the $@ command line parameter in the call to the Java class? That simply passes on the command line parameters used to call the script to the Java program.

So you can see, if I had a Java program in the com.franzone package who’s class name was TestOne, then I could create a symbolic link to my JavaCaller script to call it like this:

ln -s bin/JavaCaller TestOne

Now from the main directory I could call the ./TestOne script (symbolic link) which would actually execute the JavaCaller script with the TestOne name. That would cause my Bash script to call the TestOne Java class. Cool, eh?

Calling A Stored Procedure In Java With A Blob Field

Here is the situation. You are writing a program in Java. You need to access data in a Microsoft SQL Server via stored procedure and the data has an XML field. A what field? SQL Server 2005 introduces a new datatype for XML. It is essentially a blob field that you can perform XML operations on. For the Java program though, it doesn’t really care about that functionality and can just access it to write it out to a file (or process it inline).

JDBC Support

First off you are going to need to check your JDBC driver documentation thoroughly for which operations it supports. There also seems to be some variation in the actual implementation. For example, I tried used Microsoft’s own JDBC driver in my project but it caused a strange SQL Server error to be thrown when writing back to an XML field. I could not get around it (at least not in a timely fashion) so I switched to using the JTDS JDBC driver (go open-source!).

The Stored Procedure

Let us say that the stored procedure in SQL Server looks something like this:

Nothing major going on here, it just executes a SELECT statement against a table and returns four fields. The “LargeXMLField” column is the one we are looking at as a Blob (actually a Clob).

Writing The Code

The actual code to access the stored procedure should be nothing new and is not complicated at all. First we need to connect to the database in the standard fashion.

You will want to substitute real values in where the [host], [port] and [database] placeholders are in the database url.

Next we will use the CallableStatement object to execute the stored procedure.

So you see we use the proc.getMoreResults() to iterate through multiple ResultSet objects that may be returned from a stored procedure. We then call proc.getResultSet() to retrieve the ResultSet that has our data. We fetch our data field as a Clob object and use it’s getAsciiStream() method to get an input stream to read from. After that reading from the stream and writing out to another stream is a straightforward task.

WordPress Posting Via Java – Part 2

In the first part of this series titled “Would You Like To Post To WordPress From Java?” I introduced you (hopefully) to the XML-RPC mechanism that WordPress implements. I also showed how it is quite simple to post to a WordPress blog using Java and the Redstone XML-RPC API. In this installation I’d like to expand the example a bit further.

First, however I’d like to apologize for posting non-working code. 🙁 I posted the example without actually compiling and/or testing it. If you downloaded and fixed the errors to get it to compile you also probably noticed that the method would not successfully login to your blog. Doh! I’ll be fixing those errors in the next version of the file, so hopefully you will walk away with something you can actually modify to your liking… and use.

Now, how about that last example? Did you notice anything lacking in the data that we posted. If you recall the method signature looks like this:

This is actually part of the problem with the first implementation. Looking into the xmlrpc.php file, the method starts looking for the blog_ID at array index 1… not zero. So the method signature should actually be like this:

I’m not sure what emptyData is supposed to be or even why it is there, but I just posted an empty string here and everything was great. The method doesn’t actually ever use that argument… odd.

Ok, back to the initial question… do you notice anything lacking? Sure, a lot of things are missing. Mainly and most blaring is the lack of a title. In the above and previous example all we are posting is the content. That’s no good. I would like to at a minimum be able to specify the title of my post. For that we will take advantage of the metaWeblog API calls. There is a similar function with the following signature:

First, this method does NOT contain the empty data argument. Secondly you’ll notice that instead of passing a string for content, we are passing a struct. So what type of data might this struct contain? It contains: title, link, description, author, category, comments, enclosure, guid, pubDate and source. Wow, that’s a lot more information! I won’t be covering all of these different data elements, but you can read more about them on the RSS 2.0 Specification on the Harvard Law IT Page. There are actually a few more elements than listed here which we will make use of in an upcoming post. 🙂 For now, however I am content to just have the title.

We’ll need to change the method we are calling:

We’ll need to create our content struct, which will be created using a

. Don’t worry, the Redstone serializers will convert this into the XML that we need. Nice!

Then finally there is the method call:

That’s it! Just a few simple changes to our original example (plus the bug fixes) and we now have a title. You can also play around with adding other elements such as a Category. Here is the working file if you would like to download the source.

XmlRpcPoster.java (1.68 KB)

XML-RPC Java Sample: Sorry About The Formatting

I just installed the WP-Syntax WordPress plugin for the last post. This was so I could have my sample Java code formatted and syntax highlighted in the post. Wow! I didn’t realize how bad it was going to be formatted. The plugin is nice and the syntax highlighting is good, but the code is formatted so that it runs off the page. Sorry… mah bad! This is probably due to the fact that it is in a <PRE> tag and so does not wrap any. You can still copy-n-paste the code from the blog entry to view it better.

So I figured I’d offer the code sample as a download for convenience. Again, sorry about that.

XmlRpcPoster.java (1.38 KB)

Would You Like To Post To WordPress From Java?

I use and love the WordPress platform for this and several other blogs that I maintain. One of the many cool features of WordPress is that it supports XML-RPC. What does that mean? Well XML is… XML. It’s the extensible markup language and is basically just a way of formatting data in some meaningful way. RPC stands for remote procedure call. That is making calls to methods/procedures on a remote system. Put them together and you get the idea… making method/procedure calls on remote systems using XML to bundle up the method calls and returns.

Great, now I have to write some sort of custom program to build an XML formatted message and submit it to WordPress. Then I have to have the same program be able to parse and decipher the return XML format. Well, no you don’t! There are already libraries that will do this for you. WordPress supports the Blogger API, the metaWeblog API and the MoveableType API. These are blog specific APIs that use XML-RPC to communicate. You could very easily download one of these, read the documentation and become productive with Java in no time at all. That was actually my first step. But wait… the APIs don’t support all of the same methods. Which one should I use?

I actually decided not to use any of the APIs mentioned above. Instead I went with the Redstone XML-RPC library. This API provides a layer of abstraction from all of the XML-RPC details that is nice and it also gives me the flexibility to implement any methods that I want. This will come in handy as you will see in a later post. For now though, how might I make a post using this API? Simple really. We’ll start out with the Blogger API method call. The method signature looks like this:

blogger.newPost(blog_ID, user_login, user_pass, content, publish)

The blog_ID is used for systems that maintain multiple blogs. There is actually a method we could call to get that piece of information called blogger.getUsersBlogs. Here’s a little secret though… WordPress doesn’t currently support this feature and is just hard-coded to be “1”. So for brevity sake I’ll just use that for now.

Wait a minute… there is no XML code anywhere in your example. Exactly! The Redstone API uses serializers to take the Object[] you provide and create the appropriate XML. Spiffy, eh? Not only that, but if the return type of the method call happens to be a struct, then the token will contain a Map that you can iterate over and retrieve values from.

I hope you enjoyed this little excursion into XML-RPC and Java. Stay tuned for my next post where I’ll be kicking this example up a notch to get more functionality and meta-data into our WordPress posting.