Monday, February 18, 2013

Java VisualVM to monitor memory

Condition: You want to monitor the java heap size in your local PCs or remote servers. You could use Java VisualVM tool if your device (local PCs or remote servers) must have jdk1.5+ for program execution. For Unix and Linux, they must install X-windows first.

Step to run the Java VisualVM.
  1. echo "%JAVA_HOME%"
    (To show your installed JDK path. e.g. C:\Program Files\Java\jdk1.6.0_25\bin)
  2. cd "%JAVA_HOME
    (Go to your JDK bin)
  3. jvisualvm.exe
    (Run Java VisualVM tool)
According to the following picture, you will see that 3 Java Applications are running on my PC. They are
  1. Unknow Application (pid 3164)
  2. JDeveloper 11g (pid 248)
  3. Tomcat (pid 3560)

Double the java application you want to monitor. I chose Tomcat as an example. You can select Monitor, Threads, Sampler and Profiler tabs to view the statistic.

To monitor remote server (JBOSS), you could
  1. Right click "Remote" and select "Add Remote Host..."
  2. Input your Host Name such as 192.168.254.111
  3. Click "Advanced Settings" and input Post 2020 (jstsd port - RMI Broadcasting - Listener) and set the Refresh Interval
  4. Then you will see 192.168.254.111 under remote
  5. Right click 192.168.254.111 and select "Add JMX Connection"
  6. Input Port "1090" (JMX port setting in Jboss - Check your server setting)
  7. Input Username and password
My Personal Jboss Testing Server Setting
  • JMX port in JBoss server : 1090
  • jstatd port in JBoss server : 2020
  • jstatd port in local (PC) Tomcat server (your IP) should be as default

Monday, February 4, 2013

Vaadin 6.8 with EmbedPDF

Introduction: 

Environment:
Vaadin 6.8 + AppletIntegration 1.2.9 (Add-on)
Spring-Security 3.1.2
Spring 3.1.0
EmbedPDF.jar (Version: 0.18)

Condition: 

Due to business requirement, it only allows users to print PDF. No save function will be provided at all. (That means: No PDF file will be created at the Local directory)

I tired to find the solution on the internet but no suitable methods for me. Therefore, I used the EmbedPDF Applet instead of Adobe Reader to view PDF.

There are several steps that will be gone through in details:
  1. Add Vaadin addon AppletIntegration (not describe in details)
  2. Add the EmbedPDF Applet into your Vaadin Web Application
  3. Configure your Spring-Security file

Add the EmbedPDF Applet into your Vaadin Web Application

  1. Create a directory named "applet" under your WebContent/VAADIN/  It is because files under /WEB-INF directory are protected. It is not allowed to be directly accessed by users.
  2. Place your EmbedPDF.jar and a PDF file into "applet" directory 
  3. In your source code, please pay attention to 
    • AppletClass - it always be "EmbedPDF.class"
    • Codebase - it is the directory your EmbedPDF.jar placed into
    • AppletParams - they are the parameters being passed into the EmbedPDF applet
      • Please kindly refer to the EmbedPDF Specification Readme file. There are some parameters must be set.
     
    VerticalLayout x = new VerticalLayout();
    AppletIntegration applet2 = new AppletIntegration(){  
         private static final long serialVersionUID = 7124868233557401171L;  
         @Override  
         public void attach(){  
             setAppletArchives(Arrays.asList(new String[]{"EmbedPDF.jar"}));  
             setCodebase("/ProjectName/VAADIN/applet/");     
             setAppletClass("EmbedPDF.class");  
             setAppletParams("id", "embedId");  
             setAppletParams("pdf", "/ProjectName/VAADIN/applet/example.pdf");  
             setAppletParams("enableOpenWindow", "false");  
             setWidth("500px");  
             setHeight("500px");  
         }  
     };  
     x.addComponent(applet2);  
     getApplication().getMainWindow().addWindow(x);  
    
The file structure

Configure your Spring-Security file

  1. It is required to make the /VAADIN/applet/ directory to be Anonymously. It is because a new session will be created when the EmbedPDF applet is called. So, the spring security go through the authentication. It is required to pass the username and password to the spring security for the authentication process. There is an exception thrown in the app. server since spring security cannot get the username and password. So, it will block the EmbedPDF.jar and EmbedPDF.class to run. 
  2. There lists some exceptions will be found if the spring security setting is not correct
    • java.lang.ClassFormatError: Truncated class file
    • java.lang.ClassFormatError: Incompatible magic value 537528892 in class file
 <http auto-config="false" entry-point-ref="authenticationEntryPoint"   
     access-denied-page="/jsp/login_error" use-expressions="true">  
     <intercept-url pattern="/jsp/login*" access="permitAll" />  
     <intercept-url pattern="/jsp/login_error*" access="permitAll" />  
     <intercept-url pattern="/VAADIN/applet/**" access="permitAll" />  
     <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />  
     <logout invalidate-session="true"/>  
     <custom-filter ref="authenticationFilter" position="FORM_LOGIN_FILTER"/>  
 </http>  

