Friday, July 20, 2012

bootstrap css arrow class

.arrow {
    display: inline-block;
    width: 0;
    height: 0;
    vertical-align: top;
    content: "";
    opacity: 0.3;
    filter: alpha(opacity=30);
}

.btn .arrow {
    margin-left: 0;
}
.btn .arrow.arrow-up {
    margin-top: 6px;
}
.btn .arrow.arrow-down {
    margin-top: 7px;
}
.btn .arrow.arrow-left {
    margin-top: 5px;
}
.btn .arrow.arrow-right {
    margin-top: 6px;
}

.btn:hover .arrow {
  opacity: 1;
  filter: alpha(opacity=100);
}

.arrow.arrow-up {
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    border-bottom: 4px solid #000000;
}

.arrow.arrow-down {
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    border-top: 4px solid #000000;
}

.arrow.arrow-right {
    border-top: 4px solid transparent;
    border-bottom: 4px solid transparent;
    border-left: 4px solid #000000;
}

.arrow.arrow-left {
    border-top: 5px solid transparent;
    border-bottom: 5px solid transparent;
    border-right: 5px solid #000000;
}

reference : http://css-tricks.com/snippets/css/css-triangle/

Wednesday, July 18, 2012

Only use password hash BCrypt

The real problem with password hash is not how secure is the algorithm but how long before it can be broken by brut force using cheap hardware.

Now that you can buy computer CPU grid on the cloud for nothing, it is a matter of time that all our SHA-256 hash be broken.

So, try BCrypt : http://en.wikipedia.org/wiki/Bcrypt

I am not very good at math but I understand that, you can choose the strength of the amount of computation to hash your password, which amount means time !

If today CPU can generate millions of MD5 hash per second, with BCrypt and a strength of 12 (2 at the power of 12), it takes 1 seconde on my Intel i7 quad core. With strength 13 it takes 2, with strength 14 it takes 4 and so on.

You have understood well. To generate a hash it takes at least 1 seconde. So if your passwords database were stolen, it will take 1 million more time than MD5 to bruteforce your password.

Set this strength as a configuration value and when the tomorrow computer will compute strength 14 in less than 10 milliseconds just upgrade the strength.

Tuesday, June 19, 2012

spring-data document mongodb does not recreate your indexes after a mongoTemplate.getDb().dropDatabase()

I'm using spring-data mongodb and I wanted to do well so I've made some unit tests to verify my queries their performances against indexes. Instead of doing a removeAll form each collections, I though it will be quicker to do a mongoTemplate.getDb().dropDatabase(). Big mistakes !! Dropping the database remove everything and indexes of courses. I though they were recreate by spring-data, and it is not. So, removeAll on collection repository will do it.

Saturday, June 2, 2012

Linux Mint 13, enabled stamina on your sony vaio : how to deactivate one of the hybrid video card !

On my new computer, which is a Sony VAIO VPC-SE1A9E on Linux Mint 13 64 bits, I wanted to use the stamina feature and discovered that it deactivate/switch the video card used in an hybrid system. This works easily on Windows. Not in the mood to use windows so I browsed the web and found this to disable one of the video card.
Copy this in your '/etc/rc.local' before 'exit 0'
# deactivate ATI video card on startup in order to save battery life

# change right and owner of vgaswitcheroo
# http://ubuntuforums.org/showthread.php?p=11696293#post11696293
sudo chmod -R 705 /sys/kernel/debug
sudo chown -R USERNAME:USERNAME /sys/kernel/debug/vgaswitcheroo

# remove radeon module from kernel 
modprobe radeon

# http://doc.ubuntu-fr.org/vga_switcheroo
# http://asusm51ta-with-linux.blogspot.fr/
echo OFF > /sys/kernel/debug/vgaswitcheroo/switch
DO NOT 'blacklist radeon' at the end of /etc/modprobe.d/blacklist.conf because vga_switcheroo requires it to be enabled

To control if the device is disable type the command :
lspci -vnnn | grep VGA
The controller which is active should end with : [VGA controller] !! EDIT : laptops featuring hybrid graphics show a huge raise in power drain after a suspend/resume cycle
This is because the X server doesn't currently support GPU hot-pluggin.
A workaround is to move the code from '/etc/rc.local' to your '~/.profile' and first force to switch ON then to switch OFF.
references :
ubuntu-gnulinux-1204-precise-on-sony
radeon-module-boot-problems
vga_switcheroo
vgaswitcheroo -- permission denied
ASUSM51Ta & Linux: Enjoy Hybrid Graphics with switcheroo
Thanks you guys !

