Tuesday 23 June 2015

Integrating Active Directory from Java

Firstly you will need to setup Active Directory so that you can run and test your code.

If (like me) you don't have a machine with Windows Server no worries, this is how you can set up Active Directory even on Mac OSX.

The first thing you want to do is set up a user a in AD.  Make sure that when you set up the user he doesn't have to change his password on the first login or you will get an LDAP error from your Java code.



Clearly you can change your code to prompt a password change but just something to be aware of.

Also create a couple of groups for your user and add them to their profile.

In the code below there is just one method, authenticate. It that takes as parameters a user, password and domain and returns a list of groups of which the user is a member. If the user does not exist or there is a problem with the password an exception is thrown.


The code is fairly self explanatory and you can use it as a template to experiment with adding different attributes to the search.

Java8: Generate a Random String in One Line

This is a program which demonstrates the power of Java 8 streaming to generate random strings in Java. It chains the filter, map, limit, and collect to turn random numbers into the required string.


The program will output random strings containing numbers [0-9] and letters [a-z,A-Z].


The numbers in the code map to UniCode characters (for a full Unicode chart see here).

An explanation of the code is as follows:

  1. Generate random numbers within the range 48 (unicode for 0) to 122 (unicode for z).
  2. Only allow numbers less than 57 (the digits 0-9) or greater than 65 and less than 90 (letters A-Z) or great than 97 (the letters A-Z).
  3. Map each number to a char.
  4. Stop when you have the required length of the string. 
  5. Collect the chars produced into a StringBuilder
  6. Turn the StringBuilder in a String and return


Friday 12 June 2015

Tutorial : Setting up Active Directory on a Mac OSX

Recently I has to write some Java code to integrate with ActiveDirectory.

My first problem was setting up an environment on my machine (a MBP) where I could test my code.

Turns out this is not too tricky if you follow the right steps.

1) Install VirtualBox (Here's a blog I wrote on this subject)

2) Download and install Windows Server 2012 (This link for 180 day free eval period) into VirtualBox.

3) Before you start Windows Server 2012 run this command on your Mac (This took me a while to figure out):

VBoxManage setextradata 2012Server VBoxInternal/CPUM/CMPXCHG16B 1

4) Once Windows 2012 is installed you need to setup ActiveDirectory.  Carefully follow the directions in this excellent tutorial.

And there you have it Active Directory running on your Mac.

In the next article I'll post the Java code you need to connect to Active Directory.

Find the largest files on you Mac (Clean up you Mac)

There are lots of complicated applications for this you can download, but really the easiest and cleanest way is with these 2 commands that can be run from a Terminal window:

This one finds all files above 100MB and prints them out with the largest files at the end (so you can deal with them first):


find / -size +200000 -ls 2>/dev/null | awk '{for (i=1; i<=NF-4; i++) $i = $(i+4); NF-=4; print}' | sort -n

Feel free to change the threshold to include more or fewer files just change the +200000 in the script.

The second command just prints the largest folders with the largest at the end:

du -k / 2>/dev/null | sort -n

One interesting thing I found was how much space was wasted by Garage Band an application that I will never use.