Monday, September 14, 2015

Downloading data in PDF, XLS, WORD format in GWT

hree step are needed to perform operation to download the data in PDF, XLS, WORD format in GWT
Step -1 :-
At client side set the url from where we are going to download the data with file name as given below.

String url = GWT.getModuleBaseURL() + "downloadService?fileInfo1=" + "SiddhuFile";
Window.open( url, "_blank", "status=0,toolbar=0,menubar=0,location=0");


Step -2 :-

Make sure to enter the servlet mapping in web.xml


downloadService
com.test.pdf.server.DownloadServlet


downloadService
/testpdffileproject/downloadService


Step -3 :- Create a servlet that will do the functinality of downloading the data from server side to client side.

protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException
{

String fileName = req.getParameter( "fileInfo1" );
ServletOutputStream out = resp.getOutputStream(); 
int BUFFER = 1024 * 100;
String reportContents = getServletContext().getServerInfo();
//for PDF
resp.setContentType("application/pdf");
resp.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".pdf");

//for CVS files
//resp.setContentType("text/csv"); 
//resp.setHeader("Content-Disposition", "attachment; filename="+ fileName + ".csv"); 
   
//for Word Document
//resp.setContentType("text/doc"); 
//resp.setHeader("Content-Disposition", "attachment; filename="+ fileName + ".doc"); 

resp.setContentLength( Long.valueOf( reportContents.length() ).intValue()); 
resp.setBufferSize(BUFFER);
out.write(reportContents.getBytes()); 
out.flush(); 
out.close(); 


}

Note:- Code to download the data in xls and word is also attached. Please uncomment it if you want to download the data in specific format.


Thursday, September 10, 2015

Oracle SQL Statment to create user, grant permission , Creating Table, views, Trigger and Procedure

Reference:- http://www.techonthenet.com/oracle/schemas/create_schema.php

=======Table Space
#1 - PERMANENT TABLESPACE
CREATE BIGFILE TABLESPACE tbs_perm_siddhu_01
  DATAFILE 'tbs_perm_siddhu_01.dat'
    SIZE 10M
    AUTOEXTEND ON;

===DROP TABLESPACE tbs_01 
INCLUDING CONTENTS 
CASCADE CONSTRAINTS; 

#2 - TEMPORARY TABLESPACE

CREATE TEMPORARY TABLESPACE tbs_temp_siddhu_01
  TEMPFILE 'tbs_temp_siddhu_01.dbf'
    SIZE 5M
    AUTOEXTEND ON;

#3 Creating User called siddhu/siddhu

CREATE USER siddhu
  IDENTIFIED BY siddhu
  DEFAULT TABLESPACE tbs_perm_siddhu_01
  TEMPORARY TABLESPACE tbs_temp_siddhu_01
  QUOTA 20M on tbs_perm_siddhu_01;

#4 Assigning Role to the user siddhu

GRANT create session TO siddhu;
GRANT create table TO siddhu;
GRANT create view TO siddhu;
GRANT create any trigger TO siddhu;
GRANT create any procedure TO siddhu;
GRANT create sequence TO siddhu;
GRANT create synonym TO siddhu;  


--From here we can login to the oracle db with user siddhu


#5 Create Table
CREATE TABLE customers
( customer_id number(10) NOT NULL,
  customer_name varchar2(50) NOT NULL,
  address varchar2(50),
  city varchar2(50),
  state varchar2(25),
  zip_code varchar2(10),
  CONSTRAINT customers_pk PRIMARY KEY (customer_id)
);

#6 Create View

CREATE VIEW view_customers AS
  SELECT customers.customer_id, customers.customer_name, customers.city
  FROM customers
  WHERE customers.customer_name like '%siddhu%';


#7 Grant Object Privilage

GRANT ALL ON  customers TO siddhu;  

#7 CREATE SYNONYMS FOR OBJECTS

CREATE PUBLIC SYNONYM customers
FOR siddhu.customers;

-- Now we can execute select queries direcly on customers i.e. SELECT * FROM customers; without giving schema names.



