WordPress Backup

Comments Off on WordPress Backup

WordPress Import/Export
Wordpress allows you to export your ‘content’ in xml format. it then allows you to imports that content in a wordpress instance. The tool allows you to import data from various sources (e.g. Live Journal, Tumblr etc). Seems quite straightforward however the wordpress import/export have some caveats.

  1. It requires you to add users on the site before you import.
  2. If the exported file is very large, the import script may run into your host’s configured memory limit for PHP.
  3. If the import process is run again with the same data file after stopping midway through, it could result in duplicate data, missing data or other errors in the destination database.
  4. Some discussion about whether this process does or does not import images, files (or content in general). In my experience the imported posts have links to content residing in the wp-content of the site that I exported from.

Manual Import Export
I instead choose to do a manual import export of my wordpress blog. Below are the steps that i carried out an exact clone of my blog www.softwareeverydayblog.com on my local box.

  1. Take a mysql backup of the my wordpress DB.
  2. mysql_backup

    mysql_backup2

  3. Manually create a database ‘softwareeveryday’ on my local mysql instance. Import the db using the mysql back script into this fresh db ‘softwareeveryday’ that we just created.
    C:\scipts>c:\wamp\bin\mysql\mysql5.5.24\bin\mysql -u root -p < softwareeveryday.sql softwareeveryday
    Enter password:
  4. Copy the wordpress files using FTP somewhere into our apache home directory. Change wp-config.php to reflect local box settings.
  5. At this point, we bring up our apache server and the wordpress blog should appear. There is one issue, there are yet some references to http://www.softwareeverydayblog.com. We’ll need to make some changes in the database to get rid of those references. Change wp_options table, set wp_options to point to localhost address where option_name is ‘siteurl’, ‘home’.
  6. All the content that I uploaded (like images) is yet pointing to http://www.softwareeverydayblog.com/wp-content/uploads/2013/05/some_image.jpg. Now I notice that within the wp_posts, all posts have href hardcoded to softwareeverydayblog.com. You can use this tool to replaces the urls. The advantage over SQL replace is that the tool know how to handle serialized info in the DB. Refer to stackoverflow answer for more.

If you want to skip the last step of making DB changes, we could change our hosts file so that requests to softwareeverydayblog.com point to 127.0.0.1. I personally prefer to change my hosts file. I also soon realized that it’s NOT possible to redirect using hosts file. For example the following entry is NOT possible.

# redirects not allowed in hosts file
127.0.0.1/softwareeverydayblog/wordpress-3.4.2/		softwareeverydayblog.com

Instead we could add a simple hosts entry, and add redirects in the local apache server to redirect all requests to softwareeverydayblog.com to the wordpress directory.

  1. Add the following entries in hosts file.
    127.0.0.1		www.softwareeverydayblog.com
    127.0.0.1		softwareeverydayblog.com
  2. Add the following entry to httpd-vhosts.conf for redirects. With this all requests to softwareeverydayblog.com on this apache server will be served files from “C:/www/softwareeverydayblog/wordpress-3.4.2” which is our wordpress directory.
    <VirtualHost *:80>
        ServerAdmin webmaster@dummy-host.example.com
        DocumentRoot "C:/www/softwareeverydayblog/wordpress-3.4.2"
        ServerName softwareeverydayblog.com
        ServerAlias www.softwareeverydayblog.com
        ErrorLog "logs/softwareeverydayblog.com-error.log"
        CustomLog "logs/softwareeverydayblog.com-access.log" common
    </VirtualHost>
  3. I also uncommented the following line from httpd.conf
    Include conf/extra/httpd-vhosts.conf

Blog works fine now. Links yet show http://www.softwareeverydayblog.com but files are being served from the local box.

Http Basic Authentication

Comments Off on Http Basic Authentication

Here is a very simple way of providing authentication in your application. Everytime you send a http request to the server, the username and password are sent as part of the HTTP header (in every request). They are encoded using base64, so yes it is NOT a safe way to do things. If you decide to use it, atleast use SSL with it.

  • Users sends a request.
  • Server sends back response code 401 and Http response header WWW-Authenticate = Basic realm=”MyRealm”.
  • Browser receives this (header and response code) and prompts user to enter username and password.
  • Browser sends another GET request but with Http request header Authorization: Basic .
  • Server receives this header, authenticates the user and sends back either a Response code 200 (OK) or 401 (Unauthorized).

If the response is 200 (Ok) browser will cache the username and password so that user doesn’t have to keep reentering it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;
 
public class AuthenticateServlet extends HttpServlet {
 
