Thursday, June 27, 2013

Hello World Application using Struts 2.1


- Create a Java Project & Add Struts Capabilities to the project.

- Develop index.jsp as shown below, Ensure form action value will be used in struts.xml

- Ensure web.xml should be as shown below

- Define welcome.jsp with whatever you want to display.

- Develop a Customer.java POJO class as shown below, Ensure this should have a method called execute() , Struts framework will invoke execute() method internally.. This method can return any string value based on functional requirement.


- Modify struts.xml as shown below
  - Configure action element as shown below
  -  Add as many result elements as you want based on execute() method return values.. If execute() method returns "success" , "failure" depends on two conditions, then define two result elements as shown below.


- Run this application , Right click on project, Run As ->MyEclipse Server Application

- By default, it will launch index.jsp

- By clicking on 'Submit' button , this will invoke form action and navigate to based on execute() method return value.


Wednesday, June 26, 2013

Hello World Example With Spring Framework


- Create a Java Project in My Eclipse, Add 'Spring3.1' Capabilities, By right clicking on Project->MyEclipse->Project Facets(Capabilities)->Spriing Facet
This will load required libraries in the class path.

- Create a Customer POJO as shown below, with  one parameterized constructor and to data members


- Update applicationContext.xml as shown below by configuring the bean details.

- Write a simple Test class as shown below, Right click on this class and run as java application

- You should see below output by running above class

Monday, June 17, 2013

Deployment issue while deploying from Jdeveloper to Weblogic Console : Deployer:149140

Deployment issue while deploying from Jdeveloper to Weblogic Console : Deployer:149140


If you encounter below deployment issue while deploying from Jdev to Weblogic console,

Ensure you activated the changes in weblogic console.

[12:03:14 PM] [Deployer:149140]The task cannot be processed further until the current edit session is activated. When this occurs, task processing will continue. The user can exit the deployer tool without affecting the task.

Wednesday, June 12, 2013

How to Convert org.apache.myfaces.trinidad.model.UploadedFile to java.io.File



Convert org.apache.myfaces.trinidad.model.UploadedFile to java.io.File

// assume that you have the UploadedFile object named uploadFile
InputStreamReader reader = new InputStreamReader(uploadFile.getInputStream());
int partition = 1024;
int position = 0;
int length = 0;
char[] buffer = new char[partition];
FileWriter fstream = new FileWriter("test.tmp");
do{
    length = reader.read(buffer, position, partition)
    fstream.write(buffer, position, length);
}while(length > 0);
File file = new File("test.tmp");

    /**
     * Read the attached file content as a byte[] and convert to Datahandler
     * @param valueChangeEvent
     */
    public void onFileUpload(ValueChangeEvent valueChangeEvent) {
        String methodName = "onFileUpload(ValueChangeEvent)";
        _logger.entering(CLAZZ_NAME, methodName);
        try {
            UploadedFile file = (UploadedFile)valueChangeEvent.getNewValue();
            byte[] byteArray=org.apache.commons.io.IOUtils.toByteArray(file.getInputStream());
            DataHandler dataHandler = new DataHandler(byteArray,"application/octet-stream");

        } catch (Exception b64de) {
            _logger.logp(Level.SEVERE, CLAZZ_NAME, methodName,
                         " Exception Occured", b64de);

            b64de.printStackTrace();
        }
        _logger.exiting(CLAZZ_NAME, methodName);
    }

Tuesday, June 11, 2013

How to read, write, delete, update file content using Java

How to read, write, delete, update file content using Java


package com.fm.helloworld.customer.model.fileutil;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;

public class FileUtil {
    public FileUtil() {
        super();
      int a=34;
      Integer a1 = new Integer("34");
      System.out.println(a1);
    }

