Monday, December 7, 2015

ArithmeticOperations Example using Python

Arithmetic operation using python in windows.
In my previous post , I have written how to install python in windows and add plugin into eclipse.

In this post I am going to explain simple arithmetic operations.

Step 1: Open eclipse - File - New - PyDev Project - arithmeticoperation_test
Step 2: Right click on the - src - New- PyDevModule- ArithmeticOperation
Step 3:Paste below code into ArithmeticOperation.py file.

```'''
Created on 07-Dec-2015

@author: rajusiva

'''
a = input("Enter a value:")
b = input("Enter b value:")
print("subtract of 2 values is[",int(a)-int(b),"]")
print("Multiply of 2 values is[",int(a)*int(b),"]")
print("Division of 2 values is[",int(a)/int(b),"]")
print("Exponent of 2 values is[",int(a)**int(b),"]")
print("Modules of 2 values is[",int(a)%int(b),"]")
print("Floor Division of 2 values is[",int(a)//int(b),"]")
```

Step 4: Right click on the program-Run As- Python Run
Step 5: output as follows

```Enter a value:25
Enter b value:10
Addition of 2 values is[ 35 ]
subtract of 2 values is[ 15 ]
Multiply of 2 values is[ 250 ]
Division of 2 values is[ 2.5 ]
Exponent of 2 values is[ 95367431640625 ]
Modules of 2 values is[ 5 ]
Floor Division of 2 values is[ 2 ]
```

Sunday, December 6, 2015

Getting started with python using eclipse

Getting started with Python using eclipse

Step2:  Click on the downloaded exe file. Then it will open like below screen

Step 3: While installing , you need to  check the both check box.

Step 4: Click the Customize installation, .

Step 5: Click on the Next button,

Step 6: Change the Customize install location(C:\Python32), you can give any location where ever
you need python to be install. Then click finish

Step 7:  Python installed successfully on your machine.

Step 10:  open eclipse and select workspace
Step 11:   Click on help – Install New Software

Step 12: Provide   http://pydev.org/updates as mentioned below screenshot select all checkboxes, then click finish.
Step 13 :Select the checkboxes to install the python plugin in eclipse

Step 14: Need to configure python in eclipse.
Go to eclipse – Windows- preferences-

Step 15: Check for PyDev or python – Python Interpreter – Click on the new button
Add the Interpreter Name and Interpreter Executable – where ever your python installed location
Python plugin has been configured in your eclipse successfully.
Step 16 : create PyDev project- Give any project name (helloworld_python)
In eclipse- File- New - PyDev project

Provide name as  - helloworld_python then click finish, But make sure – Create src folder and add it to the PYTHONPATH- radio button selected

Step 17: Now python project created. Need to create Python Module in order to start the coding.
Right click on the src folder of the python project- >new -> pyDevModule

Step 18: Click finish

Copy below code into your program

```def hello(userInput):
return "Hello World:"+userInput
print(hello(userInput))
```

Step 19: How to run the python program – Right click on the program
Run As – Python Run

Output- Hello World: siva

Saturday, November 21, 2015

Oozie  -  Framework

Oozie is a workflow/coordination system that you can use to manage Apache Hadoop jobs.

Oozie server a web application that runs in java servlet container(the standard Oozie distribution is using Tomcat).

This server supports reading and executing Workflows. Coordinators and Bundles definitions.

Oozie is a framework which is used to handle to run the Hadoop jobs.
It is same like Autosys,Cron jobs, ContolM  Scheduling tools

HPDF – Hadoop process definition language – defining the job details, start node , stop node ,input directory ,output directory etc.. details.
Main features:
Execute and Monitor workflows in Hadoop
Periodic scheduling of workflows
Trigger execution of data availability
HTTP and command line interface and webconsole
Oozie work flow start
Go to installation directory of oozie
```Ex: cd /usr/lib/oozie-4.0.0/
:  ./bin/oozie-start.sh```
Once it’s started then click the below URL to check whether Oozie started or not.
localhost:11000/oozie

In Oozie webconsole, you can see the job information (logs,configuration,etc..)

Oozie Workflow:

PREP: when a workflow job is first created it will be PREP state. Job is defined but not running.

RUNNING: When a CREATED workflow job is started.it goes into RUNNING state, it will remain in RUNNING state while it does not reach it’s end state, ends in error or it is suspended.

SUSPENDED: A RUNNING workflow job can be suspended, it will remain in SUSPENDED state until the workflow job is resumed or it’s killed.

Scheduling with Oozie

Coordinator to Map Reduce  ->   Launch MR jobs at Regular intervals

Map Reduce to HDFS -> Write Files

Oozie – workflow.xml

The workflow definition language is  XML based and it is called HPDL- Hadoop Process Definiton language).

Workflow.xml minimum details we need to mention like name, starting point and ending point.

