View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.util.store;
8   
9   import org.mule.api.MuleContext;
10  
11  import java.lang.reflect.Method;
12  import java.security.AccessController;
13  import java.security.PrivilegedAction;
14  
15  /**
16   * A marker interface used to trigger post-deserialization initialization of an object. This works 
17   * in the same way as {@link Cloneable} interface. Implementors of this interface must add the 
18   * method <code>private void initAfterDeserialization(MuleContext muleContext) throws MuleException</code>
19   * to their class (note that it's private). This will get invoked after the object has been 
20   * deserialized passing in the current mulecontext when using either 
21   * {@link org.mule.transformer.wire.SerializationWireFormat}, 
22   * {@link org.mule.transformer.wire.SerializedMuleMessageWireFormat}, or the 
23   * {@link org.mule.transformer.simple.ByteArrayToSerializable} transformer.
24   *
25   * @see org.mule.transformer.simple.ByteArrayToSerializable
26   * @see org.mule.transformer.wire.SerializationWireFormat
27   * @see org.mule.transformer.wire.SerializedMuleMessageWireFormat
28   */
29  public interface DeserializationPostInitialisable
30  {
31      //private void initAfterDeserialisation(MuleContext muleContext) throws MuleException;
32      
33      public class Implementation
34      {
35          public static void init(final Object object, final MuleContext muleContext) throws Exception
36          {
37              try
38              {
39                  final Method m = object.getClass().getDeclaredMethod("initAfterDeserialisation", 
40                      MuleContext.class);
41  
42                  Object o = AccessController.doPrivileged(new PrivilegedAction<Object>()
43                  {
44                      public Object run()
45                      {
46                          try
47                          {
48                              m.setAccessible(true);
49                              m.invoke(object, muleContext);
50                              return null;
51                          }
52                          catch (Exception e)
53                          {
54                              return e;
55                          }
56  
57                      }
58                  });
59                  
60                  if (o != null)
61                  {
62                      throw (Exception) o;
63                  }
64  
65              }
66              catch (NoSuchMethodException e)
67              {
68                  throw new IllegalArgumentException("Object " + object.getClass() + " implements " +
69                          DeserializationPostInitialisable.class + " but does not have a method " +
70                          "private void initAfterDeserialisation(MuleContext) defined", e);
71              }
72          }
73      }
74  }