Saturday, December 31, 2011

Unit testing Apache CXF RESTful services - code available

So, the original post on the topic written about two and a half years ago had code snippets, but there's been comments and PMs for the complete code. So last week, as I resurrected this blog, decided to get that code out on github. Unfortunately, that was easier said than done; it has been quite some time and frankly, I'd lost the code. I must've switched machines about 3 times in the interim and gone from SVN to github for personal projects. Some hunting around ensued and thankfully, I was able to find the actual code we wrote based on the sample I'd posted. So cleaned that up - and just extracted the unit testing example out of it and pushed it to github - get it here. I haven't updated any of the dependencies - so this is still running against spring 2.5 and cxf 2.2.3 (I think) and things might've changed quite a bit since then (I haven't used the JAXRS bits of CXF much after that)


Running tests:
[sourcecode language="bash"]
mvn test
[/sourcecode]
Running the server:
[sourcecode language="bash"]
mvn jetty:run
[/sourcecode]

Syntax highlighting support in Wordpress.com with markdown

Now that I've cozied up to Vim/VimRepress combo for posting to this blog, there are a few things where I'm finding issues with posting code. With straight wordpress.com, I used to be able to mark up code with the [sourcecode][/sourcecode] tag and syntax highlighting comes in. With markdown - indenting a block of code with 4 spaces renders it as a <pre><code></code></pre> tags, but I don't know if there's a way to let WP.com know what language it is or any way to use the [sourcecode][/sourcecode] plugin from markdown.


Some googling on the topic didn't result in any wp.com specific answers (some folks have posted on using other plugins et cetera with a self hosted Wordpress - but nothing for wordpress.com)


Any ideas/pointers? Guess I should also post the question on the Unix stackexchange


Update on 1/1/2012


Using the sourcecode language="xxx" tag works - but you cant have any empty lines in your source.

Friday, December 30, 2011

Creating an interstitial login page with JqueryMobile

So, at work, we're building a mobile website using JqueryMobile. The app has a bunch of publicly visible pages however, other pages require the user to be authenticated. We didn't want the user to be forced to login on the first page. Instead, whenever a protected page is accessed, and if the user insn't logged into the app, we'd like to take him to the login page. Once he's successfully authenticated, then take him to the page he was navigating to. Doing this in a normal webapp is quite standard - however, with JqueryMobile, query params meddle with the hash navigation model. Also, the page that the user tries to access could be a div in the same physical page or a different url that needs to be fetched.


Trying to solve this was interesting as we were all really just getting started with JqueryMobile - so finding the ideal solution required a few tries. The solution takes a leaf out of JqueryMobile's approach. The outline of the solution is:



  1. Any page div that's a protected resource is marked with a data-needs-auth="true" attribute

  2. We hook into the document level pagebeforechange event to see if the user is trying to transition to a page requiring authentication. If so, then check if we have the user's authenticated context available.

  3. if the said context isnt available,

    1. Cancel default event handling since we're now going to navigate the user to the login page.

    2. save the toPage object - so once the user is logged in, we know where to take him.

    3. navigate to the login page.



  4. In the login page, the page can call the server apis to autheticate the user. Once the user is authenticated, then

    1. See if there's a valid returnTo object, if so, take the user to the page.

    2. If not, take the user to a 'default' page - in our case, this is the app dashboard page.




Code below
[sourcecode language="javascript"]
var pageVars = {}
$(document).bind("pagebeforechange", function (event, data) {
if (typeof data.toPage == 'object' &amp;&amp; data.toPage.attr('data-needs-auth') == 'true') {
if (!sessionStorage.getItem("TokenSSKey")) {
if (!localStorage.getItem("TokenLSKey")) {
pageVars.returnAfterLogin = data;
event.preventDefault();
$.mobile.changePage("#Login_Page", { changeHash: false });
}
else {
sessionStorage.setItem('TokenSSKey', localStorage.getItem("TokenLSKey"));
}
}
}
});
[/sourcecode]
The login event handler that handles the server response that's received once we pass the username and password


[sourcecode language="javascript"]
function SuccessLogin(data) {
if (data != null &amp;&amp; data.LoginResult != null) {
if (data.LoginResult.Code === 0) {
localStorage.setItem('UNameLSKey', data.LoginResult.User.AccountName);
if ($("#RememberMeChkBx").is(":checked")) {
ErrorPanel.html("");
localStorage.setItem('TokenLSKey', data.LoginResult.Token);
sessionStorage.setItem('TokenSSKey', data.LoginResult.Token);
}
else {
ErrorPanel.html("");
sessionStorage.setItem('TokenSSKey', data.LoginResult.Token);
}
if (pageVars &amp;&amp; pageVars.returnAfterLogin) {
$.mobile.changePage(pageVars.returnAfterLogin.toPage);
}
else {
$.mobile.changePage("#DashBoard_Page", { changeHash: false });
}
}
}
}
[/sourcecode]

Thursday, December 29, 2011

Learning Vim

Is it worth it?

Definitely seems to be. I've looked at VIM in the past, tried it out too a couple of times or more, failed miserably(mostly within a day or two) and then wondered Why nutheads use VI. This would usually be followed with going back to the comfort of Emacs. I think over the years, I've spent more time customizing Emacs than actually getting any work done with it. And somewhere that felt wrong. In light of that, the minimalistic VIM looked attractive and worth another try.

So what was different about this time?

So this time things worked out a bit better. Rather than firing up VIM, spent some time reading through other's experiences on picking up VIM. And the first thing I did right was to disable the arrow keys in normal mode (I still have them in insert mode)

    " disable arrow keys
    noremap   <Up>     <NOP>
    noremap   <Down>   <NOP>
    noremap   <Left>   <NOP>
    noremap   <Right>  <NOP>
Once you have that bit, you're forced to use h/j/k/l. And while h/j/k/l muscle memory is built up within a week, the nice thingthat really happens is that you dont use h/j/k/l much - instead you move to using more efficient movement commands. There're aton of resources/cheatsheets on the web - but the approach I followed was to figure out some small keystroke when I needed it.What that mean't was that I could get work done - but at the same time get more efficient gradually.

Customizations

VIM out of the box is pretty badly configured - and that's part of the reason that people seem to shy away from it. In fact, all the times that I tried out VIM before, I didnt even come close to cusotimizing my .vim. There are folks who have curated vim dotfiles on github etc - but my advice is to stay away from them. You should know what goes in your .vim and be in control of thatrather than getting a bunch of things in your .vim that you dont understand. Just so you know, looking at the github history for my vimfiles repo - the initial commit was 3 months ago - but after that, all the commits have come in only in the last 4 weeks.What that means is that while I put in a vim file initially, I didnt do much with it initially since I was just getting a hang of the basics. Once one becomes comfortable with the basics, one moves to customizing the vim environment more and more.

Parting words

To summarize, VIM definitely seems nice once you invest into it. It's easy to drop off in the initial stage and not go any further - and I believe this is what happens to the vast majority of folks who try it out. However, once you build that initial comfort level,it feels light, fast and easy.Start easy, persist, and customize bit by bit - you'll feel yourself going from struggling with Vim to feeling comfortableand then to customizing your environment for an even better experience with VIM.I've definitely been more productive with VIM than I ever felt I was with Emacs - and these posts to my blog from Vim part of that.Besides that, I've used VIM effectively with a decent sized js code, html markup etc and felt the speed of editing inspite of still beinga noob in Vim terms.

A new look

Changed the theme of this blog and moved around the widgets a bit.
Finally, I can bear looking at this blog :) - hope that holds good for you too.