```Ex:
Name=”WorkFlowRunnerTest”>

```
Flow control nodes: Provides a way to control the workflow execution path
Start node start : This specifies a starting point of an Oozie Workflow
End node end: This specifies an end point for an Oozie Workflow.
To this we need to add action , and within that we will specify the map-reduce parameters.
```

localhost:8032
hdfs://localhost:9000

mapred.input.dir
{inputDir}

mapred.output.dir
(outputDir)

```
action requires and tags to direct the next action on success or failure.

Job.properties file needs to mention details like input dir, output dir.
Job.properties file no need to move to HDFS.

Running a Oozie Application

1. Create a directory for Oozie Job(WordCountTest)
2. Write a application and create a jar (ex:Mapreduce jar). Move this jar to lib folder in WordCountTest directory.
3. Job.properties and workflow.xml inside WordCountTest directory
4. Move this directory to HDFS
5. Running the application
oozie job  -oozie http://localhost:11000/oozie  - config  job.properties –run
(job.properties should be from local path)

Workflow Job Status command
oozie job –info job_123
Workflow Job Log
oozie job  –log  job_123
Workflow Job definition
oozie job  –definition job_123
Oozie version

Oozie Coordinator

The Oozie Coordinator supports the automated starting of Oozie workflow process.

It is typically used for the design and execution of recurring invocations of Workflow processed triggered by time and/or data availability.

```

hdfs://bar:9000/app/logs/\${YEAR}/\${MONTH}/\${DAY}/\${HOUR}

\${current(0)}

hdfs://localhost:9000/WordCountTest_TimeBased

inputData
\${data(‘inputLogs’)}

```

Oozie commands

Checking the multiple workflow jobs

oozie jobs –oozie http://localhost:11000/oozie -localtime -len -filter status=RUNNING

Checking the status of the multiple coordinator jobs

oozie  jobs –oozie  http://localhost:11000/oozie -jobtype coordinator

Killing a workflow , Coordinator or Bundle Job

oozie  job –oozie  http://localhost:11000/oozie -kill

Checking the Status of a workflow , Coordinator or Bundle Job or a Coordinator Action

oozie job –oozie  http://localhost:11000/oozie –info

Hope this will guide you how to work with Oozie framework.

Saturday, September 5, 2015

struts multibox example

Struts multibox [multiple check boxes] example

1. create a memeber variables and respective setters and getters in any form class like below

```import org.apache.struts.action.ActionForm;

public class LanguageForm extends ActionForm
private String[] selectedLanguages = {};
private String[] languages = {"Java","J2EE","JSP","STRUTS","Spring"};

public String[] getSelectedLanguages() {
return selectedLanguages;
}

public void setSelectedLanguages(String[] selectedLanguages) {
this.selectedLanguages = selectedLanguages;
}

public String[] getLanguages() {
return languages;
}

public void setLanguages(String[] languages) {
this.languages = languages;
}
```

2. Add below code inside action class to display languages in JSP

```public class LanguageAction extends Action {
private static final String SUCCESS = "success";

/**
*
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String selectedLanguageValues="";
LanguageForm languageForm = (LanguageForm)form;
for (String selectedLanguage : languageForm.getSelectedLanguages()) {
selectedLanguageValues = selectedLanguageValues.concat(selectedLanguage+",");
}
if(selectedLanguageValues != null && !selectedLanguageValues.isEmpty()){
selectedLanguageValues = selectedLanguageValues.substring(0,selectedLanguageValues.length()-1);
}
System.out.println("selectedLanguageValues["+selectedLanguageValues+"]");
return SUCCESS;

}
}
```

3. JSP code as mentioned below

```<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>

```

4. Need to do respective configuratio in struts-config.xml like action class, form class etc...
Refer Struts Step By Step example in this blog for configuration related details.

Friday, September 4, 2015

Create WordCount example using mapreduce framework in hadoop using eclipse run on windows

Create WordCount example using mapreduce framework in hadoop using eclipse run on windows
1. Open Eclipse
2. Create new java project and named it as - mapreduce_demo
3. Create Java class with name WordCount.java

How map reduce will work in hadoop
Approach:1
input.txt file having the following data..
```(map)
--------------------------------------------------------------------------------------
abc def ghi jkl mno pqr stu vwx    P1- abc 1 def 1 ghi 1 jkl 1 mno-1 pqr 1  stu 1 vwx 1
abc def ghi jkl mno pqr stu vwx    P2- abc 1 def 1 ghi 1 jkl 1 mno-1 pqr 1  stu 1 vwx 1
abc def ghi jkl mno pqr stu vwx    P3- abc 1 def 1 ghi 1 jkl 1 mno-1 pqr 1  stu 1 vwx 1
abc def ghi jkl mno pqr stu vwx    P4- abc 1 def 1 ghi 1 jkl 1 mno-1 pqr 1  stu 1 vwx 1
abc def ghi jkl mno pqr stu vwx    P5- abc 1 def 1 ghi 1 jkl 1 mno-1 pqr 1  stu 1 vwx 1
abc def ghi jkl mno pqr stu vwx    P6- abc 1 def 1 ghi 1 jkl 1 mno-1 pqr 1  stu 1 vwx 1
abc def ghi jkl mno pqr stu vwx    P7- abc 1 def 1 ghi 1 jkl 1 mno-1 pqr 1  stu 1 vwx 1
abc def ghi jkl mno pqr stu vwx    P8- abc 1 def 1 ghi 1 jkl 1 mno-1 pqr 1  stu 1 vwx 1
abc def ghi jkl mno pqr stu vwx    P9- abc 1 def 1 ghi 1 jkl 1 mno-1 pqr 1  stu 1 vwx 1
abc def ghi jkl mno pqr stu vwx    P10- abc 1 def 1 ghi 1 jkl 1 mno-1 pqr 1  stu 1 vwx 1
---------------------------------------------------------------------------------------
map
-----
processes one line at a time, as provided by the specified TextInputFormat
emits a key-value pair of < , 1>.

Reducer
----------
The Reducer implementation, via the reduce method just sums up the values, which are the occurence counts for each key
emit(eachWord, sum)
```

