View Javadoc

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