Coverage Report - org.mule.model.AbstractModel
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractModel
72%
41/57
33%
6/18
1.667
 
 1  
 /*
 2  
  * $Id: AbstractModel.java 11517 2008-03-31 21:34:19Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.context.notification.ServerNotification;
 18  
 import org.mule.api.lifecycle.InitialisationException;
 19  
 import org.mule.api.model.EntryPointResolver;
 20  
 import org.mule.api.model.EntryPointResolverSet;
 21  
 import org.mule.api.model.Model;
 22  
 import org.mule.component.DefaultLifecycleAdapterFactory;
 23  
 import org.mule.context.notification.ModelNotification;
 24  
 import org.mule.model.resolvers.DefaultEntryPointResolverSet;
 25  
 import org.mule.model.resolvers.LegacyEntryPointResolverSet;
 26  
 import org.mule.service.DefaultServiceExceptionStrategy;
 27  
 
 28  
 import java.beans.ExceptionListener;
 29  
 import java.util.Collection;
 30  
 import java.util.Iterator;
 31  
 
 32  
 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
 33  
 
 34  
 import org.apache.commons.logging.Log;
 35  
 import org.apache.commons.logging.LogFactory;
 36  
 
 37  
 /**
 38  
  * <code>MuleModel</code> is the default implementation of the Model. The model
 39  
  * encapsulates and manages the runtime behaviour of a Mule Server instance. It is
 40  
  * responsible for maintaining the UMOs instances and their configuration.
 41  
  */
 42  1552
 public abstract class AbstractModel implements Model
 43  
 {
 44  
 
 45  
     public static final String DEFAULT_MODEL_NAME = "main";
 46  
 
 47  1552
     private String name = DEFAULT_MODEL_NAME;
 48  1552
     private EntryPointResolverSet entryPointResolverSet = null; // values are supplied below as required
 49  1552
     private LifecycleAdapterFactory lifecycleAdapterFactory = new DefaultLifecycleAdapterFactory();
 50  1552
     private AtomicBoolean initialised = new AtomicBoolean(false);
 51  1552
     private AtomicBoolean started = new AtomicBoolean(false);
 52  1552
     private ExceptionListener exceptionListener = new DefaultServiceExceptionStrategy();
 53  
 
 54  1552
     protected transient Log logger = LogFactory.getLog(getClass());
 55  
     protected MuleContext muleContext;
 56  
 
 57  
     /*
 58  
      * (non-Javadoc)
 59  
      * 
 60  
      * @see org.mule.api.UMOModel#getName()
 61  
      */
 62  
     public String getName()
 63  
     {
 64  6642
         return name;
 65  
     }
 66  
 
 67  
     /*
 68  
      * (non-Javadoc)
 69  
      * 
 70  
      * @see org.mule.api.UMOModel#setName(java.lang.String)
 71  
      */
 72  
     public void setName(String name)
 73  
     {
 74  1150
         this.name = name;
 75  1150
     }
 76  
 
 77  
     /*
 78  
      * (non-Javadoc)
 79  
      * 
 80  
      * @see org.mule.api.model.Model#getEntryPointResolver()
 81  
      */
 82  
     public EntryPointResolverSet getEntryPointResolverSet()
 83  
     {
 84  44
         if (null == entryPointResolverSet)
 85  
         {
 86  44
             entryPointResolverSet = new LegacyEntryPointResolverSet();
 87  
         }
 88  44
         return entryPointResolverSet;
 89  
     }
 90  
 
 91  
     /*
 92  
      * (non-Javadoc)
 93  
      * 
 94  
      * @see org.mule.api.model.Model#setEntryPointResolver(org.mule.api.model.EntryPointResolver)
 95  
      */
 96  
     public void setEntryPointResolverSet(EntryPointResolverSet entryPointResolverSet)
 97  
     {
 98  0
         this.entryPointResolverSet = entryPointResolverSet;
 99  0
     }
 100  
 
 101  
     /**
 102  
      * This allows us to configure entry point resolvers incrementally
 103  
      *
 104  
      * @param entryPointResolvers Resolvers to add
 105  
      */
 106  
     public void setEntryPointResolvers(Collection entryPointResolvers)
 107  
     {
 108  0
         if (null == entryPointResolverSet)
 109  
         {
 110  0
             entryPointResolverSet = new DefaultEntryPointResolverSet();
 111  
         }
 112  0
         for (Iterator resolvers = entryPointResolvers.iterator(); resolvers.hasNext();)
 113  
         {
 114  0
             entryPointResolverSet.addEntryPointResolver((EntryPointResolver) resolvers.next());
 115  
         }
 116  0
     }
 117  
 
 118  
     /*
 119  
      * (non-Javadoc)
 120  
      *
 121  
      * @see org.mule.api.model.Model#getLifecycleAdapterFactory()
 122  
      */
 123  
     public LifecycleAdapterFactory getLifecycleAdapterFactory()
 124  
     {
 125  76
         return lifecycleAdapterFactory;
 126  
     }
 127  
 
 128  
     /*
 129  
      * (non-Javadoc)
 130  
      * 
 131  
      * @see org.mule.api.model.Model#setLifecycleAdapterFactory(org.mule.api.lifecycle.LifecycleAdapterFactory)
 132  
      */
 133  
     public void setLifecycleAdapterFactory(LifecycleAdapterFactory lifecycleAdapterFactory)
 134  
     {
 135  0
         this.lifecycleAdapterFactory = lifecycleAdapterFactory;
 136  0
     }
 137  
 
 138  
     /** Destroys any current components */
 139  
     public void dispose()
 140  
     {
 141  1144
         fireNotification(new ModelNotification(this, ModelNotification.MODEL_DISPOSING));
 142  1144
         fireNotification(new ModelNotification(this, ModelNotification.MODEL_DISPOSED));
 143  1144
     }
 144  
 
 145  
     /**
 146  
      * Stops any registered components
 147  
      *
 148  
      * @throws MuleException if a Service fails tcomponent
 149  
      */
 150  
     public void stop() throws MuleException
 151  
     {
 152  26
         fireNotification(new ModelNotification(this, ModelNotification.MODEL_STOPPING));
 153  26
         started.set(false);
 154  26
         fireNotification(new ModelNotification(this, ModelNotification.MODEL_STOPPED));
 155  26
     }
 156  
 
 157  
     /**
 158  
      * Starts all registered components
 159  
      *
 160  
      * @throws MuleException if any of the components fail to start
 161  
      */
 162  
     public void start() throws MuleException
 163  
     {
 164  36
         if (!initialised.get())
 165  
         {
 166  0
             throw new IllegalStateException("Not Initialised");
 167  
         }
 168  
 
 169  36
         if (!started.get())
 170  
         {
 171  36
             fireNotification(new ModelNotification(this, ModelNotification.MODEL_STARTING));
 172  36
             started.set(true);
 173  36
             fireNotification(new ModelNotification(this, ModelNotification.MODEL_STARTED));
 174  
         }
 175  
         else
 176  
         {
 177  0
             logger.debug("Model already started");
 178  
         }
 179  36
     }
 180  
 
 181  
     public void initialise() throws InitialisationException
 182  
     {
 183  1536
         if (!initialised.get())
 184  
         {
 185  1536
             fireNotification(new ModelNotification(this, ModelNotification.MODEL_INITIALISING));
 186  1536
             initialised.set(true);
 187  1536
             fireNotification(new ModelNotification(this, ModelNotification.MODEL_INITIALISED));
 188  
         }
 189  
         else
 190  
         {
 191  0
             logger.debug("Model already initialised");
 192  
         }
 193  1536
     }
 194  
 
 195  
     public ExceptionListener getExceptionListener()
 196  
     {
 197  398
         return exceptionListener;
 198  
     }
 199  
 
 200  
     public void setExceptionListener(ExceptionListener exceptionListener)
 201  
     {
 202  0
         this.exceptionListener = exceptionListener;
 203  0
     }
 204  
 
 205  
     void fireNotification(ServerNotification notification)
 206  
     {
 207  5484
         if (muleContext != null)
 208  
         {
 209  5484
             muleContext.fireNotification(notification);
 210  
         }
 211  0
         else if (logger.isWarnEnabled())
 212  
         {
 213  0
             logger.debug("MuleContext is not yet available for firing notifications, ignoring event: " + notification);
 214  
         }
 215  5484
     }
 216  
 
 217  
     public void setMuleContext(MuleContext context)
 218  
     {
 219  1536
         this.muleContext = context;
 220  
         //Because we allow a default Exception strategy for the model we need to inject the
 221  
         //muleContext when we get it
 222  1536
         if(exceptionListener instanceof MuleContextAware)
 223  
         {
 224  1536
             ((MuleContextAware)exceptionListener).setMuleContext(muleContext);
 225  
         }
 226  1536
     }
 227  
 
 228  
 }