Wednesday, December 28, 2011

Compiling VIM

Running ubuntu 10.10 here and ubuntu repos have only vim 7.2. I'm sure there's a ppa out there that has 7.3, but thought
that compiling vim from source would be a good exercise - plus I get to compile it with the options that I'd like
rather than relying on someone's build.


Here's the options that I enabled:
[sourcecode language="text"]
CONF_OPT_PERL = --enable-perlinterp=dynamic
CONF_OPT_PYTHON = --enable-pythoninterp
CONF_OPT_RUBY = --enable-rubyinterp
CONF_OPT_GUI = --enable-gui=gtk2
CONF_OPT_FEAT = --with-features=huge
BINDIR = /usr/bin
DATADIR = /usr/share
[/sourcecode]
Here's hte other dependencies I had to install
[sourcecode language="bash"]
sudo apt-get install libperl-dev ruby-dev python-dev libgtk2.0-dev
[/sourcecode]
Once you have the deps installed, just run
[sourcecode language="bash"]
make
sudo checkinstall
[/sourcecode]

Blogging with Vim

So now I'm in Vim land and this is the first time I've gotten far enough to feel a bit comfy. Decided to dust off my blog and start at it again - what better to do it in than in VIM.


So - TA-DA - here's the first post - courtsey VIM on ubuntu. However, as usual, it was rougher than it's supposed to be. IN any case, I'll forget how I got this far the next time so the next few posts will be around recording how to get VIM to post to WP.com blogs.


But before that - the first thing to to is to get the VimRepress plugin. Better if you have pathogen installed, in which case you can do
[sourcecode language="bash"]
cd .vim
git submodule add https://github.com/raghur/VimRepress bundle/VimRepress.git
[/sourcecode]
That's my fork on Github of https://github.com/connermcd/VimRepress.git which fixes a few things:



  • Makes VimRepress work properly through a proxy

  • Changes the attachment filename to a '.odt' since Wordpress.com doesn't allow a text file attachment.


I still dont have a clue if doing this will break the plugin - but nevertheless, basic case of posting to my blog works and at this stage that seems good enough for me.


PS as you can see from this post - I've not yet got a hang of markdown syntax :)


