Tech Rocks

Coldfusion
Java
JQuery

An online resource for latest web technologies like Coldfusion, JRun, Pro*C, JQuery, HTML5, PHP, W3C, Java, J2EE, C, C++, ORACLE, PL/SQL, MySql, Ajax, Coldbox, Fusebox, UNIX, JavaScript, NodeJS and much more...

Tuesday, July 5, 2016

Collections & Threads in Java

package com.jeetu.app.util;

import java.util.*;

public class test {
    public static void main(String[] args) {

        List linkedList = new LinkedList();
        executeLogic(addContents(linkedList, "LinkedList"), displayContents(linkedList, "LinkedList")).start();

        List arrayList = new ArrayList();
        executeLogic(addContents(arrayList, "ArrayList"), displayContents(arrayList, "ArrayList")).start();

        Set hashSet = new HashSet();
        executeLogic(addContents(hashSet, "HashSet"), displayContents(hashSet, "HashSet")).start();

        Set treeSet = new TreeSet();
        executeLogic(addContents(treeSet, "TreeSet"), displayContents(treeSet, "TreeSet")).start();

    }

    static Thread executeLogic(final Thread t1, final Thread t2) {
        Thread t = new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        t1.start();
                        t1.join();
                        t2.start();
                        break;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        return t;
    }

    static Thread addContents(final Collection e, final String type) {
        Thread t = new Thread(new Runnable() {
            public void run() {
                long end;
                long start = System.currentTimeMillis();
                long limit = 5000000;
                for (int index = 1; index <= limit; index++) {
                    e.add(index);
                }
                end = System.currentTimeMillis();
                System.out.println("Write[" + type + "]: " + (end - start) + "  ms.");
            }
        });
        return t;
    }

    static Thread displayContents(final Collection e, final String type) {
        Thread t = new Thread(new Runnable() {
            public void run() {
                long end;
                long start = System.currentTimeMillis();
                int total = 0;
                Iterator i = e.iterator();
                while (i.hasNext()) {
                    total = total + (Integer) i.next();
                }
                end = System.currentTimeMillis();
                System.out.println("Read[" + type + "]: " + (end - start) + " ms ,Total = " + total + ".");
            }
        });
        return t;
    }
}

Result:
Write[ArrayList]: 12997  ms.

Write[LinkedList]: 13144  ms.

Read[ArrayList]: 185 ms ,Total = 1647668640.

Read[LinkedList]: 69 ms ,Total = 1647668640.

Write[HashSet]: 29986  ms.

Read[HashSet]: 69 ms ,Total = 1647668640.

Write[TreeSet]: 38823  ms.

Read[TreeSet]: 67 ms ,Total = 1647668640.



Process finished with exit code 0

Saturday, June 18, 2016

JavaScript: Understanding the Weird Parts



Examples:

var p = {name: 'a', getName: function(){ return this.name;}}

function greet(p) {
    console.log(p.lastname);
}

console.log(p.getName());
greet(p);
greet({lastname:'mary'});



/*var a;

//a = 0;

if(a || a === 0) {
        console.log(a);
}*/


/*var b;
if((b = 2) * 2 === 4) {
    console.log(b);
}*/



/*function wait3mins() {
    var date = new Date().getTime() + 30000;
    while(new Date() < date) {
        
    }
    console.log("finished");
}

function clickHandler() {
    console.log("clicked");
}

document.addEventListener("click", clickHandler);

wait3mins();
console.log("final finished");*/




/*function a() {
    
    function b() {
        //var i;
        console.log(i);
    }
    
    var i = 10;
    console.log(i);
    b();
}

var i = 20;
console.log(i);
a();*/


/*function b() {
    //var i;
    console.log(i);
}

function a() {
    var i = 10;
    console.log(i);
    b();
}

var i = 20;
console.log(i);
a();*/

/*
var a = "hello";
function b() {
    
    console.log('hello jeetu');
    
}

b();
console.log(a);
*/

/*var a;

if (a === undefined) {
     console.log(a);
} else {
    console.log("defined");
}*/

Friday, June 3, 2016

Loosely Coupled Systems


Loose coupling is a little bit like buying insurance. It's best when you don't need it, even though you might feel like you wasted the money.

Ex: which component invokes which one ?


“How do you make two systems loosely coupled? Don't connect them.” -- David Orchard, BEA

JSON Streaming With JAX-RS 2.0

Thursday, June 2, 2016

JSON Object with JAX RS

MessageRestService.java
package com.jeetu.rest;

import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;

import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.util.LinkedHashMap;
import java.util.Map;

@Path("/message")
public class MessageRestService {

    @POST
    @Path("/sendParams")
    @Consumes("application/json")
    public Response consumeJSONParams( Map student ) {
        /*
        POST-MAN
        http://jbosseap.jeetualex.info/rest/message/sendParams
        Body raw = {"id":"12","firstName":"Catain","lastName":"Hook","age":"10", "params":{"a": 1, "b": 2}}
        Content-Type = application/json
        */

        LinkedHashMap params = null;
        String id = "";

        id = (String) student.get("id");
        params = (LinkedHashMap) student.get("params");

        return Response.status(200).entity(params.toString()).build();
    }

    @POST
    @Path("/sendMapper")
    @Consumes("application/json")
    public Response consumeJSONMapper( String student ) {
        /*
        POST-MAN
        http://jbosseap.jeetualex.info/rest/message/sendMapper
        Body raw = {"id":"12","firstName":"Catain","lastName":"Hook","age":"10", "params":{"a": 1, "b": 2}}
        Content-Type = application/json
        */

        JSONObject output = null;
        JSONObject params = null;
        String id = "";

        try {
            output = new JSONObject(student);
            id = (String) output.get("id");
            params = output.getJSONObject("params");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        //return Response.status(200).entity(output.toString()).build();
        //return Response.status(200).entity(id).build();
        return Response.status(200).entity(params.toString()).build();
    }

    @POST
    @Path("/sendRequest")
    @Consumes("application/json")
    public Response consumeJSONRequest( String student ) {
        /*
        POST-MAN
        http://jbosseap.jeetualex.info/rest/message/sendRequest
        Body raw = {"id":"12","firstName":"Catain","lastName":"Hook","age":"10"}
        Content-Type = application/json
        */

        Gson gson = new Gson();
        Student output = gson.fromJson(student, Student.class);

        return Response.status(200).entity(gson.toJson(output)).build();
    }

    @POST
    @Path("/sendString")
    @Consumes("application/json")
    public Response consumeJSONString( String student ) {
        /*
        POST-MAN
        http://jbosseap.jeetualex.info/rest/message/sendString
        Body raw = {"id":"12","firstName":"Catain","lastName":"Hook","age":"10"}
        Content-Type = application/json
        */

        Gson gson = new Gson();
        Student output = gson.fromJson(student, Student.class);

        return Response.status(200).entity(output).build();
    }


    @POST
    @Path("/send")
    @Consumes("application/json")
    public Response consumeJSON( Student student ) {
        /*
        POST-MAN
        http://jbosseap.jeetualex.info/rest/message/send
        Body raw = {"id":"12","firstName":"Catain","lastName":"Hook","age":"10"}
        Content-Type = application/json
        */

        String output = student.toString();

        return Response.status(200).entity(output).build();
    }

    @GET
    @Path("/print/{name}")
    @Produces("application/json")
    public Student produceJSON( @PathParam("name") String name ) {

        /*
        http://jbosseap.jeetualex.info/rest/message/print/James
        */

        Student st = new Student(name, "Marco",19,12);

        return st;

    }



    @GET
    @Path("/{param}")
    public Response printMessage(@PathParam("param") String msg) {

        /*
        http://jbosseap.jeetualex.info/rest/message/jeetu
        */

        String result = "Restful example : " + msg;

        return Response.status(200).entity(result).build();

    }

}
Student.java
package com.jeetu.rest;

public class Student {

    private int id;
    private String firstName;
    private String lastName;
    private int age;

    // Must have no-argument constructor
    public Student() {

    }

    public Student(String fname, String lname, int age, int id) {
        this.firstName = fname;
        this.lastName = lname;
        this.age = age;
        this.id = id;
    }

    public void setFirstName(String fname) {
        this.firstName = fname;
    }

    public String getFirstName() {
        return this.firstName;
    }

    public void setLastName(String lname) {
        this.lastName = lname;
    }

    public String getLastName() {
        return this.lastName;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return this.age;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return this.id;
    }

    @Override
    public String toString() {
        return new StringBuffer(" First Name : ").append(this.firstName)
                .append(" Last Name : ").append(this.lastName)
                .append(" Age : ").append(this.age).append(" ID : ")
                .append(this.id).toString();
    }

}
pom.xml
dependency\>
                groupid>org.jboss.resteasy/groupid>
                artifactid>resteasy-jaxrs/artifactid>
                version>2.2.1.GA/version>
            /dependency>

            dependency>
                groupid>org.jboss.resteasy/groupid>
                artifactid>resteasy-jackson-provider/artifactid>
                version>3.0.4.Final/version>
            /dependency>
Web.xml
!--rest easy !!!-->
    !-- Auto scan REST service -->
    context-param>
        param-name>resteasy.scan/param-name>
        param-value>true/param-value>
    /context-param>

    !-- this need same with resteasy servlet url-pattern -->
    context-param>
        param-name>resteasy.servlet.mapping.prefix/param-name>
        param-value>/rest/param-value>
    /context-param>

    listener>
        listener-class>
            org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
        /listener-class>
    /listener>

    servlet>
        servlet-name>resteasy-servlet
servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
        /servlet-class>
    /servlet>

    servlet-mapping>
        servlet-name>resteasy-servlet/servlet-name>
        url-pattern>/rest/*/url-pattern>
    /servlet-mapping>
    !--rest easy !!!-->


 Reference link 1, link 2, link 3

Sunday, May 22, 2016

Codes for B15

Code:
This code can be used to get some interesting information about your phone and battery. It shows following 4 menus on screen:

  • Phone information
  • Battery information
  • Battery history
  • Usage statistics
*#*#4636#*#* 
Code:
This code can be used for a factory data reset. It'll remove following things:

  • Google account settings stored in your phone
  • System and application data and settings
  • Downloaded applications

It'll NOT remove:

  • Current system software and bundled applications
  • SD card files e.g. photos, music files, etc.
*#*#7780#*#*
PS: Once you give this code, you get a prompt screen asking you to click on "Reset phone" button. So you get a chance to cancel your operation. Read more...