Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Monday, April 27, 2015

Generic enum converter

  public static Enum<?> toEnum(final Class<? extends Enum> classType, final Enum<?> enumObj) {  
     if (enumObj == null) {  
       return null;  
     } else {  
       return enumObj.valueOf(classType, enumObj + "");  
     }  
   }  

Usage:

  Test1 returnedEnum = (Test1) toEnum(Test1.class, Test2.HELLO_ENUM);  

Tuesday, February 3, 2015

Documenting Spring annotated rest interface via reflection  

1:   for (Method method : YourClass.class.getMethods()) {  
2:        // url  
3:        if (method.isAnnotationPresent(RequestMapping.class)) {  
4:          RequestMapping parms = method.getAnnotation(RequestMapping.class);  
5:          String[] val = parms.value();  
6:          RequestMethod[] methodType = parms.method();  
7:          for (int i = 0; i < val.length; i++) {  
8:            System.out.println(val[i] + "     METHOD TYPE:" + methodType[i]);  
9:          }  
10:          //parameter  
11:          Class<?>[] paramsTypes = method.getParameterTypes();  
12:          Annotation[][] annot = method.getParameterAnnotations();  
13:          for (int i = 0; i < annot.length; i++) {  
14:            if (annot[i].length > 0 && annot[i][0] instanceof RequestParam) {  
15:              RequestParam new_name = (RequestParam) annot[i][0];  
16:              System.out.println("Request: " + new_name.value() + " = " + paramsTypes[i]);  
17:            }  
18:          }  
19:          //return type  
20:          if (method.getReturnType().getCanonicalName().equals(List.class.getCanonicalName())) {  
21:            Type listType = method.getGenericReturnType();  
22:            if (listType instanceof ParameterizedType) {  
23:              Type elementType = ((ParameterizedType) listType).getActualTypeArguments()[0];  
24:              System.out.println("Response: [" + elementType + "]");  
25:            }  
26:          } else {  
27:            System.out.println("Response: " + method.getReturnType().getCanonicalName());  
28:          }  
29:        }  

Thursday, March 27, 2014

java.lang.IllegalArgumentException: badly formated directory string

it is based on invalid format, try to give correct name structure for instance:

new X500Name("C=DE,O=Organiztion");

Sunday, February 23, 2014

Design patterns

Design pattern are solutions to a class of problem which is proven to be good. It's like following the big guys. The attached project contains easy to learn example for Factory, observer listener and strategy design patterns. This project will be updated with further patterns. Each if the downloadable  project is an eclipse project , you can simply import it in eclipse  if you wish.
Download all Project
Description of project via sample problems:

PROBLEM 1:
 Say you want to create people in a computer game. People can be of type enemy army, friendly army or civilians (Also further more addition in future should be also possible).  In this kind of problem you can  use Factory pattern to create these game people.

PROBLEM 2:
Say in same game scenario you want to create Aeroplanes with different capability. Say 5 aeroplanes with military capability and 1 civilian  (In future you should be also able to add more capabilities like speeds, payload types). In this kind of problem you can use Strategy pattern. You can create different capabilities and inject them in aeroplane.

PROBLEM 3:
Now say you have a game window. There are different buttons. You want to do different action in your Window class based to the location of the button clicked (Say button is developed by some other developer in his own way). In this problem you can use observer listener pattern.Window can be listener of button click event. Developer of the button should define a interface to add listeners.

Tuesday, March 27, 2012

RSA demo / example

This is the implementation of RSA tutorial given at http://www.javamex.com/tutorials/cryptography/rsa_encryption.shtml with little bit of additions so you can use it right out of the box.

CLASS:  RSAKeyGenerator
Method: void generateKeyPair("<public key file name>","<private key file name>")

CLASS: RSAEncDec
Method: String encrypt("your plane string message", "<location of public key file>")
              [Returns the base64 encoded string that you can send easily]

Method: String decrtpt("<above base64 encoded string>", "<location of private key file>")
              [Returns original plane string message]

For details see "readme.txt" and main() methods avaliable in each class
Download project at : https://github.com/meamit/rsa-demo