Wednesday, March 12, 2025

JWT Encoder/ Decoder

JWT Decoder and Encoder

JWT Decoder and Encoder

Decode JWT


            

Encode JWT


            

Tuesday, October 8, 2013

WHAT IS THERE IN CLOUD FOR US



Cloud Computing, the name may have adopted because of its internet centric nature. And internet is pictorially represented as cloud and in cloud computing everything is over internet. Cloud computing is nothing but some service offer to you over the internet be it some virtual machine, some platform or some software as well.

Why there is so much fuss about the cloud computing and everyone thinks everyone has to move to cloud sooner or later to survive.

To understand this let us take an example of electricity in our house. Imagine if in every house we have to generate electricity by our self, the amount of complexity involve and more over what about failover. What if my personal power plant inside my house failed, I will be without electricity or maybe I have to have a backup plan. This would have made our life miserable. But luckily we are not creating electricity by our own we are consuming service provided by the electricity board. We consume their service to run appliance in our house be it television, microwave or a geyser. We have a standard socket that gives AC current (of some standard output), all we know is the socket we are not bother about how the electricity is generated whether is hydro electric, nuclear power , wind power or coal power. The abstraction here makes our life easier.
Let us come to how our IT departments works, we generate our own power. We have stacks of server, data storage possible every company from small to larger enterprise has a server room with stacks of server and wires everywhere and bunches of IT service people are working there day and night in order to keep it up and running. There is lots of complexity in it. We either rent the machine or buy them. When we need more machines we again rent more or buy some more. This takes lot of time and it is probably not done by click of one button. And moreover the machine are not efficiently used , I bet lot of times you would think if some machines are there really used or laying there just like that. And for developers we have to unnecessary get coupled with the IT infrastructure for own need. We should have concentrated on business logic not building our infrastructure stack. I often believe developers should not even worry about the load balancing or scaling of the application server. 

Would not it be nice if something like electricity board were there for our rescue?  We will just plug-in to some socket to get the service without worrying about how the service is created.  For infrastructure, I just plug in to infrastructure socked to get some machine, plug into another socket to get the platform I need to run my code. This is where cloud computing come for the rescue.

Cloud provides are centralized infrastructure with massive datacenter, who can deliver massive scalable service for your need.  As they are organised and specialized they will surely do better than what we do in our small clousy server room.  And their service is cost effective as they charge per usage, so no more worrying about any unused machines lying in my server room.

Some cloud offering is public. A public cloud is one based on the standard cloud computing model, in which a service provider makes resources, such as applications and storage, available to the general public over the Internet. Some clouds are private cloud. Private cloud (also called internal cloud or corporate cloud) is a marketing term for a proprietary computing architecture that provides hosted services to a limited number of people behind a firewall. Some providers like HP provide private cloud space for companies who are sensitive to put their data across geographical location or wants to have control over their data. Some cloud is hybrid as name suggests combination of private and public cloud

Cloud offers three flavours of services:
IAAS: Infrastructure as a service: Ask some virtual machine and we get the machine. Customize the machine and hit the replicate button to get as many as you like. Amazon EC2 and SP are the most successful IAAS provider
SAAS: software as a service. There is some predefined software in the cloud as service. Google apps are best examples of SAAS.
PAAS: Platform as a service: Cloud gives you a platform to work with be it development, build , test or anything else. Today’s PAAS providers provides full range of ALM services ranging for code repository, automating build and deploy, ever monitor. They also provide production environment. they will take care of traffic, scaling up and even scaling down in case of low traffic, clustering etc. heroku, redhat openshift, cloudbees are some of the PAAS provider.
Now we have fair idea of what cloud is but what is  there for us (developer/coder) in cloud. 

What is in cloud for us?
More companies are taking on the cloud, which means more development teams have to deal with the cloud, define what it means and make it work for their applications. Cloud will provide as mainly two kinds of work.
A)     Work as cloud provider
B)      Work as application running on cloud or migrating legacy application to cloud.
As cloud provider
We can get our self involve in development for cloud provider. Every other company is now setting up their own cloud service.  Amazon, Google, Redhat, HP, Oracle to name a few. There are few open source project that provides framework for setting up cloud solution.
 
OpenStack is a open source project that delivers a massively scalable cloud operating system.  OpenStack was originally developed by NASA and Rackspace, and is also backed by the likes of IBM, HP and Dell. It's primiraly series of three core projects that can be used to build a private cloud platform: Compute, Object Storage and Image Service. These three projects provide the base for managing virtual servers, storage and machine images. The core projects do not provide a self-service portal, but there are OpenStack-incubated projects that do. OpenStack provides full hypervisor support for KVM and Xen, with limited support for Microsoft Hyper-V, Citrix Xen Server, VMware ESX and others. Persistent storage is provided using OpenStack Object Storage to manage the local disk on compute node clusters. Lastly, machine images of various types (including Raw, VHD, VDI, VMDK and OVF) are managed through the OpenStack Image Service. 

