Tag Archives: java

New side project

I had an idea for a website over the Christmas holiday, took a few hours over a few days and got some proof of concept code up and running. I bought a domain for it, and am now making it robust to handle some traffic. Today I played around with MongoDB and got it up and running and it works great. I’ll be hosting it on OpenShift (for now anyway). I’m hoping to launch the site later this month.

In the meantime, if you use Goodreads and Overdrive, let me know if you want to be a beta tester.

Apache Directory API with Active Directory and ObjectGUID

Recently I needed to access an Active Directory server via Java and get some info out of it. After poking around, I decided to see if I could use the Apache Directory API to get the job done. I hit some problems pretty quick because some of their docs out of date. The docs are unclear as to how to do the bind and I couldn’t figure out the magic incantation to get it to connect/bind until I did it this way:

1
2
3
4
5
6
7
8
9
10
11
12
  LdapConnection connection = new LdapNetworkConnection(SERVER, PORT);
  connection.setTimeOut(0);
 
  BindRequestImpl request = new BindRequestImpl();
  request.setName(USERNAME);
  request.setCredentials(PASSWORD);        
  BindResponse response = connection.bind(request);
 
  if (!connection.isAuthenticated()) {
      System.out.println("Failed to connect/authenticate");
      return;
  }

Continue reading

Unit testing JavaBeans

I always just pretty much skipped unit testing JavaBeans since the work involved didn’t seem worth it. Today though, I was troubled by the lack of coverage on the Javabeans in our Emma code coverage reports so I googled and found this bit of unit testing brilliance: Unit Testing Javabeans. It’s a simple class that uses introspection to test the bean and works great!

Overriding JComponent’s Ctrl-C Action

A little Java geekery here for my own reference and anyone else that needs it….

I needed to override the default CTrl-C (copy) behavior on a JList (though this should work on any JComponent) to copy what I wanted to the clipboard. After Googling and playing with it, I found that this works:

InputMap im = JLIST.getInputMap(JComponent.WHEN_FOCUSED);
ActionMap am = JLIST.getActionMap();
Action copyAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
String myString = "this goes to clipboard";
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
cb.setContents(new StringSelection(myString ), null);
}
};
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK), "Copy");
am.put("Copy", copyAction);