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

How to install Jenkins in Tomcat Apache

How to install Jenkins in Tomcat Apache

Continuous integration is now becoming a mandatory rule in Development.

With the help of tool like Jenkins and Hudson now developer can do automatically build and run Test case at predefined period.

The idea is that development errors are identified very early in the process.

ENVIRONMENT we need to install Jenkins

apache-tomcat-8.0.18 
jdk1.8.0_31  
Jenkins 1.626 (URL:- http://jenkins-ci.org/) 

Deployment Method

Their are different deployment method that can be used to run Jenkin

1- Jenkins can Run as a standalone application:

java -jar jenkins.war –httpPort=18080 –ajp13Port=18009

2- Or deploys in an application server as Tomcat:

This is the most simple method of deployment

Just copy the download jenkins.war inside your webapps folder of tomcat and restart the tomcat.

you will be able to see this screen.


Friday, August 21, 2015

How to validate WSDL file

1. Download and install Eclipse wtp

2. Open eclipse IDE

3. Start to create a new wsdl (File --> New --> other --> Web Services --> WSDL)

4. Give a name to the wsdl (you can provide the name of wsdl which needs to be validated) and click on next. Accept the default options and click on Finish.

5. You will see a design view of a new wsdl file. Move to source view by selecting "Source" tab.

6. You will see an skeleton source of the new wsdl. Just remove it. (remove all elements in the wsdl)

7. Copy the contents of your existing wsdl (Suppose it is Myservice.wsdl) and paste in the source tab.

8. Save it by selecting save button in eclipse tool bar.

9. Right click on the wsdl file and select Validate

If your wsdl has errors, those will be shown in problems pane.

Tuesday, August 18, 2015

Implement own CSS in htmlpanel inside Iframe in Google Web Toolkit (GWT)


Many times it happen we want to load external html file in iframe of GWT.
When we try to incorporate html file inside IFrame it become difficult to implement html file specific css. Reason parent GWT frame override the html css. To overcome it we can use following code of line.

Here we are using two static native method one is used to load iframe and another is to add our specific css to the html inside iframe.
private final native void fillIframe(IFrameElement iframe, String content) /*-{
    var doc = iframe.document;
  
    if(iframe.contentDocument)
      doc = iframe.contentDocument; // For NS6
    else if(iframe.contentWindow)
      doc = iframe.contentWindow.document; // For IE5.5 and IE6
  
     // Put the content in the iframe
    doc.open();
    doc.writeln(content);
    doc.close();
  }-*/;


private final native void addHeadElement(IFrameElement iframe, String cssUrl) /*-{
         setTimeout(function() {

           var body;
           if ( iframe.contentDocument ) {
             // FF
             iframe.contentDocument.designMode= "On";
             iframe.contentDocument.execCommand('styleWithCSS',false,'false');
             body= iframe.contentDocument.body;
           }
           else if ( iframe.contentWindow ) {
             // IE
             body = iframe.contentWindow.document.body;
           }

           if (body == null) {
             return;
           }

           body.className = "custom-body-classname";
           var head = body.previousSibling;
           if(head == null) {
             head = iframe.contentWindow.document.createElement("head");

             iframe.contentWindow.document.childNodes[0].insertBefore(head, body);
           }
           var fileref = iframe.contentWindow.document.createElement("link");
           fileref.setAttribute("rel", "stylesheet");
           fileref.setAttribute("type", "text/css");
           fileref.setAttribute("href", cssUrl);
           head.appendChild(fileref);
         }, 50);
       }-*/;

Make sure that we need to  call this above two static native method before using iframe concept.

This is the code which we use to insert html code in our IFrame.

final HTML htmlPanel = new HTML("

This is siddharatha

"
);
      
       FlowPanel innerBox = new FlowPanel() {
           @Override
           protected void onLoad() {
               super.onLoad();
        
               // Fill the IFrame with the content html
               
               fillIframe(iframe, htmlPanel.getHTML());
        
               // Add a HEAD element to the IFrame with the appropriate CSS
               
               addHeadElement(iframe, "siddhu.css");
           }
       };
       innerBox.getElement().appendChild(iframe);

       RootPanel.get().add(innerBox);





Monday, August 17, 2015

Calling GWT Function from JS in GWT Application

Calling GWT Function from JS in GWT Application
In our *.html file add this line of code

<script type="text/javascript" language="javascript">
 function localJavaScript() {
  var msg = window.gwtJSCall("SiddharthaDhumale");
  alert(msg);
 }
</script>

       <h1>Calling GWT Function from JS in GWT Application</h1>

       <table align="center">
              <tr>
                    
              </tr>
              <tr>
                     <button onclick="localJavaScript();">Button to execute Java function.</button>
              </tr>
              <tr>
                     <td colspan="2" style="color: red;" id="errorLabelContainer"></td>
              </tr>
       </table>
</body>
</html>
In our client side use this code

       public class TestExternalJS implements EntryPoint {
      
        public static String GWTClassFunction(String name) {
                return "HellGWTClassFunction called  " + name + "!";
               }

               public static native void exportMethod() /*-{
                $wnd.gwtJSCall = function(name) {
                 return @com.test.client.TestExternalJS::GWTClassFunction(Ljava/lang/String;)(name);
                };
               }-*/;
      
       /**
        * This is the entry point method.
        */
       public void onModuleLoad() {
              exportMethod();
             
             
       }
}




GWT Application calling JS Function written in External JS File

GWT Application calling JS Function written in External JS File
Lets create our own simple Js file named as MyJSTest.js
function count_siddhu() {
    for(var i=1; i<=3; i++) {
        alert("siddhu count "+i)
    }
}
      
In our *.html file add this line of code

<script type="text/javascript" language="javascript" src="MyJSTest.js"></script>

In our client side use this code

      
       public static native void onMyButtonClick() /*-{
     $wnd.count_siddhu();
       }-*/;
      
      
       /**
        * This is the entry point method.
        */
       public void onModuleLoad() {
              //onMyButtonClick();
              final Button sendButton = new Button("Send");         

              // We can add style names to widgets
              sendButton.addStyleName("sendButton");
             
              RootPanel.get("sendButtonContainer").add(sendButton);
             
              // Create a handler for the sendButton and nameField
              class MyHandler implements ClickHandler, KeyUpHandler {
                     /**
                      * Fired when the user clicks on the sendButton.
                      */
                     public void onClick(ClickEvent event) {
                           //sendNameToServer();
                            onMyButtonClick();
                     }

                     /**
                      * Fired when the user types in the nameField.
                      */
                     public void onKeyUp(KeyUpEvent event) {
                           if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
                                  onMyButtonClick();
                           }
                     }

             
              }

              // Add a handler to send the name to the server
              MyHandler handler = new MyHandler();
              sendButton.addClickHandler(handler);
             
       }

