Coverage Report - org.mule.model.AbstractModel
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractModel
0%
0/54
0%
0/10
0
 
 1  
 /*
 2  
  * $Id: AbstractModel.java 19191 2010-08-25 21:05:23Z tcarlson $
 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.model;
 12  
 
 13  
 import org.mule.api.MuleContext;
 14  
 import org.mule.api.MuleException;
 15  
 import org.mule.api.component.LifecycleAdapterFactory;
 16  
 import org.mule.api.context.MuleContextAware;
 17  
 import org.mule.api.exception.MessagingExceptionHandler;
 18  
 import org.mule.api.lifecycle.InitialisationException;
 19  
 import org.mule.api.lifecycle.LifecycleState;
 20  
 import org.mule.api.model.EntryPointResolver;
 21  
 import org.mule.api.model.EntryPointResolverSet;
 22  
 import org.mule.api.model.Model;
 23  
 import org.mule.component.DefaultComponentLifecycleAdapterFactory;
 24  
 import org.mule.exception.DefaultServiceExceptionStrategy;
 25  
 import org.mule.lifecycle.EmptyLifecycleCallback;
 26  
 import org.mule.model.resolvers.DefaultEntryPointResolverSet;
 27  
 import org.mule.model.resolvers.LegacyEntryPointResolverSet;
 28  
 import org.mule.util.ClassUtils;
 29  
 
 30  
 import java.util.Collection;
 31  
 
 32  
 import org.apache.commons.logging.Log;
 33  
 import org.apache.commons.logging.LogFactory;
 34  
 
 35  
 /**
 36  
  * <code>MuleModel</code> is the default implementation of the Model. The model
 37  
  * encapsulates and manages the runtime behaviour of a Mule Server instance. It is
 38  
  * responsible for maintaining the service instances and their configuration.
 39  
  */
 40  0
 public abstract class AbstractModel implements Model
 41  
 {
 42  
 
 43  
     public static final String DEFAULT_MODEL_NAME = "main";
 44  
 
 45  0
     private String name = DEFAULT_MODEL_NAME;
 46  0
     private EntryPointResolverSet entryPointResolverSet = null; // values are supplied below as required
 47  0
     private LifecycleAdapterFactory lifecycleAdapterFactory = new DefaultComponentLifecycleAdapterFactory();
 48  0
     private MessagingExceptionHandler exceptionListener = new DefaultServiceExceptionStrategy();
 49  
 
 50  0
     protected transient Log logger = LogFactory.getLog(getClass());
 51  
     protected MuleContext muleContext;
 52  
 
 53  0
     protected ModelLifecycleManager lifecycleManager = new ModelLifecycleManager(this);
 54  
 
 55  
     public String getName()
 56  
     {
 57  0
         return name;
 58  
     }
 59  
 
 60  
     public void setName(String name)
 61  
     {
 62  0
         this.name = name;
 63  0
     }
 64  
 
 65  
     public LifecycleState getLifecycleState()
 66  
     {
 67  0
         return lifecycleManager.getState();
 68  
     }
 69  
 
 70  
     public EntryPointResolverSet getEntryPointResolverSet()
 71  
     {
 72  0
         if (null == entryPointResolverSet)
 73  
         {
 74  0
             entryPointResolverSet = new LegacyEntryPointResolverSet();
 75  
         }
 76  0
         return entryPointResolverSet;
 77  
     }
 78  
 
 79  
     public void setEntryPointResolverSet(EntryPointResolverSet entryPointResolverSet)
 80  
     {
 81  0
         this.entryPointResolverSet = entryPointResolverSet;
 82  0
     }
 83  
 
 84  
     /**
 85  
      * This allows us to configure entry point resolvers incrementally
 86  
      *
 87  
      * @param entryPointResolvers Resolvers to add
 88  
      */
 89  
     public void setEntryPointResolvers(Collection<EntryPointResolver> entryPointResolvers)
 90  
     {
 91  0
         if (null == entryPointResolverSet)
 92  
         {
 93  0
             entryPointResolverSet = new DefaultEntryPointResolverSet();
 94  
         }
 95  
         
 96  0
         for (EntryPointResolver resolver : entryPointResolvers)
 97  
         {
 98  0
             entryPointResolverSet.addEntryPointResolver(resolver);
 99  
         }
 100  0
     }
 101  
 
 102  
     public LifecycleAdapterFactory getLifecycleAdapterFactory()
 103  
     {
 104  0
         return lifecycleAdapterFactory;
 105  
     }
 106  
 
 107  
     public void setLifecycleAdapterFactory(LifecycleAdapterFactory lifecycleAdapterFactory)
 108  
     {
 109  0
         this.lifecycleAdapterFactory = lifecycleAdapterFactory;
 110  0
     }
 111  
 
 112  
     /** Destroys any current components */
 113  
     public void dispose()
 114  
     {
 115  0
         if(getLifecycleState().isStarted())
 116  
         {
 117  
             try
 118  
             {
 119  0
                 stop();
 120  
             }
 121  0
             catch (MuleException e)
 122  
             {
 123  0
                 logger.error("Failed to stop model cleanly as part of a dispoae call: " + getName(), e);
 124  0
             }
 125  
         }
 126  
         try
 127  
         {
 128  0
             lifecycleManager.fireDisposePhase(new EmptyLifecycleCallback<AbstractModel>());
 129  
         }
 130  0
         catch (MuleException e)
 131  
         {
 132  0
             logger.error("Failed to dispose model: " + getName(), e);
 133  0
         }
 134  
 
 135  0
     }
 136  
 
 137  
     /**
 138  
      * Stops any registered components
 139  
      *
 140  
      * @throws MuleException if a Service fails tcomponent
 141  
      */
 142  
     public void stop() throws MuleException
 143  
     {
 144  0
         lifecycleManager.fireStopPhase(new EmptyLifecycleCallback<AbstractModel>());
 145  0
     }
 146  
 
 147  
     /**
 148  
      * Starts all registered components
 149  
      *
 150  
      * @throws MuleException if any of the components fail to start
 151  
      */
 152  
     public void start() throws MuleException
 153  
     {
 154  0
         lifecycleManager.fireStartPhase(new EmptyLifecycleCallback<AbstractModel>());
 155  0
     }
 156  
 
 157  
     public void initialise() throws InitialisationException
 158  
     {
 159  
         try
 160  
         {
 161  0
             lifecycleManager.fireInitialisePhase(new EmptyLifecycleCallback<AbstractModel>());
 162  
         }
 163  0
         catch (InitialisationException e)
 164  
         {
 165  0
             throw e;
 166  
         }
 167  0
         catch (MuleException e)
 168  
         {
 169  0
             throw new InitialisationException(e, this);
 170  0
         }
 171  0
     }
 172  
 
 173  
     public MessagingExceptionHandler getExceptionListener()
 174  
     {
 175  0
         return exceptionListener;
 176  
     }
 177  
 
 178  
     public void setExceptionListener(MessagingExceptionHandler exceptionListener)
 179  
     {
 180  0
         this.exceptionListener = exceptionListener;
 181  0
     }
 182  
 
 183  
 
 184  
 
 185  
     public void setMuleContext(MuleContext context)
 186  
     {
 187  0
         this.muleContext = context;
 188  
         //Because we allow a default Exception strategy for the model we need to inject the
 189  
         //muleContext when we get it
 190  0
         if (exceptionListener instanceof MuleContextAware)
 191  
         {
 192  0
             ((MuleContextAware)exceptionListener).setMuleContext(muleContext);
 193  
         }
 194  0
     }
 195  
 
 196  
     public MuleContext getMuleContext()
 197  
     {
 198  0
         return muleContext;
 199  
     }
 200  
 
 201  
     @Override
 202  
     public String toString()
 203  
     {
 204  0
         return String.format("%s{%s}", ClassUtils.getSimpleName(this.getClass()), getName());
 205  
     }
 206  
 }