Wednesday, May 2, 2018

Data source creation on Jboss eap and database Password Encription


I have used Jboss eap 5.2.yes it is bit old version and current version is 7.1.0.

First you have to create a datasource.
1.       Run the JBoss eap server and get JBoss EAP Admin Console
2.       Navigate to the Datasource type to be added, under Resources → Datasources→  Local Tx Datasources
3.       Click Add a new resource.
4.       Select the resource template from the drop-down box, and click Continue (I used default (Local Tx Datasource)).
5.       Enter required parameters of JNDI Name, JDBC Driver Class and Connection URL.
6.       Click Save. The datasource is available.
7.       check the connection with JBoss EAP Admin console, select datasource →Control → Test Connection(It should be success without and exception on server log or console)
The procedure will create a <db>-ds.xml file in your  JBOSS_HOME/server/<Profile>/deploy. The data source’s configuration is as following.
<datasources>
  <local-tx-datasource> 
  <!-- MS SQL Configuration -->
  <!--Start-->
    <jndi-name>ussd</jndi-name>
                <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>        
                <use-java-context>false</use-java-context>   
                <connection-url>jdbc:sqlserver:// 127.0.0.1:5432;databaseName=USSDTEST</connection-url>
    <driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
    <user-name>sa</user-name>
                <password>abc@123</password>         
                <!--End-->
  </local-tx-datasource>
</datasources>

Then I’m going to encrypt  the database password. You have to set security-domain for this purpose.
1.       Encrypt the data source password.
a.       Change directory to the jboss-as directory and get command prompt
b.      Run following command .abc@123 database password
(java -cp client\jboss-logging-spi.jar;lib\jbosssx.jar org.jboss.resource.security.SecureIdentityLoginModule abc@123
It will give Encoded password: -448c91357056c19d)
2.       Create an application authentication policy with the encrypted password.
a.       Open to JBOSS_HOME/server/<Profile>/conf/login-config.xml
b.      You have to add application-policy in the policy tag as follows
<policy>
  ...
      <!-- Example usage of the SecureIdentityLoginModule -->
      <application-policy name="EncryptDBPassword">
          <authentication>
              <login-module code="org.jboss.resource.security.SecureIdentityLoginModule" flag="required">
                  <module-option name="username">sa</module-option>
                  <module-option name="password">-448c91357056c19d </module-option>
                  <module-option name="managedConnectionFactoryName">jboss.jca:name= ussd,service=LocalTxCM</module-option>
              </login-module>
          </authentication>
      </application-policy>
  </policy>

3.       Configure the data source to use the application authentication policy.Here you should remove  user-name  and password and add <security-domain>EncryptDBPassword</security-domain>

<datasources>
  <local-tx-datasource> 
  <!-- MS SQL Configuration -->
  <!--Start-->
    <jndi-name>ussd</jndi-name>
                <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>             
                <use-java-context>false</use-java-context>   
                <connection-url>jdbc:sqlserver:// 127.0.0.1:5432;databaseName=USSDTEST</connection-url>
    <driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
    <!--<user-name>sa</user-name>-->
                <!--<password> </password>-->            
                <security-domain>EncryptDBPassword</security-domain>
                <!--End-->
  </local-tx-datasource>
</datasources>


4.       check the connection with JBoss EAP Admin console, select datasource→Control → Test Connection(It should be success without and exception on server log or console)




Refer: -https://access.redhat.com/documentation/en-us/jboss_enterprise_application_platform/5/html/admin_console_user_guide/admin_console_user_guide-resources-datasources
https://access.redhat.com/documentation/en-us/jboss_enterprise_application_platform/5/html/security_guide/encrypting_data_source_passwords

Tuesday, February 20, 2018

Comparable Vs. Comparator

Comparable and Comparator in Java are very useful for sorting collection of objects.
Java provides Comparable interface which should be implemented by any custom class if we want to use Arrays or Collections sorting methods. Comparable interface has compareTo(T obj) method which is used by sorting methods, you can check any Wrapper, String or Date class to confirm this. We should override this method in such a way that it returns a negative integer, zero, or a positive integer if “this” object is less than, equal to, or greater than the object passed as argument.
Comparator interface compare(Object o1, Object o2) method need to be implemented that takes two Object argument, it should be implemented in such a way that it returns negative int if first argument is less than the second one and returns zero if they are equal and positive int if first argument is greater than second one.
Comparable and Comparator interfaces use Generics for compile time type checking.

Comparable Vs. Comparator
1.Comparable interface is defined in java.lang package and Comparator interface is defined in java.util package. Comparator is used as a utility for sort objects and Comparable is provided by default because it is in java.lang package.
2.Comparable interface has method public int compareTo(Object ob) which returns negative integer, zero and positive integer when this reference object is less than ,equal and greater than with specified object in parameter. Comparator interface has method public int compare(Object ob1,Object ob2) which returns negative integer, zero and positive integer when first object(ob1) is less than ,equal and greater than with second object (ob2) in parameter.
3.Comparable compares an object with this reference object and Comparator compares 2 provided objects.
4.If a class implement Comparable interface, that object’s List or Array can be sorted by using  Collections.sort() or Arrays.sort()  according to the compareTo() method. Comparator should implement within class or outside a class then give it to sort method.
5.Objects which implement Comparable in Java  can be used as keys in a SortedMap like TreeMap or elements in a SortedSet  for example TreeSet, without specifying any Comparator.

Example
I’m going to explain these concepts using Student example.I have added extra variable “place” where you can see the data sorted way.

import java.util.Comparator;

public class Student implements Comparable<Student>{

private int id;
private String name;
private int age;
private int place;//to identify the place change against the sort



public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int id, String name, int age, int place) {
super();
this.id = id;
this.name = name;
this.age = age;
this.place = place;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}


public int getPlace() {
return place;
}
public void setPlace(int place) {
this.place = place;
}

//implementation of compareTo method in Comparable interface
public int compareTo(Student s) {
return this.id-s.id;
}

//implementation of Comparator interface according to sort by age
public static Comparator<Student>  ageComparator = new Comparator<Student>(){

public int compare(Student s0, Student s1) {

return s0.getAge()-s1.getAge();

}

};

//implementation of Comparator interface according to sort by name
public static Comparator<Student>  nameComparator = new Comparator<Student>(){

public int compare(Student s0, Student s1) {

return s0.getName().compareTo(s1.getName());

}

};

//implementation of Comparator interface according to sort by id then name
public static Comparator<Student> idNameComparator = new Comparator<Student>(){
public int compare(Student s0,Student s1){

int flag = s0.id-s1.id;
if(flag == 0)
flag = s0.getName().compareTo(s1.getName());

return flag;

}
};


@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ",place = "+place+"]";
}


}


public class App
{
    public static void main( String[] args )
    {
    testCompare();
     
    }
 
 
    public static void testCompare(){
   
   
    Student s2 = new Student(2,"disa",24,1);
   
    Student s5 = new Student(5,"ciri",20,2);
       
    Student s3 = new Student(3,"amal",30,3);
   
    Student s1 = new Student(1,"biri",10,4);
   
    List<Student> al = new ArrayList<Student>();
    al.add(s2);
    al.add(s5);
    al.add(s3);
    al.add(s1);
   
    System.out.println("Unsorted ArrayList");
    for (Student s : al) {
    System.out.println(s.toString());

}
   
    System.out.println("");
    System.out.println("Sorted using Comparable interface accroding to compareTo() method");
    Collections.sort(al);
    for (Student s : al) {
    System.out.println(s.toString());

}
   
    System.out.println("");
    System.out.println("Sorted using Comparator interface accroding to ageComparator's compare() method");
    Collections.sort(al,Student.ageComparator);
    for (Student s : al) {
    System.out.println(s.toString());

}
   
    System.out.println("");
    System.out.println("Sorted using Comparator interface accroding to nameComparator's compare() method");
    Collections.sort(al,Student.nameComparator);
    for (Student s : al) {
    System.out.println(s.toString());

}
   
    System.out.println("");
    System.out.println("Sorted using Comparator interface accroding to idNameComparator's compare() method");
    Collections.sort(al,Student.idNameComparator);
    for (Student s : al) {
    System.out.println(s.toString());

}
    }
 
 
}

Output
Unsorted ArrayList
Student [id=2, name=disa, age=24,place = 1]
Student [id=5, name=ciri, age=20,place = 2]
Student [id=3, name=amal, age=30,place = 3]
Student [id=1, name=biri, age=10,place = 4]

Sorted using Comparable interface accroding to compareTo() method
Student [id=1, name=biri, age=10,place = 4]
Student [id=2, name=disa, age=24,place = 1]
Student [id=3, name=amal, age=30,place = 3]
Student [id=5, name=ciri, age=20,place = 2]

Sorted using Comparator interface accroding to ageComparator's compare() method
Student [id=1, name=biri, age=10,place = 4]
Student [id=5, name=ciri, age=20,place = 2]
Student [id=2, name=disa, age=24,place = 1]
Student [id=3, name=amal, age=30,place = 3]

Sorted using Comparator interface accroding to nameComparator's compare() method
Student [id=3, name=amal, age=30,place = 3]
Student [id=1, name=biri, age=10,place = 4]
Student [id=5, name=ciri, age=20,place = 2]
Student [id=2, name=disa, age=24,place = 1]

Sorted using Comparator interface accroding to idNameComparator's compare() method
Student [id=1, name=biri, age=10,place = 4]
Student [id=2, name=disa, age=24,place = 1]
Student [id=3, name=amal, age=30,place = 3]
Student [id=5, name=ciri, age=20,place = 2]

Tuesday, January 9, 2018

Angular 4

First of all, I’m going to tell you what kind of thing you are going to read. As an internet user, you must have visited lot of responsive and dynamic web applications. Wait, Have you ever notice, some applications only reload the required section and remain other sections as it is rather than reload whole page. All you have experienced with Gmail, PayPal etc. These applications are called Single Page Application (SPA).They are developed by Angular. SPAs heavily use AJAX- a way to communicate with back-end servers without doing a full page refresh to get data loaded into application.

Angular is a JavaScript based open source framework for building client-side web applications. JavaScript process with dynamic content. Based on JavaScript , many custom libraries can be create and jQuery is one which is fast small and feature-rich for DOM manipulation, event handling, animation and  Ajax much simpler with an easy-to-use API. But jQuery doesn’t have any proper structure and developer have the freedom for build the project structure. When considering a large project it may easily fall into trap of “spaghetti code” which leads to confusion of code maintainability. Angular provides a structured environment to build SPAs.

                Angular
jQuery
DOM Manipulation
yes
yes
RESTful API
yes
No
Animation Support
Yes
Yes
Deep Linking Routing
Yes
No
Form Validation
yes
No
2 Way Data Binding
yes
No
AJAX/JSON
yes
yes

Two-way binding is when properties in the model get updated, so does the UI and When UI elements get updated, the changes get propagated back to the model. Don’t get confuse now you may experience it while coding. Angular community has decided to consider any version of Angular (Angular 2 onwards) as just Angular and Angular 1 as AngularJS. Angular is normally called TypeScript based open source client side web application by Google and community. TypeScript is a super set of Java Script. Angular is completely rewrite of AngularJS. Now I guess you have a depiction of Angular origination.

Tuesday, January 2, 2018

Java Parallel Processing Framework(JPPF)

JPPF is an open source grid computing framework that can be used to run multiple java applications in parallel in a distributed execution environment. It also written in java

JPPF features
·         A JPPF grid can be up and running in minutes
·         Simple programming model for abstracts the complexity of distributed and parallel processing.
·         Highly scalable, distributed framework for the parallel execution of CPU intensive tasks.
·         Graphical and programmatic tools for fine-grained monitoring and administration
·         Fault-tolerance and self-repair capabilities ensure service and reliability.
·         A set of fully documented sample applications of JPPF to real-life problems
·         Very flexible and business-friendly open source licensing
·         Multiple built-in load-balancing algorithms are available at client and server levels.

Requirements and install
Current version of JPPF is v6.0 (alpha).Java 1.7 or later and Apache Ant 1.7.0 or later should already be installed on your machine.
·         You need to download and install the following JPPF components:
·         JPPF application template: this is the JPPF-x.y.z-application-template.zip file
·         JPPF driver: this is the JPPF-x.y.z-driver.zip file
·         JPPF node: this is the JPPF-x.y.z-node.zip file
·         JPPF administration console: this is the JPPF-x.y.z-admin-ui.zip file

JPPF Topology

A JPPF grid is made of three different types of components,
·         clients are entry points to the grid and enable developers to submit work
·         servers are the components that receive work from the clients, dispatch it to the nodes, receive the results from the nodes, and redirect the results to the clients
·         nodes perform the job execution.
To mitigate single point of failure, JPPF provides the ability to connect multiple servers together in a peer-to-peer network and additional connectivity options for clients and nodes, as illustrated in this figure:


There are a number of major advantages to this design: 
·         It enables a greater scalability of the JPPF grid, by allowing the "pluging-in" of additional servers dynamically. This way, a server can delegate a part of its load to other servers.
·         No matter how many servers are present, nodes and clients communicate with them in the exact same way
·         Peer server connections benefit from the same failover and recovery features available to nodes and clients

How it works
There are 2 steps.
·     Dividing an application into smaller parts that can be executed independently and in parallel. 
JPPF provides facilities that make this effort a lot easier, faster and much less painful than without them. The result is a JPPF object called a "job", itself made of smaller independent parts called "tasks".
·         Executing the application on the JPPF Grid.
The simplest possible JPPF Grid is made of a server, to which any number of execution nodes are attached. A node is a JPPF software component that is generally installed and running on a separate machine. This is commonly called a master/slave architecture, where the work is distributed by the server to the nodes. In JPPF terms, a unit of work is called a "job", and its constituting "tasks" are distributed by the server among the nodes for parallel execution. 

JPPF Supported Platforms
JPPF will run on any system that supports Java: MacOS, Windows, Linux, zOS, on any hardware from a simple laptop up to a mainframe computer. JPPF is not only limited to running Java jobs. You can run any application that is available on your platform as a JPPF job. For instance, you might want to run your favorite graphics suite in batch mode, to render multiple large, complex images all at once.

There are similar framework as JPPF such as GigaSpacesTerracotta and GridGain.

Reference:www.JPPF.org

The Hypervisor

 The hypervisor is a piece of software that runs on top of hardware infrastructure that creates a virtualization platform. The hypervisor a...