Saturday, April 21, 2012

bash git prompts or I forgot again my git branch

To have your command prompt indicates in which branch you are :
(mybranch) root:~$

source : http://blog.piwai.info/2011/10/09/roooh-jai-encore-oublie-ma-branche-git/
source : http://www.git-attitude.fr/2010/07/14/le-prompt-bash-qui-change-la-vie-avec-git/

If you want to view how many commit you are ahead or behind from origin/master add this bash script to your .bashrc file

If you are ahead of 2 commit and behind of one commit from origin/master, you will have something like : (mybranch+2-1) root:~$
# git branch name in prompt
c_red=`tput setaf 1`
c_green=`tput setaf 2`
c_sgr0=`tput sgr0`

# http://stackoverflow.com/questions/1593051/how-to-programmatically-determine-the-current-checked-out-git-branch

parse_git_branch () {
  if git rev-parse --git-dir >/dev/null 2>&1
  then
# TODO ?          gitver=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD)
          gitver=$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')
  else
          return 0
  fi
  
  git_has_current_branch_remotes_origin=$( git branch -a | grep "remotes/origin/$gitver" | wc -l )
  git_has_remotes_origin=$( git branch -a | grep "remotes/origin" | wc -l )

  if test $git_has_current_branch_remotes_origin = 0
  then
          git_head_to_origin=""
  else
          git_ahead=$(git log origin/$gitver..HEAD --oneline | wc -l)
          git_behind=$(git log HEAD..origin/$gitver --oneline | wc -l)
          
          if [ $git_ahead != 0 ]
          then
                git_ahead_msg="+"$git_ahead
          fi

          if [ $git_behind != 0 ]
          then
                git_behind_msg="-"$git_behind
          fi
  fi
   
  echo -e "($gitver$git_ahead_msg$git_behind_msg) "
}

branch_color () {
        if git rev-parse --git-dir >/dev/null 2>&1
        then
                color=""
                if git diff --quiet 2>/dev/null >&2
                then
                        color="${c_green}"
                else
                        color=${c_red}
                fi
        else
                return 0
        fi
        echo -ne $color
}

PS1="\[\$(branch_color)\]\$(parse_git_branch)\[$(tput sgr0)\]$PS1"

Thursday, February 23, 2012

Understand Java benchmarking

Really good article about Java benchmarking which explain why not blindly trust numerics !
Never blindly trust any numbers. Know how they were obtained.

Robust Java Benchmark

  • Execution-time measurement
  • Code warmup
  • Dynamic optimization
  • Resource reclamation
  • Caching
  • Preparation

Tuesday, February 14, 2012

very agile project layout with maven, aka multi-tenant, multi-version development environment


Introduction

As a developer, it is current to switch from an ‘urgent’ task to another more ‘mega urgent’. Sometime it is necessary because a fix should be done quickly, sometime it is because someone needs help, etc.

The difficulties we’re facing each time is about the data. The database is not in synch with the code to debug, or with the code to test, or with the code to do a demo on. A developer should not spend time to set up its development environment. He should spend time to make new features, fix bugs and so on. There is no difficulties to switch the code because your favorite SCM does it for you.

Every time I loose my time to set up my dev env I take some time to enhance it. With multiple tenants application, with multiple versions and with multiple releases a week, I had enough. So I search a project layout which would fill my needs. Its layout should be modular and scalable.

The following is a description of this layout using maven.

Analyzis

We will describe how the data and the code should be versioned together.

Versioning

The version of the source code is managed by an SCM such as SVN, GIT or whatever you like. The data must be compatible with the source code. This way, when you work on a certain version of the application, your data are always up-to-date with the source code.

Data

We can distinguish 3 kinds of data to run with :
  • memory : the data are only in memory
  • local : the data are persisted on some repository on the localhost, ie : a database, web service
  • remote : the data are persisted on some repository on remote host, ie : a database, web service


Dataset can be made for each features such as feature1-memory. This way we can test features that requires data with different states. Of course, fewer dataset for most feature is the better to reduce the number of data and complexity.