Addition Information: Delete the dynamic generated PDF file

  1. The following code describe how to get the absolute path in java for server
  2. It is better to use a helper to create a PDF file and return a new AppletIntegration
  3. When user removes the Applet from the layout, the remove will be deleted (see the "detach" function)

//TODO: create your own file

VerticalLayout x = new VerticalLayout();
final String filename = "example.pdf";
final String absReportzPath = String absProjPath = ((WebApplicationContext)ProjectApplication.getInstance().getContext())
 .getHttpSession().getServletContext().getRealPath("/VAADIN/reportz/reportz/");

AppletIntegration applet2 = new AppletIntegration(){  
     private static final long serialVersionUID = 7124868233557401171L;  
     @Override  
     public void attach(){  
         setAppletArchives(Arrays.asList(new String[]{"EmbedPDF.jar"}));  
         setCodebase("/ProjectName/VAADIN/applet/");     
         setAppletClass("EmbedPDF.class");  
         setAppletParams("id", "embedId");  
         setAppletParams("pdf", "/ProjectName/VAADIN/applet/reportz/"+filename);  
         setAppletParams("enableOpenWindow", "false");  
         setWidth("500px");  
         setHeight("500px");  
     }  

     @Override
     public void detach(){ //To delete file if the applet is removed.
         String absPath = getAbsoluteReportzDirectory();
         File delFile = new File(absReportzPath+"/"+fileName);
         if(delFile.exists()){
             delFile.delete();
         }
     }
 };  
 x.addComponent(applet2);  
 getApplication().getMainWindow().addWindow(x);  

Friday, February 1, 2013

Enable HTTPS (SSL) in Tomcat7

Introduction:

To make the redirection from HTTP to HTTPS in Tomcat. There are two required steps that must be done.
  1. Generate a self-sign key (Certificate)
  2. Configure the Tomcat Server Setting
The detail steps of each part are listed below. Hope it could be useful and helpful.

Generate a self-sign key (Certificate) with the following command.

  • Open a command console and type [cmd]
  • Type [set] in order to check your java jdk directory (e.g. C:\Program Files\Java\jdk1.6.0_25\bin)
  • Type [cd ${java_directory}] (e.g. cd C:\Program Files\Java\jdk1.6.0_25\bin)
  • Type [keytool -genkey -keystore chap8.keystore -storepass rmi+ssl -keypass rmi+ssl -keyalg RSA -alias chapter8  -validity 3650 -dname "cn=chapter8 example,ou=admin book,dc=jboss,dc=org"]
    • Words/Fonts with bold style can be modified
    • chap8.keystore - Sign file name and will be generated/created in your java jdk bin directory (e.g. C:\Program Files\Java\jdk1.6.0_25\bin)
    • rmi+ssl - Password
    • chapter8 - Alias name
    • validity - How long your self-issued certificate is valid
    • inside the quote sign "chapter8, adminbook, jboss, org" are not important. It is just the information of your certificate for people to view.
  • There is a chap8.keystore file (certificate)  is created in your java jdk bin directory (e.g. C:\Program Files\Java\jdk1.6.0_25\bin)
  • Copy the generated certificate "chap8.keystore" to the folder ${cataline.home}/conf/ (e.g. C:\bin\apache-tomcat-7.0.27\conf\)

Configure the Tomcat server setting

  •  Edit ${cataline.home}/conf/server.xml and modify the following
  •  <Connector port="8080" protocol="HTTP/1.1" enableLookups="false" redirectPort="8443" /> 
  • <Connector protocol="org.apache.coyote.http11.Http11NioProtocol"
                port="8443" SSLEnabled="true" maxThreads="200"
                scheme="https" secure="true"
                keystoreFile="/conf/chap8.keystore"
                keystorePass="rmi+ssl" clientAuth="false"
                sslProtocol="TLS"/>
     
    • chap8.keystore - certificate file name you type in the previous steps
    • rmi+ssl - keystore password you type in the previous steps