Posts Tagged ‘Programming’

Spring and Jetty Integration

Wednesday, December 10th, 2008

Jetty is a pretty darn awesome J2EE web container. With amazing features like non-blocking IO, continuations and immediate integration with Cometd - I feel that it is a solid, production ready container.

I hate war files, I hate web.xml files - there’s just way too much black magic that needs to be done to get things up and running. It is nice once someone has done the dirty work, and got the initial web.xml constructed, but I wouldn’t want to be that person who starts it all off.

Oh - another thing - I absolutely LOVE dependency injection. Using the web.xml approach, you’ll almost always have to start off a servlet of some sort to initialise various services that you’ll need - moreover the easiest way to access these services on other servlets is to use singletons, and we all know why singletons are bad!

So, I end up using Jetty in an embedded setup, and used to write various wrappers around the configuration so that I can do most of the common things with minimal code. A good example will be to setup a bunch of contexts and a DefaultServlet for regular file serving. However, the way Jetty is written makes it really easy to be used in Spring - everything is a simple bean with a bunch of setters.

To start off, lets write down the bean. Notice I’ve added an init-method attribute to start(). If you don’t want Spring to kick off your server, then just grab hold of the bean and call start() on it explicitly.

<bean name="WebServer" class="org.mortbay.jetty.Server" init-method="start">
</bean>

Then, lets add some connectors to it:

<property name="connectors">
  <list>
  <bean name="LocalSocket" class="org.mortbay.jetty.nio.SelectChannelConnector">
      <property name="host" value="localhost"/>
      <property name="port" value="8080"/>
  </bean>
  </list>
</property>

You will need some handlers (one of them will be a context handler to serve your servlets). I’ve added a logging handler so that the server logs requests in the same format as apache’s combined log.

<property name="handlers">
  <list>
    <bean class="org.mortbay.jetty.servlet.Context">
      <property name="contextPath" value="/"/>
      <property name="sessionHandler">
        <bean class="org.mortbay.jetty.servlet.SessionHandler"/>
      </property>
      <property name="resourceBase" value="/var/www"/>
      <property name="servletHandler">
        <bean class="org.mortbay.jetty.servlet.ServletHandler">
          <property name="servlets"> <!-- servlet definition -->
            <list>
            <!-- default servlet -->
            <bean class="org.mortbay.jetty.servlet.ServletHolder">
              <property name="name" value="DefaultServlet"/>
              <property name="servlet">
                <bean class="org.mortbay.jetty.servlet.DefaultServlet"/>
              </property>
              <property name="initParameters">
                <map>
                  <entry key="resourceBase" value="/var/www"/>
                </map>
              </property>
            </bean>
            </list>
          </property>
          <property name="servletMappings">
            <list><!-- servlet mapping -->
            <bean class="org.mortbay.jetty.servlet.ServletMapping">
              <property name="pathSpecs">
                <list><value>/</value></list>
              </property>
              <property name="servletName" value="DefaultServlet"/>
            </bean>
            </list>
          </property>
        </bean>
      </property>
    </bean>
    <!-- log handler -->
    <bean class="org.mortbay.jetty.handler.RequestLogHandler">
      <property name="requestLog">
        <bean class="org.mortbay.jetty.NCSARequestLog">
          <property name="append" value="true"/>
          <property name="filename" value="/var/log/jetty/request.log.yyyy_mm_dd"/>
          <property name="extended" value="true"/>
          <property name="retainDays" value="999"/>
          <property name="filenameDateFormat" value="yyyy-MM-dd"/>
        </bean>
      </property>
    </bean>
  </list>
</property>

And thats about it. If you need to add more servlets, then all you have to do is add an entry to ServletHandler’s properties for servlets and servletMappings.

Now, image if I had to get a reference to a DAO, or some other service in my servlet - it’s just going to be a matter of adding a member, exposing it via a setter and whacking in the dependency on the servlet Spring config above. All done in nice dependency injected way. No more overriding init() on the servlet and picking up some context attribute via some magic string. The best part of this

Here’s the whole Spring config - hack away to your needs!

