Life Codecs Ruminations. Reflections. Refractions. Code.

December 9, 2012

Oracle JDK on Debian

Filed under: how to,software dev — Tags: , — Kamal Advani @ 21:56

Just came across JavaPackage in Debian. This allows you to cleanly replace OpenJDK with a complying alternative systemwide, the Debian Way. I was only interested in the Oracle JDK, and that worked a treat, not sure about other JDKs. In essence:

  1. Install the JavaPackage package using apt[itude]
  2. Download the .tar.gz JDK package from Oracle
  3. Run make-jpkg, installed as part of JavaPackage, against it (not as root); this produces a Debian package of the tarball
  4. Install the Debian package using dpkg
  5. Finally, use update-java-alternatives to update various Java-related symlinks systemwide. You can choose to only update a subset (e.g. just use the Oracle JDK for the browser plugins, but nothing else), or go the whole hog — all JDK binaries will then be from the Oracle JDK.

The wiki link has examples and details.

PS. I really miss saying “Sun JDK”.

Android with Ivy and Ant

Filed under: how to,software dev — Tags: , — Kamal Advani @ 20:57

Created this template project because I wanted proper dependency management when developing for Android. I chose Ivy because it was the least intrusive, and Android officially supports Ant, which Ivy cleanly integrates with, rather than Maven.

Honestly, making Eclipse and Maven play with each other seems to be a really unnecessary time-sink for most things, it’s stupid. mvn eclipse:eclipse, in my opinion, is a much better approach than m2eclipse or similar “heavy” integrating plugins. Unfortunately, I could not find a similar thing for ADT. Anyway, Ivy is quite cool as a pure dependency manager once you (mostly) get your head around “configurations”.

Anyway, hopefully it is useful to others besides me.

July 22, 2012

Javascript Core Concepts

Filed under: how to,software dev — Tags: — Kamal Advani @ 19:12

Alex Russell’s Google I/O 2011 Learning to Love Javascript video is probably the best Javascript core concepts videos I have seen to date. While I still find the boilerplate required to model what other languages have built-in annoying, the elegance and hackery of building sophisticated things from a small, composable featureset is very neat.

I also love that syntactic sugar is acknowledged to be of use to avoid this boilerplate and unnecessary overload of existing keywords. 60000 incompatible ways to do modules has always been one of my pet peeves for something so fundamental. Javascript in this sense has a case of The Lisp Curse. I do understand that essentially the JS people wish to avoid changing syntax for compatibility, however I think at some point this reticence gets a bit much. Given the alternative browser languages coming up, CoffeeScript being the most mainstream, followed by the Dart initiative, I’d say we have hit that point. Admittedly, Dart is somewhat different, apart from syntactic constraints, it is also attempting to make semantic changes for performance optimisation purposes. CoffeeScript is much more about solving the problem at hand.

“De-Sugaring” as discussed, is the way to go to keep the language core small while allowing more syntactic support. Something like CoffeeScript essentially does this — people should still learn the Javascript core, IMO, before learning CoffeeScript to appreciate the elegance of both languages.

More generally, I had an aha-moment when closures were restated as behaviour that hold on to some state — an inversion of how most of us OO-language users think. This fits very well with my renewed understanding of closures as “closing over” a bunch of state — closed over vars do not go out of scope while the closure is alive. That local vars went out of scope once the function/method completed in many languages, in retrospect, has been the main thing that stopped me from fully “getting” closures when I first looked at it. Then again, it is a subtlety that I had failed to fully appreciate even in Java which does have limited closure support via returning class instances from functions that can reference local vars, so long as they are marked final. By implication, the objects referenced by those vars do not go out of scope whilst the class instance returned holding on to them is alive.

Second satori: As stated above, “the elegance and hackery of building sophisticated things from a small, composable featureset is very neat”, but the syntax is annoying — a language with macro facilities (various LISPs), provides both this composable core, and a unified, extensible syntax. Pretty amazing. In case of LISP, non-syntax really. If only I could get used to the gazillion parentheses of LISP.

Somewhat less elegant than macros (but I love the idea nevertheless) is Groovy‘s parser/compiler hooks which essentially allow you to transform your own syntax/constructs into plain ol’ Groovy at various compilation phases of the program. This is how Groovy allows arbitrary DSLs that do not fit its grammar, a famous example being the Spock Testing Framework.

July 8, 2012

Defaulting to Dvorak on Linux

Filed under: how to,software dev — Tags: , , — Kamal Advani @ 18:15

