Archive for January, 2008

Print a PDF Document in Java

For some time now I have searched for a way to print a PDF document from Java code. Up to now, I have had to shell out and invoke Acrobat from the command line but this hack has been painful to support in the field because of the multiple version of Acrobat and its quirks.

A new PDF Renderer project has recently been released on java.net which can in addition to rendering and viewing a PDF document, it can be used to print a PDF document. If you download the PDF Renderer you can run the jar to start a sample PDF viewer application which can print PDF documents. All the code listed below is inspired from the PDF Renderer sample application.

The following code uses classes from the PDF Renderer library and standard Java. Like usual, I have omitted all import statements.

One critical class required to print a PDF document using PDF Renderer is the PDFPrintPage. The PDFPrintPage implements the Printable print method. The PDFPrintPage is included in the PDFRenderer.jar, but in the source distribution it is found under the demos project. I have included the source to PDFPrintPage print method here…

A final note, PDF Renderer does require Java 1.5 or greater.

Technorati Tags: , , , , , ,

Accessing Local Files With Mozilla XPCOM

A few months back I took a look at TIBCO General Interface, an AJAX/Web Development framework with an IDE/Builder than runs on your browser. The one thing that amazed me about General Interface was that it saved files to your local disk drive from the browser.

Earlier today I was experimenting with TiddlyWiki, a wiki that is self contained in one file and noticed that it can also save files to your hard drive from the browser. TiddlyWiki is a entire wiki system written in HTML, CSS, and JavaScript and self contained in a single HTML file. When you make a wiki edit, TiddlyWiki writes and saves the change to your local disk drive. To learn more about how to go about doing this I took a peak at the page source code, which was like opening a can of gummy worms.

As you can imagine, saving a file to the local disk from the browser is not a standardized process. IE uses ActiveX controls to go about doing this, will FireFox uses XPCOM objects. In this article I will explore getting at local files from Firefox using XPCOM.

XPCOM is Mozilla’s Cross-Platform Component Object Model used in FireFox. Many of FireFox’s services and component objects, such as your bookmarks, are available from through XPCOM. XPCOM authors can create components in either JavaScript or C which can then be added onto Mozilla as an extension. FireFox by default has a large set of core XPCOM services and objects. Once these components are available in your browser you can make use of them in JavaScript. To better illustrate this we will use the local file component in Firefox to read data from your local disk.

To get a reference to a XPCOM object you need a contract ID, which is like a registry entry string identifier for the object component, and an interface for that object. The XPCOM interface is just like a Java interface in that it defines methods and can inherit from other interfaces.

Some components, such as the local file need special privileges, since you wouldn’t want any web page to read and write files on your local system. To enable the privileges you need to invoke the following JavaScript code in your script which asks the end user for these privileges.

Now that we have the component contract id, interface, and the right permissions we can create a file object.

For the most part, a local file object is just a reference to a file. The nsILocalFile interface has methods that you would expect from a file reference such as append, create, copyTo, exists, isWritable, isReadable, isDirectory, isFile. The nsILocalFile interface also has attributes such as leafName and fileSize.

Once the file object has been created you can use any of the methods defined in the interface. The first method we should use is the initWithPath which set the path to the file.

Now that the file has been created and initialized properly, we can find out the file size and if it is a file or directory with code like the following.

In this article we just scratched at the surface of what you can do with XPCOM, in the next article I’ll go over the code to read and write bytes to a file using a file input/output stream. If you can’t wait ’till next time, just view the source code of TiddlyWiki….

Technorati Tags: , , , , , , , ,

The Great Grails Switch

