Coverage Report - org.mule.lifecycle.NotificationLifecycleObject
 
Classes in this File Line Coverage Branch Coverage Complexity
NotificationLifecycleObject
0%
0/33
0%
0/10
0
 
 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.lifecycle;
 8  
 
 9  
 import org.mule.api.MuleContext;
 10  
 import org.mule.api.MuleRuntimeException;
 11  
 import org.mule.api.context.notification.ServerNotification;
 12  
 import org.mule.config.i18n.CoreMessages;
 13  
 import org.mule.context.notification.MuleContextNotification;
 14  
 import org.mule.util.ClassUtils;
 15  
 
 16  
 import java.lang.reflect.Constructor;
 17  
 
 18  
 /**
 19  
  * TODO
 20  
  */
 21  
 public class NotificationLifecycleObject extends LifecycleObject
 22  
 {
 23  
     private String preNotificationName;
 24  
     private String postNotificationName;
 25  
     private Constructor ctor;
 26  
 
 27  
     public NotificationLifecycleObject(Class type)
 28  
     {
 29  0
         super(type);
 30  0
     }
 31  
 
 32  
     public NotificationLifecycleObject(Class type, Class notificationClass)
 33  
     {
 34  0
         super(type);
 35  
 
 36  0
         if (notificationClass==null)
 37  
         {
 38  0
             throw new IllegalArgumentException(CoreMessages.objectIsNull("notificationClass").toString());
 39  
         }
 40  
 
 41  
         // MULE-2903: make sure the notifiactionClass is properly loaded and initialized
 42  0
         notificationClass = ClassUtils.initializeClass(notificationClass);
 43  
 
 44  0
         if (!ServerNotification.class.isAssignableFrom(notificationClass))
 45  
         {
 46  0
             throw new ClassCastException("Notification class must be of type: " + ServerNotification.class.getName() + ". Offending class is: " + notificationClass.getName());
 47  
         }
 48  
 
 49  0
         ctor = ClassUtils.getConstructor(notificationClass, new Class[]{Object.class, String.class});
 50  0
         if(ctor==null)
 51  
         {
 52  0
             throw new IllegalArgumentException("No constructor defined in Notification class: " + notificationClass + " with arguments (Object.class, String.class)");
 53  
         }
 54  0
     }
 55  
 
 56  
     public NotificationLifecycleObject(Class type, Class notificationClass, int preNotification, int postNotification)
 57  
     {
 58  0
         this(type, notificationClass);
 59  0
         setPreNotificationName(MuleContextNotification.getActionName(preNotification));
 60  0
         setPostNotificationName(MuleContextNotification.getActionName(postNotification));
 61  0
     }
 62  
 
 63  
     public String getPostNotificationName()
 64  
     {
 65  0
         return postNotificationName;
 66  
     }
 67  
 
 68  
     public void setPostNotificationName(String postNotificationName)
 69  
     {
 70  0
         this.postNotificationName = postNotificationName;
 71  0
     }
 72  
 
 73  
     public String getPreNotificationName()
 74  
     {
 75  0
         return preNotificationName;
 76  
     }
 77  
 
 78  
     public void setPreNotificationName(String preNotificationName)
 79  
     {
 80  0
         this.preNotificationName = preNotificationName;
 81  0
     }
 82  
 
 83  
     @Override
 84  
     public void firePreNotification(MuleContext context)
 85  
     {
 86  0
         if(getPreNotificationName()!=null)
 87  
         {
 88  0
             setPreNotification(createNotification(context, getPreNotificationName()));
 89  
         }
 90  0
         super.firePreNotification(context);
 91  
 
 92  0
     }
 93  
 
 94  
     @Override
 95  
     public void firePostNotification(MuleContext context)
 96  
     {
 97  0
         if(getPostNotificationName()!=null)
 98  
         {
 99  0
             setPostNotification(createNotification(context, getPostNotificationName()));
 100  
         }
 101  0
         super.firePostNotification(context);
 102  0
     }
 103  
 
 104  
     protected ServerNotification createNotification(MuleContext context, String action)
 105  
     {
 106  
         try
 107  
         {
 108  0
             return (ServerNotification)ctor.newInstance(context, action);
 109  
         }
 110  0
         catch (Exception e)
 111  
         {
 112  0
             throw new MuleRuntimeException(CoreMessages.failedToCreate("Notification:" + action) ,e);
 113  
         }
 114  
     }
 115  
 }