Friday, December 16, 2016

Converting JSON to Java Objcet by using Gson library





Download Gson library and put in your project build path.


Java Object will be a pojo ResponsePojo with fields status,code and message.

Code:
           
                 String jsonString = "{\"status\": 200,\ "code\": \"007\",\"message\": \"working fine                                                    \"}";
                Gson gson = new Gson();

//JSON to java object

jsonResponse = gson.toJson(jsonString );
                ResponsePojo rp = gson.fromJson(jsonResponse , ResponsePojo .class);



Output:

{
           "status": 200,
           "code": "007",
          "message": "working fine"
}


Converting Java object to JSON string using Gson library





Download Gson library and put in your project build path.


Java Object will be a pojo javaObject with fields status,code and message.

Code:
           
                Gson gson = new Gson();
jsonResponse = gson.toJson(javaObject);

               log.info(" json string : " + jsonResponse);


Output:

{
           "status": 200,
           "code": "007",
          "message": "working fine"
}


Monday, December 12, 2016

Copy files by using Commons VFS APIs (Virtual File System) in java






Below class Test have copyFile method to move file from d:\\temp\\server.txt" local path to shared folder path \\\\<shared_folder_path>\\<folder_name>\\

public class Test {
     public static void main(String[] args) throws org.apache.commons.vfs2.FileSystemException {
         moveFile("server", "d:\\temp\\server.txt");
        }
     public static void copyFile(String fileName, String inputFolder)
                                                 throws org.apache.commons.vfs2.FileSystemException
    {

               FileObject destn = VFS.getManager().resolveFile("\\\\<shared_folder_path>\\<folder_name>\\" + fileName +                                                                                                                                                  ".txt");

             FileObject fo = VFS.getManager().resolveFile(inputFolder);     
             destn.copyFrom(fo, Selectors.SELECT_SELF);
             destn.close();
      }
}


Move files by using Commons VFS APIs (Virtual File System) in java





Below class Test have moveFile method to move file from d:\\temp\\server.txt" local path to shared folder path \\\\<shared_folder_path>\\<folder_name>\\

public class Test {
     public static void main(String[] args) throws org.apache.commons.vfs2.FileSystemException {
         moveFile("server", "d:\\temp\\server.txt");
        }
     public static void moveFile(String fileName, String inputFolder)
                                                 throws org.apache.commons.vfs2.FileSystemException
    {

               FileObject destn = VFS.getManager().resolveFile("\\\\<shared_folder_path>\\<folder_name>\\" + fileName +                                                                                                                                                  ".txt");

             FileObject fo = VFS.getManager().resolveFile(inputFolder);     
             fo.moveTo(destn);
             destn.close();
      }
}


Java Web Start Bundling jars/configuration files as one and handling versioning and auto update

Creating a single Jar

Here we are going to put all the project dependent jar files in build path of the main project which is having main entry class of our application.

Eg. In below screen we have Demo1 and Demo2, two dependent/ reference jar files and Test.java is the main entry class of our project which is using these jars.




We will create jar of JWSDemo project as Test.jar and we will be signing only this jar. Below are the steps to export main project including reference jars
Select  Runnable JAR file.




Select  highlighted options as in immediate below screen.





Click Finish.

By doing this, we need to handle versioning of the main jar not the reference jars. If there would be any change or update in any jar, it will get downloaded automatically after implementing versioning on main jar.


Creating jar of configuration files:

Below scree shows the configuration jar (conf__V1.1.jar) having multiple configuration files in it.






Steps to implement :
    1.     Select all the configuration files you want to be in a jar and export as a jar.


    











    





By default, jar will get created in you workspace folder.

    1.     Rename it to conf__V1.0.jar for version implementation and sign it as we did in signing jar steps earlier in this document.

    2.     After signing, put it in your main project or in build path and export your main project jar and rename it to a version number and sign the project jar.

3.     If you have any change to do in configuration file, then update you change and repeat step 1 to 4.But during repeating the steps maintain version names of jar files.

4.     Below screen show how to use configuration file in our project classes.






1.     Do mention the configuration jar in the servlet where we are creating dynamic JNLP as in below screen.






Thanks!!! Keep Learning !!! Keep Sharing !!!










Java Web Start (JWS) application creation, installation and execution Process

Step1:  Create a Java Application.



Step2:   Extract jar from the project.

Step3: Rename all jars for version management. Eg rename Test.jar to Test__V1.0.jar.

Step4: Compress all the jars using pack200 compression for reducing the download time.Follow below steps to do:
The following steps describe how to create and deploy a compressed JAR file for a signed RIA.
i.                    Normalize the JAR file using the --repack option.
This step ensures that the security certificate and JAR file will pass verification checks when the RIA is launched.
pack200 --repack Test__V1.0.jar
ii.                  Sign the normalized JAR file.
jarsigner -keystore myKeyStore Test__V1.0.jar me
where myKeyStore is the name of the keystore and me is the alias for the keystore.
iii.                Pack the signed JAR file
pack200 Test__V1.0.jar.pack.gz Test__V1.0.jar   
iv.                 Set the jnlp.packEnabled property to true in the RIA's JNLP file

Step5: Write a JNLP file as below: (Test.jnlp)


<?xml version="1.0" encoding="utf-8"?>
<!-- JNLP File for Web Start Demo -->
  <jnlp codebase="http://localhost:8080/examples/ "
  href="Test.jnlp">
            <security>
       <all-permissions/>
     </security>
    <information>
       <title>Swing Application - Test</title>
       <vendor>Sharma</vendor>
       <description>Swing Application - Test</description>
       <description kind="short">Swing Application - Test</description>
       <offline-allowed/>
              
               <shortcut online="true">
      <desktop/>
      <menu submenu="test" />
    </shortcut>
     </information>
           
     <resources>
               <j2se version="1.7+"  />
               <jar href="Test.jar" main="true" version="1.0"/>
              
                           <jar href="Hello.jar" version="1.3"/>
                           <jar href="ops.jar" version="1.0"/>
           <property name="java.home" value="C:\Program Files\Java\jdk1.8.0_101\"/>                     
               <property name="jnlp.versionEnabled" value="true"/>
           <property name="jnlp.packEnabled" value="true"/>
     </resources>
           
     <application-desc main-class="net.codejava.swing.download.Test">
                        <argument>John</argument>
                        <argument>http://<server_ip>:8080/examples/test.cfg</argument>
             </application-desc>
             
</jnlp>





Step6: Put all the jars, configuration and .jnlp file on the server.
Step7: Creating launcher which we will provide to client to launch our JWS                                            application.
             launcher.bat  (optional)
             javaws http://<IP_server>:8080/examples/Test.jnlp



Step8: When client double click on the launcher, all jars will get downloaded and a shortcut of the application will get created on his/her desktop.




     Thanks !!! Keep learning, keep sharing !!!









Basics of Web Start and Demo 2



Demo 2:

Passing argument dynamically in JNLP file.



ControllerServlet will dynamically generate JNLP file including the argument passed from JSP and store .jnlp file on server.




Below screen will automatically click on the link of s .jnlp file which is stored on web server.














Here we get name  ‘John’ dynamically from JSP through JNLP





Thanks!!! Keep Learning !! Keep Sharing !!!



Maven Project and Hello Spring Application


Below steps will show how to create Maven project and create a Spring Hello Application :

Step 1

Go to File --> New --> Other  
And select Maven Project as in below screen:



Step 2

Check Create a simple Project




Step 3

Here Group Id is package name and Artifact Id is name of the project




Step 4

Below is the Hierarchy of the Maven Project with pom.xml




Step 5

Create a Class




App.java

package com.sks.spring.test;

public class App {

public static void main(String[] args) {
// TODO Auto-generated method stub
Person p = new Person();
p.speak();
}


}

Person.java

package com.sks.spring.test;

public class Person {
public void speak(){
System.out.println(" I am a person");
}
}


Step 6




Step 7

Double click on pom.xml  and open Dependency tab



Step 8

Click on Add and start searching for springframework  dependencies and select highlighted basic spring dependencies.You can select as per your requirement. If you wont find required dependencies, please go to https://mvnrepository.com/ and add them in you pom.xml




Thats it !!!


Step 9

Now change App.java as below

package com.sks.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
public static void main(String[] args) {
//
//  Person p = new Person();
//  p.speak();


 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
 Person person = (Person)context.getBean("person");
 person.speak();

}


}

Step 10

Create spring applicationContext.xml in src/main/resources as below:

<?xml version ="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
       ">

       <!-- here we are injecting the bean in spring context -->

       <bean id="person" class="com.sks.spring.test.Person"></bean>

</beans>


Step 11

Run App.java and see the below output































Basics of Web Start and Demo 1




Hey Guys, I am working on the research and development on whether we can use Java Web Start in our project or not. So here are my findings till now with practical as I have already posted demo as well.

Java Web Start is a platform designed by the Sun for running java applications from the browsers.
The requirement on which I am working is whether we can handle automatic update or also only the one jar update out of multiple jars which really has code change or any other update. Version management and few others.