<bean name="WebServer" class="org.mortbay.jetty.Server" init-method="start">
<property name="connectors">
  <list>
  <bean name="LocalSocket" class="org.mortbay.jetty.nio.SelectChannelConnector">
    <property name="host" value="localhost"/>
    <property name="port" value="8080"/>
  </bean>
  </list>
</property>
<property name="handlers">
  <list>
    <bean class="org.mortbay.jetty.servlet.Context">
      <property name="contextPath" value="/"/>
      <property name="sessionHandler">
        <bean class="org.mortbay.jetty.servlet.SessionHandler"/>
      </property>
      <property name="resourceBase" value="/var/www"/>
      <property name="servletHandler">
        <bean class="org.mortbay.jetty.servlet.ServletHandler">
          <property name="servlets"> <!-- servlet definition -->
            <list>
            <!-- default servlet -->
            <bean class="org.mortbay.jetty.servlet.ServletHolder">
              <property name="name" value="DefaultServlet"/>
              <property name="servlet">
                <bean class="org.mortbay.jetty.servlet.DefaultServlet"/>
              </property>
              <property name="initParameters">
                <map>
                  <entry key="resourceBase" value="/var/www"/>
                </map>
              </property>
            </bean>
            </list>
          </property>
          <property name="servletMappings">
            <list><!-- servlet mapping -->
            <bean class="org.mortbay.jetty.servlet.ServletMapping">
              <property name="pathSpecs">
                <list><value>/</value></list>
              </property>
              <property name="servletName" value="DefaultServlet"/>
            </bean>
            </list>
          </property>
        </bean>
      </property>
    </bean>
    <!-- log handler -->
    <bean class="org.mortbay.jetty.handler.RequestLogHandler">
      <property name="requestLog">
        <bean class="org.mortbay.jetty.NCSARequestLog">
          <property name="append" value="true"/>
          <property name="filename" value="/var/log/jetty/request.log.yyyy_mm_dd"/>
          <property name="extended" value="true"/>
          <property name="retainDays" value="999"/>
          <property name="filenameDateFormat" value="yyyy-MM-dd"/>
        </bean>
      </property>
    </bean>
  </list>
</property>
</bean>

del.icio.us:Spring and Jetty Integration  digg:Spring and Jetty Integration  spurl:Spring and Jetty Integration  wists:Spring and Jetty Integration  simpy:Spring and Jetty Integration  newsvine:Spring and Jetty Integration  blinklist:Spring and Jetty Integration  furl:Spring and Jetty Integration  reddit:Spring and Jetty Integration  fark:Spring and Jetty Integration  blogmarks:Spring and Jetty Integration  Y!:Spring and Jetty Integration  smarking:Spring and Jetty Integration  magnolia:Spring and Jetty Integration  segnalo:Spring and Jetty Integration  gifttagging:Spring and Jetty Integration

Related Posts:

Converting PEM certificates and private keys to JKS

Wednesday, December 3rd, 2008

If there is one irritating, arcane issue about Java, it is their SSL and Crypto framework. It is a pile of mess. I remember using openssl as a library about 3-4 years ago in a project that was pretty crypto heavy and their library can be used by any junior developer - it’s that simple to use.

However, Java’s crypto framework is just absolutely irritating to use - tons of unnecessary boiler plate, and not enough of self discovery of file formats (as an example). Try to do SSL client certificate authentication from ground up and you’ll know what I mean. Knife, wrist - sound familiar ?

Last night, I had to convert some PEM formatted certificates and private keys to JKS (was getting SSL nicely configured under Jetty). I remember doing this a few years back and there were molehills mountains of issues to jump across and I did pull my hair out back then. Last night was no different. However, I did manage to solve it and ended up with much less hair.

So, to save everyone else the trouble (and their hair!), I’m jotting down some notes here on how to convert a certificate and private key in PEM format into Java’s keystore and truststore in JKS format.

The Keystore

If we’re starting with PEM format, we need to convert the certificate and key to a PKCS12 file. We’ll use openssl for that:

Remember to use a password for the command below, otherwise, the Jetty converter (the following step) will barf in your face!

openssl pkcs12 -export -out cert.pkcs12 \
  -in cert.pem -inkey key.pem