#8 Delete all rows of the Table
DELETE FROM customers;

#8 Alter table
alter table
   customers
add
   salary  NUMBER NOT NULL;

#8 Trigger

CREATE OR REPLACE TRIGGER trigger_siddhu
BEFORE DELETE OR INSERT OR UPDATE ON customers
FOR EACH ROW
WHEN (NEW.CUSTOMER_ID > 0)
DECLARE
   sal_diff NUMBER;
BEGIN
   sal_diff := :NEW.salary  - :OLD.salary;
   dbms_output.put_line('Old salary: ' || :OLD.salary);
   dbms_output.put_line('New salary: ' || :NEW.salary);
   dbms_output.put_line('Salary difference: ' || sal_diff);
END;
/


#10 Procedure
CREATE OR REPLACE PROCEDURE adjust_salary(
    new_customer_id IN NUMBER,
    salary IN NUMBER
) IS
BEGIN
 IF salary > 7000 
THEN 
UPDATE customers
   SET salary = '7007'
   WHERE customer_id = new_customer_id;
END IF;
END;

How to run Video file in GWT project


Use following belwo given code to execute file bbb_trailer_360p.webm of type webm to run on the screen using GWT application.

Video videoPlayer = Video.createIfSupported();
if (videoPlayer == null) {
RootPanel.get().add(new Label("Your browser doesn't support HTML5 Video"));
return;
}
//Video videoPlayer = new Video("siddhugwtvideoexample/videos/bbb_trailer_3601p.webm");

videoPlayer.addSource("siddhugwtvideoexample/videos/bbb_trailer_360p.webm", VideoElement.TYPE_WEBM);
videoPlayer.setControls(true);
videoPlayer.setPoster("siddhugwtvideoexample/videos/bbb480.jpg");
videoPlayer.setPixelSize(960, 640);
RootPanel.get().add(videoPlayer);



There is also another method which gives you addition control to run the file on the screen for that we need follwing jar files

https://code.google.com/p/gwt-html5-video/downloads/list

VideoWidget videoPlayer = new VideoWidget(true, false, null);

List sources = new ArrayList();
sources.add(new VideoSource("siddhugwtvideoexample/videos/bbb_trailer_3601p.webm", VideoType.WEBM));

videoPlayer.setSources(sources);
videoPlayer.setControls(true);
videoPlayer.setAutoPlay(false);
videoPlayer.setPoster("siddhugwtvideoexample/videos/bbb480.jpg");
videoPlayer.setPixelSize(960, 640);
RootPanel.get().add(videoPlayer);








Wednesday, August 26, 2015

How to resolve [org.jboss.as] JBAS015899: JBoss AS 7.1.1.Final "Brontes" starting error

While starting your jboss-as-7.1.1.Final sometimes it happen that our JBoss stop after this line on command prompt

[org.jboss.as] JBAS015899: JBoss AS 7.1.1.Final "Brontes" starting


This is know issue with JBoss in JDK8.


To get ride of this please add following line in your standalone.bat

rem setting JAVA_HOME by siddhu
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_75

Note: Make sure we need to add above given lines below 
set JAVA_OPTS=-Dprogram.name=%PROGNAME% %JAVA_OPTS%

by stating set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_75 we force JBoss to take JDK7 for execution.

Deadlock in java

As the word state this is the point where programe execution is not possible. This generally occured in multithreading.

Deadlock can occur in a situation when a thread is waiting for an object lock, that is acquired by another thread and second thread is waiting for an object lock that is acquired by first thread.

As both threads are waiting for each other to release the lock, the condition reached is called deadlock.