CloudStack has been around since 2009 and is implemented in more than 100 production clouds (including GoDaddy, KT and Tata). It's governed by the Apache Software Foundation and supported by Citrix and about 50 other technology partners. Unlike OpenStack, the CloudStack installation is very streamlined and well documented. The CloudStack open source code was acquired by Citrix with the Cloud.com acquisition and released as open source. This gives CloudStack a head start as it was already software being used in production. CloudStack is designed for massive scalability and centralized management, allowing tens of thousands of geographically distributed servers to be managed from a single portal. CloudStack software works with a variety of hypervisors, including Oracle VM, KVM, vSphere and Citrix XenServer, with Oracle VM being a key differentiator from CloudStack. Another differentiator is support for bare-metal servers. Additionally, CloudStack supports multiple networking models, such as OpenFlow, VLANs and flat networks.
More on openstack and cloudstack is out of scope for this blog. Refer the reference column to know more about openstack and coudstack. They have readymade virtual machines to download and kick start your own private cloud.

Work on application running on cloud or migrating legacy application to cloud.
We can also tailor our existing application to deploy it into cloud or create application that can run on cloud. There will be a time when if not all, majority if the software that we will develop will be cloud supported. There are lot of PAAS provider that provides us to development and runtime environment for our application. Though most of them are still in their beta phrase, I picked some of them and almost all of them have free account for development. So created account in some of the providers to see how much I have to come out of my traditional programming to make my application run on cloud. Most of PAAS provider support programming language like java, ruby, scala mostly language that runs on multiple platform. For .Net programmer you have the option to check out Microsoft Azure cloud, they support .Net. I being java developer from my birth chose to evaluate java application with some of the cloud providers

Salesforge, Heroku, Cloudbees.
To my surprise I don’t have to do much refactoring to deploy my application to cloud. There are changes like
a) I cannot code to specific application server or database. Most of the providers hibernate for database connectivity so that the application developers are abstracted to the underlying database type.  Providers like Heroku expose data source name as environment variable to be used by the code and they provides MySql database only.
b) I cannot write to a file directly , In case I need to I have to use their API like Salesforce provides some objects base I/0 provider API to make write operations.
c) As scaling /clustering will be there we are discourage to depend heavily on HTTP sessions state. If at all any state needs to be maintained save it with their persistent API.
Similarly there are some areas we need to refractor our application to support cloud environment. The list is minimal but there are different list for different providers. Refer to the provider’s website to know more.
We developer’s scope is not limited here, with the popularity of cloud lots of new trends are immerging like NoSQL, Object based I/O to name a few. With application on cloud there won’t be issues in scaling or infrastructure, this will give us more time to make the application do what it is meant to do better. But the downside of cloud is that it is mostly based on “Everyone fits one size”. Remember the example of electricity, we cannot ask for socket with 420 volts or socket of 100 volts. It is standard 220 volts so we have to ensure our application works on 220 volts. Same goes with cloud where we have to tailor our application to fit to cloud not change the cloud to fit our application needs.

References:

Friday, August 10, 2012

ClassFind Utility

Often we come across need to search for a class file in ear and wars. This tool will help to search class file inside ears/jars/ears/rar

This is the next version of JarFind utility.



