You must be logged in to do that

LocalStorage serialization question

  4 posts   Feedicon  
Replies: 3 - Last Post: March 09, 2011 18:41
by: etf
showing 1 - 4 of 4
 
Posted: March 05, 2011 14:29 by KickMeToAndy
Hi,
i was wondering how the LocalStorage serialization does handle transient fields. In my application i have a preference dialog where you can enter a password, which should not be stored in the configuration file. that is why i marked the field in the Preferences bean as transient. However, when using the LocalStorage to save my preferences the password is serialized even though it is transient.

Is this a bug or do i have to reset all transient fields myself before saving the configuration?

thanks in advance,
cheers,
Andy
 
Posted: March 07, 2011 21:32 by etf
Hello Andy,
Thanks for you interest in the project.
The situation you have described in your message is not a bug of the BSAF. The key word "transient" works for java serialization. BSAF doesn't use this facility. XMLEncoder/XMLDecoder are used instead. This technology support transient properties also but in slightly different way. To make a long story short you need to use BeanInfo. Here is a brief example:
BeanInfo info = Introspector.getBeanInfo(JTextField.class);
PropertyDescriptor[] propertyDescriptors =
                             info.getPropertyDescriptors();
for (int i = 0; i < propertyDescriptors.length; ++i) {
    PropertyDescriptor pd = propertyDescriptors[i];
    if (pd.getName().equals("text")) {
        pd.setValue("transient", Boolean.TRUE);
    }
}

To make a transient property in a custom bean you have to create a ...BeanInfo class with following string in it:
properties[PROPERTY_PASSWORD].setValue("transient", Boolean.TRUE);

where PROPERTY_PASSWORD is index of a transient property.

I hope this helps.
 
Posted: March 09, 2011 17:42 by KickMeToAndy
Thanks for your reply,
i finally fixed the problem with a custom Annotation and code derived from your posting. Here is
the code if somebody is interested in it. It'd be nice if something like that could be added to the
framework, since the bsaf intends to reduce the amount of code you have to write for you application.
quite a lot of code to reset the password attribute before saving the bean in the local storage Wink

public class BeanInfoUtil {
        public static void markTransientAttributes(Class<?>... classes) {
                for (final Class<?> tmpClazz : classes) {
                        try {
                                final BeanInfo info = Introspector.getBeanInfo(tmpClazz);
                                final PropertyDescriptor[] propertyDescriptors = info.getPropertyDescriptors();
                                for (int i = 0; i < propertyDescriptors.length; ++i) {
                                        final PropertyDescriptor pd = propertyDescriptors[i];
                                        if (pd.getReadMethod().isAnnotationPresent(Transient.class)) {
                                                pd.setValue("transient", Boolean.TRUE);
                                        }
                                }
                        } catch (IntrospectionException e) {
                        }
                }
        }
}


cheers,
Andy
 
Posted: March 09, 2011 18:41 by etf
It really makes sense. Can you create a ticket it jira with your code snippet?
Replies: 3 - Last Post: March 09, 2011 18:41
by: etf
  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2010, Oracle Corporation and/or its affiliates
(revision 20120518.3c65429)
 
 
Close
loading
Please Confirm
Close