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