Archive

Archive for July, 2012

Free Website Infrastructure monitoring service

July 10, 2012 Comments off

Dear friends,

Let me take the privilege of introducing my Cloud based Website Monitoring Service MyMonitor.in.

With a simple sign-up u will be able to monitor your URL’s,Machine Ping and the Port

you can keep monitor your URLs,machines and device with ping and services like ssh,tomcat,jetty,apache,e.t.c with port monitoring.

This tool will send u mails when ever there is a problem with your infrastructure.

You can check the reports and in graph format by login to the website.

here you can access any number of URL’s any number of machines ping for free..

give a try and let me know your feed back.

URL : http://www.mymonitor.in

Advertisements
Categories: General

How to create SOAP web service client in java with cxf, Spring, Maven

July 9, 2012 Comments off

In continuation with my previous post i am showing a way how to invoke a SOAP service from java.

There are different ways to create a SOAP Clinet in java with cxf.Here im giving the most fastest and easiest way to create.

Get the wsdl URL form the server.

open up the pom.xml file.

Add the below wsdl-to-java plugin.This plugin will generate the java code by parsin the wsdl which is given as input to it.

<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>generated/cxf</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>http://localhost:7009/cxf-spring-test/calculatorService?wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>

now open the commanprompt or terminal,navigate to the project

type mvn clean generate-sources

this will generate the wsdl correspondin code in the <Project Folder>/generated/cxf location.

Add this to the project build path.

now i am writing a simple class and wired to the bean in the application context

Application Context File:

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

<!-- Start up bean which will invoke server webservice -->
<bean id="calculatorService" init-method="invoke">
<constructor-arg index="0" value="http://localhost:7009/cxf-spring-test/calculatorService?wsdl"/>
</bean>

</beans>

Java Client Class(As per the above example this will get invoked automatically during start up)

package com.test;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class CalculatorServiceClient
{
private String targetURL;

public CalculatorServiceClient(String targetURL)
{
this.targetURL = targetURL;
}

public void invoke()
{
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(CalculatorIntf.class);

factory.setAddress(this.targetURL);
CalculatorIntf calculatorIntf = (CalculatorIntf) factory.create();

//invoking add web servcie
System.out.println("10 + 15 = "+calculatorIntf.add(10, 15));

//invoking subtract web service
System.out.println("15 - 5 = "+calculatorIntf.subtract(15, 5));

//invoking multiply web service
System.out.println("10 * 5 = "+calculatorIntf.multiply(10,5));
}
}

In the next post i will show how to add SSL Support to the above client code.

Source Code Download : cxf_spring_maven.zip

Categories: Web Services

How to create SOAP WebService with spring, cxf, Maven, Java

July 7, 2012 2 comments

Steps to create MAVEN SOAP Based Web Service with Eclipse.

Pre-Requsites :

  1. Java >= 1.5 version
  2. Maven installation
  3. Eclipse Maven Plugin

Steps to create :

Create a new Maven Project with packaging type as war

open the pom file and maven and spring dependencies

pom.xml file


4.0.0
com.test
cxf-spring-test
0.1
war

UTF-8
3.0.5.RELEASE
1.6.4
2.4.1

maven2-repository.java.net
Java.net Repository for Maven
http://download.java.net/maven/2/
default

${project.artifactId}

maven-compiler-plugin
2.3.2

1.6
1.6

org.mortbay.jetty
jetty-maven-plugin
7.4.2.v20110526

10

7009

/${project.artifactId}

<!-- CXF Dependencies -->

jta
jta
1.0.1b

org.apache.cxf
cxf-rt-core
${cxf.version}

org.apache.cxf
cxf-rt-frontend-simple
${cxf.version}

org.apache.cxf
cxf-rt-frontend-jaxws
${cxf.version}

org.apache.cxf
cxf-rt-databinding-aegis
${cxf.version}

org.apache.cxf
cxf-rt-transports-local
${cxf.version}

org.apache.cxf
cxf-rt-transports-http
${cxf.version}

org.apache.cxf
cxf-rt-transports-http-jetty
${cxf.version}

org.apache.cxf
cxf-rt-transports-jms
${cxf.version}

org.apache.cxf
cxf-rt-management
${cxf.version}

org.apache.cxf
cxf-common-utilities
${cxf.version}

<!-- End of CXF Dependencies -->