JWS (Java Web Start) can run on any platform, so it’s a cross platform.
We can handle memory, heap and other parameters through JWS.We can bundle multiple jars of a project and execute JNLP file (will explain) to download all those jars on local system to speed up the frequent application access. If there is any change in the project or in any jar or configuration file, we can handle it and can download only changed jar or file from the server by changing our JNLP file.

JNLP is Java Network Launching protocol and can be used for web start. It’s an XML file with configuration settings for our java application. It has XML schema which specify that how we can execute or launch our java application.

For our browser or the server on which we are running JNLP, to recognize this file we need to set MIME type application/x-java-jnlp-file. For example to make it recognized by the Web Sphere server we need to add MIME type as mentioned below.



and then add jnlp extension



JNLP features:
1.       Includes .jar file path and main class name
2.       The way server is configured, we can also configure if browser is not configured and execute jnlp which will download applicaition. We need to configure server or browser because it will pass jnlp to JRE.
3.       We can create a jnlp which will check JRE or its version to run application or even prompt user to install appropriate jre.
4.       After execution, user don not neet to be get connected to internet.
5.       Updates automatically gets downloaded when user will get connected to interbet and version handling will be handled automatically if JNLP is properly configured.




JNLP file: (jar versioning and argument passing to main method)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jnlp PUBLIC "-//Sun Microsystems, Inc.//DTD JNLP 1.5//EN" "http://www.netbeans.org/jnlp/DTD/jnlp.dtd">
<jnlp codebase="http://localhost:8080/examples/" href="Test.jnlp">
  <information>
    <title>test</title>
    <vendor>test.com</vendor>
   
    <description>Test</description>
   
    <offline-allowed/>
   <!-- Prefer a shortcut for online operation -->
<shortcut online="true">
  <!-- create desktop shortcut -->
  <desktop/>
  <!-- create menu item for this app under the major heading Esperanto -->
  <menu submenu="AAA"/>
</shortcut>
  </information>


 <resources>
          <j2se version="1.7+"  />
          <jar href="Test.jar" main="true" version="1.0"/>
          <jar href="SwingDemo.jar" version="1.0"/> 
                 <jar href="Hello.jar" version="1.3"/>
                 <jar href="World.jar" version="1.0"/>
           <property name="java.home" value="C:\Program Files\Java\jdk1.8.0_101\"/>                
          <property name="jnlp.versionEnabled" value="true"/>
          
     </resources>
  <application-desc main-class="com.sks.test.Test">
              <argument>John</argument>
              <argument>http://localhost:8080/examples/test.cfg</argument> 
  </application- desc>
</jnlp>










JNLP file (jar version and configuration version)

<?xml version="1.0" encoding="utf-8"?> <!-- JNLP File for Web Start Demo -->
<jnlp codebase="http://170.197.229.152:8080/deployment " href="">
       <security>
              <all-permissions />
       </security>
       <information>
              <title>Swing Application - Test</title>
              <vendor>Sharma</vendor>
              <description>Swing Application - Test</description>
              <description kind="short">Swing Application - Test</description>
              <offline-allowed />
       </information>
       <resources>
              <j2se version="1.7+" />
              <jar href="http://<IP>:8080/examples/Test.jar" main="true"
                     version="9" />
              <jar href="http://<IP>:8080/examples/conf.jar" version="1.1" />
              <property name="jnlp.versionEnabled" value="true" />
       </resources>
       <application-desc main-class="com.sks.test.Test">
              <argument>test</argument>
       </application-desc>
</jnlp>



Demo 1.

    1.       Below screen showing Java Swing application or you can create any Java application.



    2.       Configured JNLP file to download multiple jars through link mentioned in green circle:



       3. Exporting Java project as a Test.jar


     4.       Providing main entry class while generating jar file


    



     






Store jar file to D:\apache-tomcat-7.0.72\webapps\examples location on web server.

  5. Sign this jar as per jar signing process mentioned above in the document. 
  6. Rename it for versioning eg. Test__V1.0.jar and SwingFileDownloadHTTP__V1.0.jar
      7. Update JNLP file with below line,

   <jar href=" Test.jar" main="true" version="1.0" />
   <jar href=" SwingFileDownloadHTTP.jar" main="true" version="1.0" />
       SwingFileDownloadHTTP__V1.0

    8. Creating web link to run this file.
9  9. JSP file to run JNLP file

   

a   run on server.




    10. After clicking on JNLP Run hyper link, you will be able to see your java application running through web start.

























      
   11.       Checking download and versioning..

    Go to Control Panel à Java à View


        


    12. The jar which was configured in build path gets downloaded.

     


     Thanks !!! Keep learning, keep sharing !!!


   




Fixing yum command on linux

You may find yourself having to fix more packages. So you can just remove everything you had installed via  pip and reinstall everythin...