Wednesday, March 28, 2012

How to Connect 2 Windows 2008 R2 VisualBox VMs with virtual network

       When you create 2 Windows 2008 R2 VisualBox VMs, you must want to connect them together with virtual network to form a small LAN. Your goal is these 2 VMs must have internet access and be able to access each other by sharing and HTTP. Following are the steps how to setup with VisualBox 4.1.10:

       1. You need to install AD Domain Service and DNS roles on the VM works as domain controller. At the end of this step, you setup your local domain.

       2. Shutdown both VMs, add second network adapter to both VMs selecting "Host-only networking" mode.

       3. Start both VMs, you will find a new network connection. In its property, set DNS server address to your domain controller and disable TCP/IP V6. Restart both VMs.

       4. Join the VM to your domain.
 
       You will still have some small issues, such as, you can not enable network discovering. But actually you can access each VMs' shared resources. Another problem is you can only access the website on the other VM by IP (for example, http://192.162.3.66, rather than http://Yukon). I believe you can figure that out easily.

Sunday, March 25, 2012

Design Patterns: Interface

       The most important concept of OOP is probably interface. I personally think it's the beginning of all OOP magic. The terminology "interface" is independent of certain programming language, although most modern programming languages have a "interface" keyword. A interface is a contract between consumer object and supplier object. For example, you hire a painter to paint your walls, the painter must be competent to paint your walls no matter what his skin color is. So, the "painter" is a contract between you and the person you hire to paint your walls. With this contract concept, you don't have to hire a specific person to do your painting job, you can hire any qualified painter to do it. In OOP, this is called loosely coupled. This is very important for writing extendable, maintainable, testable software.
       Following is a very simple example to show how interface works. We have a IPlan interface for Customer, there are two classes implementing IPlan, named PlanBasic and PlanStandard. Different customers can have different plan. Let's take a look at the code in C#:
       The IPlan interface:
       public interface IPlan
    {
        string PlanName { get;}
        float getMonthlyPayment();
    }

       PlanBasic and PlanStandard:
       public class PlanBasic : IPlan
    {
        public string PlanName
        {
            get
            {
                return "Basic Plan";
            }
        }

        public float getMonthlyPayment()
        {
            float ret = 9.99f;
            return ret;
        }
    }

       public class PlanStandard : IPlan
    {
        public string PlanName
        {
            get
            {
                return "Standard Plan";
            }
        }

        public float getMonthlyPayment()
        {
            float ret = 29.99f;
            return ret;
        }
    }

       The Customer class that is the consumer class of contract IPlan:
       public class Customer
    {
        private string name;
        private IPlan plan;

        public string Name
        {
            get
            {
                return name;
            }
        }

        public Customer(string _name, IPlan _plan)
        {
            name = _name;
            plan = _plan;
        }

        public float getMonthlyPayment()
        {
           return plan.getMonthlyPayment();
        }

        public override string ToString()
        {
            return "Name: " + name + " Plan: " + plan.PlanName + " Monthly Payment: " + getMonthlyPayment();
        }
    }

       Let's write an unit test code to test the interface pattern:
       [TestMethod]
        public void InterfaceTest()
        {
            Customer customer = new Customer("John Smith", new PlanBasic());
            Assert.AreEqual("Name: John Smith Plan: Basic Plan Monthly Payment: 9.99", customer.ToString());

            customer = new Customer("John Doe", new PlanStandard());
            Assert.AreEqual("Name: John Doe Plan: Standard Plan Monthly Payment: 29.99", customer.ToString());
        }
       You can see the customer class don't have to know any information about any specific plan when it is written.

Thursday, March 22, 2012

Retrieving MD5 fingerprint by JDK 1.7 for generating Google Map API key

        When you develop Google Map API application on android platform, Google requires you sign your application with a Map API key and you need to include this key in your activity's layout xml. To signup Google Map API key, you need a Google account and a MD5 fingerprint. Basically, you need two fingerprints, one is for running your application on simulator, the other is for running your application on real Android device. Where do you get your finger print?

        If you use Eclipse, it signs your application with debug key storing in C:\Users\<your username>\.android\debug.keystore. The MD5 fingerprint of this debug key should be used to signup Google Map API key. For your release version, you should create your own release keystore file using JDK's keytool.exe in JDK1.7.0_01\bin folder. Once you have a keystore file, either debug or release, you can use keytool.exe to retrieve the MD5 from the keystore file.

        Retrieving MD5 fingerprint from keystore file on JDK 1.7 is a little bit tricky. You need to use the command like below to retrieve the MD5 fingerprint:

                keytool -list -v -keystore "[the path to your keystore file]"

         The tricky part is you must use "-v" to display all fingerprints, otherwise you only get the SHA1 fingerprint, which is not we need to signup Google Map API.

Thursday, March 15, 2012

Install Snow Leopard 10.6.2 in VisualBox on Windows 7

        To develop software for iOS, you need a Mac system. However, it's too expensive if you buy a Mac system just for learning how to write code on iOS especially when you are not sure if iOS or Mac software development is suitable for your taste. Naturally, virtual machine becomes inevitable inexpensive choice for studying purpose. There are a lots of articles on internet talking about installing Mac OS on virtual machine. This blog is a simple guide based on those articles.

       To install Snow Leopard 10.6.2, we need following software:
              1. Snow_Leopard_Client_Server_10.6.2_SSE2_SSE3_Intel_AMD iso.
              2. Boot loader iBoot (iBoot-Legacy-2.7.2 works for me, someone else may want to try iBoot-3.2.0)
              3. VisualBox (the one I used is 4.1.8).

       Preparation:
              1. Enable the VT-X / AMD-V CPU feature in BIOS of your motherboard.
              2. Create a Mac OS VM in VisualBox. The most important thing is UNCHECK the "Enable EFI" checkbox in system page.

       Install Snow Leopard 10.6.2:
              1. Mount Snow_Leopard_Client_Server_10.6.2_SSE2_SSE3_Intel_AMD iso to VM's DVD drive. Start the VM.
              2. Install snow leopard using default setting.
              3. Unload Snow_Leopard_Client_Server_10.6.2_SSE2_SSE3_Intel_AMD iso and load iBoot-Legacy iso when the installer reboots VM.
              4. Use iBoot boot the installed snow leopard and finish the post install configuration.

       You may need iBoot to boot every time when you reboot the VM.
      
       To check your Mac OS version in terminal:
              $ /usr/bin/sw_vers
              ProductName: Mac OS X
              ProductVersion: 10.6.2
              BuildVersion: 10C540
              $