org.eclipse.jetty
jetty-servlets
7.4.0.v20110414
provided

org.eclipse.jetty
jetty-client

<!-- Spring Dependencies ${spring.version} -->

org.springframework
spring-beans
${spring.version}

org.springframework
spring-jdbc
${spring.version}

org.springframework
spring-web
${spring.version}

org.springframework
spring-webmvc
${spring.version}

org.springframework
spring-orm
${spring.version}

org.springframework
spring-expression
${spring.version}

org.apache.geronimo.specs
geronimo-annotation_1.0_spec
1.1.1

org.slf4j
slf4j-log4j12
${slf4j.version}
jar
compile

Create a web.xml file in src/main/webapp/WEB-INF folder

In the web.xml mention the servlet mapping for cxf servlet and add spring listner to load the application context during the webapp startup time.

Web.Xml file

<!--?xml version="1.0" encoding="UTF-8"?-->

<!-- Servlet Mapping for CXFServlet -->

CXFServlet
org.apache.cxf.transport.servlet.CXFServlet

CXFServlet
/*

<!-- Context parameters -->

log4jConfigLocation
classpath:sitemonitor_log4j.properties

log4jExposeWebAppRoot
false

		org.springframework.web.util.Log4jConfigListener

contextConfigLocation
classpath:sitemonitor_appContext.xml

		org.springframework.web.context.ContextLoaderListener

Now create a package in the src/main/java folder as mentioned in the pom file

create a interface with the service you want to expose

package com.test;

import javax.jws.WebService;

@WebService
public interface CalculatorIntf
{
public long add(long num1,long num2);

public long subtract(long num1,long num2);

public long multiply(long num1,long num2);
}

Create a inplementation for this service and implement all the methods of the interface

package com.test;

public class CalculatorImpl implements CalculatorIntf
{
public long add(long num1, long num2)
{
return (num1 + num2);
}

public long subtract(long num1, long num2)
{
return (num1 - num2);
}

public long multiply(long num1, long num2)
{
return (num1 * num2);
}

}

create a spring application context file and add the file to the application class path.

Application Context file

<!--?xml   version="1.0" encoding="UTF-8"?-->

<!-- Initiliaing Client Webservices -->

<!-- JAX-WS Service Endpoint -->

now open the command prompt and navigate to the porject folder in the command prompt

run the maven command : mvn clean package jetty:run -DskipTests (for running the inbuild jetty we need to add this in the pom file.In the above pom has that jetty plugin)

now open the browser and hit http://localhost:7009/cxf-spring-test

now you should see the wsdl link.click on the wsdl link will show you the wsdl file.

you can test the service with soap-ui.

In the comming post i will post how to invoke this wsdl from the java program.

Categories: Web Services

Find median of 2 sorted arrays

July 7, 2012 Comments off

Let ar1 and ar2 be the input arrays.
Algorithm:
1) Calculate the medians m1 and m2 of the input arrays ar1[]
and ar2[] respectively.
2) If m1 and m2 both are equal then we are done.
return m1 (or m2)
3) If m1 is greater than m2, then median is present in one
of the below two subarrays.
a)  From first element of ar1 to m1 (ar1[0…|_n/2_|])
b)  From m2 to last element of ar2  (ar2[|_n/2_|…n-1])
4) If m2 is greater than m1, then median is present in one
of the below two subarrays.
a)  From m1 to last element of ar1  (ar1[|_n/2_|…n-1])
b)  From first element of ar2 to m2 (ar2[0…|_n/2_|])
4) Repeat the above process until size of both the subarrays
becomes 2.
5) If size of the two arrays is 2 then use below formula to get
the median.
Median = (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1]))/2
Example:
ar1[] = {1, 12, 15, 26, 38}
ar2[] = {2, 13, 17, 30, 45}
For above two arrays m1 = 15 and m2 = 17
For the above ar1[] and ar2[], m1 is smaller than m2. So median is present in one of the following two subarrays.
[15, 26, 38] and [2, 13, 17]
Let us repeat the process for above two subarrays:
m1 = 26 m2 = 13.
m1 is greater than m2. So the subarrays become
[15, 26] and [13, 17]
Now size is 2, so median = (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1]))/2
= (max(15, 13) + min(26, 17))/2
= (15 + 17)/2
= 16

Program:

#include<stdio.h>
int max(int, int); /* to get maximum of two integers */