Dec 29th - PPS a couple of posts and one more tip for Wordpress.com. WP.com does <br/> for hardbreaks in the markdown text. Obviously, this doesnt leave the post looking very good. I have the following in my .vimrc to get around this


[sourcecode language="text"]
augroup Markdown
autocmd FileType markdown set wrap
\ linebreak
augroup END
[/sourcecode]


PPS
You will also need to have python markdown installed once you have VimRepress running.
[sourcecode language="bash"]
easy_install markdown
[/sourcecode]

Thursday, August 18, 2011

Troubleshooting nandroid backup/restore

Have been having all sorts of weird problems with Nandroid backup/restores. Essentially, here's the symptoms of the problem - I'd get a nandroid and restore it successfully (Amon RA/CWM would report success) - however either will get stuck at boot or if it boots successfully, will have tons of FCs and/or data loss. In most cases, I would dread seeing the green Android on boot up asking me to log in to my google account :(

Essentially, my nandroids were useless... to the extent that I had only one nandroid backup that was known to work - and I was keeping 4 backups of that lest I lose it somehow.

So today, thought I'd get deeper into it and see where the problem was

  1. It was unlikely that its a problem with CWM/AmonRA - I myself had an old working backup. Since my backups were created and restored successfully with MD5 verification, it seemed that there's something wrong in the backup image itself.

  2. Still, that seemed inexplicable, since creating images just doesn't seem that flaky. A couple of times after reboot, I had got a "UID has changed - it is recommended to wipe data" or something similar message - so I thought something was wrong with permissions after the restore. In any case, I tried the CWM menu item of fix permissions - but didnt get anywhere with that. At this point, I was desperate enought to get adb out !!

  3. Now in full blown investigation mode, I didnt care if I couldnt restore my data - just wanted to figure this thing out. So restored a  "non working" backup and did a adb logcat while the phone booted... turns out that I was seeing tons of messages like so:


  4. I/PackageManager(  205): /system/app/ContactsProvider.apk changed; collecting certs
    I/PackageManager( 205): New shared user android.uid.shared: id=10006
    W/PackageManager( 205): System package com.android.providers.contacts has changed from uid: 10003 to 10006; old data erased


  5. So that explained what was going wrong... thought it would be an easy fix to do the 'fix permissions' thing in CWM advanced menu. Restored again and went over to the advanced menu, did fix permissions and rebooted. What I got was a big naught for all my work - same problems and no resolutions. At this point I was stumped enough - but sheer bull headedness forced me to look at the log again... and lo it says 'data erased'. So that explains why fix permissions after boot wont work since the data is erased during boot itself!


At this point, the key to the problem was really understanding how and where android's UIDs are generated,  stored  and regenerated. Headed over to Cyanogen wiki and read up the details on fix permissions which explained the packages.xml file. Somehow the packages.xml was borked in the nandroid (every time) and that was causing it to be regenerated.

Armed with that, got a germ of a solution in place, which is roughly

  1. Restore nandroid with borked packages.xml

  2. Let the system boot. Will lose data but a new packages.xml will be regenerated

  3. Reboot into recovery and adb pull /data/system/packages.xml out.

  4. Do an advanced restore and again restore the data only.

  5. mount /data and adb pull /data/system/packages.xml to compare differences. Found that packages.xml was indeed corrupt.

  6. adb push packages.xml (this is the generated one pulled in step 3) to /data/system. Now you have all the old data but the packages.xml is newly generated one and known to be valid. Obviously UIDs will mismatch - but fix permissions has a valid file to work on.

  7. Still in recovery, run fix permissions. It should fix permissions properly.

  8. Reboot


It worked like a charm!!!! I'm still a little worried as I dont know what else is borked in my nandroid data.img. And I dont know why that image has the exact same problem every time - I have tried 3 different versions of CWM recovery, Amon RA 2.2.1 and ensured that my sd card was clean (ran chkdsk on it). In any case, since I'm able to restore, will just use the phone for the next few days and hopefully I'll run into any wonkiness soon.

And going forward, I think I'll take my own backup copy of the /data/system/packages.xml file along with each nandroid.

Tuesday, August 02, 2011

Note To Self: Fixing broken market links on Android after wipe/ROM upgrade

Force stop market and clear data
Launch market again - it will ask you to accept terms. Do so.
Should force it to rebuild the database and you should see all your apps linked to market again.

Thursday, July 28, 2011

Note to self: ROM install/upgrade


  1. nandroid backup - amon ra recovery

  2. Reboot recovery, install zip

  3. Install Link2SD-preinstall.zip (only on cyanogen based ROMs)

  4. Boot

  5. Play around...make sure things work.

  6. Install other niceties/Troubleshoot


    1. Link2SD - database error.Just uninstall and reinstall.

    2. /etc/gps.conf - change to sg.pool.ntp.org


  7. Charge to 100%

  8. Reboot into recovery

  9. wipe battery stats

  10. reboot

  11. Run down the battery

  12. Recharge to 100%