Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Tuesday, 22 April 2014

Sample Java code for obtaining an authorisation token for your Twitter application

Here is an example of how to obtain an authorisation token from Twitter, using your registered application's API key and API secret.

The relevant REST API documentation can be found at: https://dev.twitter.com/docs/api/1.1/post/oauth2/token

For the HTTP client I have made use of the ever popular Apache HTTP client (v4).

The response should include a JSON document containing a property keyed by access_token.

There are a few potential gotchas involved with including this in a multithreaded application, so I'd recommend that you read Twitter's documentation carefully.


        final String basicAuthentication = TwitterCredentials.API_KEY + 
                ":" + TwitterCredentials.API_SECRET;
        String base64EncodedAuthentication = Base64.getEncoder().encodeToString(basicAuthentication.getBytes(StandardCharsets.UTF_8));

        HttpClient client = new DefaultHttpClient();

        HttpHost httpHost = new HttpHost("api.twitter.com", 443, "https");

        HttpPost httpRequest = new HttpPost("https://api.twitter.com/oauth2/token?grant_type=client_credentials");

        Header authenticationHeader = new BasicHeader("Authorization", "Basic " + base64EncodedAuthentication);
        httpRequest.addHeader(authenticationHeader);
        httpRequest.addHeader("Content-Type", "application/json; charset=utf-8");

        HttpResponse httpResponse = client.execute(httpHost, httpRequest);

        HttpEntity entity = httpResponse.getEntity();

        String responseBody = EntityUtils.toString(entity);

Wednesday, 19 October 2011

Ensuring your threads shut down

A while ago I joined a project which had some old school (naive) approaches to concurrent processing in an object oriented language.

In these enlightened times I automatically double check whenever I see a Java class that has been declared with "extends Thread" (not just because some former colleagues regarded the extends keyword as blasphemy).

I've read enough to know to prefer implementing an interface rather than extending a concrete class.  In this case the appropriate Interface is Runnable, without even delving into the excellent java.util.concurrent APIs.

For this most recent project I have gradually eliminated all "extends Thread", sometimes by simply implementing Runnable, and sometimes by introducing a TimerTask.

Today I have been troubleshooting some problems which our Java application has when interacting with native code (compiled C++ libraries for interacting with hardware).  As part of this exercise the application needed to be overhauled to ensure that all threads that are started are correctly shut down when Tomcat is shutdown.

jvisualvm proved to be a useful tool once again for inspecting which threads were not terminating when I ran the application on my laptop.

Like practically everything else in programming, assigning a useful name to each and every Thread and Timer made it so much easier to trace.

Remember the @PreDestroy annotation is your friend.

Thursday, 2 June 2011

Is London actually quite small?

I am now eight months into my current job and I have been involved in a handful of projects which have somehow involved no less than four of the other organisations that I interviewed with in the last couple of years.

Interestingly each of the organisations had either made me some sort of offer, or is still trying to recruit me.

I didn't do a shotgun approach to applying for jobs, so is the industry really quite small?


Update: Now my former employer is trying to form a strategic partnership with my current employer.  I'm not quite arrogant enough to say it's because they miss me so much.

Sunday, 3 October 2010

Property websites development experience

I was fortunate enough to be involved in a range of projects for real estate companies and large property developers between 1999 and 2008.

