13 Jun 2016

UML - Unified Modeling Language

UML has two types of diagrams - 

  1. Structure diagrams - defines the static structures of the design - Class diagram 
  2. Behavior diagrams - defines the activities of the design - Sequence diagram, activity diagram, use case diagram.

1. UML Class diagram structures - A structure diagram

Conceptual model - Helps to understand the entities and the interaction between them.

You must know the following structures before you claim that you know UML class diagrams!!!


  1. UML building blocks
  2. Rules to connect the building blocks
  3. Common mechanism of UML

+ - Public access specified in class diagram.
# - Protected access specified in class diagram.
- - Private access specified in class diagram.

In class diagram the abstract class is defined by writing the class name in italic.

Implementing an Interface is called as Realization in UML class diagram. i.e Car realizes a Vehicle behavior. 


Default value to an attribute in a class


Represent Static method or attribute in a class


      To represent static method or attribute in a class diagram underline the specific method or attribute in the class diagram.





References:



2. UML Sequence diagram structures - a kind of Interaction Diagram

Message can be of two types

  • Synchronous - Represented by solid arrow mark 

  • Asynchronous - Represented by stick arrow head









References:

http://www.ibm.com/developerworks/rational/library/3101.html

3 Jun 2016

Composition vs Aggregation in Association.

Association is a term used to define two related classes in Object oriented Environment.

Aggregation and Composition defines how the two classes are related.

Aggregation - Aggregation says that two classes are related but each individual class has its own existence. For Example : School and Student, these two classes are interrelated, School has Students and Student belongs to a School. But if the School is closed that does not implies that a Student does not exist. The Student can join other School.

Aggregation is mentioned as a hollow diamond ending arrow in UML - class diagram.

Composition - Composition defines a "Has a" relationship between two classes. Common example of composition is Human and Human heart. If Human dies then the heart will die certainly.

Composition is mentioned as a filled diamond ending arrow in UML - class diagram.



Note: In the above diagrams the diamond should be towards Contained class. i.e Car as engine so the diamond should be close to Car.

3 Aug 2015

Configuring Virtual Hosts as Proxy Server on Linux Mint with Apache2.4

Configuring Virtual Hosts as Proxy Server on Linux Mint with Apache2.4

To configure a virtual host as proxy server we use Apache as a virtual host and as a proxy server to redirect requests to JBOSS application server. In this regard we separate the  whole process into multiple steps as below

  1. Installing Apache
  2. Configuring Virtual Hosts in Apache
  3. Enabling and configuring Proxy server in Apache

1. Installing Apache

              As specified in the header we are doing the configuration in Mint, so we user apt-get to install Apache2 as below

#apt-get install apache2

2. Configuring Virtual Hosts in Apache

        Copy the following block in to the file "/etc/apache2/sites-enabled/000-default.conf"

         <VirtualHost *:80>
                  ServerName www.example.com
                  ServerAdmin webmaster@localhost
                  DocumentRoot /var/www/html
         </VirtualHost>

      "*" in the above configuration implies all the requests to this host i.e all the requests to this server with 80 port number will be served by this virtual host.
      "ServerName" directive specifies the name server identifies itself with.

3. Enabling and configuring Proxy server in Apache

                To enable Proxy in Apache use the command a2enmod as shown below

#a2enmod
        The above line will show you modules that are supported by Apache and lets you select the modules to be enabled. So enter the following options to enable 
proxy proxy_ajp proxy_http rewrite deflate headers proxy_balancer proxy_connect proxy_html

              To configure the above Virtual host as proxy change the Virtual Host block to the following


<VirtualHost *:80>
    ServerName www.example.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
   
    ProxyPreserveHost On
    ProxyPass /examples http://localhost:8080/examples
    ProxyPassReverse /examples http://localhost:8080/examples 
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
         "ProxyPreserveHost" directive uses the incoming request's header for proxy redirections.
         "ProxyPass" directive maps /examples to our local server URL "http://localhost:8080/examples"
          "ProxyPassReverse" adjusts the response header URL sent from the reverse proxied server.


Reference docs:

https://www.digitalocean.com/community/tutorials/how-to-use-apache-http-server-as-reverse-proxy-using-mod_proxy-extension

https://httpd.apache.org/docs/2.2/vhosts/examples.html

7 May 2015

JSON library Usage

JSON Library usage for replacing the value of particular key in JSON object.


JSONObject: JSONObject is an unordered collection of key value pairs.

Ex:{
         "Employee1": {"Name":"John", "EmpId":"1002", "Address":"California" }
         "Employee2": {"Name":"Alex", "EmpId":"1001", "Address":"Mexico" }
         "Employee3": {"Name":"Luci", "EmpId":"1003", "Address":"New york" }
     }

With the above JSONString create a JSONObject.

String strEmployees ="{
         \"Employee1\": {\"Name\":\"John\", \"EmpId\":\"1002\", \"Address\":\"California\" },
         \"Employee2\": {\"Name\":\"Alex\", \"EmpId\":\"1001\", \"Address\":\"Mexico\" },
         \"Employee3\": {\"Name\":\"Luci\", \"EmpId\":\"1003\",\"Address\":\"New york\" }
     }";

