Monthly Archives: March 2013

Taking backup of WordPress site

For sometime now I was planning to try out some things/experimentation with this blog. But before that I wanted to take a backup of whole thing in case I mess up stuff.

Now when I say taking a backup, I am not much worried about wordpress files as such as I know I can just download and install wordpress anytime. Most of the cpanels come with wordpress installation these days or the software can be downloaded from http://wordpress.org/download/. In case you have a customized theme or images, you can simply ftp into your web root folder (www), and copy the wordpress directory to a location where you want to back it up.

For me the only important thing was database. Mostly posts, comments, category and tags. So here are the steps to do that.

  1. Login to your phpmyadmin (or any other way you access your database/ check with your service provider)
  2. Go to your wordpress database, it has tables most probably in form of wp_name. We are concerned about- wp_comments, wp_postmeta, wp_posts, wp_terms, wp_term_relationships, wp_term_taxonomy.
    Note, I am not copying user related or wordpress related information as it is always good to create new information when installing new setup. I am only concerned about the real stuff- post related stuff to be backed up.
  3. Now Click export at the top panel  in PHPMyAdmin. Use  custom as export method.
    Choose the tables you want to export (mentioned above), default format is sql, we can leave that as it is.
    The only thing of concern on this page is “Maximal length of created query” field. I figured out that the default value was too low for me. Adding 2 extra zeros to default “50000” helped me. If your posts are larger, you might need one more zero. The way to check it, is to open the exported sql file and see wp_posts data is available.
  4. Click on Go and you have your backup sql script ready.

Getting the script is one thing and using it another. We need to make sure that the script we just created will actually work. That is simpler than taking backup.

I created a new wordpress installation locally and imported the sql file created above into my new installation.

mysql -u kamal -p -v -h localhost wordpress <D:\data.sql

And this is how my backup blog was ready. That’s it

Understanding Strategy Pattern

Let’s first try to understand what a strategy is. In simple worlds a strategy is a plan (for something). Or a little more elaboration “a plan, method, or series of maneuvers or stratagems for obtaining a specific goal or result”.

For purpose of understanding, let’s take a simple example. Say we want to create a piece of code to calculate the tax for a given salary. An added complexity is that code should work for multiple countries. All these countries will have different strategy (formula) for tax calculation. Based on the country provided as input, we will need to change our strategy for tax calculation at the run time.

So how does strategy pattern help us? Let’s solve the above mentioned problem with tax calculation to make it clear.

Strategy Pattern

The above image makes it clear. We have 2 strategies to calculate tax, one for India and other for Germany. While initializing the strategy pattern class object(TaxCalculator), we will define which strategy is to be followed.

TaxCalculator taxCalculator(new IndiaTaxStrategy());
taxCalculator.calculateMyTax();//calculateMyTax will simply call the parent class calculate tax method with the income

Spring Implementation of Dependency Injection

In last post I explained what is dependency injection (DI). Now I will focus on how the concept is implemented in Spring framework. If we are clear on the concept of DI, remaining is piece of cake.

There are 2 ways to implement DI in Spring

1. Using annotations

Spring is basically annotation driven language (see here). @Autowired is the annotation which will help us inject dependencies in Spring. @Autowired will tell the code to look for given object somewhere in code. The dependency injection can be implemented in 3 ways

a. Using my previous Employee- Address example for dependency injection, adding address dependency through constructor

Class Employee

{

public Address address;

public String name;//other stuff

@Autowired
Employee(Address address)
{
this.address=address
}

}

And In application context xml add

<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

	<bean id="EmployeeBean" class="com.test.Employee">
		<property name="name" value="kamal" />

	</bean>

	<bean id="AddressBean" class="com.test.Address">
           <property name="street" value="43rd" />
		<property name="apt" value="29" />
		<property name="city" value="New york" />
	</bean>

b. using Setter- similar to constructor

Class Employee

{

public Address address;

public String name;//other stuff

Employee()
{
}

@Autowired
public void setAddress(Address address)
{
this.address=address;
}

}

c. Using Field

Class Employee

{
@Autowired
public Address address;

public String name;//other stuff

Employee()
{
}

}

2. Using XML configuration (application context)

Same as above (almost), we will not use @Autowired but add the dependency in the xml itself

<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

	<bean id="EmployeeBean" class="com.test.Employee">
		<property name="name" value="kamal" />
<property name="address" ref="AddressBean" />

	</bean>

	<bean id="AddressBean" class="com.test.Address">
           <property name="street" value="43rd" />
		<property name="apt" value="29" />
		<property name="city" value="New york" />
	</bean>

Finally to run your example add these lines to code

ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {“SpringBeans.xml”});

Customer cust = (Customer)context.getBean(“EmployeeBean”);

Further References

http://www.vogella.com/articles/SpringDependencyInjection/article.html

http://www.mkyong.com/spring/spring-auto-wiring-beans-with-autowired-annotation/

More on Dependency Injection

Sometime back I talked about dependency injection. Dependency injection is a design pattern that implements inversion of control principal. That is, you take out a dependency from within a class and inject it from outside.

So lets say there are 2 classes, Employee and Address, say Employee is dependent on Address

Class Employee

{

public Address address;

public String name;//other stuff

Employee()

{

address=new Address(‘some values here’);

}

}

Here we are leaving the responsibility of creating the address with Employee class. Using DI (Dependency Injection) we will pull out this responsibility of creating the address object from Employee class and give it to calling/initializing class/container.

There are 2 ways to implement this DI concept

1. Through constructor:

class Employee{

public Address address;

Employee(Address address)

{

this.address=address;

}

}

2. Using a setter method:

Class Employee

{

public Address address;

public String name;//other stuff

Employee()

{

}

public void setAddress(Address address)

{

this.address=address;

}

}

Lets make it a bit more interesting. Say we have a AddressFactory which can create either a LocalAddress or PermanentAddress. Both LocalAddress and PermanentAddress implement Address Interface.

In case of non DI scenario

Class Employee

{

public Address address;

public String name;//other stuff

Employee()

{

//check if employee has local address

address=new AddressFactory.getAddress(“LocalAddress”);

//else case get permanent address

}

}

In this case DI becomes more useful as Employee class does not need to worry about the type of address which will be provided by calling class or container. No need to change the code given for DI implementation, it will work fine.

Spring MVC- Getting Started

For understanding anything it is best to gets hand on. Let us try creating a simple spring app using spring MVC framework (This tutorial assumes Eclipse IDE)

0. Create a sample J2EE project (or Spring project if you already have it installed)

1. Download Spring http://www.springsource.org/spring-community-download and include jars to lib folder.

2. Modify web.xml to direct all the requests ending with .do to spring MVC’s fromt controller servlet (DispatcherServlet)


  <servlet>
    <servlet-name>springapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>springapp</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

3. Spring by default assumes webapplication context to be present in <servletname>-servlet.xml. In this case create springapp-servlet.xml.

4. Spring is mostly annotation driven, to take advantage of same add following to springapp-servlet.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:mvc=”http://www.springframework.org/schema/mvc” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:p=”http://www.springframework.org/schema/p” xmlns:context=”http://www.springframework.org/schema/context”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd”
>

<!– JSR-303 support will be detected on classpath and enabled automatically –>
<mvc:annotation-driven />
<!– Activates various annotations to be detected in bean classes –>
<context:annotation-config />

<!– Tell Spring where to automatically detect controllers –>
<context:component-scan base-package=”spring.sample.controllers” />
<!– Define Simple View Resolvers –>
<bean id=”viewResolver” class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>
<property name=”viewClass” value=”org.springframework.web.servlet.view.JstlView”></property>
<property name=”prefix” value=”/WEB-INF/jsp/”></property>
<property name=”suffix” value=”.jsp”></property>
</bean>

</beans>

We have done two things, first, we told spring that all our controllers will be in spring.sample.controllers and second look for view files under /WEB-INF/jsp folder

5. As we are going for annotations driven approach, we can use any POJO file as controller. It just needs to be in spring.sample.controller.

package spring.sample.controllers;

@Controller
/*this url automatically gets redirected here*/
@RequestMapping(value = “/mylink”)
public class SampleController {

@RequestMapping(method = RequestMethod.GET)
public ModelAndView login() {
String message = “Welcome to the Spring 3.0 version!”;
return new ModelAndView(“welcome”, “message”, message);
}

6. And finally a simple welcome.jsp

<%@ page language=”java” contentType=”text/html; charset=UTF-8″
pageEncoding=”UTF-8″
%>
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Welcome to Spring</title>
</head>
<body>${message}
</body>
</html>

Infosys launches Big Data Edge

Infosys has launched “Big Data Edge” tool for analyzing large amount of data and extracting information. The data analysis tool claims to find required information in 40 % lesser time compared to existing solutions. It is backed by 50 customizable dashboards and 250 built-in algorithms to provide it a world class performance.

It is good to see some innovative products coming out of India. More info available here

http://www.infosys.com/newsroom/press-releases/Pages/bigdataedge-rapid-business-insights.aspx

http://www.infosys.com/industries/financial-services/features-opinions/Pages/change-game-big-data.aspx

Implementing Scalability

These days there is a lot of buzz around the word ‘scalability’ in IT world. The concept is actually not new, I have been designing scalable systems for last 9 years, but the idea has definitely changed since then.

How meaning of scalability has changed in last few years?

If 5-6 years back, I was creating a system to support say 10K users, and someone would have told me to make it scalable, I would have thought of making the system it in such a way that it can support double or may be 4 times or max 10 times the users in next 3-4 years. But with the applications like facebook, amazon, ebay, twitter the idea about scalable system is different. Now the user base can increase exponentially in matter weeks. And that is what every organization wants.

What is the impact of change?

Impact of the change is that  now you do not want a system which will need a good amount of change if your user base is increasing. Earlier, as number of users used to grow slowly, it will give you time to think, design, redesign, upgrade the system, but now, as user base can increase with much more speed, you want a system which can scale up within minutes and it should be able to it automatically.

How to achieve scalability in today’s world?

As the demand has changed, so has technology. Cloud computing has made it much easier for us to create scalable systems. Key component choices to be made while creating a scalable solution

1. Design: Design of application/ code should be able to handle infinite load.

2. Database: If you are expecting your data load to grow beyond a few million, you might want to go for NOSQL over RDBMS.

3. Hardware: you should be able to add in new hardware and replicate the application on new servers within minutes. Cloud system can help you here.

4. Load balancing: If our application is getting distributed/replicated over multiple servers, we will need to take care of load balancing so that no server will choke.