  private static final long serialVersionUID = 1L;
 
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
    if ( request.getHeader("Authorization") != null ) {
      String auth = request.getHeader("Authorization");
      String coded_user_password = auth.split(" ") [1];
      String decoded_user_password = StringUtils.newStringUtf8(Base64.decodeBase64(coded_user_password));
 
      String username = decoded_user_password.split(":")[0];
      String password = decoded_user_password.split(":")[1];
 
      PrintWriter out = response.getWriter();
      out.println("<p>Username: " + username + "</p><p>Password: " + password + "</p>");
 
    } else {
 
      response.setHeader("WWW-Authenticate", "Basic realm=\"MyRealm\"");
      response.setStatus(401);
 
    }
  }
}

Get the 8.3 (or short file name) of a folder in windows

Comments Off on Get the 8.3 (or short file name) of a folder in windows

It seems like on an NTFS partition there is something called 8.3 Name Creation. Its basically a short file name with a tilde ~. Problem i was facing was how to get the short file name for a folder? One way is just to keep trying different combinations, the other is is using the command dir /x /a. /a includes even hidden files.

XML Namespaces 101

Comments Off on XML Namespaces 101

XML Namespaces also carry typical advantages of using namespaces, i.e. all variables related to a particular category can go under that namespace.

Here is an example of namespaces being used for Spring.

Lets say in an xml file we have information about an html table and a real table.

<root>
  <item>
    <table>
      <name>Ikea Coffee table</name>
      <size>Large</size>
      <html>
        <table>
          <tr>
            <td>Name</td>
            <td>Size</td>
          </tr>
          <tr>
            <td>Ikea Coffee table</td>
            <td>Large</td>
          </tr>
        </table>
      </html>
    </table>
  </item>
</root>

Ok, there is a problem here. Clearly, there should be a distinction between an html table and real table. Namespaces to the rescue.

<root xmlns="http://www.softwareeverydayblog.com/" xmlns:table="http://www.softwareeverydayblog.com/realtable" xmlns:htmltable="http://www.softwareeverydayblog.com/htmltable"</strong> >
  <item>
    <table:table>
      <name>Ikea Coffee table</name>
      <size>Large</size>
      <html>
        <htmltable:table>
          <tr>
            <td>Name</td>
            <td>Size</td>
          </tr>
          <tr>
            <td>Ikea Coffee table</td>
            <td>Large</td>
          </tr>
        </htmltable:table>
      </html>
    </table:table>
  </item>
</root>

This works. An html table (xmlns:htmltable) and a real table (xmlns:table) have their own namespaces. xmlns is the default namespace. By default all the elements without a namespace go into this namespace (e.g. , etc).

Character Encoding

Comments Off on Character Encoding

ASCII uses 7 bits, extended ASCII allows you to use the 8th bit, but that’s NOT a standard. Problems started when bits in ASCII were too less to represent more characters.

How does UNICODE work? Map characters to code points (or numbers). Representation of code points to disk is an independent concern. Different Unicode encoding’s will do it differently. Encoding’s UTF-8 UTF-16 and UTF-32 are most common. Due to this separation of concern if there is an issue with a certain encoding (say UTF-32 takes too much space), it could be phased out without affecting the mappings from character to code points. In a nutshell, you map all chars to numbers or code points and map code point to encoding (like UTF-8,16,32).

Lets look at some terminologies. A coded character set is a set of characters mapped to numbers e.g. ASCII. Code point is a valid integer value that can be mapped to a character in UTF. What is a Code unit? UTF-8 has one code unit. UTF-16 has two code units. UTF-32 has four code units. Get the drift…

UTF32:- Fixed size encoding of 4 bytes. Problem is characters now take 4 times more size!!! so much wastage of space. Files become 4 times bigger. 00000000 00000000 00000000 00000001

UTF16:- Variable length character set. It can be one or two chunks of 16 bits to represent a character. So, at best your files will be 2 times as big (assuming you need just one 16 bit chunk). You also have to deal with problem like endianess. Byte order marks helps us to know endianess. If Byte order mark is not present a reader will assume some endianess. Since UTF-16 is variable length, how do we know if particular char is two chunks or one? It probably uses some marker bits to mark code units, just like UTF-8 does.

UTF8:- There was a need for compatibility with ASCII. Meaning if i create a file using ASCII character set, even if i read it using some unicode encoding, it should work. Here comes UTF-8 to the rescue. Variable length encoding, a UTF-8 character can have 1,2,3 or 4 bytes. Since its variable length, how do we know how many bytes does a character represent? Every byte has some bits set in the most significant bit positions.

