View Javadoc

1   /*
2    * $Id: EndpointService.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.module.management.mbean;
12  
13  import org.mule.MessageExchangePattern;
14  import org.mule.api.endpoint.ImmutableEndpoint;
15  import org.mule.api.endpoint.InboundEndpoint;
16  import org.mule.api.endpoint.OutboundEndpoint;
17  import org.mule.api.transport.MessageReceiver;
18  import org.mule.config.i18n.CoreMessages;
19  import org.mule.util.ObjectNameHelper;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  /**
25   * The EndpointServiceMBean allows you to check the confiugration of an endpoint and
26   * conect/disconnect endpoints manually.
27   */
28  public class EndpointService implements EndpointServiceMBean
29  {
30      /**
31       * logger used by this class
32       */
33      protected transient Log logger = LogFactory.getLog(getClass());
34  
35      private ImmutableEndpoint endpoint;
36      private MessageReceiver receiver;
37      private String name;
38      private String componentName;
39  
40      public EndpointService(ImmutableEndpoint endpoint)
41      {
42          this.endpoint = endpoint;
43          init();
44      }
45  
46      public EndpointService(MessageReceiver receiver)
47      {
48          if (receiver == null)
49          {
50              throw new IllegalArgumentException(CoreMessages.objectIsNull("Receiver").getMessage());
51          }
52          this.endpoint = receiver.getEndpoint();
53          this.receiver = receiver;
54          this.componentName = receiver.getFlowConstruct().getName();
55          init();
56      }
57  
58      private void init()
59      {
60          if (endpoint == null)
61          {
62              throw new IllegalArgumentException(CoreMessages.objectIsNull("Endpoint").getMessage());
63          }
64          if (receiver == null && endpoint instanceof InboundEndpoint)
65          {
66              throw new IllegalArgumentException(
67                  "Recevier is null for Endpoint MBean but the endpoint itself is a receiving endpoint");
68          }
69  
70          name = new ObjectNameHelper(endpoint.getConnector().getMuleContext()).getEndpointName(endpoint.getEndpointURI());
71      }
72  
73      public String getAddress()
74      {
75          return endpoint.getEndpointURI().getAddress();
76      }
77  
78      public String getName()
79      {
80          return name;
81      }
82  
83      public boolean isConnected()
84      {
85          return receiver == null || receiver.isConnected();
86      }
87  
88      public void connect() throws Exception
89      {
90          if (receiver != null && !receiver.isConnected())
91          {
92              receiver.connect();
93          }
94          else if (logger.isDebugEnabled())
95          {
96              logger.debug("Endpoint is already connected");
97          }
98      }
99  
100     public void disconnect() throws Exception
101     {
102         if (receiver != null && receiver.isConnected())
103         {
104             receiver.disconnect();
105         }
106         else if (logger.isDebugEnabled())
107         {
108             logger.debug("Endpoint is already disconnected");
109         }
110     }
111 
112     public boolean isInbound()
113     {
114         return endpoint instanceof InboundEndpoint;
115     }
116 
117     public boolean isOutbound()
118     {
119         return endpoint instanceof OutboundEndpoint;
120     }
121 
122     public MessageExchangePattern getMessageExchangePattern()
123     {
124         return endpoint.getExchangePattern();
125     }
126     
127     public String getComponentName()
128     {
129         return componentName;
130     }
131 
132     public void setComponentName(String componentName)
133     {
134         this.componentName = componentName;
135     }
136 }