int min(int, int); /* to get minimum of two integeres */

int median(int [], int); /* to get median of a single array */

/* This function returns median of ar1[] and ar2[].

Assumptions in this function:

Both ar1[] and ar2[] are sorted arrays

Both have n elements */

int getMedian(int ar1[], int ar2[], int n)
{
int m1; /* For median of ar1 */
int m2; /* For median of ar2 */

/* return -1  for invalid input */
if(n <= 0)
return -1;

if(n == 1)
return (ar1[0] + ar2[0])/2;

if (n == 2)
return (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1])) / 2;

m1 = median(ar1, n); /* get the median of the first array */
m2 = median(ar2, n); /* get the median of the second array */

/* If medians are equal then return either m1 or m2 */
if(m1 == m2)
return m1;

/* if m1 < m2 then median must exist in ar1[m1....] and ar2[....m2] */
if (m1 < m2)
return getMedian(ar1 + n/2, ar2, n - n/2);
else if(m1>m2)
/* if m1 > m2 then median must exist in ar1[....m1] and ar2[m2...] */
return getMedian(ar2 + n/2, ar1, n - n/2);
}

/* Driver program to test above function */
int main()
{
int ar1[] = {1, 12, 15, 26, 38};
int ar2[] = {2, 13, 17, 30, 45};

printf("%d", getMedian(ar1, ar2, 5)) ;

getchar();
return 0;
}

/* Utility functions */
int max(int x, int y)
{
return x > y? x : y;
}

int min(int x, int y)
{
return x > y? y : x;
}

/* Function to get median of a single array */
int median(int arr[], int n)
{
if(n%2 == 0)
return (arr[n/2] + arr[n/2-1])/2;
else
return arr[n/2];
}

Time Complexity: O(logn)
Space Complexity: O(1)
Algorithmic Paradigm: Divide and Conquer

Categories: Algorithms

Given n red balls and m blue balls and some containers, how would you distribute those balls among the containers such that the probability of picking a red ball is maximized, assuming that the user randomly chooses a container and then randomly picks a ball from that

July 7, 2012 Comments off

Given n red balls and m blue balls and some containers, how would you distribute those balls among the containers such that the probability of picking a red ball is maximized, assuming that the user randomly chooses a container and then randomly picks a ball from that

Solutiuon :

Let’s say x containers.

Put 1 red ball in x-1 containers and rest with m blue balls.

Probability of selecting red ball in this case is: 1-(1/x)*(n-x-1)/m.

Categories: Algorithms

kth smallest element without using any static/global variable in Binary Search Tree

July 7, 2012 Comments off
public BSTNodeSize findKth (int k, BSTNodeSize t) {
if (t == null)
throw new IllegalArgumentException();
int lsize= 0; // size of left subtree
if (t.left != null)
lsize= ((BSTNodeSize)(t.left)).size;
if (k = lsize + 1)
return t;
if (k < = lsize)
return findKth(k, (BSTNodeSize)t.left);
return findKth(k – lsize – 1, (BSTNodeSize)t.right);
}
Categories: Algorithms

You are given an array of n+2 elements. All elements of the array occur once between 1 and n except two numbers which occur twice. Find the two repeating numbers in O(n) (eg) say for n = 5

July 7, 2012 Comments off

Question:

You are given an array of n+2 elements. All elements of the array occur once between 1 and n except two numbers which occur twice. Find the two repeating numbers in O(n)

(eg) say for n = 5

array = 4 2 4 5 2 3 1

The above array has n+2=7 elements with all elements occurring once except 2 and 4 which occurs twice. Find those repeating numbers in O(n) time and constant space.

The output should be 4 2.

Solution: Let summation of all numbers in array be S and product be P

Let the numbers which are being repeated are X and Y.

X + Y = S – n(n+1)/2
XY = P/n!

Using above two equations, we can find out X and Y. For array = 4 2 4 5 2 3 1, we get S = 21 and P as 960.

X + Y = 21 – 15 = 6

XY = 960/5! = 8

X – Y = sqrt((X+Y)^2 – 4*XY) = sqrt(4) = 2

Using below two equations, we easily get X = 4 and Y = 2
X + Y = 6
X – Y = 2

Categories: Algorithms
%d bloggers like this: