View Javadoc

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