In short

1-  Import the JS Lib in your .html file.
2-  Create a method like this:
public static native void onMyButtonClick() /*-{
     $wnd.count_siddhu();
       }-*/;

3-  Bind your button like this:
public void onClick(ClickEvent event) {
                           //sendNameToServer();
                            onMyButtonClick();
                     }

Please make sure javascript containing your function is loaded before the generated GWT javascript.




Thursday, August 06, 2015

SSL using Keytool and integrating it with Tomcat

use this command to create keystore in specific folder

C:\Certitificate>keytool -genkey -alias siddhu -keyalg RSA -keystore C:\Certitificate\siddhukeystore
fill all the necessary information it is asked

Enter keystore password:
Re-enter new password:
What is your first and last name?
[Unknown]: siddharatha dhumale
What is the name of your organizational unit?
[Unknown]: test
What is the name of your organization?
[Unknown]: test
What is the name of your City or Locality?
[Unknown]: pune
What is the name of your State or Province?
[Unknown]: maharashtra
What is the two-letter country code for this unit?
[Unknown]: IN
Is CN=siddharatha dhumale, OU=test, O=test, L=pune, ST=maharashtra, C=IN correct?
[no]: yes
Enter key password for
(RETURN if same as keystore password):
Re-enter new password:
After this you will be able to see a file with name siddhukeystore inside C:\Certitificate folder
Verify the content using 
keytool -list -keystore C:\Certitificate\siddhukeystore
Change server.xml of Tomcat placed inside conf folder 
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" keystoreFile="C:\Certitificate\siddhukeystore"
keystorePass="password" />

and restart server and finally check the url using https protocal.

Note: As this certificate is self authenticated it will ask the end user to add exception and allow them to move forward. 






Create WAR File for GWT project

First compile project with GWT Compile.

Go into project's war directory C:\workspace\TestGWTPRroject\war

Select all the files & folders available inside war directory.

Zip all the selected files & folders in a file called TestGWTProject.zip.

Rename TestGWTProject.zip to TestGWTProject.war.