Once that’s done, you need to convert the pkcs12 to a JKS. Here, I will be using a small utility that comes bundled with Jetty called PKCS12Import. You can download the necessary library (you’ll need the main jetty.jar) which can be a huge download for such a small thing, or just grab the jar from here. Run the following command and use the password from the step above and your keystore password:

java -cp /path/to/jetty-6.1.7.jar \
  org.mortbay.jetty.security.PKCS12Import \
  cert.pkcs12 keystore.jks

The Truststore

Next, you’ll almost definitely need to import the certificate into your truststore whenever you need to do anything related to SSL.

First, export the certificate as a DER:

openssl x509 -in cert.pem -out cert.der -outform der

Then import it into the truststore:

keytool -importcert -alias mycert -file cert.der \
  -keystore truststore.jks \
  -storepass password

And that’s it! You have your key in the keystore, and your certificate in the truststore. Hope this helps some of you out there.

del.icio.us:Converting PEM certificates and private keys to JKS  digg:Converting PEM certificates and private keys to JKS  spurl:Converting PEM certificates and private keys to JKS  wists:Converting PEM certificates and private keys to JKS  simpy:Converting PEM certificates and private keys to JKS  newsvine:Converting PEM certificates and private keys to JKS  blinklist:Converting PEM certificates and private keys to JKS  furl:Converting PEM certificates and private keys to JKS  reddit:Converting PEM certificates and private keys to JKS  fark:Converting PEM certificates and private keys to JKS  blogmarks:Converting PEM certificates and private keys to JKS  Y!:Converting PEM certificates and private keys to JKS  smarking:Converting PEM certificates and private keys to JKS  magnolia:Converting PEM certificates and private keys to JKS  segnalo:Converting PEM certificates and private keys to JKS  gifttagging:Converting PEM certificates and private keys to JKS

Related Posts:

Python for IntelliJ

Saturday, November 22nd, 2008

Jetbrains have released the long awaited Python plugin for IntelliJ. I had a test drive with it, and all looks pretty much OK as a first cut version. I’m sure there will be a ton of added features in the coming month. Having said that, it is quite usable at the moment. There is no debugger for now and I wouldn’t use it for  complex, mature projects as yet. But kudos to them! It’s a very good start!

Source http://xkcd.com/353/

del.icio.us:Python for IntelliJ  digg:Python for IntelliJ  spurl:Python for IntelliJ  wists:Python for IntelliJ  simpy:Python for IntelliJ  newsvine:Python for IntelliJ  blinklist:Python for IntelliJ  furl:Python for IntelliJ  reddit:Python for IntelliJ  fark:Python for IntelliJ  blogmarks:Python for IntelliJ  Y!:Python for IntelliJ  smarking:Python for IntelliJ  magnolia:Python for IntelliJ  segnalo:Python for IntelliJ  gifttagging:Python for IntelliJ

Related Posts:

Follow up on XP-Dev.com release

Thursday, November 20th, 2008

A quick follow up post on the recent release of XP-Dev.com. There were some bugs that needed to be resolved. Some pretty critical ones, while others were just minor annoyances. However, the dust should have settled now. I’m not saying that there aren’t any bugs left, but most have been fixed. Of course, if you do notice anything odd, please raise a support ticket and we’ll resolve it asap.

I decided to take it easy the past couple of days, as I’ve been working on XP-Dev.com like a maniac for the past 4 weeks. Just to give this some context - I began rewriting the whole of XP-Dev.com from 16th October 2008 and released it on 17th November 2008, which is about a months worth of work. I current site has 13k lines of Java (not including unit tests) and 2k lines of HTML/Velocity templates. There’s about 1k lines of XML, but most of that is just Spring configuration (NOT their web framework - I’m only using Spring for IOC), and I had a day job to take care of. There was plenty of refactoring done during this time, and these numbers are only the final figures. All in all - a crazy month, but well worth it!

It is a complete re-write with a whole new custom MVC framework and object relational mapper (ORM) layer. I’m seriously considering releasing the MVC framwork as open source (the ORM needs some work!), which I think is pretty darn good - writing new dynamic pages and forms is a breeze! Now, I’m sure a lot of you might be wondering why I embarked on such an ambitious journey ? Well, stay tuned as I will be posting a series of blog posts on how I re-engineered and re-architected XP-Dev.com, and the reasons behind those decisions.

The new XP-Dev.com has only been released for 3 days and I did get quite a few mails from users who expressed their gratitude. I can’t begin to thank all of you for those kind emails and support tickets - it is really good to know that there are plenty of you out there who are happy users, and that the past month has been well worth it! I’ve got many more ideas to put into XP-Dev.com, so, think of the recent release as the beginning of something new :)

del.icio.us:Follow up on XP-Dev.com release  digg:Follow up on XP-Dev.com release  spurl:Follow up on XP-Dev.com release  wists:Follow up on XP-Dev.com release  simpy:Follow up on XP-Dev.com release  newsvine:Follow up on XP-Dev.com release  blinklist:Follow up on XP-Dev.com release  furl:Follow up on XP-Dev.com release  reddit:Follow up on XP-Dev.com release  fark:Follow up on XP-Dev.com release  blogmarks:Follow up on XP-Dev.com release  Y!:Follow up on XP-Dev.com release  smarking:Follow up on XP-Dev.com release  magnolia:Follow up on XP-Dev.com release  segnalo:Follow up on XP-Dev.com release  gifttagging:Follow up on XP-Dev.com release

Related Posts:

New XP-Dev.com (finally!) Released

Tuesday, November 18th, 2008

Folks, there’s a new version of XP-Dev.com out. This release features a new platform (which I will blog about soon!) and some key features that everyone has been asking for profusely, namely:

  • Subversion imports and exports (tons, upon tons of users have asked for this)
  • Allowing anonymous (public repositories) checkouts
  • Multiuser project and task management (tons of users have asked for this)

There are some obvious bug which I will sort out in the next few days. However, under the new platform, extending and adding more features to XP-Dev.com will be a breeze (and unit tested of course)! There is a whole lineup of features coming up, and will keep everyone posted about it. These are exciting times for XP-Dev.com and we really appreciate all the support that’s been given to us.

I personally would love to see XP-Dev.com being the best agile tool out there, and we’ll get there!

If you’re a current user, give it a whirl - any feedback will be great - good and bad!

If you’re a new user - register now and see what it can do to improve your development deliveries.

del.icio.us:New XP-Dev.com (finally!) Released  digg:New XP-Dev.com (finally!) Released  spurl:New XP-Dev.com (finally!) Released  wists:New XP-Dev.com (finally!) Released  simpy:New XP-Dev.com (finally!) Released  newsvine:New XP-Dev.com (finally!) Released  blinklist:New XP-Dev.com (finally!) Released  furl:New XP-Dev.com (finally!) Released  reddit:New XP-Dev.com (finally!) Released  fark:New XP-Dev.com (finally!) Released  blogmarks:New XP-Dev.com (finally!) Released  Y!:New XP-Dev.com (finally!) Released  smarking:New XP-Dev.com (finally!) Released  magnolia:New XP-Dev.com (finally!) Released  segnalo:New XP-Dev.com (finally!) Released  gifttagging:New XP-Dev.com (finally!) Released

Related Posts:

More on XP-Dev.com

Wednesday, November 5th, 2008

Doug Bromley has made a comparison list of free Subversion Hosting providers on his blog, and XP-Dev.com is listed there (thanks Doug)! If you’re just skimming it, then it might look as if XP-Dev.com is a little “thin” on the features end of it. The odd thing I found was the “Tracking” portion had XP-Dev.com listed as a “NO”, which is not entirely true. Having said that, the project tracking portion is not as polished as I want it to be. There are more features in the works, and one of the main ones that most users have asked for is the Project tracking part to be multi-user.

I will keep everyone posted on the progress as soon as I can confirm dates, but it is really really close.

On a side note, I’m going to start a blog for XP-Dev.com at blog.xp-dev.com and wish to invite anyone out there who is enthusiastic about agile programming and software development (and delivery!) in general and can contribute some time to blog on best practices and other fun stuff, do drop me a note and we can discuss specifics!

del.icio.us:More on XP-Dev.com  digg:More on XP-Dev.com  spurl:More on XP-Dev.com  wists:More on XP-Dev.com  simpy:More on XP-Dev.com  newsvine:More on XP-Dev.com  blinklist:More on XP-Dev.com  furl:More on XP-Dev.com  reddit:More on XP-Dev.com  fark:More on XP-Dev.com  blogmarks:More on XP-Dev.com  Y!:More on XP-Dev.com  smarking:More on XP-Dev.com  magnolia:More on XP-Dev.com  segnalo:More on XP-Dev.com  gifttagging:More on XP-Dev.com

Related Posts:

IntelliJ 8

Sunday, November 2nd, 2008

I have blogged in the past on a very sensitive issue - Eclipse vs IntelliJ. I come from the IntelliJ camp where I strongly feel that IntelliJ really improves developer efficiency tremendously. It might be a little opinionated, but here’s the drill: I have been working at my current job (no, its not a career, but that is content for another blog) for roughly 8 months, and I can only use Eclipse, for a multitude of reasons. So, I might be able to claim that I do use Eclipse and IntelliJ a lot.

Here’s what I found when comparing Eclipse to IntelliJ: Eclipse has way too much clutter. It does not refactor right. It is really slow in auto-completion. It just does not offer the right suggestions during auto-completion. The list goes on. It could be that I’m facing these issues because I’m using Eclipse 3.3. I really should upgrade to Eclipse 3.4 and hopefully some of these might go away.

My IDE has to be really fast, and has to be smart enough to give me the right auto-completion. My IDE has to be able to help me develop real quick. Eclipse falls short - way short. I tend to develop much faster (about 2x productivity) thanks to IntelliJ’s pretty smart auto-completion, and somehow the code comes out cleaner, as I don’t have to worry about the IDE screwing up big refactors. I end up spending less time typing, and more time refactoring.

However, the one thing that I always liked about Eclipse is that it supported various other languages and IntelliJ fell short in the category. Well, not for long - IntelliJ is catching up and now with version 8 - there’s a ton of features added with new language support. This list is just the tip of the iceberg on the new features.

Grab your evaluation copy, and try it out. Tips to using IntelliJ properly:

  • Shortcuts, shortcuts, shortcuts! Learn all the keyboard shortcuts. Use the reference card (Windows + Linux, Mac) and memorise it.
  • Stop touching that mouse! Every single time you move one hand from the keyboard to the mouse you have just wasted a few seconds of good coding time. Break this habit.
  • Learn to type fast, without looking at your keyboard.
  • Get in the habit of refactoring. There maybe once, or twice in my 3 years of using IntelliJ that it failed on my when refactoring, and I use that bloody rename shortcut like it is going out of fasion. The more often you refactor, the cleaner your code will become. It will be more maintainable as well.
  • When you’re editing something that is not exactly code, like XML for instance, don’t use notepad or some old crummy text editor! Chuck it into IntelliJ and you might get a suprise that it does auto-completion for you.

Some useful sites to help you get your head around IntelliJ:

del.icio.us:IntelliJ 8  digg:IntelliJ 8  spurl:IntelliJ 8  wists:IntelliJ 8  simpy:IntelliJ 8  newsvine:IntelliJ 8  blinklist:IntelliJ 8  furl:IntelliJ 8  reddit:IntelliJ 8  fark:IntelliJ 8  blogmarks:IntelliJ 8  Y!:IntelliJ 8  smarking:IntelliJ 8  magnolia:IntelliJ 8  segnalo:IntelliJ 8  gifttagging:IntelliJ 8

Related Posts:

Marsenne Prime and Python

Saturday, September 6th, 2008

A couple of weeks ago, I read the Slashdot entry on the discovery 45th Marsenne Prime. In a nutshell, a Marsenne Prime is a prime number that is calculated via: Mn = 2n - 1

This got me thinking - how does one calculate such a large number (the 44th prime has 9.8 million digits). The obvious problem in calculating this number would be the fact that the largest number most compilers and languages support would be an unsigned long long, which is not even close enough to hold such a large number. At this point I recalled reading somewhere that Python has support for handling numbers of any size. I knocked up the following script and ran it on my dev “box”:

#!/usr/bin/env python
import sys

if len(sys.argv) != 2:
	print 'Need power'
	sys.exit(1)

exp = long(sys.argv[1])
print 'Exponent %s' % exp
ans = pow(2, exp)

fp = open('out.write', 'w')
try:
	fp.write(str(ans))
finally:
	fp.close()

print 'Done'

All it does is grab the exponent from a command line parameter and writes the output to a file called out.write. I only cared in calculating the exponent part (2n) as the subtraction is trivial. The processor is a 1GHz Transmeta Crusoe with 256MB RAM. Not exactly the fastest CPU on the block, but it gets the job done as my development machine. CPU details below:

processor	: 0
vendor_id	: GenuineTMx86
cpu family	: 6
model		: 4
model name	: Transmeta(tm) Crusoe(tm) Processor TM5800
stepping	: 3
cpu MHz		: 993.322
cache size	: 512 KB
fdiv_bug	: no
hlt_bug		: no
f00f_bug	: no
coma_bug	: no
fpu		: yes
fpu_exception	: yes
cpuid level	: 1
wp		: yes
flags		: fpu vme de pse tsc msr cx8 sep cmov mmx longrun lrti constant_tsc up
bogomips	: 2026.55
clflush size	: 32

In total, it took 4020 minutes (2 days 19 hours) to calculate the 44th Marsene prime on this weakling machine:

rs@small:~/apps$ time ./dopow.py 32582657
Exponent 32582657
Done

real    4020m29.975s
user    3836m26.106s
sys     5m49.542s

And the number has 9,808,358 digits!

rs@laptop:~/mounts/petite/apps$ wc out.write
      0       1 9808358 out.write

Have a look at the file (10MB RAW), or grab the compressed version (5MB ZIP).

I am impressed by Python’s ability to handle such large numbers. Kudos to Guido and his team of brainiacs for coming up with this feature.

del.icio.us:Marsenne Prime and Python  digg:Marsenne Prime and Python  spurl:Marsenne Prime and Python  wists:Marsenne Prime and Python  simpy:Marsenne Prime and Python  newsvine:Marsenne Prime and Python  blinklist:Marsenne Prime and Python  furl:Marsenne Prime and Python  reddit:Marsenne Prime and Python  fark:Marsenne Prime and Python  blogmarks:Marsenne Prime and Python  Y!:Marsenne Prime and Python  smarking:Marsenne Prime and Python  magnolia:Marsenne Prime and Python  segnalo:Marsenne Prime and Python  gifttagging:Marsenne Prime and Python

Related Posts:

Software Metrics

Friday, July 4th, 2008

I’ve been reading Bruce F. Webster’s articles and blogs lately. He’s written an article on project management metrics (more directed on IT and software development) on BaselineMag.com. In his article, he quotes The Metric Law of 90s:

The first 90 percent of a development project
takes 90 percent of the schedule.
The remaining 10 percent of the project
takes the other 90 percent of the schedule.

I have to agree with this! When I first started working (not 3 years ago!), a former colleague once told me (tounge-in-cheek): If you ever have to put a date on a delivery for the business, think in your head how long you think it will take, and then multiple that estimate by 3.

del.icio.us:Software Metrics  digg:Software Metrics  spurl:Software Metrics  wists:Software Metrics  simpy:Software Metrics  newsvine:Software Metrics  blinklist:Software Metrics  furl:Software Metrics  reddit:Software Metrics  fark:Software Metrics  blogmarks:Software Metrics  Y!:Software Metrics  smarking:Software Metrics  magnolia:Software Metrics  segnalo:Software Metrics  gifttagging:Software Metrics

Related Posts:

Applications and Software

Monday, May 26th, 2008

I’ve added an application and software section to this blog, and plan to add all the little bits of apps and scripts that I’ve been writing over the past couple of years. All of them will be open source and free.

del.icio.us:Applications and Software  digg:Applications and Software  spurl:Applications and Software  wists:Applications and Software  simpy:Applications and Software  newsvine:Applications and Software  blinklist:Applications and Software  furl:Applications and Software  reddit:Applications and Software  fark:Applications and Software  blogmarks:Applications and Software  Y!:Applications and Software  smarking:Applications and Software  magnolia:Applications and Software  segnalo:Applications and Software  gifttagging:Applications and Software

Related Posts: