Thursday, August 27, 2009

Launch Explorer.exe from Cygwin

Working in cygwin, I often need an explorer window to my current directory or a sub-directory there of. I called this script expl.sh.
#!/bin/bash
export p=`pwd`
export pc=`cygpath -w $p/$1`
explorer.exe /root,$pc &
Once you have this the directory on your path where you keep your scripts: perhaps ~/scripts:
expl.sh
expl.sh some/sub/directory

Monday, August 24, 2009

Google App Engine and GWT

Well, I started poking around with Goole app engine in Eclipse. By default they want to use GWT. fine. Someone once said, "when drinking the tainted cool-aid you might as well eat the cookie too." (if no one said it then I just did)

Well, I develop on Ubuntu and immediately ran into an UnsatisfiedLinkError.
** Unable to load Mozilla for hosted mode **
java.lang.UnsatisfiedLinkError: /home/user/projects/gwt-linux-1.5.3
/mozilla-1.7.12/libxpcom.so: libstdc++.so.5:
Thankfully, this was the first hit on by search. Thank you

Monday, August 3, 2009

When to Refactor

It's common knowledge that you don't write code in a software project to solve future problems. Don't implement code that is not needed.

It occurred to me that the inverse is true for refactoring. I will not delay a refactor because we expect to have a requirement that will make it obsolete. You never know which features/tasks will be canceled or delayed. You should ask yourself, "How long am I willing to support this ugly code?"

Tuesday, May 12, 2009

Subversive Eclipse on Jaunty Ubuntu

I got all obsessed over the weekend. I needed stable sound from my laptop. I mean really, who can code without music!? I don't want to talk about that. I had a painful path form Hardy to Jaunty. But that's not why I'm posting.

After a complete re-install of Ubuntu I was reinstalling Eclipse and Subversive. It failed. The "SVN Connectors" drop down was empty. Turned out I fell victim to a bad link over to Polarion. That was answered over on stackoverflow. As stated in that answer, this is the right update site url to get the connectors from Polarion: http://www.polarion.org/projects/subversive/download/eclipse/2.0/update-site/


I wanted to use the javaHL connector, so I also needed this step.

  • sudo apt-get install libsvn-java
  • put "-Djava.library.path=/usr/lib/jni" in eclipse.ini


Thanks guys/gals. Hopefully I won't forget how to do this stuff. (next time an OC urge strikes to set myself back 3 days)

Sunday, March 1, 2009

J2EE Connector Architecture

J2EE Connector Architecture (JCA) is a little used aspect of J2EE that is probably now relegated to obscurity due to the popularity of web services. It is however a viable solution to many problems. You can still find it documented at sun. You can also rather nicely integrate Spring.

I put this short presentation together. Contact me if you would like the three Eclipse projects.

Tuesday, February 17, 2009

Android Custom Component Merge

I created my custom component that extends LinearLayout. In that post I show that the root element of the components XML is a <LinearLayout ...> element. That ended up getting in the way. When the LayoutInflater built the view in my Java PersonComposite extends LinearLayout the default behavior was that my java class now had one child, the LinearLayout that is the root of the XML. This meant that each use of <my.app.PersonComposite ...> in other view XML would never apply configuration to the Linearlayout that has any effect.

Wow. Even I don't understand that. Let's try this way. The LayoutInflater, actually created the following structure (details omitted for brevity):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout >
<LinearLayout >
<TextView .../>
<TextView .../>
</LinearLayout>
</LinearLayout>
So any attempts to use the component like this would never change the orientation of the two nested TextView Components because the configuration was applied to the outer LinearLayout:
<my.app.PersonComposite
android:id="@+id/person"
android:orientation="vertical"
/>
Solve this problem by using <merge> as the root element. And make sure you invoke LayoutInflater:

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflatedView = inflater.inflate(R.layout.person_composite, this);
Now the person_composite.xml looks something like this:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView .../>
<TextView .../>
</merge>
You can see how this works by getting the android source from git and looking at the source code for the LayoutInflater.inflate method. then search that same source tree for an xml that uses <merge> as the root element. You'll find more than one.

Sunday, February 8, 2009

Unit Test Your Custom Parcelable

Your Android application needs to pass some custom data between processes in an Intent. You will be calling Intent.putExtra(String, Parcelable). It would be easier to test if the Parcel class where not final with only private constructors. That keeps us from testing the methods individually. We are forced to perform only round trip testing -- write our Parcelable to a Parcel, then call the creator with the same Parcel instance.

There is one important step right in the middle of the round trip. The Parcel needs to be reset to be ready for read. Think of this just like working with java.nio.ByteBuffer. With ByteBuffer, when you are done writing, you call flip. With Parcel, when you want to read -- call setDataPosition(0). Here is a sample test.

I'll decoreate a Person object as a ParcelablePerson that implements the Parcelable interface and has the requisite public static final CREATOR. Following TDD I've only created enough of the implementation code below to make the test compile.
    public void testPersonTakesRoundTripThroughParcel() throws Exception {
Person testPerson = new Person();
ParcelablePerson testObject = new ParcelablePerson(testPerson);
Parcel parcel = Parcel.obtain();
testObject.writeToParcel(parcel, 0);
//done writing, now reset parcel for reading
parcel.setDataPosition(0);
//finish round trip
ParcelablePerson createFromParcel = ParcelablePerson.CREATOR.createFromParcel(parcel);

assertEquals(testPerson, createFromParcel.getPerson());
}
public class Person {
...
}
public class ParcelablePerson implements Parcelable {
private Person person;
public ParcelablePerson(Person person) {
this.person = person;
}

public Person getPerson() {
return person;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
//call dest.write... methods
}

public static final Creator CREATOR = new Creator() {
@Override
public ParcelablePerson createFromParcel(Parcel source) {
return null;
}

@Override
public ParcelablePerson[] newArray(int size) {
return null;
}};
}