This is (mostly) a Debian (or derivatives) specific guide to get the Dvorak keyboard layout across the system. Unfortunately, the power of a Unix is also sometimes its curse, in this case, at least as far as I know, there’s no central place to say “use Dvorak”, and have the system honour it. But broadly speaking, there are 3 input areas:

  1. Virtual Console
  2. Display Manager
  3. X session / Desktop Environment (DE)

A ‘modern’ DE like KDE or Gnome, will easily allow you to configure Dvorak through some GUI. I am documenting the case where it is DE-agnostic, and focuses on the X session starting in Dvorak to begin with. So unless there’s a DE override, this should work across all DEs.


Virtual Console

To use Dvorak on the virtual console, on the console issue loadkeys dvorak. This requires the various console keymaps installed. Under Debian, ensure the package console-data is installed; if you issue the command and get a file dvorak not found error or some such, you know you are missing the aforementioned package.

I have not been able to persist a Dvorak-by-default-console setting unfortunately. Will Google some more. There must be a nicer Debian Way, besides just issuing loadkeys directly in some init script. At a per user session, putting it in ~/.profile (or your shell’s equivalent) should work.


Display Manager

Important: This will start your display manager (e.g. GDM, LightDM, XDM) in Dvorak mode! For a shared computer you may not want this!

Edit /etc/default/keyboard, and set/replace XKBLAYOUT=”dvorak”, here’s mine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# !/bin/sh
# If you change any of the following variables and HAL and X are
# configured to use this file, then the changes will become visible to
# X only if HAL is restarted.  In Debian you need to run
# /etc/init.d/hal restart

# The following variables describe your keyboard and can have the same
# values as the XkbModel, XkbLayout, XkbVariant and XkbOptions options
# in /etc/X11/xorg.conf.

XKBMODEL="pc105"
XKBLAYOUT="dvorak" # was "us", replaced with "dvorak"
XKBVARIANT=""
XKBOPTIONS=""

# If you don't want to use the XKB layout on the console, you can
# specify an alternative keymap.  Make sure it will be accessible
# before /usr is mounted.
# KMAP=/etc/console-setup/defkeymap.kmap.gz
BACKSPACE="guess"

Note the comment on HAL, I missed it and simply restarted my machine to get it to reload, very lame.


X Session/Desktop Environment

To simply enable a per user X session to start with Dvorak, I set setxkbmap dvorak in ~/.xsessionrc before spawning the window manager. Here’s the full contents of ~/.xsessionrc file (for my own reference):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# see: http://productforums.google.com/forum/#!msg/chrome/t3vPnF_CTBE/UBba0Dp5w3AJ
nvidia-settings -a InitialPixmapPlacement=0

# enable dvorak by default
setxkbmap dvorak

# rotate portrait monitor output, use 'xrandr -q' to find out output names.
xrandr --output DVI-I-1 --rotate left

# compositing manager
xcompmgr -c &

# accented chars support
xmodmap -e "keysym Alt_R = Multi_key"

# Using xfce4-paneli (and thus all panel plugins) with openbox
# openbox is a lighter wm compared to xfwm
exec dbus-launch openbox-session

Note that you may have to configure your display manager to launch an appropriate X client script, I use Xsession with LightDM.

A slight digression: Many larger DEs do various magic to start-up, I really dislike this lack of transparency — I can see the value of it for absolute beginners of course, but you grow out of it pretty quickly once you come back to the Unix Way. To that effect, I modified LightDM — the display manager I use — to use Xsession. Here are my relevant changes to /etc/lightdm/lightdm.conf:

1
session-wrapper=/etc/X11/Xsession

This will find a ~/.xsessionrc, and execute that post starting the X server. Note that my script is documented above, it’s nice having a central script to hook in everything you need to start your X session with, including the window manager itself.

Switching to Dvorak

Filed under: how to,software dev — Tags: , — Kamal Advani @ 17:19

In the past 4 months or so, I have switched to the Dvorak Simplified Keyboard layout. This post documents the specifics of the journey.

Motivation*

*Added at Will’s request.

  • I never really learnt to touchtype properly. Thought this would be an interesting skill to have.
  • I was a little bored, and feeling a little crazy.
  • I wanted to be a l33t dvorakista. Work in progress.

Learning

  • To learn, I don’t think you can get any better than Dan Wood’s ABCD. It has been a joy to use!
    It is lowtech, so you will need to gauge your own accuracy; for me this worked out better. What you type out is also fairly entertaining!
  • ABCD does not list finger positions visually, so google a guide accordingly.
  • ABCD — it rocks. This is the only guide I used during the learning phase.
  • For general Dvorak information, check out Marcus Brooks’ Dvorak Page, very useful.