import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import java.util.TreeSet;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class SearchArchive {

    static Map<String, String> locationMap = new HashMap<String, String>();
    public static final String intend = "------";
    private Stack<String> node = new Stack<String>();
    private File archive;

    public SearchArchive(File archive) {
        this.archive = archive;
        System.out.println();
        System.out.println("Scanning :" + archive.getAbsolutePath());
    }

    public SearchArchive(String archive) {
        this(new File(archive));
    }

    public InputStream find(String name) {

        ZipInputStream root;
        try {
            root = new ZipInputStream(new BufferedInputStream(
                    new FileInputStream(archive)));

            return find(root, name); // enter the recursion
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            System.out.println("Error occured while searching "
                    + archive.getName());
            System.out.println(e.toString());
        }
        return null;

    }

    public static boolean isArchive(String name) {
        String s = name.toLowerCase();
        return s.endsWith(".jar") || s.endsWith(".war") || s.endsWith(".zip")
                || s.endsWith(".ear");
    }

    protected InputStream find(ZipInputStream in, String name) {

           try {
            ZipEntry entry;
            while ((entry = in.getNextEntry()) != null) {
                String entryName = entry.getName();

                if (entryName.endsWith(name)) {
                    node.push(entryName);
                    print(entryName + "*********");
                    String value = locationMap.get(archive.getAbsolutePath());
                    if (value == null)
                        value = "";
                    else
                        value = value + ",";

                    for (String nodeElement : node) {
                        value = value + ">>" + nodeElement;
                    }

                    locationMap.put(archive.getAbsolutePath(), value);
                    node.pop();
                    // return in;
                }

                if (isArchive(entryName)) {
                                       node.push(entryName);
                    print(entryName);
                    // System.out.println("Scanning file :" + entryName);
                    InputStream result = find(new ZipInputStream(in), name);
                    if (result != null) {
                        return result;
                    }
                }
                in.closeEntry();
            }
            if (node.size() > 0) {
                node.pop();
            }
        } catch (Exception e) {
            System.out.println("Error occured while searching "
                    + archive.getName() + " >>" + node.peek());
            System.out.println(e.toString());

        }
        return null; // nothing found
    }

    private void print(String entryName) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < node.size(); i++) {
            sb.append(intend);
        }
        sb.append(entryName);
        System.out.println(sb);
    }

    public static void main(String args[]) throws IOException {
        if (args.length != 2) {
            System.out
                    .println("Usage java SearchArchive <location> <classname>");
            System.out.println("Example");
            System.out
                    .println("java SearchArchive \"c:\\program files\\application\" MyClass.class");
            System.exit(0);
        }
        String location = args[0];
        String className = args[1];
        searchDir(location, className);
        printResult();
    }

    public static void searchDir(String location, String className)
            throws IOException {
        File file = new File(location);
        for (File afile : file.listFiles()) {

            if (!afile.isDirectory() && afile.getName().endsWith(".jar")) {
                // for Jar file JarFile.class is better than the ZipInputStream
                // in terms of performance.
                searchJarFile(afile, className);
            }

            else if (!afile.isDirectory() && isArchive(afile.getName())) {
                // since we have to deal with nested archive here
                // we are using ZipInputStream
                SearchArchive af = new SearchArchive(afile);
                af.find(className);
            }
            if (afile.isDirectory())
                searchDir(afile.getPath(), className);
        }
    }

    private static void searchJarFile(File afile, String className) {
        try {
            JarFile jarfile = new JarFile(afile);
            System.out.println();
            System.out.println("Scanning :" + afile);
            Enumeration enumeration = jarfile.entries();
            while (enumeration.hasMoreElements()) {

                String s2 = enumeration.nextElement().toString();
                s2 = s2.replace('/', '.');
                if (s2.indexOf(className) != -1) {
                    String value = locationMap.get(afile.getAbsolutePath());
                    if (value != null) {
                        value = value + "," + s2;
                    } else {
                        value = s2;
                    }
                    locationMap.put(afile.getAbsolutePath(), value);

                }
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Error occured while searching "
                    + afile.getName());
            System.out.println(e.toString());

        }
    }

    private static void printResult() {
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("------------------");
        System.out.println("Found in " + locationMap.size() + " files");
        System.out.println("------------------");
        System.out.println("");
        for (String key : new TreeSet<String>(locationMap.keySet())) {
            System.out.println(key);
            String value = locationMap.get(key);
            String splitValue[] = value.split(",");
            for (String sValue : splitValue) {
                System.out.println(intend + sValue);
            }
            System.out.println();

        }

    }
}

Saturday, May 7, 2011

JAR FINDER

While working on application, i always find the need to search directories containing all the jars to find a particular class file. So here is java code to search jars files from specified directory to find a class file.


Usage : java JarFind /usr/home/tomcat AuthenticatorBase.class

This will search AuthenticatorBase.class in all the jar files inside /usr/home/tomcat and display the result of all the match.

This is simple yet useful, at least i felt it is useful ;)




