View Javadoc
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.processor;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.NamedObject;
11  import org.mule.api.lifecycle.LifecycleException;
12  import org.mule.api.lifecycle.LifecycleState;
13  import org.mule.api.lifecycle.Startable;
14  import org.mule.config.i18n.CoreMessages;
15  import org.mule.processor.AbstractFilteringMessageProcessor;
16  
17  public class ProcessIfStartedMessageProcessor extends AbstractFilteringMessageProcessor
18  {
19  
20      protected Startable startable;
21      protected LifecycleState lifecycleState;
22  
23      public ProcessIfStartedMessageProcessor(Startable startable, LifecycleState lifecycleState)
24      {
25          this.startable = startable;
26          this.lifecycleState = lifecycleState;
27      }
28  
29      @Override
30      protected boolean accept(MuleEvent event)
31      {
32          return lifecycleState.isStarted();
33      }
34  
35      @Override
36      protected MuleEvent handleUnaccepted(MuleEvent event) throws LifecycleException
37      {
38          throw new LifecycleException(CoreMessages.isStopped(getStartableName(startable)), event.getMessage());
39      }
40  
41      protected String getStartableName(Startable startableObject)
42      {
43          if (startableObject instanceof NamedObject)
44          {
45              return ((NamedObject) startableObject).getName();
46          }
47          else
48          {
49              return startableObject.toString();
50          }
51      }
52  
53  }