Practicing

  • To practice short daily-use words, I use 10FastFingers.
  • For much longer words, dvorak.nl fits the bill rather well.

Migration Strategy and Execution

  • Practice at home for about 2-3 weeks for 30 mins to an hour. It will take you at least that much time to complete an ABCD lesson at first!
  • After 3 weeks, use Dvorak exclusively at home, at the cost of feeling like a retard when chatting with friends. And crying each time I had to code.
  • After a month or so, switch to Dvorak at work. I am a programmer, so this was risky, I had to be confident enough to use it. Code completion was indispensable. Having a dev/team/architect lead who also uses Dvorak is likely even more indispensable. (Is “indispensable” subject to gradation? Also google “indispensIble” vs. “indispensAble”, what a mess.)
  • Around 2 to 2.5 months later, finally feel comfortable typing with Dvorak, though by no means fast. Around this time, reached 40-45wpm. Enough to stick to Dvorak!
  • Due to disuse, Qwerty muscle-memory began to fade!
  • 3.5 months or so later, likely covering my previous qwerty speeds. Able to chat without being considered a keyboard noob.
  • Qwerty muscle-memory gone.

Tips and Personal Ideosynchrasies

  • I found using sophisticated typing tutors, especially those with beeps very annoying. Visual cues are fine and useful, but beeps are frustrating and counterproductive IMO.
  • It will be painful at first, have no doubt about it.
  • It helped that I never learnt to formally touchtype, so this was a skill learnt from scratch.
  • Actually memorising the layout was extremely intuitive to me, all my attempts to learn qwerty touchtyping had failed in the past — it just made no sense. Dvorak actually has, for the most part, a logical system to the keyboard.
  • Shortcuts are one case where I have had to visually mind-map qwerty keys to Dvorak, so I do not have to move to the home keys just to execute a shortcut! It honestly is not that much effort though.
  • Passwords are another pain-point, you need to really let go and have your muscles do their thing, instead of relying on visual feedback too much — which you obviously don’t have when entering passwords.
  • Shell-foo: special characters and shell commands, some not being English words per se takes practice. I am still not as fast as I wish on the shell, unless I am in the zone.

Greasemonkey Scripts

Filed under: software dev — Tags: , , — Kamal Advani @ 16:40

I am very late to the greasemonkey party, and likely this has all been done, however:

Converting from Mercurial to Git

Filed under: software dev — Tags: , , , — Kamal Advani @ 16:31

This is basically augmenting (and repeating) this guide.

1
2
3
4
5
6
7
8
9
10
11
12
# get the hg-fast-export util
git clone git://repo.or.cz/fast-export.git

# create your git target repo, and cd into it.
git init git_migration_target_repo
cd git_migration_target_repo

# export the hg repo into (current git repo)
/path/to/hg-fast-export.sh -r /path/to/old/mercurial_repo

# don't forget to do this! or git status will show you a bunch of deletes
git checkout HEAD

Augments:

  • Under Debian, you can issue aptitude update && aptitude hg-fast-export, rather than cloning the repo, unless you want the latest and greatest I suppose.
  • My main use case was converting my bitbucket repo to git, I did the following:
    • bitbucket: renamed the mercurial repo (say call it “foo”) to foo-hg
    • bitbucket: created a git foo repo
    • local: git clone the empty foo bitbucket repo
    • local: perform the migration above into the cloned repo
    • local: push the cloned repo to bitbucket
    • bitbucket: trash foo-hg!

April 23, 2011

Love Will Keep Us Alive

Filed under: software dev — Tags: , — Kamal Advani @ 22:45

Love Will Keep Us Alive1

I was standing
All alone against the world outside
You were searching
For a place to hide

Lost and lonely
Now you’ve given me the will to survive
When we’re hungry…love will keep us alive

Don’t you worry
Sometimes you’ve just gotta let it ride
The world is changing
Right before your eyes
Now I’ve found you
There’s no more emptiness inside
When we’re hungry…love will keep us alive

I would die for you
Climb the highest mountain
Baby, there’s nothing I wouldn’t do

I was standing
All alone against the world outside
You were searching
For a place to hide
Lost and lonely
Now you’ve given me the will to survive
When we’re hungry…love will keep us alive
When we’re hungry…love will keep us alive
When we’re hungry…love will keep us alive

1Of course, these are lyrics to the famous Eagles song. In the
context, I am messing with my
textile formatted
email to blog software.

September 25, 2010

Indonesia Linux Conference (ILC) Oktober 2010

Filed under: software dev — Tags: , , — Kamal Advani @ 14:54