0xxxxxxx -> 1 byte unicode char
110xxxxx (leading byte) 10xxxxxx (continuation byte) -> 2 byte unicode char
1110xxxx (leading byte) 10xxxxxx (continuation byte) 10xxxxxx (continuation byte) -> 3 byte unicode char
11110xxx (leading byte) 10xxxxxx (continuation byte) 10xxxxxx (continuation byte) 10xxxxxx (continuation byte) -> 4 byte unicode char

How many possible code points could exist? Lets take a character that needs 4 bytes to be represented in UTF-8. It will be of the form 11110xxx 10yyyyyy 10zzzzzz 10mmmmmm. Now the code point is xxxyyyyyyzzzzzzmmmmmm which are 21 bits. So max number of characters (or symbols) that UTF can describe is 2^21. Other bits are all marker bits.

There is an important reason why continuation bytes are marked. Assuming you connect to a stream of bytes, you might come accross a continuation byte first, so you cannot start reading from there. You need to wait until a new character starts.

Upto a code point of 127, single byte representation, there is no difference between ASCII mappings and UNICODE mappings. This means that if you pass an standard ASCII file to a UTF reader it will read it correctly. Note: it will NOT be able to read extended ASCII file.

Decoding UTF-8
Say we had a UTF-8 char represented with two code units 110xxxxx 10yyyyyy. You strip the marker bits and merge the code point bits. It will represent code point xxxxxyyyyyy. Now goto unicode mapping and check what character xxxxxyyyyyy stands for.

Encoding UTF-8
Say you want to encode अ which has code point 905 (U+0905). Binary for 905 is 1110001001. Lets populate the trailing byte first 10-001001 (10xxxxxx). The starting byte will be 110-01110 (110yyyyy), notice we adding an extra 0 in the beginning. Ok so UTF-8 encoding for अ will be 11001110 10001001. Here is the unicode standard for Devanagri.

Characters in Java
Java character’s are 2 bytes. Yes, even an ASCII char that could have taken just one byte will take two bytes in memory. Advantage? Well, your single char object in java can refer to code points from 0 to 2^16. That means a single char can represent a lot of chars (even foreign chars). What happens to code points more than 2^16 (e.g. Deseret, LONG I U+10400), we will use two char values to represent this. Read this document to know more about representing supplementary characters. In java, Characters that are in the range U+10000 to U+10FFFF are called supplementary characters. The set of characters from U+0000 to U+FFFF are sometimes referred to as the Basic Multilingual Plane (BMP)

Here is an example code to write hello world in Devanagari script in a text file.

//Hello World in devangri script. 
String content = "\u0939\u0947\u0932\u094B \u0935\u0932\u0921";		
File newTextFile = new File("hinditest.txt");
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newTextFile), "UTF8"));
out.append(content).append("\r\n");	 
out.flush();
out.close();

Here is an Awesome lecture series on Unicode

Dining Philosophers

Comments Off on Dining Philosophers

Dining Philosophers is one of those classical problems to understand (and resolve) deadlocks in software. Lets look at examples were we cause deadlocks, come up with a solution to avoid deadlocks and finally come up with a solution to avoid starvation. Also, here is a great link describing many more ways to solving the Dining Philosophers problem.

Here is a simple class to represent a fork on the table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Fork {
 int forkid;
 Semaphore mutex=new Semaphore(1, true);
 public Fork(int forkid) {
    this.forkid = forkid;
  }
  public String toString() {
    return String.valueOf(forkid);
  }
  public void pickUpFork() throws InterruptedException {
    mutex.acquire();
  }
  public void putDownFork() {
    mutex.release();
  }
}

Deadlock Philosopher
This guy blindly picks up the left fork and then the right fork. We create a circular wait condition. Imagine scenario where all philosophers have a left fork and waiting for the right fork.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class DeadlockPhilosopher implements Philosopher {
 String philosopher;
 Fork left;
 Fork right;
 public DeadlockPhilosopher(String philosopher, Fork left, Fork right) {
  this.philosopher = philosopher;
  this.left = left;
  this.right = right;
 }
 @Override
 public void run() {  
  while( true ) {   
   try {    
    left.pickUpFork();
    System.out.println(philosopher + " picked up " + left);
    Thread.sleep(5000); // this will just cause the deadlock quicker!  
    right.pickUpFork();
    System.out.println(philosopher + " picked up " + right);
    System.out.println(philosopher + " is eating...");
    right.putDownFork();
    System.out.println(philosopher + " put down " + right);
    left.putDownFork();
    System.out.println(philosopher + " put down " + left);   
    System.out.println(philosopher + " is thinking...");
    Thread.sleep(5000); // think!   
   } catch (InterruptedException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }   
  }
 }
 @Override
 public String getPhilosopher() {
  return philosopher;
 }
}

