View Javadoc

1   /*
2    * $Id: EndpointService.java 7976 2007-08-21 14:26:13Z 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   * @author <a href="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
26   * @version $Revision: 7976 $
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 UMOEndpoint endpoint;
36      private UMOMessageReceiver receiver;
37      private String name;
38      private String componentName;
39  
40      public EndpointService(UMOEndpoint endpoint)
41      {
42          this.endpoint = endpoint;
43          init();
44      }
45  
46      public EndpointService(UMOMessageReceiver 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.getComponent().getDescriptor().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 && !UMOEndpoint.ENDPOINT_TYPE_RECEIVER.equals(endpoint.getType()))
65          {
66              throw new IllegalArgumentException(
67                  "Recevier is null for Endpoint MBean but the endpoint itself is a receiving endpoint");
68          }
69  
70          name = ObjectNameHelper.getEndpointName(endpoint);
71  
72      }
73  
74      public String getAddress()
75      {
76          return endpoint.getEndpointURI().getAddress();
77      }
78  
79      public String getName()
80      {
81          return name;
82      }
83  
84      public boolean isConnected()
85      {
86          return receiver == null || receiver.isConnected();
87      }
88  
89      public void connect() throws Exception
90      {
91          if (receiver != null && !receiver.isConnected())
92          {
93              receiver.connect();
94          }
95          else if (logger.isDebugEnabled())
96          {
97              logger.debug("Endpoint is already connected");
98          }
99      }
100 
101     public void disconnect() throws Exception
102     {
103         if (receiver != null && receiver.isConnected())
104         {
105             receiver.disconnect();
106         }
107         else if (logger.isDebugEnabled())
108         {
109             logger.debug("Endpoint is already disconnected");
110         }
111     }
112 
113     public boolean isSynchronous()
114     {
115         return endpoint.isSynchronous();
116     }
117 
118     public String getType()
119     {
120         return endpoint.getType();
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 
133 }