<bilingual>
    <lang type=”id”>Jika anda seorang antusias Linux di Indonesia, mungkin tertarik dengan konferensi ILC! Sayang saya lagi di luar negeri.</lang>
    <lang type=”en_AU”>If you’re a Linux enthusiast in Indonesia, you may be interested in the ILC! Too bad I’m not in the country.</lang>
</bilingual>

banner ilc2010

April 5, 2010

Generics and Method Selection: A Backwards Compatibility Gotcha

Filed under: software dev — Tags: , , , — Kamal Advani @ 22:45

If you managed to read through the rather long post title… let’s move on to a much longer post.

I recently came across an interesting bug at work which was rather eye-opening on just how nasty backwards-compatibility (which prompted type erasure!) can be. Credits are due to my friend and colleague Marcus Hasslinger who reasoned through the issue with me from a backwards compatibility viewpoint.

Some background: we use Java 5 and EJB 3, we also make use of annotated methods that are inspected by custom EJB 3 interceptor(s). A sample EJB might implementation might be:

1
2
3
4
5
6
7
8
9
10
11
@Local
public interface Foo {
    void doSomething();
}

@Stateless
@Interceptors({TransactionHandlerInterceptor.class})
public class FooServiceImpl implements Foo {
    @RollbackWhen(...)
    public void doSomething() { /* ... */ }    
}

In the above, the interceptor (TransactionHandlerInterceptor) is an around-invoke type interceptor which intercepts doSomething(), reads the annotation on it (RollbackWhen) and decides some rollback logic.

The defect was that in a particular EJB, it would not detect the method as having this annotation, and thus the necessary rollback logic was never executed. Thus begins our saga. Rather than going through work artifacts (which I probably can’t show anyway) and bore you with our business domain, I’ll create an essentially similar example using the classic ‘Shape’ set of objects (I know.. I know…).

This particular EJB had the following structure:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// package foo
// Domain objects
public interface Shape {
}

public class Circle implements Shape {
}

// Services
public interface ShapeService<T extends Shape> {
    void draw(T shape);
}

@Local
public interface CircleService extends ShapeService<Circle> {
}

@Stateless
@Interceptors({TransactionHandlerInterceptor.class})
public class CircleServiceImpl implements CircleService {
    @Override
    @RollbackWhen(...)
    public void draw(final Circle shape) {
        System.out.println("Drawing circle");
    }
}

// Sample dumb, non-EJB client
public class Client {
    public static void main(final String[] args) {
        final CircleService circleService = new CircleServiceImpl();
        circleService.draw(new Circle());
    }
}

The issue was that whenever the interceptor intercepted CircleServiceImpl.draw(), it would not detect that it was annotated with @RollbackWhen. So I printed out the actual method.toString() output from within the interceptor – this turned out to be:

1
public void draw(Shape)

instead of the expected:

1
public void draw(Circle)

Obviously draw(Shape) is not annotated with @RollbackWhen, draw(Circle) is. Hmm. So where exactly does draw(Shape) come from though, we only ever implemented draw(Circle) in CircleServiceImpl. It’s time to look at type-erasure in action under the hood using ‘javap’ – a useful little bytecode disassembler that comes as part of the stock JDK. The following output was obtained by running ‘javap -c <classfiles…>’ (without the .class).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// javap -c Shape Circle ShapeService CircleService CircleServiceImpl Client
Compiled from "Shape.java"
public interface foo.Shape{
}

Compiled from "Circle.java"
public class foo.Circle extends java.lang.Object implements foo.Shape{
public foo.Circle();
  Code:
   0:   aload_0
   1:   invokespecial   #10; //Method java/lang/Object."<init>":()V
   4:   return

}

Compiled from "ShapeService.java"
public interface foo.ShapeService{
public abstract void draw(foo.Shape);

}

Compiled from "CircleService.java"
public interface foo.CircleService extends foo.ShapeService{
}

Compiled from "CircleServiceImpl.java"
public class foo.CircleServiceImpl extends java.lang.Object implements foo.CircleService{
public foo.CircleServiceImpl();
  Code:
   0:   aload_0
   1:   invokespecial   #10; //Method java/lang/Object."<init>":()V
   4:   return

public void draw(foo.Circle);
  Code:
   0:   getstatic       #18; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   ldc     #24; //String Drawing circle
   5:   invokevirtual   #26; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   8:   return

public void draw(foo.Shape);
  Code:
   0:   aload_0
   1:   aload_1
   2:   checkcast       #35; //class foo/Circle
   5:   invokevirtual   #37; //Method draw:(Lfoo/Circle;)V
   8:   return

}