public class TestDeadlockExample1 {  
  public static void main(String[] args) {  
    final String resource1 = "This is Resource1";  
    final String resource2 = "This is Resource2";  
    // thread1 tries to lock resource1 then resource2  
    Thread thread1 = new Thread() {  
      public void run() {  
          synchronized (resource1) {  
           System.out.println("Thread 1: locked Resource 1");  
  
           try { Thread.sleep(100);} catch (Exception e) {}  
  
           synchronized (resource2) {  
            System.out.println("Thread 1: locked Resource 2");  
           }  
         }  
      }  
    };  
  
    // thread2 tries to lock resource2 then resource1  
    Thread thread2 = new Thread() {  
      public void run() {  
        synchronized (resource2) {  
          System.out.println("Thread 2: locked Resource 2");  
  
          try { Thread.sleep(100);} catch (Exception e) {}  
  
          synchronized (resource1) {  
            System.out.println("Thread 2: locked Resource 1");  
          }  
        }  
      }  
    };    
      
    thread1.start();  
    thread2.start();  
  }  
}  



RMI (Remote Method Invocation)

The RMI (Remote Method Invocation) is an API that help developer to create distributed application in java.

With the help of RMI developer can invoke methods on an object running in another JVM.

The RMI provides remote communication between the applications using two objects stub and skeleton.

Stub

The stub is an gateway object resides at the client side. All the outgoing requests are passed through it. When the caller invokes method on the stub object, it does the following tasks:

- Initiates a connection with remote Virtual Machine (JVM),
- Writes and transmits (marshals) the parameters to the remote Virtual Machine (JVM),
- Reads (unmarshals) the return value or exception, and
- Finally, returns the value to the caller.


Skeleton

The skeleton is an gateway resides on the server side . All the incoming requests are passed through it. When the skeleton receives the incoming request, it does the following tasks:

-reads the parameter for the remote method that is send to it from stub.
-invokes the method on the actual remote object
-writes and transmits (marshals) the result to the caller.

Diagram



Following are the steps involved in RMI program

1- Create the remote interface
2- Provide the implementation of the remote interface
3- Compile the implementation class
4- Create the stub and skeleton objects using the rmic tool
5- Start the registry service by rmiregistry tool
6- Create and start the Server application
7- Create and start the client application

Lets try to have one smaller example


1- Create the remote interface
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Adder extends Remote{  
public int add(int x,int y)throws RemoteException;  
}  

2- Provide the implementation of the remote interface
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class AdderRemote extends UnicastRemoteObject implements Adder{  
AdderRemote()throws RemoteException{  
super();  
}  
public int add(int x,int y){return x+y;}  
}
3- Compile the implementation class
4- Create the stub and skeleton objects using the rmic tool




5- Create and start the Server application
import java.rmi.*;  
import java.rmi.registry.*;  
public class MyServer{  
public static void main(String args[]){  
try{  
Adder stub=new AdderRemote();  
Naming.rebind("rmi://localhost:5000/siddhu",stub);  
}catch(Exception e){System.out.println(e);}  
}  
}



6- Create and start the client application
import java.rmi.*;  
public class MyClient{  
public static void main(String args[]){  
try{  
Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/siddhu");  
System.out.println(stub.add(34,4));  
}catch(Exception e){}  
}  
}




In this was we can write server class that has connection with the DB or calling 3rd Party application and by doing RMI we can allow Client residing on the same or different JVM can call that method to perform operation.

Tuesday, August 25, 2015

Configuring Jenkin to create deployment component

Now lets try to configure simple build script to create our simple GWT Test Programe deployment component.

Let say we had created TestGWTProject project using Eclipse GWT Pluin inside our J2EE-workspace

To create the deployment component of this project we need to create a war file using jar command of JAVA.

Simple step involved are

Step 1- Go to

C:\J2EE-workspace\TestGWTProject\war

Step -2 :- Exeuctre jar command
jar -cvf TestGWTProject.war *.*

now we will try to do the same thing using Jenkins

Step -1:- Click on create new Project screen



Step 2- Give Name to the Item i.eTestSiddhuProject




Step -3:- Select Freestyle project reason we are not using Maven , Extern job, Multi-Configuration project



Step 4:- Select Execute windows batch process as we are using window OS and window bat file to create deployment component.



Step 5:- click on save button and you will see this screen and click on build now option





we will get our deployment war file inside C:\J2EE-workspace\TestGWTProject\war