1 /* 2 * $Id: DeserializationPostInitialisable.java 22129 2011-06-06 15:50:31Z dirk.olmes $ 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 public class Implementation 36 { 37 public static void init(final Object object, final MuleContext muleContext) throws Exception 38 { 39 try 40 { 41 final Method m = object.getClass().getDeclaredMethod("initAfterDeserialisation", 42 MuleContext.class); 43 44 Object o = AccessController.doPrivileged(new PrivilegedAction<Object>() 45 { 46 @Override 47 public Object run() 48 { 49 try 50 { 51 m.setAccessible(true); 52 m.invoke(object, muleContext); 53 return null; 54 } 55 catch (Exception e) 56 { 57 return e; 58 } 59 60 } 61 }); 62 63 if (o != null) 64 { 65 throw (Exception) o; 66 } 67 68 } 69 catch (NoSuchMethodException e) 70 { 71 throw new IllegalArgumentException("Object " + object.getClass() + " implements " + 72 DeserializationPostInitialisable.class + " but does not have a method " + 73 "private void initAfterDeserialisation(MuleContext) defined", e); 74 } 75 } 76 } 77 }