Infinity Investment Group
- Responded to request for proposal with a detailed estimate and planned approach for the Pegasus Town properties site (still live in a less functional form at: http://properties.pegasustown.com/ ).

- Participated in discussions with CEO and executives during inception.

- Liaised with designers to fit site within look and feel of their existing content managed platform.

- Coordinated development of dynamically updating township images and associated bid registration system.

- Updated system to work as a live auction held at the Westpac Arena for their sale day.

- Configured a secondary website for the sale of land for the Peninsula Bay land development project.

Technologies involved included:
- Java 5
- Struts
- Tiles
- PostgreSQL with GIS extensions (aka PostGIS)
- SQL Server 2008
- Javascript, HTML, CSS
- JPox JDO for Object Relational Mapping
- Geotools
- Debian Linux
- Tomcat
- Eclipse and CVS as the development environment and version control respectively.
- Servlets
- JSPs
- JSTL

Harcourts Group (New Zealand's largest real estate franchiser)
- Development of UI components in Swing for a rich client trust accounting system used by frachisee offices administrators.

- Development of data feeds to and from national real estate bodies in New Zealand and Australia. Built up modules and scripts in Perl to access large volumes of data in XML and CSV formats.

- Pair programming for the new listing and open home alert system, where visitors to the site could register to receive alerts by text message and email as new listings and open homes were coming up for properties that matched their criteria.

Technologies involved included:
- Java 1.2 - 5
- Perl 5
- Tomcat
- IIS 4
- SOAP web service for SMS gateway
- SQL Server 6, 7

Colliers Jardine New Zealand
- Development of a public website for property listings of different types of commercial property in a team of 3 developers.

- Implementation of change requests, ranging from tweaks to full campaigns with separate domain names.


Technologies involved included:
- Internet Information Server 4
- Classic ASP pages written in Perlscript
- Custom developed Perl modules
- CPAN Perl modules
- SQL Server 7

Sunday, 9 May 2010

Mac for Java development

I've been stuck in the world of Windows as my desktop development environment for my entire professional career thus far (11+ years).

In the last couple of companies that I have worked at several of the developers have been successfully using a Mac for their day to day development.

It seems to be the case that "Once you go Mac, you never go back".

There are a couple of Macbook Pro laptops sitting around unused at my current work, so I'm wondering whether I should make the move.

Given that I seem to be spending quite a bit of time configuring *nix servers, it would make sense for me to have a unix-based operating system.


Thursday, 13 August 2009

Maven and friends can only get you so far

I think that one of the best things about software development is that it is always changing - unfortunately this ever changing environment doesn't often allow for things to change as simply as dropping in the next version.

Deploying applications to some servers recently I discovered that some of the components used different versions of some low level libraries. This could have made me appreciate more what projects developed with Maven or Ivy get to see while in progress, but in this case it was not some component of our code that had jumped ahead of some other component - it was actually a container application on the server.

Fortunately enough the versions of the Apache commons jars are backwards compatible, so it was a relatively painless upgrade process.

Tuesday, 4 August 2009

My experience on the London IT job market during economic downturn

After a holiday in Ireland and the UK back in 2007, I decided that if nothing much was happening for me in New Zealand then I should head to the UK where I would have more opportunities to see the rest of the world.

In September 2008 my paperwork came through for my tier 1 visa to live and work in the UK.

I booked my flights so that I would arrive on the same date that the visa became valid, hoping to make a quick start at securing work.

Strange as it may sound, I didn't have any real experience of looking for work. My job in New Zealand had resulted from knowing a bit about a particular technology - CORBA - and a little bit of good luck.

I had initially hoped for some parttime work during my post-graduate study, but got invited to take on a fulltime job - with the option to continue studying parttime. Given that I was only studying to improve my chances of getting a job, this was an opportunity that I could not refuse.

Nearly 10 years later, and on the other side of the planet I had to actually apply for jobs and wear a suit to the interviews - as opposed to the "Pink and The Brain" t-shirt that had been part of my clothing selection back in 1999.

Working in a single company for a long time has some down sides, one of which turned out to be that the choice of technologies used did not match up well with what many job advertisements listed. Spring and Hibernate were the two biggies that I soon identified as being a big deal, so I read some books attended user group meetings etc. and even managed to win a 4 day training course.

After following other people's advice by applying for contracting roles, I started to consider permanent roles. After about the 3rd face to face interview I was lead to believe that an offer of employment was just a formality, so I stopped applying for other roles and considered having a little holiday over Christmas.

In early January the role fell through due to corporate re-structuring that had been followed by a companywide recruitment freeze. It was tough to get myself back into interview mode, so the next couple of companies didn't get a good impression of what I was capable of - 1 even told the recruitment agent who had represented me that I came across as though I wasn't that interested in being there.

After lowering my salary expectations I had more roles to consider, and soon came to the awkward situation of having to choose between two roles.

I chose the role that was located closer to home and the venue of the various technical user groups that I attend during weeknight evenings.

After being hired as a Java developer I accidentally painted myself into a corner that was labelled "Grails/Groovy development" at a time when Grails 1.1 and 1.1.1 were having stability issues.

At the end of my standard 3 month probation period the company shocked me by saying they were "letting me go". Given that I wasn't making much headway with the battle against the Grails issues, and several of their former colleagues had recently become available I can understand their decision.

Second time around on the London job market was a little easier. After six weeks and interviewing for eight different roles, I had two offers to choose from again.

I chose the well-established consultancy that offered greater potential for professional development, over that startup that offered more money.

No regrets.

Monday, 13 July 2009

Java language specification makes for interesting reading

As a different approach to preparing for the latest round of interviews, I've been reading snippets of the Java Language Specification. After a decade of programming in Java, I was surprised to come across a keyword that I didn't recognise, strictfp.

Evidently it's been there since version 1.2, but I guess back then I was more interested in developing Swing components and troubleshooting garbage collection issues from not tidying up those components' listeners etc.

The old expression is still valid, you do learn something new every day.

Monday, 6 July 2009

Maven - more than meets the eye

Earlier this evening I attended a presentation by John Ferguson Smart, a fellow kiwi who is in London running his Java Power Tools Bootcamp at SkillsMatter.

The title of the session was, "Getting Serious About Build Automation: Using Maven in the Real World". Based on his recent JavaOne presentation of the same name (slides available for download).

Before attending the talk I was probably a lot like most of the readers of this blog (both of you? :-) ) in that I had used Maven to get some jars/artifacts into projects that I have worked on, set up some internal project dependencies, groaned aloud in frustration at the maintainers of other external projects for not having their config in place as part of their upgrade process..., but haven't really taken the time to see where to go from there.

Here is a brief summary of some aspects of Maven that I will be looking into further because of the talk:
- the m2eclipse plugin for Eclipse, including its visualization of dependency conflicts (I've seen these conflicts in a project I worked on, but didn't know quite what they meant or how to resolve them by exclusions or specific declarations)
- the ability to standardize project settings using inheritance, including the option to specify versions of artifacts in such a way that they will only be applied to child projects if the child project goes to introduce the dependency ( block)
- using multiple modules to reduce the build time
- the Nexus Maven Repository Manager - refered to as an enterprise repository for caching external dependencies and holding copies of locally built artifacts
- the use of patches to manipulate differences in artifacts being built for different platforms, rather than having to introduce runtime configuration through external dependencies

I find John's blog is a good source of up-to-date info about tools that Java developers should be using to make their lives easier, and their projects progress more smoothly.

Tuesday, 30 June 2009

Google App Engine play

I've gotten back around to trying out Google App Engine.

The main area that I have heard about it has been in my dealings with Grails, but I am sticking with the plain Java capabilities for my initial experimenting.

It seems fairly trivial to build and deploy some servlets using the Google plugin for Eclipse - even though that has meant switching back to a Ganymede version of Eclipse instead of the shiney new Galileo.

Being the curious sort, the first chunk of original-ish code that I deployed within my application was a servlet that iterates over the System properties of the server that is running the application and outputs them to the response.

After some sleep I think I will have a go at implementing a distributed game of noughts and crosses using AJAX and some servlets. This seems like it should be a simple enough game to implement, but will have the challenges of keeping track of players and game state.

Monday, 22 June 2009

Grails gotchas / showstoppers effecting uptake

I was somewhat relieved when I came across this blog post which highlights the fact that other developers have also faced a loss of productivity when working with Grails.

The "convention over configuration" approach speeds up development and keeps the learning curve fairly easy for most developers, but the list of open issues on the Jira suggests that there are still some aspects that need tuning - or detuning in some cases (e.g. domain objects not having GORM functionality due to something missing in lazy decoration/injection).

I've been wondering what sort of risks are involved when you use a software product that is essentially just providing a layer of abstraction over several loosely related other products.

For example, what can you do if you want to upgrade to a later version of one of the underlying technologies (for a fixed bug, or to use a new feature), when the layer above that hasn't been tested or made configurable for that new release? At present, I would expect that you would be stuck with whatever version is compatible with your version of Grails. This could change once OSGi becomes a bit more mainstream, allowing for the use of multiple versions of the same libraries within a single application. I wouldn't want to rely on that being the saviour of any application of mine though.

Monday, 23 February 2009

It's unique, just like all the others

So, I've been going to a few job interviews lately and I can honestly say that no two have been quite the same.

For the first few weeks after arriving in London, I was applying for contracting roles. It didn't take me long to realise that Spring and Hibernate are a big deal in the local job market, so I started to do a bit of upskilling - Winning 4 days worth of training in the latest Spring technologies was a good start, but the timing of the course - December - was awkward as I had to second guess how that might be treated by a prospective employer - Good: training; - Bad - 4 days off before he's even started really working.

Reading the books and having a course coming up was never going to be enough to get me up to the standard expected when a company is paying top £s for a specialist, so I eventually agreed to look at permanent roles.

The first interview was in Devon and involved a long train journey - which half convinced me that it was not going to be suitable, given that at least some of the reason for my moving to the UK was to be close to Ireland and Europe for quick weekend getaways. After I had proven my technical prowess in a coding exercise and a general "problem solving around the whiteboard" discussion, I had the panel interview with 2 techos and an HR person. I decided not to proceed with the team lead role they were looking to fill as they were also looking to change their entire development methodology - and I could see that being painful.

The next interview was for a role as a consultant and was run in a cafeteria with a couple of techos, that would have involved me working at client sites and from home - which doesn't really fit with my preference to stick with developing as part of an agile team.

The third interview was unusual in that the company involved approached me directly after finding my CV online - as opposed to the agencies that I usually hear from. Their offices seemed nice, and the discussion went well - but I wasn't really expecting to hear back from them, given the timing and the industry sector they are in.

The fourth interview involved another out of town train ride, but ended promisingly with the guy saying HR should be in touch with me with an offer - hopefully before Christmas. So, leading up to Christmas I felt that my quest was at an end, but the holy grail of a steady income with that company ran away in corporate re-structuring and an unrelated subsequent recruitment freeze.

January proved to be quite a quiet month on the recruitment front, so I applied directly with a few companies and followed up on some companies that I had been in touch with pre-Christmas.

In two interviews I foolishly attempted to bluff my way through describing the design of a system that I had been responsible for developing. I kept tripping over how to describe a particular sub-system which a colleague had designed, but which was critical to the description of the product.

The feedback was mixed:
- one company said I was technically great, but recommended that I read a particular couple of books (one of which I had already read, the other I am in the process of reading right now);
- the other company told the agent that I gave the impression that I wasn't interested in working there

Given that I had just spoken to the agent to let them know that I would lower my salary expectations by 10%, as I felt that the company had a great approach and were really innovative in applying agile methodologies to their development, getting that kind of feedback came as a bit of a shock to me.

So, in the next interview I made a point of giving well thought out responses to the touchy feely HR questions as well as the technical ones. I thought that I was on a roll when the interviewer said that a lot of candidates hadn't known some of the aspects that I had covered... The interviewer deliberately skipped his questions about Hibernate, as I had stated on my applciation that I did not have commercial experience with it. So, I think that I was justified in feeling frustrated upon hearing their excuse for not proceeding further with me: the other candidates they spoke to had more experience with Hibernate.

So, now I've finished reading Java Persistence With Hibernate and have started posting solutions to problems other people are reporting on the Hibernate forum site. I like to learn from other people's mistakes, and get a feel for what areas are proving to be challenging. It's been re-assuring to find that the issues encountered are much the same as I found with JPA - but saying that "I don't have experience with Hibernate, but I've got a year of JPA and 3 years of JDO" doesn't mean much to the recruiters.

Now that I have some casual part time work I should be able to fill a few of those silly gaps in the CV and be set to hit the ground running - maybe I could give contracting another look?

Friday, 13 February 2009

Implementation of the Singleton pattern

The last successful job interview that I had included a minor practical pen and paper exercise - "Show me how you would implement a Singleton in Java".

I'd been using the Singleton pattern for about 6 years by this stage, so I just jotted down the most basic implementation that I knew, something along the lines of:


public class Singleton
{
private static Singleton instance = new Singleton();

private Singleton()
{
}


public static Singleton getInstance()
{
return instance;
}
}


As I was doing this I thought of all the fancy examples I had seen in various projects that I have worked on and code snippets from various books that I had been reading at the time. So I did my typical self-deprecating thing of saying out loud, "I should probably have some lazy instantiation happening in the getInstance, but that could just be premature optimization".

That lead into a discussion with the interviewer who congratulated me on being one of the few candidates who had actually produced a truly threadsafe implementation. If I had delayed the instantiation until the getInstance was called, then I would have had to introduce some synchronization to prevent the possibility of the instantation happening multiple times, something along the lines of:


public final class Singleton
{
private static Singleton instance;

private Singleton()
{
}


/**
* synchronized to prevent multiple instantiations of Singleton
*/
public static synchronized Singleton getInstance()
{
if (instance == null)
instance = new Singleton();

return instance;
}
}


Then the overhead of the synchronization would have been introduced for each and every call to getInstance - not ideal.

Head First Design Patterns gives a good overview of a slightly better approach involving double-checked locking that I have added to my repertoire (suitable for JVMs 5 and upwards).

Tuesday, 10 February 2009

Relational Databases - PostgreSQL

Is it just me, or are there a lot more free and open source database systems out there than there were 3 or 4 years ago?

I was following a thread on a Hibernate forum the other day and they mentioned a couple of options that I've used in the past - PostgreSQL and MySQL, a couple I'd heard a little about (e.g. SQLite and Firebird) as well as something called H2.

Of these options, I prefer PostgreSQL. I've been using it on and off for almost a decade, but I don't think that necessarily makes me biased as I could just as easily have been frustrated by it if it wasn't such a flexible and reliable system.

The extensive feature set really saved me some embarassment with a project that had a very tight schedule a few years back.

My team had developed some Java code based around the Geotools APIs to manipulate some GIS data in a shape file. That all appeared to be working fine - until I attempted to deploy it onto our in-house test server which was a Linux system and had the same setup as the server that we intended to host the web-based application on when it went live!

I did some digging around and established that Shapefile manipulation was just for demonstration purposes (so would not have suited our needs anyway), then found that there was also the option of accessing the data via a PostGIS data source. So, I spent my Saturday afternoon building a new PostgreSQL system from source code, with the GIS extensions and the optimal geometry packages available compiled in .... and did a bit of tweaking of the PostGIS access code in Java and had the project right back on schedule for Monday morning's, "So, what did you get up to on the weekend?" coffee break.

I subsequent got the chance to pick up the same set of technologies and have them in place from day 1 in another project, so got to spend some time looking for decent tools for visually verifying the data - uDig was the best nice free system I could find - it connects to PostGIS fairly easily and exists within an Eclipse framework.