import java.io.File;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class JarFind {

public static ArrayList arraylist = new ArrayList();

public static void searchDir(String s, String s1) throws Exception {

File file = new File(s);
if (!file.exists() || !file.isDirectory()) {
System.out.println("Error cannot find the folder specified");

System.out.println(":)");
System.exit(0);
}
File afile[] = file.listFiles();
for (int i = 0; i < afile.length; i++)
if (!afile[i].isDirectory() && afile[i].getName().endsWith(".jar")) {
JarFile jarfile = new JarFile(afile[i]);
System.out.println("Scanning jar :" + afile[i]);
Enumeration enumeration = jarfile.entries();
do {
if (!enumeration.hasMoreElements())
break;
String s2 = enumeration.nextElement().toString();
s2 = s2.replace('/', '.');
if (s2.indexOf(s1) != -1
&& !arraylist.contains(afile[i].getPath()))
arraylist.add("Jar :" + afile[i].getPath()
+ " FileName :" + s2);
} while (true);
} else if (afile[i].isDirectory())
searchDir(afile[i].getPath(), s1);

}

public static void main(String args[]) throws Exception {
if (args.length < 2) {
System.out.println("Format: JarFind rootfolder pattern");
System.exit(0);
}
String s = args[0];
String s1 = args[1];
searchDir(s, s1);
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("------------------");
System.out.println(arraylist.size() + " Result Found");
System.out.println("------------------");
System.out.println("");

for (String str : arraylist) {
System.out.println(str);
}
}



}


Class version Finder

What version of java compiler has compiled my class file ?

What happens when we do bulldozed our java code with javac?

A class file is created in bytecode format.A class file consists of a stream of 8-bit bytes. All 16-bit, 32-bit, and 64-bit quantities are constructed by reading in two, four, and eight consecutive 8-bit bytes, respectively. Multibyte data items are always stored in big-endian order, where the high bytes come first. In the Java and Java 2 platforms, this format is supported by interfaces java.io.DataInput and java.io.DataOutput and classes such as java.io.DataInputStream and java.io.DataOutputStream.

How do we know if class file is valid class file and with which version of java compiler it is been compiled ?

In a valid class file,
first four byte is hardcoded hexadecimal number(also known as magic number or Hexspeak) as 0xCAFEBABE.
next two byte contains the minor version of the compiler that compiled the class file.
next two byte contains the major version of the compiler that compiled the class file.
rest of the file contains about class name , super class, fields, methods etc.

A complete structure of a class file is as follows
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}

Open the class file in your favroute hex editor and and it will look llike this

"000000000 CA FE BA BE 00 00 00 2E-00 AD 0A 00 35 00 45 08 |Êþº¾
"000000010 00 46 08 00 47 0A 00 48-00 49 07 00 4A 0A 00 05 |
"000000020 00 4B 0A 00 05 00 4C 0A-00 05 00 4D 09 00 4E 00 |
.............................
.............................

Notice the first four byte is CA FE BA BE (the magic number). JVM will first look ou for this magic number if not present will throw java.lang.ClassFormatError.

Next two byte is 00 00 which is minor version.
Next two byte is 00 2E which is major version. As mentioned earlier multibyte data are stored in big-endian order. Here 2E is hexadecimal format of 46 ( JDK 1.2)


Possible major/minor value :
major           minor               Java platform version
45                3                     1.0
45                3                     1.1
46                0                     1.2
47                0                     1.3
48                0                     1.4
49                0                     1.5
50                0                     1.6

JAVA Program to identify the version of class file compiler


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.*;

public class FindVersion {

 /**
  * @param args
  */
 public static void main(String[] args)throws Exception {
  // TODO Auto-generated method stub
  //System.out.println(args.length!=1);
  if (args.length != 1)
  {
   System.out.println("Usage :");
   System.out.println("java FindVersion ");
   System.exit(1);
  }
  DataInputStream data=null;
  String sfile=args[0];
  File file =new File(sfile);
  if (file.exists() && file.isFile()){
   data =new DataInputStream(new FileInputStream(file));
  }
  else{
   System.out.println("File not found :(");
      System.exit(1);
  }
  int magic= data.readInt();
  //System.out.println(magic);
  if (magic!=0XCAFEBABE)
  {
   System.out.println("not a valid java class file :(");
   System.exit(1);
  }
  int minor_version=data.readUnsignedShort();
  int mazor_version=data.readUnsignedShort();
  System.out.print(file.getName()+" is ");
  if (mazor_version==45.3)
   System.out.println("Compiled with JDK 1.1 :)");
  else if (mazor_version==46.0)
   System.out.println("Compiled with JDK 1.2 :)");
  else if (mazor_version==47.0)
   System.out.println("Compiled with JDK 1.3 :)");
  else if (mazor_version==48.0)
   System.out.println("Compiled with JDK 1.4 :)");
  else if (mazor_version==49.0)
    System.out.println("Compiled with JDK 1.5 :)");
  else if (mazor_version==50.0)
   System.out.println("Compiled with JDK 1.6 :)");
  else
   System.out.println("I dont know the compilation version :( ");

  System.out.println("Version Details");
  System.out.println("Minor version :"+minor_version);
  System.out.println("Mazor version :"+mazor_version);


 }

}



Referenced Material:
http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html