4. Paste the below code in the WordCount.java file
```import java.io.IOException;
import java.util.StringTokenizer;

public class WordCount {

public static class Map extends Mapper{

public void map(LongWritable key, Text value, Context context
) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
value.set(itr.nextToken());
context.write(value, new IntWritable(1));
}
}
}

public static class Reduce extends Reducer {

public void reduce(Text key, Iterable values,
Context context) throws IOException, InterruptedException {

int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
context.write(key, new IntWritable(sum));
}
}

public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = new Job(conf,"mywordcount");

job.setJarByClass(WordCount.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);

job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);

job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);

Path outputPath = new Path(args[1]);

FileOutputFormat.setOutputPath(job, new Path(args[1]));

outputPath.getFileSystem(conf).delete(outputPath);

System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
```
5. Set up the build path to avoid errors.
Right Click on the project(mapreduce_demo)->Properties->Java Build Path-> Libraries ->Add External Jars
select jar's from
```c:\hadoop\hadoop-2.4.1\share\hadoop\common
```
6. Once you completed the above steps, now we need to create jar to run the WordCount.java in hadoop.
7. Create Jar:

Right click on the project(mapreduce_demo)->Export->Jar(Under java)->Click Next->Next->Main Class->
Select WordCount->Click Finish
8. Now we have created jar successfully.
9. Before Executing the jar, need to start the (namenode,datanode,resourcemanager and nodemanager)
12. Check whether any inut files available in the hdfs system already if not create the same.

```c:\hadoop\hadoop-2.4.1\bin>hdfs dfs -ls input (If it is not created) then use below command to create directory
Copy any text file into input directory of hdfs
Verify file has been copied to hdfs or not using below command
Verift the data of the file which you copied into hdfs
```
13. Once above steps's done. Now run the mapreduce program using the following command

14. verify the result.

verify the status of the job details and output through web url

http://localhost:50075
http://localhost:8088/cluster

Friday, August 29, 2014

RPC (Remote Procedure Call)

RPC- Remote Procedure Call
The term itself defines calling remote methods through client program.
In this post added both server and client in the same package.
Normally we have to write server classes which having business functionality
and client will only will access that methods.