The Apple Switch Ad campaign was very successful for Apple, so much so that there seems to be a sort of switch meme campaign amongst Grails folks to try to get Ruby on Rails developers to switch to Grails. Originally this blog post presented 10 reasons to switch from Rails to Grails and now Graeme Rocher of G2One has added another ten. Here is the complete list of reasons to switch to Grails… or at least top reasons to give Grails a try…

  • GORM with hibernate/spring and jpa is much better than ActiveRecord
  • No distribution problems; runs in many production ready containers
  • Internationalization out of the box (not specifically ignored as DHH does)
  • Transactions actually work, and they include save-points.
  • Not only dynamic finders and counters, but dynamic listOrderBy
  • No green threads (although this may be fixed in Ruby 2.0, around 2010?)
  • Ability to use pessimistic locking out of the box
  • Real-live prepared statements and .withCriteria method
  • Production level test reporting with built in Mocking and Stubbing
  • Search operations are based on Lucene (with a plugin)
  • A view technology that doesn’t suck
  • Mixed source development made easy with the Groovy joint compiler (no falling back to C to solve those performance problems ;-)
  • Built in support for rich conversations with Web Flow
  • A rich plug-in system that integrates Grails with technologies Java people care about like GWT, DWR, JMS etc.
  • A buzzing and growing community with a larger traffic mailing list as opposed to a stagnating one
  • Built on Spring, the ultimate Enterprise Application Integration technology
  • A Service layer with automatic transaction demarcation and support for scopes
  • More books coming and being adopted by enterprise organizations near you

For the true die hard Ruby on Rails developers this might not be reason enough to switch to Grails, but for those Java developers on that are on the fence Grails might seem a bit more attractive now.

For those interested in getting started with Grails, take a look at my Groovy and Grails tutorials.

Technorati Tags: , , , , , ,

Practices of an Agile Developer - Book Critique

As you may gather from the title, this book explains in detail many of the extreme and agile programming best practices. I think a more suitable name would be Common Practices of a Software Developer as I felt that some of the practices where just common sense advice for a experience developer. I mean, do you need to read two pages to understand to check in code into version control as soon as possible, as soon as you made a fix, every half day if possible but not every five minutes? Well if you do read practice 43, Share Code Only When Ready.

What I liked about this book was that each practice was introduced by a little programming devil that tried to influence the reader into ignoring the practice altogether. This little devil is the one makes us forget to comment our code, that likes to code rather than test. At the end of each practice there is a little angel that describes the benefits of incorporating the practice as a habit into your daily routine, a routine that should include stand up meetings, check out/ins, testing, continuous integration, mentoring, etc.

A book like this might be better suited for a junior developer that does not have experience with extreme programming or any other process for that matter.

Here are some pearls of common sense from Practices of an Agile Developer.

Fix the problem, not the symptom.

The best thing for developers may not be the best for users, and vice versa.

Most users would rather have good software today than wait for superior software a year later.

Having short iterations and smaller increments helps developers stay focused.

Unit testing is an investment. Invest wisely. Testing accessors or trivial methods is probably not time well spent.

Here are some common sense practices I adhered too.

There is more than one way to do it, but do it how more than one developer can understand it.

Design a software solution for the end user, not for a fellow developer, or worse yet your database.

If the end user can’t use your software, that’s a bug.

You may consider your code as art but no one is going to hang your code in the Louvre.

Eat fruit and vegetables before you program.

Technorati Tags: , , , , ,

Ruby on Rails Radioactive Fallout

Rails has been getting a lot of flack lately, instead of the usual hype. Zed Shaw, author of Mongrel, recently blew his top off against the Ruby on Rails community leaders and said that Rails is a ghetto. Zed is getting a lot of attention from this explosive rant but this is not the first time a prominent developer/blogger has spoken out against the Rails framework and it’s leadership. Before Zed went AWOL on the Rails community, Derek Sivers of CD Baby switched back to PHP after working with Rails for two years without releasing a product. Perhaps the first notable negative article regarding Rails’ performance was a five question interview with Twitter developer Alex Payne. Since then there has been many more potty mouthed articles against Rails and its infallible dear leader, better known by his initials DHH, such as Imploding Rails, Jesus DHH, and the Uncle Ben Principle, 10 Areas Where Rails Fails, Ruby, Rails and RSpec burned a day off my lifespan, and How to ruin a Rails project.

So why all the Rails bashing? Most Rubyist and Railist will brush off and dismiss such comments as coming from Java playa hatin’ developers, but this is not the case, these articles are written from the perspective of fellow Ruby on Rails hackers, developers, and contributers. The experience of these Ruby on Rails developers remind me of the testimonials of people that escaped from a Peoples Temple cult; they been to the other side and it is not as green as it was portrayed, especially if you do not adhere without question to the opinionated papal bull-shit of the core Rails church leaders. I think some of the comments that Rails nay sayers are trying to address is that the Rails community is itself falling into the trap of eating it own dogma. But I am not a Rails heretic, so instead of paraphrasing, let me quote some of the most interesting remarks from the aforementioned articles.

Any discussion about Rails with ultimately lead to it’s performance.

Running on Rails has forced us to deal with scaling issues - issues that any growing site eventually contends with - far sooner than I think we would on another framework.
Five Question Inverview

None of these scaling approaches are as fun and easy as developing for Rails. All the convenience methods and syntactical sugar that makes Rails such a pleasure for coders ends up being absolutely punishing, performance-wise.
Five Question Inverview

Consider that Rails is the Love Child of Web 2.0 and Disillusioned Web 1.0 Developers, whole shops and corporate departments are adopting Rails as a way to push their applications into production more rapidly - what happens when they try to scale these apps and get “Twitter’d”?
Imploding Rails

The main Rails application that DHH created required restarting ~400 times/day. That’s a production application that can’t stay up for more than 4 minutes on average.
Rails is a ghetto

The greatest thing about Ruby on Rails is neither Ruby nor Rails, the best aspect of Rails is that it questioned the ‘best practices’ (or worst nightmares) of the current state of web development with its philosophy of Convention over Configuration and Don’t Repeat Yourself principle. Ruby and Rails is not the next best thing, it is just the missing link to the next best programming language and framework, or at least this is the logic of some PHP developers…

Is there anything Rails can do, that PHP CAN’T do? The answer is no.
7 reasons I switched back to PHP

Rails was an amazing teacher. I loved it’s “do exactly as I say” paint-by-numbers framework that taught me some great guidelines. … But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because he’s a better programmer now!
7 reasons I switched back to PHP

Zed’s great rant opus against the Rails elite provided insight from an insider’s point of view, although he complains as he was an outsider. Zed did not mince words and especially goes nuclear against ThoughtWorks and Dave Thomas.

This is exactly what makes Rails a ghetto. A bunch of half-trained former PHP morons who never bother to sit down and really learn the computer science they were too good to study in college.
Rails is a ghetto

They [Rails community] were all a bunch of little ghetto fabulous princesses trying to make it in this tiny little Rails world and not enough brains between them all to make it happen.
Rails is a ghetto

When you combine stupid businesses with stupid people using a stupid framework based on a big fat fucking lie on a shitty platform you get the perfect storm of dumbfuck where a man like me can’t find work.
Rails is a ghetto

Sorry guys, but having a 1:4 code:test ratio is not focusing on code quality. It’s focusing on test quality.
Rails is a ghetto

He [Dave Thomas] basically had full access to the code and the people who wrote Rails and a direct line to DHH and still couldn’t write a decent book on writing a Rails application.
Rails is a ghetto

Technorati Tags: , , , , , , , , ,

Perforce Admin

Recently I had to do some light Perforce administrative management tasks, such as creating new users and setting up their privileges. If you like to create a new user just invoke the following Perforce command from the command line.

p4 user -f username

If you try to connect to Perforce with the new user you might get a warning message that reads something like: Access for user ‘username’ has not been enabled by ‘p4 protect’. You need to explicitly give the user permissions or add the new user to a group that has the corresponding permissions. To add a user to a group simply execute something like the following command as the Perforce administrator.

p4 group groupname

You would replace groupname with the correct name of your group. The above command will open your default Perforce editor, most likely Notepad, with the group description, you can simply add the new user in the users list. The user will inherit the permission from the group.

You could have also giving the user permissions by editing the Perforce protect file. To edit the p4 protect file simply execute the following command as the super user.

p4 protect

Technorati Tags: , , ,

Next Page »