How to mask a String while showing part of the String (last 4 digits)

Apr 26, 2010 Posted by Roshan Priyadarshana 0 comments
Following code sample shows how to mask a string while showing last 4 digits. Normally you are not allwoed to save the entire account number or credit card number in your database. Better option is to mask the number and saving.

public class Mask {

    public static void main(String[] args) {
        String accno = "4444333322221221";
        StringBuffer sb = new StringBuffer();
        if (accno != null && !accno.isEmpty()) {
            Integer length = accno.length();
            Integer mask = length;
            if (length > 4) {
                mask = length - 4;
                for (int i = 0; i < mask; i++) {
                    sb.append("x");
                }
                sb.append(accno.substring(mask));
               
            }
            System.out.println("Masked String : " + sb);
        }

    }

}
result wil be shown like this:

Masked String : xxxxxxxxxxxx1221


Here a link that describes why account number should be masked.
http://www.pinoytechblog.com/archives/does-your-bank-mask-your-account-number
Labels:

Single-ended association proxies issues

Dec 24, 2009 Posted by Roshan Priyadarshana 0 comments
you may often face the following exception in Hibernate.
java.lang.ClassCastException:
com.roshan.services.payment.gateway.domain.GatewayProfile_$$_javassist_11

This happens when you try to do something like this:
public void doSomething(IPaymentModeProfile paymentModeProfile){
//something goes here
switch (id) {
  // ACH(101), CPA(102), CREDIT(103), DIRECT_DEBIT(104), MAESTRO(105),
  // PINLESS_DEBIT(106)
  case 101: {
   //something goes here
  }
   break;
  case 102: {
   //something goes here
  }
   break;
  case 103: {
   CreditCard creditCard = (CreditCard) paymentModeProfile;
   //do something with creditCard

  }
   break;
  case 104: {
   //something goes here

  }
   break;
  case 105: {
   //something goes here
  }
   break;
  case 106: {
   //something goes here
  }
   break;

  }

}

Using interface proxies together with polymorphism can lead to sporadic ClassCastExceptions. This problem occurs whenever two mapped subclasses have an identical property and the instances are loaded polymorphically through the base class.

This was due to the fact that a lazily loaded List of objects retrieved by hibernate contained proxied objects. The entities that were proxied were using a single table inheritance strategy, so there is a base class that is subclassed several times. The entities once loaded were being sorted into separate lists that are typed according to the subclasses, they were being cast into the appropriate subclass but this cast was failing because instead of being an instance of say GatewayProfile it was an instance of GatewayProfile_$$_javassist_11 i.e.

Assume CreditCard implements the IPaymentModeProfile interface. The gist of the problem is that Hibernate is trying to optimize and avoid going to the database by returning a proxy object. However, a proxy object cannot be downcast to the subclass object, the exception occurs because of downcasting. There are lot of solution to avoid this problem.

The first solution to this issue is use their own interfaces instead of your entity and its subclass. In this case cglib creates a proxy class that implements all the interfaces specified in the proxy attribute (in both class and joined sub-class tags). Everywhere in your code, you should access the entity through the interfaces
and not the implementation classes.

Ex: create a Interface Called ICreditCard instead of CreditCard class.

Follow this link for more information. http://sysin.wordpress.com/2009/02/27/hibernate-inheritance-classcastexception-part-1/


The second solution is that use visitor pattern.

Follow this link for more information. http://andykayley.blogspot.com/2009/05/how-to-avoid-classcastexceptions-when.html

The third solution is extract the real object from hibernate proxy. This is very simple and not taking too much time to implement. This is the only solution that you don't need to write extra classes.
if (paymentModeProfile instanceof HibernateProxy) {
// convert the hibernate proxy to an object.
    Object object = paymentModeProfile;
 // get the real object
    CreditCard creditCard = (CreditCard) ((HibernateProxy) object).getHibernateLazyInitializer().getImplementation();
 
 // do anything with creditCard
 }

cheers..!!!
Labels:

Google Chrome Extensions: Blog This! (by Google)

Dec 9, 2009 Posted by Roshan Priyadarshana 0 comments
Labels:

Easymock Tutorial

Sep 24, 2009 Posted by Roshan Priyadarshana 1 comments
Check out this SlideShare Presentation:
Labels:

Spring Security 3.0.0.M2 Released

Sep 22, 2009 Posted by Roshan Priyadarshana 0 comments
Luke Taylor , A enior Member of Acegi Security System TeamSpring Team announced that they have reached the second milestone release for Spring Security 3.0.0. It contains some notable changes to the configuration namespace which spring users should be aware of.

Full details can be found in the changelog as usual, but the most notable change is that the use of the "decorator" style namespace elements has been discontinued.

To summarize the namespace changes:

  • Namespace elements which create an AuthenticationProvider instance must be declared as children of the <authentication-manager>.
  • The use of <custom-authentication-provider> has been dropped. Use <authentication-provider ref='yourProviderBeanName'> as a child of the <authentication-manager> element.
  • <custom-filter> should no longer be used to decorate filter beans, but used with a ref="yourFilterBean" attribute as a child of the <http> block.
  • <custom-after-invocation-provider> declarations should be replaced by <after-invocation-provider ref='yourProviderBeanName'> within the <global-method-security> element.
  • The session-controller-ref has been moved to the concurrent-session-control element (SEC-1197).
    All namespace configurations must now include the element in order to instantiate the AuthenticationManager (it is no longer created internally). These changes are also described in the updated manual and are used in the sample applications.

    As with Spring 3.0, this is the second release which requires a minimum JDK 1.5 to run and also require Spring 3.0, so you should get hold of the Spring 3.0.0.M3 release if you aren't already using it.


    Reference : SpringSource
    Labels:

    Love Aaj Kal [2009] / New Source PDVD Rip / XViD / 700 MB

    Sep 21, 2009 Posted by Roshan Priyadarshana 0 comments



                     
    Complete name : Love aaj kal 2009.avi
    Format : AVI
    Format/Info : Audio Video Interleave
    Format/Family : RIFF
    File size : 697 MiB
    PlayTime : 2h 11mn
    Bit rate : 737 Kbps
    Video #0
    Codec : XviD
    Codec/Family : MPEG-4
    Codec/Info : XviD project
    PlayTime : 2h 11mn
    Bit rate : 636 Kbps
    Width : 688 pixels
    Height : 256 pixels
    Audio #1
    Codec : MPEG-1 Audio layer 3
    Codec profile : Joint stereo
    PlayTime : 2h 11mn
    Bit rate : 94 Kbps
    Bit rate mode : CBR
    Channel(s) : 2 channels






    Labels: ,

    Log In with Your Facebook Username

    Sep 17, 2009 Posted by Roshan Priyadarshana 0 comments
    A couple of months ago, Facebook has introduced a own user name for users. That helps to find and connect with each other very easily. Beginning today, they have introduced another new feature using Facebook user name. With this new feature users will be able to log in to their Facebook accounts using
    Facebook user name a and password from any browser,mobile phone or Facebook Connect-enabled website.

    For example, user can enter username, "roshansn" in place of my email address when he log in to Facebook.



    This doesnt mean that logging with email address is dissabled. You will still be able to log in with your email.This is a very convenient way to access your Facebook account compare with logging with email address. A username gives you an easy-to-remember web address for your Facebook profile so your friends can more easily find and connect with you. If you don't have a Facebook username yet, you can choose one here.



    Labels: