View Javadoc

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