    /**
     * This method reads the contents of the given file
     * and returns the String
     * @param fileName
     * @return
     */
    public String readFileContent(String fileName) {
        StringBuilder sb = new StringBuilder();
        // Get the reference of given File
        File file = new File(fileName);
        int ch;
        // FileInputStream variable
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(file);
            // Iterate through every character of given File
            while ((ch = fin.read()) != -1)
                sb.append((char)ch);
            fin.close();
        } catch (Exception e) {
            System.out.println(e);
        }
        return sb.toString();
    }

    /**
     * This method creates a new File
     * @param fileName
     * @param fileContent
     * @return
     */
    public boolean createFile(String fileName, String fileContent) {
        boolean isSuccess = true;
        try {
            File f1 = new File(fileName);
            BufferedWriter bw = new BufferedWriter(new FileWriter(f1));
            bw.write(fileContent);
            bw.close();
        } catch (Exception e) {
            System.out.println(e);
            isSuccess = false;
        }
        return isSuccess;
    }

    /**
     * This method reads the contents of the given file using Buffer Reader
     * and returns the String
     * @param fileName
     * @return
     */
    public String readFileContentUsingBufferReader(String fileName) {
        StringBuilder sb = new StringBuilder();
        // Get the reference of given File
        File file = new File(fileName);
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            while (true) {
                String str = br.readLine();
                if (str == null) {
                    break;
                } else {
                    sb.append(str);
                }
            }
            br.close();
        } catch (Exception e) {
            System.out.println(e);
        }
        return sb.toString();
    }
    /**
     * This method updated the given file with given content.
     * If everything goes fine, return true otherwise return false
     * @param fileName
     * @param fileContent
     * @return
     */
    public boolean updateFile(String fileName, String fileContent) {
        boolean isSuccess = true;
        StringBuilder sb = new StringBuilder();
        try {
            File f1 = new File(fileName);
            if (f1.canWrite()) {
                String existFileContent =
                    readFileContentUsingBufferReader(fileName);
                sb.append(existFileContent);
                BufferedWriter bw = new BufferedWriter(new FileWriter(f1));
                sb.append(fileContent);
                bw.write(sb.toString());
                bw.close();
            }

        } catch (Exception e) {
            System.out.println(e);
            isSuccess = false;
        }
        return isSuccess;
    }

    /**
     * This file copies data from srcFile to tgtFile
     * @param srcFile
     * @param tgtFile
     * @return
     */
    public boolean copyFile(String srcFile, String tgtFile) {
        boolean isSuccess = true;
        try {
            String existFileContent =
                readFileContentUsingBufferReader(srcFile);
            createFile(tgtFile, existFileContent);

        } catch (Exception e) {
            System.out.println(e);
            isSuccess = false;
        }
        return isSuccess;
    }

    /**
     *
     * @param fileName
     * @return
     */
    public boolean updateFileStatus(String fileName,boolean isReadOnly) {
        boolean isSuccess = true;
        StringBuilder sb = new StringBuilder();
        try {
            File file = new File(fileName);

            if(isReadOnly){
                file.setReadOnly();
            }else{
                file.setWritable(true);
            }
           
            boolean readOnly = file.canWrite();
            System.out.println("File Status : " + readOnly);

        } catch (Exception e) {
            System.out.println(e);
            isSuccess = false;
        }
        return isSuccess;
    }
    /**
     *
     * @param dirName
     * @return
     */
    public boolean createDirectory(String dirName){
        boolean isSuccess = true;
       
        try {
            File file = new File(dirName);
            file.mkdirs();

        } catch (Exception e) {
            System.out.println(e);
            isSuccess = false;
        }
        return isSuccess;
    }
    public static void main(String[] args) {
        FileUtil util = new FileUtil();
        //String fileContent=util.readFileContent("C:/RKP/emp.txt");
        //System.out.println(fileContent);
        String fileName = "C:/RKP/fmtest.txt";
        String tgtFileName = "C:/RKP/fmtest_copy.txt";
        String fileContent = null;
        boolean isSuccess = false;
        fileContent = " || Fortune Minds Inc - April 23";
        String dirName="C:/tmp/tmp1";
       
        //isSuccess= util.createDirectory(dirName);
         //isSuccess = util.createFile(fileName, fileContent);
        //fileContent = util.readFileContentUsingBufferReader(fileName);
        //isSuccess = util.updateFileStatus(fileName,true);
        //isSuccess = util.updateFile(fileName, fileContent);
        //isSuccess = util.updateFileStatus(fileName, false);
        //isSuccess = util.updateFile(fileName, fileContent);
        isSuccess=util.copyFile(fileName, tgtFileName);
        System.out.println(isSuccess);
        System.out.println(fileContent);
    }
}

Monday, June 3, 2013

How to get the logged in Host & User Details in Java

How to get the logged in Host Name & User Details in Java?

import java.net.InetAddress;

 String hostName = InetAddress.getLocalHost().getHostName();

To get logged in user name

String user = System.getProperty("user.name");