2. Sensible Philosopher (No deadlocks)
Ok how do we break deadlocks? We get rid of the circular wait condition. We give forks (or resources) numbers. We make sure that philosophers access lowered numbered resources over higher numbered resources. In this solution all philosophers are right handed except E who is left handed. That’s enough to break the circular wait. Another solution could also be to make two adjacent philosophers request forks in different orders (i.e. make them right handed and left handed). The 5th and the 1st philosopher will be same handed however other philosopher being different handed, the circular wait will be broken.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class OrderlyPhilosopher implements Philosopher {
 
  String philosopher;
  Fork left;
  Fork right;
 
  public OrderlyPhilosopher(String philosopher, Fork left, Fork right) {
    this.philosopher = philosopher;
    this.left = left;
    this.right = right;
  }
 
  @Override
  public void run() {
 
    while( true ) {
 
      try {
 
        Fork pickUpFirst = null;
        Fork pickUpSecond = null;
 
        if ( left.forkid < right.forkid ) {
          pickUpFirst = left;
          pickUpSecond = right;
        } else {
          pickUpFirst = right;
          pickUpSecond = left;
        }
 
        pickUpFirst.pickUpFork();
        System.out.println(philosopher + " picked up " + pickUpFirst);
        Thread.sleep(5000);  // this will just cause the deadlock quicker!
        pickUpSecond.pickUpFork();
        System.out.println(philosopher + " picked up " + pickUpSecond);
        System.out.println(philosopher + " is eating...");
        pickUpSecond.putDownFork();
        System.out.println(philosopher + " put down " + pickUpSecond);
        pickUpFirst.putDownFork();
        System.out.println(philosopher + " put down " + pickUpFirst);      
        System.out.println(philosopher + " is thinking...");
        Thread.sleep(5000);  // think!
 
      } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
 
    }
 
  }
 
  @Override
  public String getPhilosopher() {
    return philosopher;
  }
}

3. Fair Philosopher (No deadlocks, No Starvation, Average Performance)
Ok, so in all these cases there are chances that starvation might occur, i.e. some philosopher might not get a chance to eat as often. How do we tackle this? Well instead of having semaphores on the forks, why not have semaphores on philosophers themselves Check if both forks are empty at any point of time, if not wait, else use. Make sure neighbors are not eating. Look at Solution #7: Preventing Starvation A Little Better to know what i am talking about.

1. Ok, we know in order to avoid spurious wake ups we should put wait() in a while loop but what happens when you wake a philosoper up and its a spurious wake up? He waits() again and goes back to end of waiting list. That’s not good, instead lets 1.) make sure we don’t have spurious wake ups and 2.) Output an error (and return from function) if he wakes up but cannot eat so that we know we are causing spurious wakeup’s in the code.

2. So you MUST wait if people are waiting (even if you can eat). You will only be awakened, by a thread if you can eat. So when you are awakened, as a safety measure check if you can eat, output error message (and return) if you can’t and go ahead and eat!

3. Check most waiting thread when a philosopher finished eating or starts eating. In both scenarios, there is a possibility of the most waiting thread to start eating. Remember, until the most waiting thread cant eat, nobody behind him can eat too.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
class Conductor {
 
  Philosopher philosophers[];
  List<String> eating;
  List<String> waiting;
  Lock lock=new ReentrantLock(true);
  Condition con=lock.newCondition();
 
  public Conductor(Philosopher philosophers[]) {
    this.philosophers = philosophers;
    this.eating = new ArrayList<String>();
    this.waiting = new ArrayList<String>();
  }
 
  public void eat(String philisopher) throws InterruptedException {
    if ( !validPhilosopher(philisopher) )
      return;
    //System.out.println(philisopher + " wants to eat...");
    //System.out.println(eating.size() + " people eating...");
    lock.lock();
    try {
      if ( waiting.size() > 0 || eating.contains(philisopher) || eating.contains(getLeftNeighbhor(philisopher)) || eating.contains(getRightNeighbhor(philisopher)) ) {
        System.out.println(philisopher + " blocked...");
        waiting.add(philisopher);
        con.await();
        //System.out.println(philisopher + " awakened...");
        waiting.remove(philisopher);
 
        if ( eating.contains(philisopher) || eating.contains(getLeftNeighbhor(philisopher)) || eating.contains(getRightNeighbhor(philisopher)) ) {
          System.out.println(philisopher + " awakened however he cant eat! Problem!!!");
          return;
        } 
      }
      System.out.println(philisopher + " eating...");
      eating.add(philisopher);
 
      // CHECK for WAITING threads Q, and signal last waiting thread, if you can handle it!
      if ( waiting.size() > 0 ) {
        String waitingPhil = waiting.get(0);
        if ( !eating.contains(waitingPhil) && !eating.contains(getLeftNeighbhor(waitingPhil)) && !eating.contains(getRightNeighbhor(waitingPhil)) ) {
          //System.out.println(philisopher + " eat signalling awake to " + waitingPhil);
          con.signal();
        }
      }
    } catch ( Exception e ) {
 
    } finally {
      lock.unlock();
    }
  }
 