Source code

We can distinguish 5 kinds of source code :
  • main : the production code, configuration must be externalized;
  • test : unit test code (should run very quickly);
  • it : integration (functional) test code (may take some time to run);
  • perf : performance test code, microbenchmark;
  • dev : code to speed up development by tweaking developer environment.

Maven layout

This layout is done with maven because it offers a lot of extension mechanisms. We mainly use profile to provide different configurations, coupled with configuration file and filters. It is important to know that this is done only by convention. There is no plug-in which does that structuring from end-to-end.

Data module

To set up dataset which are in database we use sql-maven-plugin (but this can be done also with mock web service).

Possible actions should be :
  • create : create a schema
  • init : inititialize constant data
  • insert : insert the dataset
  • clear : clear the data
  • drop : drop the schema


With a data module, dataset can be shared among dev, test, it and perf source code. It simplify the reuse of data for each use case of a feature.

Locally create a schema and set up default dataset :
# mvn verify -Plocal,create,insert -Ddataset=default

Locally create a schema and set up feature-foo dataset :
# mvn verify -Plocal,create -Ddataset=feature-foo
Locally drop a schema
# mvn verify -Plocal,drop

Remotly clear and set up default dataset on desk5:8080 host :
# mvn verify -Premote,clear,insert -Ddata.host=10.1.1.5:8080

Application module (web,desktop...)

To run web application we use the jetty-maven-plugin.

As of 99% of the development time is done doing development and not releasing, the tools should be easy oriented development (It should be easy to release and deploy as well :-p).

Running

Application must check data version prior to run to indicate if the schema and dataset are compatible (because we can run against different branch data).
The data are loaded from the data module
Memory (default)
The default command runs the application with dev and main classes with memory default dataset :
# mvn jetty:run
is equivalent to
# mvn jetty:run -Pmemory -Ddataset=default

Runs with memory feature-foo dataset :
# mvn jetty:run -Ddataset=feature-foo

Choosing a dataset is only available for memory data because local and remote data are persistent. So a set up command should be run to change the data.
Local
Runs with local current dataset :
# mvn jetty:run -Plocal
Remote
Does not run because there should not had remote default host :
# mvn jetty:run -Premote (should fail)
Runs with remote desk5 host on port 8080 and current dataset :
# mvn jetty:run -Premote -Ddata.host=desk5:8080

Unit test

Run the unit test from src/test with memory dataset because assertion cannot change :
# mvn test

Integration test

Run the integration test from src/it with memory dataset because assertion cannot change :
# mvn verify

Packaging

Package the application as for the production because configuration must be externalized. So there should be only one kind of packaging :
# mvn package

Release module

To make a release we use maven-assembly-plugin.

A release profile is required to clearly differentiate production release from another. So without default, a choice is required. Profiles are only shortcuts, configuration should be done using maven configuration ${param} and properties filters.

Make a release-prod.zip with artifacts and production configuration :
# mvn package -Prelease,prod
should be equivalent to something like :
# mvn package -Prelease -Dapplication.param1=something ...
where parameters are set in prod profile.

Make a release-int.zip with artifacts and integration configuration :
# mvn package -Prelease,int

Make a release-dev.zip with artifacts and development configuration :
# mvn package -Prelease,dev

Make a release-standalone.zip with artifacts and standalone configuration :
# mvn package -Prelease,standalone
We can use winstone-maven-plugin to embed webapp and web server into a JAR.


Conclusion

Following this layout using maven should not be very difficult but requires strictness. As most of the work is done by profile, extension should not be very difficult.

You will see now how you’ve stop loosing time and how you’d become only focused to feature, and how quick it is to fix bug from a version and sharing and set up dataset.

Moreover, you can simplify these tasks by using Hudson/Jenkins CI. This will reduce making a last-stable release to just a download click ;-)

Good hack.

Wednesday, January 18, 2012

Wikipedia Blackout page

Imagine a World
Without Free Knowledge
For over a decade, we have spent millions of hours building the largest encyclopedia in human history. Right now, the U.S. Congress is considering legislation that could fatally damage the free and open Internet. For 24 hours, to raise awareness, we are blacking out Wikipedia. Learn more.

Wikipedia Blackout page : https://wikimediafoundation.org/wiki/SOPA/Blackoutpage