Sunday, April 19, 2015

How to use Figaro with Travis CI

Although I knew that including my Facebook app_id and app_secret on the omniauth.rb file is a security risk but I went ahead because I could reset it anytime I wanted. However the correct way of handling the sensitive API keys or database credentials is by never committing them to version control. Also the 3rd commandment of a 12 factor app is to store them in environment variables!

I decided to use figaro and got up and running fast. You should follow their tutorial in the Github page. After I ran the figaro install command I added the configuration values to the application.yml. My application.yml file looked something like below (but of course with the correct credentials). Note that setting the values in "quotes" is important as otherwise you'd get errors when trying to set the keys in heroku.

app_id: "342342342343"
app_secret: "1233866op343435212389267622c123b"

production:
  app_id: "342342342343"

  app_secret: "4563866ba3434352b4569988222c466a"

Then the next task was to set the production credentials in heroku. That was easily done with 

figaro heroku:set -e production

Ran the specs (All green!), committed and pushed to Github. After that it was smooth sailing for me! Or was it ?

Travis CI sent an email immediately complaining that the build was broken. I checked and all my specs were broken and to see why, it was because I added the following line in application.rb


Figaro.require_keys("app_id", "app_secret")

So the environment variables were not set in Travis and it wouldn't run any of the specs. Figaro would throw the following error if the required keys are missing.

Missing required configuration keys: ["app_id", "app_secret"] (Figaro::MissingKeys)

So how to set the environment variables? Turns out Travis CI provides a gem that can encrypt the environment variables and add them to travis.yml, and they will be set before running specs.

After I installed the 'travis' gem it, I used the travis encrypt command to get encrypted values.

travis encrypt FOO=bar

You can use the -a option to automatically add it to the travis.yml as well.

After the values were added to the travis.yml file, it looks as shown below. Note that the the encrypted "secure" variable includes BOTH the environment variable name and value.

language: ruby
rvm:
  - "2.2.1"
# uncomment this line if your project needs to run something other than `rake`:
script:
  - RAILS_ENV=test bundle exec rake db:migrate --trace
  - bundle exec rspec spec/
env:
  global:
    - secure: "VERY LONG ENCRYPTED KEY VALUE ENV VAR"
    - secure: "ANOTHER VERY LONG ENCRYPTED ENV VAR"

I committed this file and pushed to Github and then Travis could the find environment variables and build successfully. The result was green like a cuke!

Wednesday, June 13, 2012

JsonMappingException when serializing POJO reverse engineered using Hibernate, to JSON

We were working recently on a Java project with Hibernate as the ORM. We created the database and reverse engineered the entities to POJO using Hibernate (See how).

I am putting up a simple ER diagram to demonstrate the issue.

Each Item has a list of item details which are name/value pairs.



And we needed to serialize this object to JSON so we used Jackson. And when trying serialize we were confronted with an exception.

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ItemDetail.item, no session or session was closed

Closely looking at the generated ItemDetail class I found that there was a Item defined there. 

   @ManyToOne(cascade={}, fetch=FetchType.LAZY)  
   @JoinColumn(name="item_id", unique=false, nullable=false, insertable=true, updatable=true)  
   public Item getItem() {  
     return this.item;  
   }  
   public void setItem(Item item) {  
     this.item = item;  
   }  

The first (stupid) thing I tried was to change to FetchType from LAZY to EAGER. which I thought would solve the problem.  However that resulted in a StackoverflowException because the serializer kept recursing and it would never reach the end.

 Exception in thread "main" org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: ItemDetail["item"])

The problem here is that Hibernate provides us a way to navigate back using the foreign key relationship where in this instance to get the parent Item that belongs to each ItemDetail. But Jackson did not like it!

The solution was VERY simple.

Simply inform the serializer to ignore such parent relationships. You can inform Jackson to leave it aside using @JsonIgnore (See all the annotations here).

   @ManyToOne(cascade={}, fetch=FetchType.LAZY)   
   @JoinColumn(name="item_id", unique=false, nullable=false, insertable=true, updatable=true)   
   @JsonIgnore  
   public Item getItem() {   
    return this.item;   
   }   

And Jackson did not try to serialize it and it worked just as expected!

Saturday, June 2, 2012

Setup and forget automated backup with Cobian + Your favorite cloud hosting app!

There are numerous file cloud syncing/backup tools available such Sugar Sync, Dropbox, SkyDrive, Box.net and Google Drive to name a few.



These tools helps you keep your files safe in the event of a disk failure, virus attack or any other destructive situation, automatically.

You add a file/folder to the "special" sync folder and all is backed up and synced or use "selective sync", a highly sought after feature implemented in most applications, to select which folder to be synced to the cloud.

Now this is all good. But there are times when you really don't want some files to be synced always, all the time. The perfect example is the Outlook PST file which stores all your emails. You want it to be backed up, but it's going to be a waste of bandwidth if each time an email arrives and your file starts syncing to the cloud. There maybe many other such files that need not be synced ASAP.

What you need is some way to specify WHEN exactly do you want the sync to happen. How awesome would it be if you can tell "Hey, backup this file/folder every hour, every 135 minutes, every week or every first Monday and Thursday" ?

Cobian Backup to the rescue! I was looking for a flexible backup solution and found a gem. Luis Cobian has created a wonderful piece of software. Kudos!

Download Cobian Backup version 11 (Code named Gravity) , the latest as of writing and install it. I am assuming you have your sync tool installed as well.

Once Cobian Backup is installed, in the "Task" menu, click "New Task" or alternatively press Ctrl + A to add a task.


Note the setting "Use Volume Shadow Copy". Using that option, you can be sure that your file can be backed even while it's locked by an application.

After that add a source which could be any file or folder that you want to backup. The destination should be your magic folder (Dropbox/SugarSync Magic Briefcase") or any folder setup to be selectively synced. You can have multiple sources and multiple destinations.



As you can scheduling is very advanced and will cater to almost any need.


There's compression + industry standard encryption too!


with filters!



 I unfortunately cannot go through all the features in Cobian backup due to the sheer size!

So the idea is to use Cobian Backup to backup your files to the synced folder and you have a bullet proof, automated backup!

Download Cobian Backup

Thursday, March 8, 2012

The new iPad : Resolutionary!

No it's not iPad 3 or iPad HD! It's the new "iPad". 

Nothing entirely innovative or NEW, but the spec bump beats most of the tablets out there!




So there's a reason after call Apple calls it resolutionary! The resolution of the retina screen is 2048 x 1536 pixels with a 264 pixels per inch!

Other notable features are the new processor and GPU that Apple calls A5X and it claims to be twice as fast and contains four times the graphical processing power than the Tegra 3. Goodbye Asus Transformer Prime? Probably. It's still dual core, though.

There's a new camera, a 5MP, obviously with 1080p recording and the front VGA camera is still present.

There's 4G LTE connectivity too!

With all the processing power, the battery life still seems to be par with the iPad 2!

The 16GB WiFi Only Model starts from $499. Go, pre-order!

More info on New iPad: The Third Generation article on Gizmodo.

Monday, March 5, 2012

Android Key Lime Pie to follow Jelly Beans!

Yesterday it was confirmed that Jelly Beans will follow Ice Cream Sandwich in the Android sweets related OS naming convention.

However, therage.com has uncovered what's next. I took a wild guess thinking it would be Kola Kubes but it's Key Lime Pie!



Image courtesy of http://kestrachern.net

Sunday, March 4, 2012

It's confirmed! Android 5.0 will be called Jelly Beans

It's been a while since we knew that the next version of Android (After ICS) would be called Jelly Beans. But now, Asus has confirmed it! Speaking to TechRadar, ASUS' Corporate Vice President Benson Lin has said


"Asus is very close to Google, so once they have Android 5.0 I think there will be a high possibility that we will be the first wave to offer the Jelly Bean update."


So the version number 5.0 indeed will be called Jelly Beans.

Also, placed in the middle of Google's private meeting area is a bowl of Jelly Beans!

  google jelly beans
(Image courtesy of theverge.com)

So what's next ? Kola Kubes ?

Information taken from GSMArena.

Monday, February 27, 2012

HTC announces the One Series at MWC

HTC announced the HTC One Series a while ago at MWC held in Barcelona. Four phones were officially announced namely, One X, One XL, One S, and One V.

Sorted by top to bottom specification wise, HTC One V is the smallest of 'em all.

HTC One V


One V runs 1GHz single core processor with 512MB RAM and packs Ice Cream Sandwich inside coupled with their proprietary Sense UI. The resolution is 480 x 800 pixels on a 3.7 inch screen.

See the HTC's One V Page website for more information.


HTC One S


One S packs dual core power clocked at 1.5 GHz with an Adreno 225 GPU. With 1GB of RAM and qHD resolution of 540 x 960 on a 4.3 inch screen, HTC One S takes pictures at 8MP resolution. According to the HTC website, HTC One S supports only microSIM.

See the HTC's One S Page website for more information.

HTC One XL


The XL lives up to the name! Almost similar to the One S but with a beastly 4.7 screen with a 720p resolution and supports LTE!



See the HTC's One XL Page website for more information.

HTC One X








This is the beast that's going to storm the world of Android! Gizmodo calls it the "The Most Exciting Android Phone to Date"

One X (reminds of me of Professor X) is packs serious power inside. When I say serious, it's quad core serious! This beast with such raw power and magnificence was called HTC Endeavor, HTC Supreme and HTC Edge before.

Running on a Nvidia Tegra 3 clocked at 1.5 GHz this phone boasts 1GB of RAM. One X is equipped with the same screen as the One XL.

All these phones run the HTC Sense UI on top of Ice Cream Sandwich (Android 4.0)

HTC One X vs HTC One XL


They look almost the same with One XL packing LTE connectivity which One X lacks. Also One X is running on a quad core while XL is limited to a dual core.

Super IPS LCD2


We are yet to here more information on the Super IPS LCD2 which is the screen technology used in One X and XL.

But  @PaulOBrien  says

"I have never seen a screen like the SLCD2 on the@htc One X. Totally blew me away, INSANE. Made my Galaxy Nexus look 10 years old."



Looks like it's some beauty!

Check HTC  and GSM Arena for more information