  public void finisheat(String philisopher) throws InterruptedException {
    lock.lock();
    if ( !validPhilosopher(philisopher) || !eating.contains(philisopher) )
      return;
    eating.remove(philisopher);
 
    // CHECK for WAITING threads Q, and signal last waiting thread, if you can handle it!
    if ( waiting.size() > 0 ) {
      String waitingPhil = waiting.get(0);
      if ( !eating.contains(waitingPhil) && !eating.contains(getLeftNeighbhor(waitingPhil)) && !eating.contains(getRightNeighbhor(waitingPhil)) ) {
        //System.out.println(philisopher + " finisheat signalling awake to " + waitingPhil);
        con.signal();
      }
    }
    lock.unlock();
  }
 
  public boolean validPhilosopher(String philisopher) {
    for ( int i = 0 ; i < philosophers.length ; i++ )
      if ( philosophers[i].getPhilosopher().equals(philisopher) )
        return true;
    return false;
  }
 
  public String getLeftNeighbhor(String philisopher) {
    int i = 0;
    for ( ; i < philosophers.length ; i++ ) {
      if ( philosophers[i].getPhilosopher().equals(philisopher) )
        break;
    }
 
    if ( i == 0 )
      return philosophers[philosophers.length-1].getPhilosopher();
    else
      return philosophers[i-1].getPhilosopher();    
  }
 
  public String getRightNeighbhor(String philisopher) {
    int i = 0;
    for ( ; i < philosophers.length ; i++ ) {
      if ( philosophers[i].getPhilosopher().equals(philisopher) )
        break;
    }
 
    if ( i == philosophers.length-1 )
      return philosophers[0].getPhilosopher();
    else
      return philosophers[i+1].getPhilosopher();      
  }
}
 
class ConductorPhilosopher implements Philosopher {
  String philosopher;
  Conductor c;
  public ConductorPhilosopher(String philosopher, Conductor c) {
    this.philosopher = philosopher;
    this.c = c;
  }
 
  @Override
  public String getPhilosopher() {
    return philosopher;
  }
 
  @Override
  public void run() {
    try {  
      while ( true ) {
        //System.out.println(philosopher + " wants to eat...");
        c.eat(philosopher);
        //System.out.println(philosopher + " eating...");
        Thread.sleep(new Random().nextInt(2000));
        c.finisheat(philosopher);
        Thread.sleep(new Random().nextInt(2000));
        //System.out.println(philosopher + " thinking...");
      }
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }    
  }
}

Here is the philosophers table and main method for running the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class PhilosphersTable {
 
  ExecutorService executorService;
  Philosopher philosophers[];
 
  public PhilosphersTable(Philosopher philosophers[]) {    
    this.philosophers = philosophers;
    this.executorService = Executors.newFixedThreadPool(5);
 
    for ( int i = 0 ; i < philosophers.length ; i++ )
      executorService.submit(philosophers[i]);
  }
 
  public static Philosopher[] getConductorPhilosophers() {
 
    ConductorPhilosopher philosophers[]=new ConductorPhilosopher[5];
    Conductor conductor=new Conductor(philosophers);
 
    philosophers[0] = new ConductorPhilosopher("A", conductor);
    philosophers[1] = new ConductorPhilosopher("B", conductor);
    philosophers[2] = new ConductorPhilosopher("C", conductor);
    philosophers[3] = new ConductorPhilosopher("D", conductor);
    philosophers[4] = new ConductorPhilosopher("E", conductor);
 
    return philosophers;
 
  }
 
  public static Philosopher[] getOrderlyPhilosophers() {
 
    Fork forks[]=new Fork[5];
    forks[0]=new Fork(1);
    forks[1]=new Fork(2);
    forks[2]=new Fork(3);
    forks[3]=new Fork(4);
    forks[4]=new Fork(5);
 
    OrderlyPhilosopher philosophers[]=new OrderlyPhilosopher[5];    
    philosophers[0] = new OrderlyPhilosopher("A", forks[0], forks[4]);
    philosophers[1] = new OrderlyPhilosopher("B", forks[1], forks[0]);
    philosophers[2] = new OrderlyPhilosopher("C", forks[2], forks[1]);
    philosophers[3] = new OrderlyPhilosopher("D", forks[3], forks[2]);
    philosophers[4] = new OrderlyPhilosopher("E", forks[4], forks[3]);
 
    return philosophers;
 
  }
 
  public static Philosopher[] getDeadlockPhilosophers() {
 
    Fork forks[]=new Fork[5];
    forks[0]=new Fork(1);
    forks[1]=new Fork(2);
    forks[2]=new Fork(3);
    forks[3]=new Fork(4);
    forks[4]=new Fork(5);
 
    DeadlockPhilosopher philosophers[]=new DeadlockPhilosopher[5];    
    philosophers[0] = new DeadlockPhilosopher("A", forks[0], forks[4]);
    philosophers[1] = new DeadlockPhilosopher("B", forks[1], forks[0]);
    philosophers[2] = new DeadlockPhilosopher("C", forks[2], forks[1]);
    philosophers[3] = new DeadlockPhilosopher("D", forks[3], forks[2]);
    philosophers[4] = new DeadlockPhilosopher("E", forks[4], forks[3]);
 
    return philosophers;
 
  }
 
}
 