JSONObject employess = new JSONObject(strEmployees);

To replace the details of "Employee1" in the above JSONString, use the JSONObject employees and method put with key value pair, which replaces if the key is already present in the JSON, otherwise it puts the new entry into the JSONObject.

System.out.println(employees.getJSONObject("Employee1"));

Output:

{"Name":"John", "EmpId":"1002", "Address":"California" }

Now to change Employee1 to {"Name":"Stacy", "EmpId":"1005", "Address":"Texos" }

Create a new JSONObject with the above details.

JSONObject employee1 = new JSONObject("{\"Name\":\"Stacy\", \"EmpId\":\"1005\", \"Address\":\"Texos\" }");

employees.put("Employee1", employee1);

Now print the output of employee1

System.out.println(employees.getJSONObject("Employee1"));

The output will be as below.

 {"Name":"Stacy", "EmpId":"1005", "Address":"Texos" }




7 Jan 2015

JAVA Management package

Management package in java consists of classes that has managed beans for environment of JAVA in the system. The following is a small example of what this Management package can do:


import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class Toworld {
        public static void main(String args[]){
            OperatingSystemMXBean os =  ManagementFactory.getOperatingSystemMXBean();
            Method[] methods = OperatingSystemMXBean.class.getDeclaredMethods();
            for( int nMethodCntr = 0; nMethodCntr < methods.length ; nMethodCntr++ )
            {
                Method method = methods[nMethodCntr];
                try
                {
                    System.out.println(method.getName()+": "+method.invoke(os));                 
                }
                catch( IllegalArgumentException e )
                {
                    e.printStackTrace();
                }
                catch( IllegalAccessException e )
                {
                    e.printStackTrace();
                }
                catch( InvocationTargetException e )
                {
                    e.printStackTrace();
                }
            }
           
    }
}


The following will be the output:

getName: Linux
getAvailableProcessors: 4
getArch: amd64
getVersion: 3.6.11-4.fc16.x86_64
getSystemLoadAverage: 0.25
 

6 Nov 2014

How to get the method name of the current method that is running in JAVA?

The following code helps you to identify what is the method that is being executed in JAVA at runtime:


StackTraceElement[] ste = Thread.currentThread().getStackTrace();
String strMethodName = ste[1].getMethodName(); // Get this method name

If you specify 2 in place of 1 in the second statement, it will give you the method that has called this method and so on in the stack.

10 Jul 2014

Pass by reference in JAVA methods

This post will deal with how the objects passed to a JAVA method will be accessed inside the method and what the method can actual do with the arguments and what it cannot do.

    Consider the following example:

/*
    This is a plane Java object we will be using to express how this object is referenced and how the        String inside this class will be accessed
*/

class Obj
{
    String strVal;

    public Obj()
    {
      
    }
  
    public Obj(String str)
    {
        this.strVal = str;
    }
  
    public String getStrVal()
    {
        return strVal;
    }

    public void setStrVal(String strVal)
    {
        this.strVal = strVal;
    }
  
}

/*
    This is the main class where we will be playing with Objects of Obj class to learn how Java considers the pass arguments. This class mainly tries to exchange the objects, inturn the strings the objects holder to swap the contents.
*/
public class PassByRef
{
   /*
      This method tries to swap the objects that are being passed as arguments to this method.
   */
    public void exchange(Obj objA1, Obj objB1)
    {
        Obj objTemp = new Obj();
        objTemp = objA1;
        objA1 = objB1;
        objB1 = objTemp;       




   













    }
    
  /*
     This method tries to swap the String in the passed arguments to this method.
  */
    public void exchangeString(Obj objA2, Obj objB2)
    {
        String strTemp = objA2.getStrVal();
        objA2.setStrVal(objB2.getStrVal());
        objB2.setStrVal(strTemp);




   













    }
  
    public static void main(String[] args)
    {
        Obj objA = new Obj("FirstString");
        Obj objB = new Obj("SecondString");




       














       PassByRef pbr = new PassByRef();
        pbr.exchange(objA, objB);



      













        
        System.out.println("String in objA="+objA.getStrVal()+" String in objB="+objB.getStrVal());
        pbr.exchangeString(objA, objB);
        System.out.println("String in objA="+objA.getStrVal()+" String in objB="+objB.getStrVal());
    }
}
   The output of the above program being as follows:

 String in objA=FirstString String in objB=SecondString
 String in objA=SecondString String in objB=FirstString


 In the above main method after initializing the objA and objB with String "FirstString" and "SecondString" respectively, we pass these objects into method exchange of class
PassByRef, here the method tries to change the object references that are passed to this method, but this will not be taken back to the calling method. So the new reference is not reflected in the method main.
Where as in the method exchangeString, the String contents of the object that is passed by main  is changed and the main method is referencing the same object, so the changed contents can be got from main.