Saturday, January 16, 2016

Cyanogenmod Android

What is Cynogenmod?

It is a kind of mobile operating system and works on the top of well-known Android. Android is very popular operating system and most of the devices today run Android. Android is Unix based operating system and is open source. Due to its availability of source code, the users of android can test different varieties.

Basically, CM is developed after Google, who actually controls the Android development, releases the source code. The new Android version, google adds new features, and then releases them on their nexus devices. But we know there are millions of devices that run by Android, and the google stock android may not be capable of handling all different devices, so CM basically tweaks, adds and makes it better and more easier.


Why Cynagenmod ?

There are some reasons why somebody wants to install CM version of Android. I try to list out some reasons which came in my mind.

1) To get or test latest Android features: 

I own Nexus 4 device, and I used to get each and every updates google made in android from Android 4.2 to 5.1. It is very nice to be among the first to test the current version of Android. That did not last longer until google released Android 6.0 and I did not get the update, Initially, I thought to download and install manually using root, but it is problematic. Anyhow I want to test the latest version of android, I heard alot about the battery improvements in version 6.0. Then I got Cynagenmod website, where I could fulfill my desire of installing the latest Android. I already have installed it in my nexus 4 and its working like a charm. And good news is that they update very frequently, every week.

2)  Low Resource Devices

I own an Android device with very low RAM, space, and weak processor. It has Android 4.0.4
preloaded with a lot of bulky applications included which I could not even remove. A lot of backgrond applications and services which I never use, never need, they just consume memory, and drain battery. That device was running very slow and I even could not use that for any purpose. Luckily, CM has an image for this device, and successfully could flash the image into my device. That also ran without any problem and very fast. At least I can use this device as a camera,a nativator,remote mouse,radio,music player.



3)  Very Nice Wiki

The CM website is very nice and informative. Each device has their own wiki, and the instllation manual and procedure is so clear that even a normal person can do this. I think they have utilised a lot of resources for that to test for every device, I also heard they have colleced a huge amount of money for this project.


4) Easy to Reinstall

Most of the when we install new version of android, there are chances we got stuck somewhere, or even we could get out of it, that mean the device will never be usable. But the story is totally different with Cynagenmod flash. The recovery image basically runs without any problem and very easy to use. Even it was possible to use touch in recovery console. I just copied some installer to the device and everything else done by recoverer. If there are some problems or incompatabilities, they we can easily replace with another installer.

Finally, it was nice experience for me to flash Android device, and install with total new Cyanogenmod and its new features, Initially I thought I could not succeed, it was not that difficult, rather very intersting. I see some bright future for this operating system if it goes this way. I highly recommend at least to test this new mobile operating system.










Friday, April 17, 2015

Cool Shell Utilities

In this page, I will try to include the cool bash shell utilities that will help us a lot.


Task 1. Search all files in sub directories in iterative way

find . -type f -name '*.mp3'  | while read i
do 
      echo $i
done


Alternative:

for file in 'find . -iname "*.mp3" 
do 
    echo $file
done 

Task 2. Create python/scala directly runnable from script

For Linux system, bash is very powerful. We can do almost anything using shell scripts. And Ubuntu comes with python already installed. So, to write python scripts is very straightforward. And if you have already installed JVM, then a more powerful and faster programming language "Scala" makes it even better to write scripts.  So, basically, I wanted to write, a python or scala program and run from a shell script.

Python:
 //1.py
#!/usr/bin/env python
print("hellow")

#get aruguments using argv array
# more python commands

Run: ./1.py




Scala:
//1.scala
#!/usr/bin/env scala
println("hellow")


Run: ./1.scala

or 

//1.sh
#!/bin/sh
exec scala $0 $@
!#
// Say hello to the first argument
println("Hello, " + args(0) + "!")


This one is interesting!

It can be executed as normal script

sh  1.sh 
or 
./1.sh

File extensions do not matter after we define environment


Task 3:  Get run time of the script

If we need to calculate how much time it takes to get an script executed, we can do that using date  command as follows.

#!/bin/bash
START_TIME=`date +%s`
# Run commands
# Run commands
END_TIME=`date +%s`
EXECUTION_TIME=`expr $END_TIME-$START_TIME`
 
Task 4: Pass parameters to bash script

We frequently need to send command arguments to the bash script and this is quite easily implemented in bash script. We execute the command with parameters.

$COMMAND [ARGUMENTS]

In command itself, we should receive the parameters and the parameters are automatically stored in the variables such as
$1, $2

That means, if we send two parameters, these parameters are stored in variables $1,$2.

We can get the number of parameters using the variable $#.



FORCE PUSH TO REMOTE 

Forcefully push your local contents to the remote repository:
(Caution: the remote repository files could be replaced by the local files)

git push --force-with-lease





Saturday, January 10, 2015

Your first web application


I am quite interested to know the web principles, and develop web applications. I have done a lot researches regarding the web programming languages, their suitability even the support provided by web hosting companies. I will try to include my ideas and knowledge I gained recently; although this single article is not fair enough to cover everything.

TOOLS

Before I start, I will tell you the development platform I am bound to. Basically, I do not really like purchasing software or tools for development as a free-lancer. So, I am quite bound to open source software, libraries or tools. So, everything I explain here is tested for linux based Ubuntu Operating System, probably, it works for all linux based system, but no guaranty.

So, if we have the latest version of Ubuntu(14.04 LTS), we can start creating our first dynamic website.  Then we install the following application softwares:

1) Apache Server: The apache server which is container of our web pages i.e. it serves the request for the web-pages it contains.  Its very easy to install it from terminal.

sudo apt-get install apache2

After installation completes, we can test it by browsing localhost, it display the apache server information.

2) MySql: This is quite popular opensource database and almost all hosting companies provide mysql database. And it is really simple and lightweight, so easy for startutp.

sudo apt-get install mysql-server

There is a very nice client administrator tool for mysql called mysql-workbench. It make everything simple, otherwise you can just use mysql command lines via terminal.

3) PHP: This is very very popular language for dynamic website programming. I guess everybody who involves web development is familiar with php to some extent. It is quite easy, interesting and we see the output at the time of writing. Generally, we do not prefer writing codes using core php, so there are a bunch of frameworks which makes the development task easier with manageable and scalable source code. 

sudo apt-get install php5

Now it is good time to test whether php installed properly,or not. We create a sample php file and place it under /var/www/html which is root directory and browsing that file.

A note: If you are using php encryption module, then  you have to install it

sudo apt-get install php5-mcrypt

After installation we have to enable it.

sudo php5enmod mcrypt

Then restart apache server

sudo service apache2 restart


FRAMEWORKS

There are many php frameworks to simplify the development php work. I have started from Laravel, although it is advanced framework, I got a lot of problems because of composer and other files management. The number of files the framework contains is so huge, it is really difficult to handle and manage those files, even if one file is missed or corrupted(sometimes composer itself does that), it can not be compiled. Another problem is I could not run composer to the hosting server where I do not have root access to the server.

Then I tried CodeIgniter, its really simple and huge community and good documentation. So, as a beginner, I started development of application using Codeigniter. I got a lot of help and support from Codeigniter community.

Step 1:

You have to download the Codeigniter, extract it to /var/www directory. Since this is not root directory, so, user can not view from browser. Suppose the name of directory is CI_BASE. After we extract it, copy the index.php file from Codeigniter to root directory i.e. /var/www/html. Now, we modify the system and application path based on the current directory structure.

For example: replace  $system_path = 'system'; by $system_path=__DIR__.'/../CI_BASE/system';

Step 2

We have to define database and controller configurations. Database is quite simple and straightforward. (CI_BASE/application/config/database.php)

In the config file (CI_BASE/application/config/config.php), for simplicity, we modify the following values:

$config['base_url'] = '';
$config['index_page'] = '';

In $CI_BASE/application/config/autoload.php,  you define to auto load library and helper classes:

$autoload['helper'] = array('url','form');
$autoload['libraries'] = array('database','email');


SECURITY

Before deploying a web application, we have to define some sort of security mechanism, so that our deployed files are secure. That is maintained by .htaccess file placed in root directory where we define the files or folder that are accessible from the browser.

A sample .htaccess file looks something like this:

 <IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On

   RewriteCond $1 !^(index\.php|css|user_guide|images|robots\.txt)
   RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

It is defining the rewrite condition and rewrite rule. 

If you need to redirect any http traffic to https traffic, we have to include .htaccess file needs some modification as follows:

 <IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On
        RewriteCond %{HTTP:X-Forwarded-Proto} !https 

RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
 RewriteCond $1 !^(index\.php|css|user_guide|images|robots\.txt)
 RewriteRule ^(.*)$ /index.php/$1 [L]
 </IfModule>


APACHE CONFIGURATION

Okay we are in the final step. We configure Apache (/etc/apache2/apache2.conf) and modify Directores, in our case it is /var/www. So, we replace that one by the following
<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>



Last but not least, we activate the apache mod_rewrite module:

sudo a2enmod rewrite


Please do not forget to restart Apache server.   There are already welcome controller and welcome views defined, so, when the root URL is access it displays the content of welcome view. 







FINAL NOTE: VIRTUAL HOST

It is not necessary to place all files in /www/html folder. You can define any other directories(working directory e.g.) to host the web contents.

We need two steps to implement that:

Step 1: /etc/apach2/apache2.conf : Add Directory element as follows

<Directory /home/krishna/openshift/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>


Step 2: /etc/apache2/sites-available :  Replace the DocumentRoot with your document root. For example:

  DocumentRoot /home/krishna/openshift/pm

Now, restart apache server, then this time when you browse localhost, it will search for the index page in the folder /home/krishna/openshift/pm.













Monday, September 22, 2014

10 reasons why windows 8 sucks

Microsoft windows operating system is very popular and majority of people are using this. Initially, this operating system looks good, and pretends to be user friendly. I also started learning computer using windows and I am pretty sure the previous windows OS's were quite nicer. 

When I started using computer, Windows 98 was very popular at that time. Later, we got Windows 2000 which was little bit improved version from windows 98. Then a drastically improved OS Windows XP appeared in the market. That got popular also and I used windows XP until Window 7 released. Yes, I preferred windows 7 very much and started using it from RC version. Till Windows 7, I did not have any complain with Microsoft operating system.

Now, lets go to the actual topic: Windows 8. Why the fuck Microsoft developed this opearting system. I have no idea, why they want something new which sucks. I feel very uncomfortable using this OS. I think you guy also have the same situation; I have not found any guys who is happy with this OS.

Now, I will list out some points which are worth mentioning here:

1) Cost: 
Everybody pays money to use windows OS because it is not free. So, even if you buy a laptop, you have to pay for the OS, even if you don't want to use it. I rarely used windows 7 and windows XP that came with my laptops. Its rare that we can buy a laptop without windows system. So, windows is making a sort of compulsion to pay for its operating system even if you are not interested.  It sounds like b**sh*t.

2) Booting:

Windows always try to be on the top of everybody, and not friendly with other operating systems. You know what I mean. Its the real problem when you install windows system, you have to compromise by erasing the previously installed operating systems. I have this problems several times, I install windows, my previously installed ubuntu and mint system gone! Then again I have to install those. And the latest operating system i.e. Window 8 even doesn't have that facility. We can not even install other operating system so easily. I believe this means microsoft is forcing users to use windows operating system. I got a new notebook with windows 8 pre-installed, and I did hard disk partition and installed linux mint. Then the problem was, I have to go to book and change settings there to select linux mint. WTF! I tried to display list of OSs to display while booting, but I was unsuccess. So, I just go on using window 8 which is not my favourite OS. 


3) Upgrade:

The upgrade system of windows also sucks. You are hurry to go somewhere, and want to turn off your computer, then computer says "don't turn off you machine, updates are going on!" Why they don't want to let you go after turning off??
I have waited many times till updates finishes, that was very bad experience. And updates are too slow, I have very fast (50 mbps) internet speed, still it takes several hours for updates.  


4) Bulky
Windows system are bulky, they use hard disk space of around 20 GB after you completely install it. So, to have a windows system installed you have to have at least 25 GB of hard disk space. If you wanna install more software, you need more. And it is true, that minimum space required for windows system is more than enough for linux based system. I have separated 30 GB space for linux mint and using for more than one year, I am not needing any extra memory. 

5) Upgrade to Windows 8.1 
I don't like so call Metro system of windows 8. So, I wanted to upgrade my system to get window 8.1 which I heard is improved and windows 7 like desktop is shown in the first place. Unfortunately, I got a lot of problems, till now I could not do it. Its seems like driver related problem but the acetic question is why they do not these things before they deploy it, they are money to the user and again there are so many problems. I have spend almost 20 hours upgrading to windows 8.1, still no luck. I did not face any problems with ubuntu and linux mint which are open source free operating system. 


6) High System Requirements
The system requirements are exponentially increasing from windows 98 to windows 8. Really, I dont like it, windows system sucks a lot of system resources such as memory, CPU. There is clear different when compared with linux mint which looks like windows, it consumes far less memory and CPU doing the same task. 

7) Internet Explorer:
One of the unanswered question from microsoft is why they can not improve internet explorer. It looks like 98's design, and seems like there is no improvement. Same bulky, not looking so user friendly like modern browsers- mozilla or chrome. After I install windows system, my first task is download chrome from internet explorer and disable internet explorer. I feel terrible even if I accidently click on internet explorer, it annoys me to properly close it.

8) Unwanted Services 
Everybody knows windows system run a lot of unwanted programs and services in the background. One of the reason why windows is slower is because of this. Although user can turn off unwanted service, but it is very hard to determine which service they want and which they do not want. 

9) Antivirus
Windows OS is prone to virus attack. So, you have to buy antivirus software along with windows. I guess there is some collaboration between antivirus company and microsoft, that is why, antivirus company prepares antivirus for microsoft windows. Interesting, why can not  microsoft make their system secure enough so that there is no need of antivirus. 

10)  Slow

Windows operating systems are slow as compared to other systems, booing itself seems slower (windows 8 did some trick to make it faster booting, that is just trick, not reality) and installation, updation process are so lengthy, I hate it. 

Tuesday, August 12, 2014

Java Performance Monitoring and Testing

After a couple of months gap, I am continuing this blog. This time, the topic is not about development, this is a bit complicated but interesting topic, and I have spent quite a lot of time for performance monitoring in java.

Java is very popular programming language and we can develop any type of application from desktop, web, mobile, embedded and many more. The use of advanced IDE's (Eclipse for example) make the development process very fast, easy and with a lot of customization preferred by developer. The software development, nowadays, is so fast that most of the time, we forget about performance that is how well and how consistently the application works for the long run after deploying. 

We have to deploy applications which has to run for several months or even several years! To make the development process faster, we use a lot of third party libraries which might not have been fully tested to work for long run. Sometimes, we program incorrectly, or we forget to properly manage the created objects, which actually affects the application for long run, for example, memory leak. It is the most prevalent issue found in applications which run for  a long time. This article does not go deep into performance analysis theory, but gives quick idea on how performance analysis in java is carried out using available tools.

1) JConsole

JConsole is a simple performance monitoring tool which displays alot of useful information of a running java application. I am using Java 1.7 and JConsole is included in this version. You have to be sure, I guess old java version do not support JConsole, if that happens, you have to upgrade you java.

For local testing, its really straight forward, JConsole provides list of java applications and we have to select one. Then JConsole displays memory, threads, classes related to that application. A nice presentation of these information in graph or table, makes it easy to see if there is any subtle performance problem. If we let it running for long time, we can easily notice if there is any memory leak or system problem with heavy consumption of processor by the application, maybe my huge number of threads creation.

   Fig:   JConsole

We can remotely connect to the application running in different system through network. For that we have to enable Remove Management and Monitoring Service (JMX) in JVM. If you are running tomcat, its rather straight forward. We just append the following text in the setenv.sh file in $CATALINA_BASE directory.

export CATALINA_OPTS="-Dcom.sun.management.jmxremote
                                               -Dcom.sun.management.jmxremote.port=
                                               -Dcom.sun.management.jmxremote.authenticate=false
                                               -Dcom.sun.management.jmxremote.ssl=false
                                               -Djava.rmi.server.hostname="


If you want to increase heap memory size, you can append the following line.(this is optional)

export CATALINA_OPTS="$CATALINA_OPTS -Xms512m"

export CATALINA_OPTS="$CATALINA_OPTS -Xmx1024m"

Then you have to restart tomcat server. Then we can remotely connect the application running in remote system by using the IP and PORT specified above. 

Since JConsole is quite simple, it is used to find out if there are any performance issues in the application. It is the first step for performance monitoring. Normally we run JConsole for a couple of days, and study the performance graph. From the graph, we can speculate whether there are any performance issues or not. If we know that there are performance issues, then the next step is to go for more advanced monitoring tool which is capable of finding specific object or specific thread causing the performance issues. For example, if the application is spawning a lot of threads and creating CPU overhead, then we have to find out which specific threads are creating such problem, or  a lot memory is consume by objects and Garbage collector is not successful to release the memory, then we have to find out which specific object is causing such a memory leak.

2)  JVisualVM

This is more advanced tool and this is built-in with latest Java. It includes all functionality provided by JConsole, apart from that we can create thread dump, heap dump and application snapshot. The dump files can be analysed later using some other dump analyzer tools to get more performance monitoring. The dump analyzer tools such as JProfiler, YourKit are very nice commercial tools(you can have around 10 days of trial use). I feel really excited if we can find some free analyzer, then we should not bother about paying and licencing.


For remote connection, as mentioned in JConsole, we have to enable JMX in JVM.

3) Automation Approach

Yes, we can dump heap memory as a file, if we want to test it for whole night, but nobody stays in front of computer dumping memory.. lol. So, we should have mechanism that we can dump heap memory in regular interval, so that we can figure out which object is getting bloated with time; also can track the number of threads, and their characteristics.

There is a command line tool jmap which dumps the heap memory. Jmap needs process id to work, so we find out pid of the application from the list of java application using "jps" command from terminal.

#Find id of java applicaiton
jps 
#dump
jmap -dump:format=b,file=FILE_NAME PROCESS_ID


    This is complete script that can dump heap memory in regular intervals until specified dump files are     obtained.
 #!/bin/bash
#file:heapdump.sh
if [ $# -eq 0 ]; then
    echo >&2 "Usage: heapsump.sh  [ [  [  ] ] ]" 
    echo >&2 "    Defaults: run_user=root, count = 10, delay = 0.5 (seconds)" 
    exit 1
fi
pid=$1                  # required
user=${2:-root}         # defaults to root
count=${3:-10}          # defaults to 10 times
delay=${4:-0.5}         # defaults to 0.5 seconds
while [ $count -gt 0 ]
do
    sudo -u $user jmap -dump:format=b,file=dump.$pid.$(date +%Y%m%d%H%M%S.%N) $pid
    sleep $delay
    count=$(( $count-1 ))    
done
     Call this script this way: 
sh heapdump.sh PID [root [15 [10]]]
     All arguments are optional except PID.   Other parameters are user, count and interval(delay for next       dump).

4) Eclipse Memory Analyzer

  Till writing this text, I found Eclipse Memory Analyzer plugin which can be installed using
  http://download.eclipse.org/mat/1.4/update-site/

After installing this, we open memory analyzer perspective where we can dump heap memory from locally running VM. Not only this, you can open previously saved dump files and analyze. So, this is a great plugin, really great because it is free! I have to explore more to write more. So, later I will try more interesting features provided by this plugin. 

I could not perform online analyzing using Eclipse Memory Analyzer. However, this is possible using JProfiler. I guess Eclipse Memory Analyzer is sufficient for fully analysis of heap dumps. Again, JProfiler sucks when the heap size is huge(around a couple of gigs).

Friday, February 14, 2014

Defining Persistent Objects

The persistent objects can be defined either in xml file, or using annotation. The annotation method is easy to define simple relationship while defining in xml file provides more flexibility.

This article demonstrates, rather shortcut, how we can create persistence objects:

//Base Class
@MappedSuperclass
public abstract class BaseClass implements Interface{
@Id
@Column(name = "id")
@GeneratedValue
@Override
private int id

@Transient
private String unwantedField;

public int getId(){
    return id;
}

public void setId(int id){
    this.id=id;
}
}

//Child Class
@Entity
@Table(name = "table_name")
public class ChildClass extends BaseClass{

@Column(name = "user_name")
private String useName;

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
@JoinColumn(name = "id")
@Override
private List comments;
}


//Associated class
@Entity
@Table(name = "another_table")
public class AnotherClass {
@Id
@Column(name = "comment_id") //this can not be "id", because "id" is referenced from ChildClass
@GeneratedValue
private int id;
@ManyToOne
@JoinColumn(name = "id") //this id is child class identifier column
private ChildClass child;

}

In above example, we implemented one to many relationship from ChildClass to AnotherClass instances.(Getters and setters not shown above). So, the "id" in "another_table" is foreign key from the table "table_name".

[Note: Annotations should be either at properties definition or at getters(CAN'T  be mixed).]





Accessing Persistent Objects

In the previous article, I have discussed how to create session instance. This article demonstrates how session object can be used to access persistence objects, insert, update or remove objects.
[Assumption: we have session object, and we have to close transaction and session after we complete operation]

1) Insert, Update or Delete persistence object

  session.saveorUpdate(object);
  session.delete(object);

2) Retrieve a collection of data
   i) If you want all persistent objects
       //using hibernate query
       List list=session.createQuery(HQL).list();
       //using sql 
       List list=session.createSQLquery(SQL).list();//gets raw
       List list=session.createSQLquery(SQL).addEntity(className).list();//entity objects

   ii) If you want selected object only (similar to WHERE clause in SQL)
 
     String HQL="SELECT * FROM ENTITY entity WHERE entity.fieldName=:fieldname";
     session.createQuery(HQL).setString(fieldname, value).list();
 
     For SQL, we create the sql query to get the list.      

    Criteria is more elegant for selection.
   
      Criteria criteria = session.createCriteria(className);
       criteria.addOrder(Order.desc(fieldName));
       criteria.add(Restrictions.ge(fieldName, value));
       criteria.add(Restrictions.le(fieldName, value));
      criteria.setMaxResults(limit);
      
      // Also define union and intersection
       Disjunction disjunction = Restrictions.disjunction();
       disjunction.add(Restrictions.eq(fieldName, "#BLABLA#"));
       criteria.add(disjunction);      
       Conjunction conjunction=Restrictions.conjunciton();
       conjunction.add(Restrictions.eq(fieldName, "#BLABLA#"));
       criteria.add(conjunction);    

        criteria.setFetchMode("whatever", FetchMode.SELECT);

     List objects= criteria.list(); 

3) Retrieve specific object

We have to note here that all persistence objects have their own id. So,  with the id, we can get object

 Object object=session.get(className, id);