Step 1: Start Eclipse
Step 2: Create new dynamic project(name it as you like Ex: rpc-test
Step 3: Create a server class(Ex:Calculator)under src folder
Step 4: Write methods as of your requirement

Ex:
```package com.siva;

public class Calculator
{
public int add(int i1, int i2) {
return i1 + i2;
}
public int substract(int i1, int i2) {
return i1 - i2;
}
public int multiply(int i1, int i2) {
return i1 * i2;
}
}
```

Step 5: We have to write configuration of xmlrpcservlet in web.xml
Step 6: Open web.xml, which is under WEB-INF/
```
XmlRpcServlet
org.apache.xmlrpc.webserver.XmlRpcServlet

enabledForExtensionstrue
Sets, whether the servlet supports vendor extensions for XML-RPC.

XmlRpcServlet
xmlrpc

```
Step 7: Need to declare server class location in the properties file name called-
XmlRpcServlet.properties under package - org.apache.xmlrpc.webserver
Step 8: XmlRpcServlet.properties file details as mentioned below

Calculator=com.siva.Calculator

The bold Calculator is being used in clent class to access the Calculator methods.

Step 9: Write the Client class

```package com.siva;

import java.net.URL;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

public class SimpleClient {

public SimpleClient() {

try {
System.out.println("Try to call calculator methods via XML-RPC...");
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();

//server ip addess or machine name/project name/servlet url pattern
config.setServerURL(new URL("http://127.0.0.1:8080/rpc-test/xmlrpc"));

XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);

Object[] params = new Object[] { new Integer(2), new Integer(3) };
Integer result = (Integer) client.execute("Calculator.substract", params);

System.out.println("The returned values is: " + result);

} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
new SimpleClient();
}

}
```
in the above client class to call the RPC methods, first we have to provide the server ip addrss, url pattern name
While calling the method name which being used for to access the business logic.

Step 10: required jars

commons-logging-1.1.jar
ws-commons-util-1.0.2.jar
xmlrpc-client-3.1.3.jar
xmlrpc-common-3.1.3.jar
xmlrpc-server-3.1.3.jar

Step 11 : Required Tomcat server or any webserver to run the application

Step 12 : Start the server after adding project to the server and run the client application,
Run As Java Application, you will see the result.
If you want to call another methods , change the method name from Clicent class and stop start the
server.

I hope this post will help to get basic knowledge about RPC.

Thursday, July 31, 2014

struts example

Struts example

Below post shows how to build simple application with struts.

open eclipse -> project -> create dynamic Webproject->

provide name as - struts_sample [As you like]

select Dynamic web module version as 2.5

Then click on Finish button.

-> open the web.xml , which available inside WEB-INF folder

copy the below code and paste in side web.xml .
```
action
org.apache.struts.action.ActionServlet

config                   /WEB-INF/struts-config.xml
1

action
*.do

```

-> Create xml and name it as struts-config.xml inside WEB-INF.
->open the struts-config.xml and paste the below code inside the struts-config.xml

```

```

-> Now it's time to start our work like creating jsp files, form classes, action classes and need to add required jars.

-> first we will write jsp page. Here i am writting 4 jsp pages
2. welcome.jsp
4. success.jsp

-> create one folder inside WebContent with name jsp [WebContent/jsp/login.jsp] -- this is for login details which will have user name and password along with validation.
paste the below code inside login.jsp

```	<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%><%@taglib
uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>

.error {
background-color: #b9ecfd;
}

```

-> create welcome.jsp [WebContent/jsp/welcome.jsp] -- this is for after successful login details .
paste the below code inside welcome.jsp
```
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

Welcome page

<%        String message = (String)request.getAttribute("message");    %>
Welcome User :
<%= message %>You have successfully logged in.

```

-> create adduser.jsp [WebContent/jsp/adduser.jsp] -- this contains user details along with required validation.
paste the below code inside adduser.jsp

```	<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%><%@taglib
uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>

.error {
background-color: #b9ecfd;
}

```

-> create success.jsp [WebContent/jsp/success.jsp] -- this page will display after successful user details.
paste the below code inside success.jsp
```	<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%><%@taglib
uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>

```
-> We need to create java classes.
-> create a package -> com.siva.form
-> com.siva.action

->create LoginForm.java inside com.siva.form package and paste the below code.
```package com.siva.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
*
* @author sivaraju
*
*/
public class LoginForm extends ActionForm {
/**
*
*/
private static final long serialVersionUID = -1743531020254647308L;

public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors actionErrors = new ActionErrors();
}
try {
}
} catch (Exception e) {
e.printStackTrace();
}
return actionErrors;
}

}

}

}

}
}
```
->create UserForm.java inside com.siva.form and paste below code

```  package com.siva.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
*
* @author sivaraju
*
*/
public class UserForm extends ActionForm {
/**
*
*/
private static final long serialVersionUID = -2087861874662967982L;
private String sex;
private int age;

public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors actionErrors = new ActionErrors();
try {
}
if (sex == null || sex.trim().equals("")) {
}
if (age <=0 ) {
}

} catch (Exception e) {
e.printStackTrace();
}
return actionErrors;
}

}

}
public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

}
```
-> create LoginAction.java inside com.siva.action pakage and paste the below code
```  package com.siva.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LoginAction extends Action {
private static final String FAILURE = "failure";
private static final String SUCCESS = "success";

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String target = null;
target = SUCCESS;
} else {
target = FAILURE;
}
return mapping.findForward(target);
}
}
```
-> create AddUserAction.java inside com.siva.action package and paste below code.
```package com.siva.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.siva.form.UserForm;
/**
*
* @author sivaraju
*
*/
public class AddUserAction extends Action {
private static final String SUCCESS = "success";

/**
*
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserForm userForm = (UserForm) form;
System.out.println("Age["+userForm.getAge()+"]");
System.out.println("Sex["+userForm.getSex()+"]");
//If this details need to store in db. Then need to get the db details from data source object. Store as per requirement.
return mapping.findForward(SUCCESS);
}

}
```
->create MessageResource.properties file inside src/resource [src/resource/MessageResource.properties] and paste the below code
```label.username = User Name
label.welcome = Welcome

label.age = Age
label.sex = Sex

error.age = Age is required field
error.sex=Sex is required field
```
-> Add the below jars inside lib folder.(Among below jar's few of the jar's may not be required]
```    bcprov-jdk14-121.jar
commons-digester-2.1.jar
commons-io-1.2.jar
commons-lang-2.4.jar
org-apache-commons-logging.jar
org.apache.commons.beanutils.jar
servlet-api.jar
struts.jar

```
-> Right click on the project-> Run As-> Run On Server-> [select the server which ever server you want to run the application]. Tomcat is prefered. -> http://localhost:8080/struts_sample/login.jsp [ after submitting it will redirect to welcome.jsp]- username - admin / password - admin123 -> http://localhost:8080/struts_sample/jsp/adduser.jsp [after submitting it will redirec tto success.jsp]. In this way stuts application will work.

Wednesday, February 27, 2013

Convert word document (.docx) to PDF

This post will describes how to convert word document to PDF using Java.

To convert document to Pdf we will have different type of approaches.
But in this post i am using  docx4j. It is one of the good API for conversion from XSLT to PDF and Word Document to PDF etc..

We can convert from document to Pdf with Simple java program.

Steps to follow.

Step1 :open Eclipse and create new java project- provide name as you like.

Step 2: Create new Java class  which ever you like (ex: ConvertDocToPDF )

Step 3: Paste the below lines of code inside main method of created java class

``` try {

long start = System.currentTimeMillis();

// 1) Load DOCX into WordprocessingMLPackage

InputStream is = new FileInputStream(new File("test.docx"));
//If your header and body information got over lapped then use the below code
List sections = wordMLPackage.getDocumentModel().getSections();
for (int i = 0; i < sections.size(); i++) {

System.out.println("sections Size" + sections.size());
}

//if you want use any Physical fonts then use the below code.

Mapper fontMapper = new IdentityPlusMapper();

PhysicalFont font = PhysicalFonts.getPhysicalFonts().get("Comic Sans MS");

fontMapper.getFontMappings().put("Algerian", font);

wordMLPackage.setFontMapper(fontMapper);

// 2) Prepare Pdf settings

PdfSettings pdfSettings = new PdfSettings();

// 3) Convert WordprocessingMLPackage to Pdf

org.docx4j.convert.out.pdf.PdfConversion conversion = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wordMLPackage);

OutputStream out = new FileOutputStream(new File("test.pdf"));
conversion.output(out,pdfSettings);
System.err.println("Time taken to Generate pdf  "+ (System.currentTimeMillis() - start) + "ms");
} catch (Throwable e) {

e.printStackTrace();
}
```

Step 4: Now you can run the Java program, PDF will be generate for your Document file.

Wednesday, February 13, 2013

spring and xslt example

This post will describe to work with spring and xslt.

1. open Eclipse and create a dynamic webproject - provide name as  spring-xslt(you can give what ever name you want)

2. After that select the target runtime environment(ex: location of tomcat or Jboss or respective server)

3. open web.xml which already created by eclipse for you, modify as mentioned below.

``` ```
``````<web-app id="WebApp_ID" version="2.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>spring-xslt</display-name>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

</div>

<servlet>

<servlet-name>simple</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>simple</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

</web-app>

``````

4. After that create simple-servlet.xml like mentioned below and put inside WEB-INF

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

<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver" >
<property name="order"><value>1</value></property>
<property name="basename" value="views" />
</bean>
<bean id="xsltViewResolver"
class="org.springframework.web.servlet.view.xslt.XsltViewResolver" >
<property name="order"><value>2</value></property>
<property name="viewClass"
value="org.springframework.web.servlet.view.xslt.XsltView" />
<property name="sourceKey" value="obj" />
<property name="suffix" value=".xsl" />
<property name="prefix" value="/WEB-INF/xsl/" />
</bean>
<bean id="simpleUrlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
<property name="mappings" >
<value>
</value>
</property>
</bean>
<bean id="myxsl"
class="com.siva.HomePageController" />
<!--<bean id="myxsl"
class="com.siva.XsltDisplayController" />
--></beans>
```
5. Now we have to create xslt file under /WEB-INF/xsl/home.xslt and place the below mentioned code

```<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes"/>
<xsl:template match="/">
<html>
<body>
<h1>My First Words</h1>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="word">
<xsl:value-of select="."/><br/>
</xsl:template>
</xsl:stylesheet>
```

6. Now Need to create Contoller class by extending AbstractCommandController.
```package com.siva;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class HomePageController extends AbstractController{
protected ModelAndView handleRequestInternal(
HttpServletRequest request,
HttpServletResponse response) throws Exception {

Map map = new HashMap();
List wordList = new ArrayList();

map.put("wordList", wordList);
return new ModelAndView("home", map);
}
}

```
7. Need to create xslt view class to iterate the xslt elements.

```package com.siva;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import org.springframework.web.servlet.view.xslt.AbstractXsltView;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
public class HomePage extends AbstractXsltView {
protected Source createXsltSource(Map model, String rootName, HttpServletRequest
request, HttpServletResponse response) throws Exception {
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element root = document.createElement(rootName);

List words = (List) model.get("wordList");
for (Iterator it = words.iterator(); it.hasNext();) {
String nextWord = (String) it.next();
Element wordNode = document.createElement("word");
Text textNode = document.createTextNode(nextWord);
wordNode.appendChild(textNode);
root.appendChild(wordNode);
}
return new DOMSource(root);
}
}
```
8.Create views.properties file under WEB-INF/classes/views.properties

home.(class)=com.siva.HomePage
home.stylesheetLocation=/WEB-INF/xsl/home.xslt
home.root=words

9.finally create one index.html or jsp to submit the form.

<body>
Word Id :<input type="text" value="" name="wordId">
Word NAME :<input type="text" value="" name="wordName">
<input type="submit" value="submit">

</form>

</body>

Above mentioned html/jsp is for to submit the form, if we required fileds need to be eneterd by the user then we have to get that values from command object, and then needs to process.

10. Now Everthing is completed. Once you configured and copied the above code into respective places.
We can exceuting the application and can able to see the results.
11. for this project required jars are..

commons-logging-1.1.1.jar
log4j-1.2.15.jar
spring-2.5.6.jar
spring-beans.jar
spring-webmvc.jar
jdom-1.0.jar

Wednesday, February 15, 2012

Axis Webservice implemenation with Eclipse

I get a chance to post small topic related to webservice implementation with eclipse galilio.
This is the easy way to implement webservice.
This example is bottom up approach- from Java - to - WSDL

Step1 : Start Eclipse
Step2 : Open Eclipse-> File -> New Dynamic web project
Step3 : Give project name as you like (ex: test_webservice_server)
Step4 : Select Dynamic web module version as 2.4
Step5 : Select target run time as Apache tomcat version and specify the loaction of
tomcat server installed location.(Ex: c:/tomcat)
Step6 : Click on Finish button.

Before creating webservice we need to write the interface and implementation classes to which methods we need to create webservice.

In this example i created one interface called ArthimeticService, having different methods for doing arthemetic operations.

Then created one Class- ArthimeticServiceImpl which implemnts ,ArthimeticService interface.

Now It's time to create webservice.

Step1 : Right Click on the Created project
Step2 : Select New - Other - Webservice

Step3 : Click On Next button.

Step4 : Select Service Implementation(Which we already created ex:
ArthimeticServiceImpl)
Step5 : Click on Next- Next -(If screen shows the start server button click on that)- Finish

Once we completed above steps our webservice is ready to use by clients.

We need to check whether our webservice is working or not

Open Created wsdl under Webcontent folder
Go to the <wsdlsoap:address location> copy the value and paste in browser address bar with wsdl like the below.

http://localhost:8888/test_webservice_server/services/ArthimeticServiceImpl?wsdl
Please check the port your server is running(most of the cases it will be 8080)
Once this ready then we can create the client from any system.

Now the time for creating webservice client to access the webservice server.

Step1 : create new - project - dynamic web project (ex: test_webservice_client)
Step2 : Select Dynamic web module version as 2.4.
Step3 : Rigth Click on the created project- New - webservice client.

Step4 : Click on Next button.
Step5 : Select the service definition
Step6 : Click on the Browse button

Step7 : Locate the webservice wsdl location
(where wsdl is running : ex http://localhost:8888
/test_webservice_server/services/ArthimeticServiceImpl?wsdl)
Step8: If webservice is running from another system

Step9: After Locating WSDL, click on OK- Finish

Step 10: Now we our webservice client also ready.
Step 11: Now we need test this application, wether it is working or not, for that i
am creating simple Test class which will call the Proxy class and
internally it will call the webservice server and will give the response
to us.

Ex:

package com.siva;

import java.rmi.RemoteException;

public class Test {

public static void main(String[] args) {
ArthimeticServiceImplProxy proxy = new ArthimeticServiceImplProxy();
int result = 0;
try {
} catch (RemoteException e) {
e.printStackTrace();
}
System.out.println("result is.." + result);
}
}
}

Now you will get result as -- 50

Like this webservices will work.

If you want to call this webservice from JSP and servletes
Then write jsp - with two input paramaters
servlet- will access the two values and return the result after
calling the webservice.
Once everything is completed try this url from your system

http://localhost:8888/test_webservice_client/
then provide input values and click on the Get result then result will display on the screen.

Now you can elaborate what ever ypou like and do modifications for your requirement.

Thursday, December 15, 2011

I got a time to post the important feature for web applications. User need to access statements through PDF. But that PDF needs to be Password protected.

To open the PDF, we can have different Password implemenations like (telephone no, DOB,Bank id, Last 4 digit of account no and name Etc...)

Now i am implementing password feature through the Random number Genaration.

import java.io.FileOutputStream;
import java.util.Random;

import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public static void main(String[] args){
try {

Document Document_For_Protection = new Document();
PdfWriter EncryptPDF= PdfWriter.getInstance(Document_For_Protection, new FileOutputStream(generatePin()+".pdf"));
PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
Document_For_Protection.open();
Document_For_Protection.close();
}
catch (Exception i)
{
System.out.println(i);
}
}

public static int generatePin() throws Exception {
Random generator = new Random();
generator.setSeed(System.currentTimeMillis());
int num = generator.nextInt(99999) + 99999;
System.out.println("Random number value---" + num);

if (num < 100000 || num > 999999) {
num = generator.nextInt(99999) + 99999;

if (num < 100000 || num > 999999) {

throw new Exception("Unable to generate PIN at this time..");
}
}
return num;
}

Required Jars's

itext-5.0.2.jar
bcprov-jdk15-136.jar

Wednesday, September 21, 2011

Create a Executable jar file from the command line or ANT build, Build Simple ANT File

Here i am posting how to make executable jar file from the command line as well through ANT tool, then how to run that executable jar file from the command prompt

This is the simple way to create executable jar file.

Step 1 : create folder inside your respective directory [Ex: c:/simple_java/src>]
Step 2 : create java file/files under this project[Package name is optional]
Ex: HelloWorld.java ,Person.java
under com.siva pacakage
Ex: c:/simple_java/src/com/siva>HelloWorld.java
Person.java
compile : c:/simple_java/src>javac com/siva/*.java

Step 3 : create one MANIFEST.MF or manifest.txt
under src folder.[this file can be created any place in your computer]

Ex: c:/>simple_java/src/manifest.txt

We need to mention the main class details.

Manifest-Version: 1.0
Main-Class: com.siva.HelloWorld

Step 4 : Now we need to create the executable jar file with the following command

c:/simple_java/src>jar cvfm simple_java.jar manifest.txt com/siva/*.class

Step 5: Run the executable jar file

c:/simple_java/src>java -jar simple_java.jar

Simple ANT build.xml- which used to compile , run and create the executable jar file which includes manifest file.

Before Starting with build.xml- We need to know What is ANT(Another Neat Tool]. Used for to build the application.

setting the ANT in our system.

Step 1: Down load the latest code from the ANT site .[Prefer to download the .ZIP file]

Setting the Path
Right Click on My Computer ->Properties ->Advanced-> Environment Variables-> System variables -> select Path -> Edit ->give semicolon(;) end of the path value-> paste the ant location upto bin

Ex:
C:\apache-ant-1.8.2-bin\apache-ant-1.8.2\bin

Step 4: Create java programs related to your purpose.
Step 5: create build.xml file, then write the code inside build.xml, for
compilation, creating executable jar file the application.

Step 6: Run the build.xml-> by using the ant command

EX:
c:/simple_javaproject>ant

Friday, September 16, 2011

Getting Started With DWR

This post contains the information about the DWR usage.
DWR- Direct Web Remoting is a java library that enables java on the server and JavaScript in a
browser to interact and call evch other as simply as possible.

DWR is Easy Ajax for Java.
DWR is a concept to implement Ajax based applications in java side.
I am posting the simple Dynamic Address Entry example. But you can modify this according to your requirement like fetching the data from data base, and assign that values to the respective inuput values. Here i am writing with simple hard coded data.

Follow below steps to learn DWR in minutes.
Step 1 : Start the eclipse- File -New- Dynamic web project- [provide project name] eg: dwr-project
Step 2: Open web.xml which is there in inside WEB-INF. Add the dwr related classes configuration.
Web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app id="dwr" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt;
<servlet>

<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
</servlet>
<servlet-mapping>

<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>

Step 3: create one foldercalled address inside WebContent
Step 4: create index.html inside address folder which we created in Step 3.
index.html
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
<script type='text/javascript' src='../dwr/engine.js'>
</script> <script type='text/javascript' src='../dwr/util.js'>
</script>
<script type="text/javascript" src='index.js'> </script>
<body>
<p>This is a practical example of making form fill easier for users, and howsimple this is with

DWR.</p>
<div id="tabContents">
<div id="demoDiv">
<p>We can enter the below Post codes in the post code text box, and check the address fill up in the respective text boxes. In the same way we can save the details in data base and call the respective function.</p>
<ul>

<li>600041</li>
<li>400708</li>
<li>400707</li>
<li>508517</li>
<li>516269</li>
</ul>
<table class="plain">

<tr>
<td>Postcode:</td>
<td>
</td>
</tr>
<tr>
<td>House name/number:</td>
<td><input id="house" type="text"/></td>
</tr> <tr>
<td>Line 2:</td> <td><input id="line2" type="text"/></td>
</tr> <tr>
<td>Line 3:</td> <td><input id="line3" type="text"/></td> </tr> <tr> <td>Line 4:</td> <td><input id="line4" type="text" size="30"/></td>
</tr> </table>
<p> </p> <p> </p>
</div>
</div>
</body></html>

In the index.html the bold lines are important. we no need to create AddressLookup.js, but we have to mention this. This should be same like a java class name.
Step 5: create index.js under address folder. this index.js is used to get the deatils from java and append the details in html page with out page refreshing.
index.js

var postcode = dwr.util.getValue("postcode");

});
}

Step 6: Create one AddressLookup java class under src folder ,
This class is having all the data. It might be database data or any type of data.
Here i am mentioning Hard coded data.

package com.siva;
import java.util.HashMap;
import java.util.Map;
import org.directwebremoting.util.LocalUtil;

{
private static final String LINE4 = "line4";
private static final String LINE3 = "line3";
private static final String LINE2 = "line2";
private static final String HOUSE_NO = "house";
/**
* @param origpostcode the code to lookup
* @return a map of postcode data
*/
{
String postcode = LocalUtil.replace(origpostcode, " ", "");
if (postcode.equalsIgnoreCase("600041"))
{
}
else if (postcode.equalsIgnoreCase("400708"))
{
}
else if (postcode.equalsIgnoreCase("400707"))
{
}
else if (postcode.equalsIgnoreCase("508517"))
{
}
else if (postcode.equalsIgnoreCase("516269"))
{
}
else
{
}
}
}

Step 7: create dwr.xml inside WEB-INF
dwr.xml
<?xml version="1.0" encoding="UTF-8"?>
<dwr>
<allow>
</create>
</allow>
</dwr>

Step 8 : Add below jars in the lib folder
bsf-2.3.jar
bsh-2.0b4.jar
commons-logging-1.0.4.jar
commons-validator-1.1.4.jar
dwr.jar
jakarta-oro-2.0.8.jar
log4j-1.2.12.jar

Step 9: Right click on the project and Run As Server- After successful run open the browser.
enter the post code which mentioned in the index.html.
Automatically the data has beed in filled in the remaining text boxes. in this way DWR work. you can explore more things on the DWR (http://directwebremoting.org/dwr/index.html)
Address bar has to be given in the following way
Step 10 : your DWR application started.

SOURCE CODE

Wednesday, September 14, 2011

Spring MVC MultiActionController - simple login application

Hello All,

Here i am posting simple Spring MVC example with IOC(setter , Constructor Injection).

Step1 : Open Eclipse - Create new Dynamic Web Project

Step2 : After creating Project, open web.xml which is there inside created project.

edit Web.xml and add the below code.

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>spring-features</display-name>

<servlet>
<servlet-name>springweb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>springweb</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

</web-app>

Step 3: As per above web.xml code- the servlet name as springweb so As per spring mvc we have to create xml file called springweb-servlet.xml under WEB-INF

Step 4: Inside springweb-servlet.xml , we have to put configuration details for the controller's, jsp pages., IOC information. as mentioned 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"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd;

<!-- no 'id' required, HandlerMapping beans are automatically detected by the DispatcherServlet -->

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="mappings">

<props>

</props>

</property>

</bean>

<property name="name" value="siva"/>

<property name="age" value="27"/>

<constructor-arg type="int" value="27"/>

<constructor-arg type="java.lang.String" value="raju"/>

</bean>

<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>

<property name="prefix" value="/WEB-INF/jsp/"/>

<property name="suffix" value=".jsp"/>

</bean>

</beans>

Step 5: Create LoginForm.java inside src folder- under any package

package com.siva.form;
import java.io.Serializable;
public class LoginForm implements Serializable {

private static final long serialVersionUID = 1L;
}
}
}
}
}

Step 6: Create LoginController which extends MultiActionController,

package com.siva.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class LoginController extends MultiActionController {

private String name;
private String age;

}

//This is constructor type setting in IOC - Check on springweb-servlet.xml for further reference.
//If you are still confusing the Study IOC basics.

public LoginController (String name1 , int age1)
{
System.out.println("Name------" + name1);
System.out.println("Age------" + age1);
}

/**
* This method has been called when ever the user enter login.htm under project.
* In this GetName, getAge methods are one way of IOC through set methods. We set the values
* in springweb-servlet.xml as a property and value. So when ever we called respective get methods
* we will get the results from the springweb-servlet.xml.
*/

ModelAndView mav = new ModelAndView();
System.out.println("Name for the set IOC " + getName());
System.out.println("Age for the set IOC " + getAge());
return mav;
}

/**
* It will check whether user name is 'siva' and password is 'raju'. if it's true then
* it will redirect to the success page. otherwise it will remain in the same page
*/

public ModelAndView success(HttpServletRequest request, HttpServletResponse response, LoginForm form){
ModelAndView mav = new ModelAndView();

// Injected name and age through IOC
System.out.println("Name for the set IOC " + getName());
System.out.println("Age for the set IOC " + getAge());
mav.setViewName("success");
}
else{
}
return mav;
}
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}
}

Step 7 : Create login.jsp, success.jsp . Place these 2 jsp pages under WEB-INF/jsp/

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<html>
<body>

action="success.htm" method="POST" >

</form:form>

</body>

</html>

success.jsp

success

commons-beanutils-1.7.0.jar
commons-digester-1.8.jar
commons-lang-2.4.jar

commons-logging-1.1.1.jar

commons-pool-1.3.jar

commons-validator-1.3.1.jar

j2ee.jar

jstl-1.1.0.jar

log4j-1.2.15.jar

spring-2.5.6.jar

spring-beans.jar

spring-context.jar

spring-modules-validation.jar

spring-security-core-2.0.4.jar

spring-web.jar

spring-webmvc.jar

standard.jar

all the above jar's can be used to run the application.

Step 9: Now right click on the project- Run As - Run on Server-

select the appropriate server, and run the application.

Step 10: These are the steps to run simple MVC MultiActionController example.

Get the source code from this link