Compiled from "Client.java"
public class foo.Client extends java.lang.Object{
public foo.Client();
  Code:
   0:   aload_0
   1:   invokespecial   #8; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[]);
  Code:
   0:   new     #16; //class foo/CircleServiceImpl
   3:   dup
   4:   invokespecial   #18; //Method foo/CircleServiceImpl."<init>":()V
   7:   astore_1
   8:   aload_1
   9:   new     #19; //class foo/Circle
   12:  dup
   13:  invokespecial   #21; //Method foo/Circle."<init>":()V
   16:  invokeinterface #22,  2; //InterfaceMethod foo/CircleService.draw:(Lfoo/Shape;)V
   21:  return

Let’s start with the simple cases: Shape and Circle – Shape is a simple interface, all fine and dandy. Circle is a class implementing Shape, we can see the default constructor generated since we didn’t provide one of our own.

Next we look at ShapeService – a bit more interesting – it has an abstract method draw(Shape). Generics information is erased, there’s no ‘T’ anywhere. T is replaced by its upperbound, i.e. Shape. Thus the generated method too is draw(Shape). So far so good.

Moving on to CircleService – once more, the parameterisation is nowhere to be seen, there’s therefore no notion of Circle in CircleService, it simply extends ShapeService, and thus inherits the same method signature: draw(Shape). This is what got me – it of course, needs to be this way due to type erasure. You get the illusion of full type-safety, as if a new method draw(Circle) is automatically generated in CircleService – since after all, compile-time code completion, etc. in your IDE works just dandy on a reference of type CircleService – CircleService.draw(Circle) – never does it show CircleService.draw(Shape)!

Let’s move on further – things are getting more interesting. CircleServiceImpl has 2 overloaded methods, draw(Circle) which is our code converted to bytecode, and a compiler generated draw(Shape) – which casts (or at least does a cast-check – haven’t read the bytecode description) to ensure the supplied Shape is indeed a Circle, and then delegates on to our draw(Circle). Think about this for a moment – by itself, it’s just madness – but from a backwards compatibility viewpoint — 2 methods need to be there, our own draw(Circle) which as you can see, did not override any method in the interface as such(!), and draw(Shape) the compiler generated one – which is needed to satisfy the type-erased interfaces we implemented!

One final piece of the puzzle — client code: once more we see a default constructor is generated since we didn’t create one. And then the main() method which in fact calls draw(Shape), not draw(Circle). This is once more correct in a backwards-compatible-whacky way — as we saw above CircleService only ever had draw(Shape) — since we used the CircleService reference, that’s the method we got. And thus the implementation class needs to generate draw(Shape) and delegate to our draw(Circle).

In as far as our defect goes — this is exactly why it did not detect the annotation — it was intercepting the client-invoked, compiler-generated draw(Shape) which in turn invoked draw(Circle). draw(Circle) is of course an internal invocation, not accessed via the EJB proxy anymore, and hence never intercepted again.

Ah backwards compatibility.

That aside, there are a few observations to be made:

  • A generics call in the above fashion will always incur an additional cast check and method call, not a big deal for most code, but within a loop, etc. best to measure it. On the bright side, the checkcast is a single JVM instruction. Another optimisation – if you can even call it that! – is to explicitly declare a method draw(Circle) in CircleService – then it is generated as per normal, and client code goes directly to CircleServiceImpl.draw(Circle) – at this time, this is our workaround for the defect. I’ll leave the discussion of where the annotation should belong for another time (i.e. implementation class or interface – this is more a design principle/responsibility issue)
  • It’s interesting that specifically from an EJB 3 interception and transactional semantics viewpoint – this is quite nasty since you do not intercept a method that you think is part of your EJB interface, nor does the transactional semantics get applied (apart from the default ‘required’ of course)! I wonder if it might be better, if in an EJB 3 class, the generated draw(Shape) method did not do a plain invocation of draw(Circle), but accessed it via an internal EJB proxy reference (circleServiceEjbRef.draw(Circle)) – this would ensure proper transactional semantics and interception. Wait – the generated method in fact should not enforce any transaction type either — it should just be (if I recall correctly) using ‘supports’ semantics. As it stands, if draw(Circle) were annotated with a transaction attribute other than ‘required’ – this would not be picked up at all since it was a direct invocation, not via the EJB transactional proxy! Nasty, nasty! It could lead to some very obscure bugs!

Hmm.. I never remember reading about this in any generics book :P . Anyway, if you got this far, hope it was an informative read.

Older Posts »

Powered by WordPress