Monday, December 10, 2012

Hello World for Webservices

I will try to put simple hello world webservice examples here for different implementations.

Before starting with this, we will have the below service common for all.

Interface
package com.helloworld;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style = Style.RPC)
public interface HelloWorld {
    @WebMethod String sayHello(String name);
}

Implementation
package com.helloworld;

import javax.jws.WebService;

@WebService(endpointInterface = "com.helloworld.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

Without Container example.
For deploying the service without container.
package com.helloworld;

import javax.xml.ws.Endpoint;

public class HelloWorldPublisher {

    public static void main(String[] args) {
        Endpoint.publish("http://localhost:8080/helloworld/hello", new HelloWorldImpl());
        System.out.println("hello world service published ....");
    }
}

Webservice client
package com.helloworld;

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class HelloWorldClient {

    public static void main(String[] args) throws MalformedURLException {
        URL url = new URL("http://localhost:8080/helloworld/hello?wsdl");
        QName qname = new QName("http://helloworld.com/", "HelloWorldImplService");
        Service service = Service.create(url, qname);
        HelloWorld hello = service.getPort(HelloWorld.class);
        System.out.println(hello.sayHello("abdullah"));
    }
}

Start the publisher, run the client.
Note we are just using Java 6 no other jars are required.


Thursday, July 5, 2012

querying/filtering in-memory data using JFilter

I was looking for a framework which allows to query/filter in memory data. Specific to my case, I was having a list of maps which I wanted to filter. I found JFilter which allows you to filter objects. It works on objects. Below I will be showing how to use JFilter if you have Map as object to filter on.

Before begining I would like to thank Kamran (author of JFilter) for the great framework.

JFilter works on object to make it work on Map you need to wrap your map. For that I have created a Store object with accessor methods for Map.

 import java.util.HashMap;  
 import java.util.Map;  
 public class Store {  
      private Map<String, Object> s = new HashMap<String, Object>();  
      public Map<String, Object> getMap() {  
           return s;  
      }  
      public void setMap(Map<String, Object> s) {  
           this.s = s;  
      }  
      public String toString() {  
           return s.toString();  
      }  
 }  

Now that we have our object ready to query/filter on. We will create a list of Store for our test purpose.

Preparing list of Store

 List<Store> list = new ArrayList<Store>();  
 Map<String, Object> map1 = new HashMap<String, Object>();  
 map1.put("name", "abdullah");  
 map1.put("balance", 700);  
 Store s1 = new Store();  
 s1.setMap(map1);  
 list.add(s1);  
 Map<String, Object> map2 = new HashMap<String, Object>();  
 map2.put("name", "david");  
 map2.put("balance", 850);  
 Store s2 = new Store();  
 s2.setMap(map2);  
 list.add(s2);  
 Map<String, Object> map3 = new HashMap<String, Object>();  
 map3.put("name", "jeffery");  
 map3.put("balance", 400);  
 Store s3 = new Store();  
 s3.setMap(map3);  
 list.add(s3);  

Creating JFilter object and passing it the list of Store to work on.
 JFilter<Store> filter = new JFilter<Store>(list, Store.class);  

Preparing parameters to be passed to JFilter for querying/filtering objects.
 Map<String, Object> parameters = new HashMap<String, Object>();  
 parameters.put("keyName", "name");  
 parameters.put("value", "abdullah");  

To hold the result of querying/filtering we need to pass list, so every time we filter we will pass a list to hold the result.

Equal to
 ArrayList<Store> res1 = new ArrayList<Store>();  
 filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$eq':'?value'}}}", parameters).out(res1);  
 System.out.println("= result -> " + res1);  

To hold the filtering result we have res1. We will be callign filter method of JFilter and pass it query, which is in json format and the parameters (you can also use var-args to pass parameters.)

We will be doing the same thing for all the operators below, so I won't be repeating all these details.

Not equals to

 ArrayList<Store> res2 = new ArrayList<Store>();  
 filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$ne':'?value'}}}", parameters).out(res2);  
 System.out.println("!= result -> " + res2);  

Preparing parameters for like operations
 parameters.put("keyName", "name");  
 parameters.put("value", "ab");  

Starts with
 ArrayList<Store> res3 = new ArrayList<Store>();  
 filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$sw':'?value'}}}", parameters).out(res3);  
 System.out.println("starts with result -> " + res3);  

Ends with
 ArrayList<Store> res4 = new ArrayList<Store>();  
 parameters.put("value", "ry");  
 filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$ew':'?value'}}}", parameters).out(res4);  
 System.out.println("ends with result -> " + res4);  

Contains
 ArrayList<Store> res5 = new ArrayList<Store>();  
 parameters.put("value", "vi");  
 filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$cts':'?value'}}}", parameters).out(res5);  
 System.out.println("contains result -> " + res5);  

Preparing parameters for in / not in operation
 parameters.put("keyName", "balance");  
 List<Integer> balance = new ArrayList<Integer>();  
 balance.add(300);  
 balance.add(500);  
 balance.add(700);  
 parameters.put("value", balance);  

In
 ArrayList<Store> res6 = new ArrayList<Store>();  
 filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$in':'?value'}}}", parameters).out(res6);  
 System.out.println("in result -> " + res6);  

Not In
 ArrayList<Store> res7 = new ArrayList<Store>();  
 filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$nin':'?value'}}}", parameters).out(res7);  
 System.out.println("not in result -> " + res7);  

Preparing parameters for relational operators
 parameters.put("keyName", "balance");  
 parameters.put("value", 700);  

Less than
 ArrayList<Store> res8 = new ArrayList<Store>();  
 filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$lt':'?value'}}}", parameters).out(res8);  
 System.out.println("< result -> " + res8);  

Less than or equal to
 ArrayList<Store> res9 = new ArrayList<Store>();  
 filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$le':'?value'}}}", parameters).out(res9);  
 System.out.println("<= result -> " + res9);  

Greater than
 ArrayList<Store> res10 = new ArrayList<Store>();  
 filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$gt':'?value'}}}", parameters).out(res10);  
 System.out.println("> result -> " + res10);  

Greater than or equal to
 ArrayList<Store> res11 = new ArrayList<Store>();  
 filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$ge':'?value'}}}", parameters).out(res11);  
 System.out.println(">= result -> " + res11);  

Here is the working code for all operators
 import gk.jfilter.JFilter;  
   
 import java.util.ArrayList;  
 import java.util.HashMap;  
 import java.util.List;  
 import java.util.Map;  
   
 public class Filtering {  
   
      public static void main(String[] args) {  
   
           List<Store> list = new ArrayList<Store>();  
   
           Map<String, Object> map1 = new HashMap<String, Object>();  
           map1.put("name", "abdullah");  
           map1.put("balance", 700);  
   
           Store s1 = new Store();  
           s1.setMap(map1);  
           list.add(s1);  
   
           Map<String, Object> map2 = new HashMap<String, Object>();  
           map2.put("name", "david");  
           map2.put("balance", 850);  
             
           Store s2 = new Store();  
           s2.setMap(map2);  
           list.add(s2);  
   
           Map<String, Object> map3 = new HashMap<String, Object>();  
           map3.put("name", "jeffery");  
           map3.put("balance", 400);  
   
           Store s3 = new Store();  
           s3.setMap(map3);  
           list.add(s3);  
   
           JFilter<Store> filter = new JFilter<Store>(list, Store.class);  
             
           //parameters to be passed for filtering  
           Map<String, Object> parameters = new HashMap<String, Object>();  
           parameters.put("keyName", "name");  
           parameters.put("value", "abdullah");  
             
           //to hold the result after filtering we will be creating list which will be passed to out method.  
   
           //for equal  
           ArrayList<Store> res1 = new ArrayList<Store>();  
           filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$eq':'?value'}}}", parameters).out(res1);  
           System.out.println("= result -> " + res1);  
   
           //for not equal  
           ArrayList<Store> res2 = new ArrayList<Store>();  
           filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$ne':'?value'}}}", parameters).out(res2);  
           System.out.println("!= result -> " + res2);  
   
           //for like operations  
           parameters.put("keyName", "name");  
           parameters.put("value", "ab");  
             
           //starts with  
           ArrayList<Store> res3 = new ArrayList<Store>();  
           filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$sw':'?value'}}}", parameters).out(res3);  
           System.out.println("starts with result -> " + res3);  
             
           //ends with  
           ArrayList<Store> res4 = new ArrayList<Store>();  
           parameters.put("value", "ry");  
           filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$ew':'?value'}}}", parameters).out(res4);  
           System.out.println("ends with result -> " + res4);  
   
           //contains  
           ArrayList<Store> res5 = new ArrayList<Store>();  
           parameters.put("value", "vi");  
           filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$cts':'?value'}}}", parameters).out(res5);  
           System.out.println("contains result -> " + res5);  
   
           //parameters for in / not in operations  
           parameters.put("keyName", "balance");  
             
           List<Integer> balance = new ArrayList<Integer>();  
           balance.add(300);  
           balance.add(500);  
           balance.add(700);  
           parameters.put("value", balance);  
   
           //for in  
           ArrayList<Store> res6 = new ArrayList<Store>();  
           filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$in':'?value'}}}", parameters).out(res6);  
           System.out.println("in result -> " + res6);  
       
           //for not in  
           ArrayList<Store> res7 = new ArrayList<Store>();  
           filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$nin':'?value'}}}", parameters).out(res7);  
           System.out.println("not in result -> " + res7);  
   
           //preparing parameters for relational operators   
           parameters.put("keyName", "balance");  
           parameters.put("value", 700);  
             
           //for <  
           ArrayList<Store> res8 = new ArrayList<Store>();  
           filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$lt':'?value'}}}", parameters).out(res8);  
           System.out.println("< result -> " + res8);  
             
           //for <=  
           ArrayList<Store> res9 = new ArrayList<Store>();  
           filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$le':'?value'}}}", parameters).out(res9);  
           System.out.println("<= result -> " + res9);  
             
           //for >  
           ArrayList<Store> res10 = new ArrayList<Store>();  
           filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$gt':'?value'}}}", parameters).out(res10);  
           System.out.println("> result -> " + res10);  
   
           //for >=  
           ArrayList<Store> res11 = new ArrayList<Store>();  
           filter.filter("{'map':{'key':{'$eq':'?keyName'}, 'value':{'$ge':'?value'}}}", parameters).out(res11);  
           System.out.println(">= result -> " + res11);  
   
      }  
 }  
   

Monday, June 25, 2012

Logging to different log files based on condition using Log4j

There are times when you need to log the entire flow to some specific file or maybe based on some condition you need to log something specific in a specific log file.

You can achieve this using NDC or MDC. I will be using MDC

Let's get started.

Step 1: You need to write a Custom appender in which you will put the condition logic.

 public class MyRollingFileAppender extends RollingFileAppender {  
  @Override  
  public void append(LoggingEvent event) {  
  String condition = (String)event.getMDC("keyForConditionVariable");  
  fileName = fileName.substring(0, fileName.lastIndexOf('/'));  
  if(condition == null) {  
   fileName += "/default.log";  
  } else if("condition1".equals(condition)) {  
   fileName += "/condition_1.log";  
  } else if("condition2".equals(condition)) {  
   fileName += "/condition_2.log";  
  }  
  try {  
   setFile(fileName, fileAppend, bufferedIO, bufferSize);  
  } catch (IOException ioe) {  
             System.err.println("Exception while setting service log file ")  
  }  
  super.append(event);  
  }  
 }  

The fileName is the name of the log file. What we are doing is getting a value from MDC, I will explain how to put that in MDC later, which we are using for the condition, and decides on the log file based on that.

Step 2: Configuring our custom appender. Sample configuration
 <appender class="com.my.MyRollingFileAppender" name="rollingfile">  
     <param name="maxFileSize" value="10MB" />  
     <param name="maxBackupIndex" value="5" />  
     <param name="File" value="C:/mdc/default.log" />  
     <layout class="org.apache.log4j.PatternLayout">  
       <param name="ConversionPattern" value="%d %p [%c] - %m%n %X{keyForConditionVariable}" />  
     </layout>  
 </appender>  
If you want to log the value from the MDC you need to use %X option as configured above.

Step 3: The last piece. Putting the value in MDC. You need to put the value at the very start of the flow and also need to remove it at the end of the flow.
To put the value in the MDC
MDC.put("keyForConditionVariable", "condition1")

To remove the value from MDC
MDC.remove("keyForConditionVariable")
So here we are having "keyForConditionVariable" key the value of which we are using for the condition check in our custom appender. That's it to use MDC in log4j