Tag Archives: java

Finding the Versions of All Java Jars in a Directory

Tasked with converting a project to Maven, I needed to find out the version of the dependencies, as not all of them had the version in the name.


Looking Up a Single Jar

If it’s a single jar you want to look up, it’s quite easy.  Simply take the sha1 hash of the file, head over to the Maven Central Advanced Search, and put the hash in the “SHA-1 Checksum” field.


Looking Up Many Jars

The above becomes very tedious very quick if you have dozens of jars to lookup.  A whole directory of mysterious dependencies.

Maven Central has a search API that I quickly took advantage of to automate the above process in Python.  I created a script that will, for every file in a given path, hash the file and search for its version.

You simply run it as >jar_version.py <path>


The Script

Feel free to use the script.

import sys
import urllib2
import json
import os

def sha1_of_file(file_path):
    import hashlib
    with open(file_path, 'rb') as f:
        return hashlib.sha1(f.read()).hexdigest()

def search_maven_central(sha1_hash):
    maven_hash_url = "http://search.maven.org/solrsearch/select?q=1:%22" + sha1_hash + "%22&rows=20&wt=json"
    response = urllib2.urlopen(maven_hash_url)
    return response.read()

def pull_result_version_from_results(maven_central_response):
    results_parsed = json.loads(maven_central_response)
    if results_parsed["response"]["numFound"] == 0:
        return "Not Found"
        specific_results = results_parsed["response"]["docs"][0]
        return specific_results["g"] + " " + specific_results["a"] + " v" + specific_results["v"]

def process_artifact(artifact_path):
    sha1_hash = sha1_of_file(artifact_path)
    html = search_maven_central(sha1_hash)
    result_value = pull_result_version_from_results(html)
    print artifact_path + " -> " + result_value

def main():
    path = sys.argv[1]
    if os.path.isfile(path):
        for root, _, files in os.walk(path):
            for f in files:
                full_path = os.path.join(root, f)



Quickly Navigate to a Package in “Package Explorer” in Eclipse

Often while working on a project, I will want to navigate to the same package that a class I’m inspecting is defined in.

I may want to get a better feel for what a class does / where it belongs, or want to add a new one in the same location, or various other reasons for wanting to do this.

The way I’ve always done this is the manual way.

“This class is defined in the ca.tu.horizon.consumer… ok ca – *click* – tu – *click* – horizon – *click* – consumer – *click*, there we go…”

However I just came across a very nice way of instantly getting there from within the hover tooltip that displays the package.  This is probably already known by most of you, and I’m a little mad at myself for taking so long to find this out, but you can just click on the package you want from within the tooltip!

1: In the hover tooltip, click on the package level you wish to navigate to

Package Nav 1

2: Click on the package icon

Package Nav 2

3: You’re brought to the corresponding package!

Package Nav 3

Using AspectJ to Track Down Performance Issues in a Massive Legacy Project

I’ve been involved with a rather large project lately.  The code was initially written by a consulting firm, and it was written poorly:

  • Single classes reaching 15k lines
  • Code duplication everywhere
  • Very basic Java functionality reimplemented from scratch
  • Pretty much zero code gets passed the information it needs – everything reaches out to static and global variables
  • Zero unit tests (although the code is extremely hard to unit test, since everything depends on everything else)

Just to name a small fraction of the issues that were handed to us.

It’s like being a gardener – you normally add flowers, remove weeds, etc.  And you get dropped off somewhere in the middle of the Rain Forest with a list of rivers to dam, and mature redwoods to add.

It would’ve been nice if those of us tasked with completing and maintaining this project were allowed to simply start over.  But that’s been a tough sell.

So instead we’ve had to learn to navigate this mess and do what we can to improve it, all while implementing new features, and fixing bugs.

We’ve managed to improve it substantially.  However, it’s still way too slow for the poor users.  They get understandably frustrated when navigating to a small form easily takes 5-10 seconds to load.  So it’s become time to speed it up!

But where do we begin?

First, We Measure

We need to understand what’s slow.  We have to analyze the code, find spots that are redundant, and parts that are just taking way longer than they should.

Enter AOP

AOP (Aspect Oriented Programming) is fantastic for long-term gathering of performance on a production system.  It essentially allows you to weave functionality into your code base using patterns.  And specifically for performance monitoring, you can use it to start a timer before any method is called, stop the timer when the method has completed, and log the result. 

It’s also rather easy to use, if your application is configured with Spring.  Since you configure AOP inside the Spring configuration, and tell it what classes and methods to intercept.

Then, We Attack the Worst Parts

Using this we start to see a picture of what parts of the program take the longest to run.  We can then poke around inside these areas that are slow, or are called an unusually high number of times.  For example, it turns out that a “registerLogin” function was being called thousands of times from all over the place, seemingly at random.

This helps a huge amount, but there’s a problem.  Only a small fraction of the application is Spring managed.  Which is the context that AOP likes to run in.  It wouldn’t wrap around plain old Java objects.

Turns out that instead of using Spring for large sections of the application, the original consulting company had come up with their own framework that dynamically finds the appropriate class by ID and creates a new instance of it.

This framework was responsible for making some of the largest and most frequently used objects in the entire application.  We were missing so much information by not being able to automatically time these.

Programmatic AOP Functionality with Any Java Object

AspectJ provides a rather simple Factory method for wrapping an object with AOP code.

AspectJProxyFactory factory = 
    new AspectJProxyFactory( instanceOfSlowObject ) ;

factory.addAspect( MyAopAspect.class ) ;

SlowObject proxyOfSlowObject = factory.getProxy() ;

On its own, however, it defaults to returning a proxy for an interface that the passed in Object inherits.  Which can cause some problems, so we want to proxy the class.  Normally, yes, you want to deal only with interfaces if you can.  But, when left to work in that way, many of the proxies started throwing cast exceptions, so we had to change it.

factory.setProxyTargetClass( true ) ;

This was all wrapped up in a rather easy to use Utils helper class:

public final class PerformanceLoggingProxyUtils {

    private PerformanceLoggingProxyUtils(){}

    * Allows easy performance logging of Objects. It will take the 
    * Object, generate a proxy for it, and pin the performance 
    * logging Aspect to it, so that every method call (as 
    * configured in the Aspect) is timed and logged with Perf4j.
    * @param baseToWrap                                            
    * @return                                                      
    public static <T> T withPerformanceLogging( T baseToWrap ) {   

        AspectJProxyFactory factory = 
            new AspectJProxyFactory( baseToWrap ) ;

        // This tells AspectJ to proxy the Class instead of the 
        // Interface that the class implements.
        factory.setProxyTargetClass( true ) ;

        // The Aspect that is going to perform the performance 
        // logging we are wrapping.
        factory.addAspect( PerformanceLoggingAspect.class ) ;

        return factory.getProxy() ;



Which allowed us to easily hook the same AOP performance logging into the custom framework that dominated the code base. 

And now the performance data is just flowing in!

Know your Tools: An Introduction to Eclipse Templates

How often do you find yourself writing the same small piece of code over and over and over?

Never, because I follow the D.R.Y. methodology, or “Don’t Repeat Yourself”, so I-

Let me stop you right there.  I mean really small pieces of code.  Things like this:

System.out.println( message );

That’s a good couple dozen keystrokes, and I’m positive you write other recurring code snippets over and over.  For me a common one is a null and empty check on Collections:

if( CollectionUtils.isNotEmpty( bar ) ) {
	//Do some stuff

Let’s go back to the print line example and see how we can speed that up, using a built-in template. In Eclipse, simply type “syso” and then hit the content assist shortcut (ctrl + space). Eclipse will replace the “syso” with the only named template that has a match in its name, which is the above print line code.

To more easily see what’s happening, hit the content assist shortcut first, and then type “syso”.  First, you’ll see the familiar dialog pop up:

content assist

Then, while typing “syso”, you’ll notice the options fall away as Eclipse filters out options that don’t match, and you’ll be left with one option, a template:

content assist filtered

That template is then used to generate code for you.

Built-in Templates

There are numerous templates already available for you.  Such as quickly generating a for-loop.

content assist for loop

Hint: Hitting ctrl+space a second time will show you just templates.  

Writing Your Own

For when the built-in templates have failed you and there isn’t one that does what you want, you can write your own.  For example, the second code snippet at the beginning of this post makes use of the class “CollectionUtils”, which is an Apache Commons utility class.  There wasn’t a built in template for that.  So to make it, go to:

Window -> Preferences -> Java -> Editor -> Templates

preferences templates

Click on “New”, and create whatever template you like!  Here’s mine to check Collections for null or being empty:
custom template creation

Hint: “Insert Variable” can help you figure out those “${…}” values.

Which is now in the content-assist window and will allow me to rapidly check future collections before I begin to use them:
custom template in action

In Conclusion

Using templates, both built-in and custom, can save you a lot of time and keystrokes.  Try ’em out.