public class DiningPhilosphers {
 
  public static void main(String args[]) {
 
    //PhilosphersTable philosopherstable=new PhilosphersTable(PhilosphersTable.getDeadlockPhilosophers());
    //PhilosphersTable philosopherstable=new PhilosphersTable(PhilosphersTable.getOrderlyPhilosophers());
    PhilosphersTable philosopherstable=new PhilosphersTable(PhilosphersTable.getConductorPhilosophers());
  }
 
}

Reentrant vs NonReEntrant locks

Comments Off on Reentrant vs NonReEntrant locks

Java 5 has a ReentrantLock implementation of the Lock interface. Meaning if the same thread tries to acquire the lock again, it will allow that. It does have information about which thread is holding the lock. The following code will not cause a deadlock.

1
2
3
4
5
6
7
8
9
10
11
12
Lock l=new ReentrantLock(true);
public void funcReentrantLock(int level) {
  System.out.println("entered funcReentrantLock: " + Thread.currentThread().getName() + " at level " + level);		
  if ( level == 0 )
    return;
 
  l.lock();
  System.out.println(Thread.currentThread().getName() + " locked at level " + level);
  funcReentrantLock(level-1);
  l.unlock();
  System.out.println(Thread.currentThread().getName() + " unlocked at level " + level);
}

Semaphore’s on the other hand are non-re entrant meaning if the same thread locks and then re-locks it will cause a deadlock. If the same thread tries to acquire the lock again, it wont allow that. It does not have information which thread is holding the lock. The following code will cause a deadlock.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Semaphore available = new Semaphore(1, true);
public void funcNonReentrantLock(int level) {
  System.out.println("entered funcNonReentrantLock: " + Thread.currentThread().getName() + " at level " + level);		
  try {
    if ( level == 0 )
	return;
    available.acquire();
    System.out.println(Thread.currentThread().getName() + " acquire at level " + level);
    funcNonReentrantLock(level-1);
    available.release();
    System.out.println(Thread.currentThread().getName() + " release at level " + level);
  } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
}

The synchronized keyword is a re-entrant lock. Why use one over the other? Well for starters it seems like re-entrant lock performs much better. Also, the lock interface provides some other api methods like trylock() which acquires the lock only if it is free at time of invocation.

Deadlocks in java

Comments Off on Deadlocks in java

Ok, lets start with seeing a simple example of how deadlocks can be created in java. Simple way to achieve this is to try to acquire locks in different order and you will see it happen.

Thread1 – Resource 1, Resource 2
Thread2 – Resource 2, Resource 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Resource1 {
}
 
class Resource2 {
}
 
class DeadlockThread1 implements Runnable {
 
  public void run() {
    while ( true ) {
      synchronized ( Resource1.class ) {
        System.out.println("DeadlockThread1 acquired Resource1");
        synchronized ( Resource2.class ) {
          System.out.println("DeadlockThread1 acquired Resource1 and Resource2");
        }
      }
    }
  } 
 
}
 
class DeadlockThread2 implements Runnable {
 
  public void run() {
    while ( true ) {
      synchronized ( Resource2.class ) {
        System.out.println("DeadlockThread2 acquired Resource2");
        synchronized ( Resource1.class ) {
          System.out.println("DeadlockThread2 acquired Resource2 and Resource1");
        }
      }
    }
  } 
 
}
 
public class Deadlock {
  public static void main(String args[]) {
    Thread dlock1=new Thread(new DeadlockThread1());
    Thread dlock2=new Thread(new DeadlockThread2());
    dlock1.start();
    dlock2.start();
  }
}

Release of Locks:- Ok, say that we acquired resources in the same order, in what order do we release them? In other words, is one of these sequences better than the other?

Sequence 1

  • lock R1
  • lock R2
  • unlock R2
  • unlock R1

Sequence 2

  • lock R1
  • lock R2
  • unlock R1
  • unlock R2

Sequence 1 is recommended. Sequence 2 doesn’t cause issues iff you don’t acquire/unacquire locks between unlock R1 and unlock R2. For e.g.

  • lock R1
  • lock R2
  • unlock R1
  • In the mean time another thread got hold of R1 and is waiting for you to release R2
  • lock R1 <- you are trying to re-lock R1, its a deadlock!

Quartz within Tomcat using XML job descriptions

Comments Off on Quartz within Tomcat using XML job descriptions

Objective is to make a counter using Quartz. Step by step we’ll do the following

  1. Create a custom context listener to initialize a counter,
  2. Create quartz properties file and xml jobs file,
  3. Schedule a job that would keep incrementing the counter,
  4. Create a servlet to view that counter.

1. Create a custom context listener to initialize a counter:- If we need to listen to various events within the servlet container event listeners give us the flexibility to do that. We have Servlet context-level (application-level) events and Session-level events. For more, look at the documentation.

“After the application starts up and before it services the first request, the servlet container creates and registers an instance of each listener class that you have declared. For each event category, listeners are registered in the order in which they are declared. Then, as the application runs, event listeners for each category are invoked in the order of their registration. All listeners remain active until after the last request is serviced for the application.”

We declare all the listeners we need in the xml file. QuartzInitializerListener will create the scheduler (MyScheduler) from the quartz.properties and set scheduler factory instance into the servletContext.

1
2
3
4
5
6
7
8
9
10
11
12
<context-param>
  <param-name>initcounter</param-name>
  <param-value>100</param-value>
</context-param>
<listener>
  <listener-class>
	com.napp.listener.MyServletContextListener
  </listener-class>	
  <listener-class>
	org.quartz.ee.servlet.QuartzInitializerListener
  </listener-class>
</listener>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class MyServletContextListener implements ServletContextListener {
  public static int counter = 0;
  final static Logger logger = LoggerFactory.getLogger(MyServletContextListener.class);
 
  public void contextDestroyed(ServletContextEvent arg0) {
    // TODO Auto-generated method stub
  }
 
  public void contextInitialized(ServletContextEvent arg0) {
    String initCounter = arg0.getServletContext().getInitParameter("initcounter");
    if ( initCounter!= null) {
	counter = Integer.parseInt(initCounter);
	logger.info("Set counter-" + counter);
    } else {
	logger.info("Coulnt read init counter, default is 0");
    }
  }
}

2. Create quartz properties file and xml jobs file:- Quartz.properties that contains information that the Scheduler Factory uses to create a scheduler.

1
2
3
4
5
6
7
8
org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames = jobs.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
#org.quartz.plugin.jobInitializer.scanInterval = 1
#org.quartz.plugin.jobInitializer.wrapInUserTransaction = false
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<job-scheduling-data
  xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
  version="1.8">
  <schedule>	
    <job>
	<name>hello-world-job</name>
	<group>hello-world-group</group>
	<description>Print a welcome message</description>
        <job-class>com.napp.job.HelloWorldScheduledJob</job-class>
        <job-data-map>
          <entry>
	    <key>param1</key>
	    <value>value1</value>
          </entry>
        </job-data-map>
    </job>
    <trigger>	
	<cron>
	  <name>helloworld-trigger</name>
	  <job-name>hello-world-job</job-name>
	  <job-group>hello-world-group</job-group>
	  <!-- It will run every 5 seconds -->
	  <cron-expression>0/5 * * * * ?</cron-expression>
	</cron>	
    </trigger>
  </schedule>
</job-scheduling-data>

3. Schedule a job that would keep incrementing the counter:-

1
2
3
4
5
6
7
public class HelloWorldScheduledJob implements Job {
  final static Logger logger = LoggerFactory.getLogger(HelloWorldScheduledJob.class);
  public void execute(JobExecutionContext arg0) throws JobExecutionException {
    logger.info("Incremening counter.");
    MyServletContextListener.counter = MyServletContextListener.counter+1;
  }
}

4. Create a servlet to view that counter:-

1
2
3
4
5
6
7
8
9
10
11
12
13
public class SimpleServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  public void doGet(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
    rsp.setContentType("text/html");
    PrintWriter out = rsp.getWriter();
    out.println("<html>");
    out.println("<head><title> Simple Servlet </title></head>");
    out.println("<body>");
    out.println("<p>Counter - " + MyServletContextListener.counter + "</p>");
    out.println("</body></html>");
    out.close();
  }
}

You can download the sample-web-app from here.

Creating installer using IzPack and Maven

Comments Off on Creating installer using IzPack and Maven

I recently created a wordpress installer using IzPack and uploaded the source code on Google Code. It will 1.) Download wordpress source code, 2.) copy wordpress folder into any directory (usually its the www directory) and 3.) Create an empty DB into MySql. For all practical purposes in doesn’t do much, however it was a good learning experience of IzPack which i am sharing here. Even the izPack Maven plugin website has some good examples.

Here’s how the entire build process goes:-

  1. As soon as you update, http://core.svn.wordpress.org/tags/3.4.2/ is downloaded into externals\wordpress-src. If you’re not sure how this works look at svn externals.
  2. Compile all your izpack related sources. Add dependencies to pom.xml . Add all dependencies that you need. Include izpack-standalone-compiler for all izpack related classes.
    1
    2
    3
    4
    5
    6
    
      <dependency>
        <groupId>org.codehaus.izpack</groupId>
        <artifactId>izpack-standalone-compiler</artifactId>
        <version>${izpack-standalone.version}</version>
        <optional>true</optional>
      </dependency>
  3. Every file that we would need while building (and eventually running) the installer, we would need to copy them into some folder (lets call it a staging folder). We use the maven-antrun-plugin to copy izpack related xml files and wordpress source code. We use the maven-dependency-plugin to copy dependencies.
  4. Finally, the izpack-maven-plugin is called to create our standalone installer. It asks for the staging folder location which we provide. Remember at this point the staging folder contains everything we need to be packaged in the installer.
  5. install.xml – Notice how the jar elements are included. You MUST include all the jar files that you would need while running the installer.
    1
    2
    
     <jar src="../wordpressinstaller-1.0-SNAPSHOT.jar"/>
     <jar src="mysql-connector-java-5.1.21.jar"/>

    In our example we have included mysql-connector-java-5.1.21.jar (remember it’s in the same folder as install.xml, the staging folder) for access to mysql. We also included wordpressinstaller-1.0-SNAPSHOT.jar which actually had all the izpack related components. Since it goes in the target folder which is one hierarchy above the staging folder, we need to use ‘../’. Remember, whatever files we need while running the installer, we need to include using the jar element, just including them in the dependencies in pom is not enough.

    Add all the panels in order using the panels element. All these are inbuilt izpack panels. The userinput panels are specified within userInputSpec.xml. The MySqlDetails user input panel will be displayed only if the condition wordpress-izpack-installer.condition.dbinstallation is true (, when checkbox is checked).

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    <panels>
      <panel classname="HelloPanel"/>
      <panel classname="PacksPanel"/>
      <panel classname="TargetPanel"/>
      <panel classname="UserInputPanel" id="SpecifyWebServerDir"/>
      <panel classname="UserInputPanel" id="MySqlDetails" condition="wordpress-izpack-installer.condition.dbinstallation">
        <validator classname="com.validators.MySQLAccessValidator"/>
      </panel>
      <panel classname="SummaryPanel"/>
      <panel classname="InstallPanel"/>
      <panel classname="ProcessPanel"/>
      <panel classname="SimpleFinishPanel"/>
    </panels>

    The packs panel displays a list of all the packs declared in the install.xml, in this case just one option of copying the wordpress source code will be displayed. The dir=”wordpress-src” is releative to the staging directory where we have copied the wordpress-src during our build process.

    1
    2
    3
    4
    5
    6
    
    <packs>
      <pack name="Wordpress Sourcecode" required="no" >
       <description>Wordpress source code!</description>
       <fileset dir="wordpress-src" targetdir="${wordpress-izpack-installer.variable.wwwdir}/${wordpress-izpack-installer.variable.wordpressfoldername}" />
      </pack>    
    </packs>

    The InstallPanel actually copies all packs data to respective folders when installer is run. The ProcessPanel calls all the processes from the ProcessPanel.Spec.xml one after the other. Usually creating services, creating and running db scripts etc is done here. Validators are used to make sure users enter correct information while installation. We have added a MySQLAccessValidator to the panel, to validate the mysql connection when next is clicked. izPack documentation of validators.

  6. When the standalone jar is built it literally packages everything within itself, all packs, all necessary jars (unpackaged) etc. Thus, while running you dont need to package anything else except that jar itself.

    wordpressinstallerjar

IzPack workflow on paper:-

izpackworkflow

Older Entries